2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2015-10-12 19:55:00 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Filename : Constructor.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Direct device access.
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Prepares a device for direct access.
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
2015-10-12 19:55:00 +01:00
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// This library 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
|
|
|
|
|
// Lesser General Public License for more details.
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2015-10-12 19:55:00 +01:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2015-10-12 19:55:00 +01:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2015-10-12 19:55:00 +01:00
|
|
|
using System;
|
2020-07-22 13:20:25 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2017-12-21 14:30:38 +00:00
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
2017-12-21 07:08:26 +00:00
|
|
|
using System.Linq;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
using Aaru.CommonTypes.Interop;
|
|
|
|
|
using Aaru.CommonTypes.Structs.Devices.ATA;
|
|
|
|
|
using Aaru.CommonTypes.Structs.Devices.SCSI;
|
|
|
|
|
using Aaru.Decoders.SCSI;
|
|
|
|
|
using Aaru.Decoders.SCSI.MMC;
|
|
|
|
|
using Aaru.Decoders.SecureDigital;
|
|
|
|
|
using Aaru.Devices.FreeBSD;
|
|
|
|
|
using Aaru.Devices.Windows;
|
2020-07-20 15:43:52 +01:00
|
|
|
using Aaru.Helpers;
|
2017-12-19 19:33:46 +00:00
|
|
|
using Microsoft.Win32.SafeHandles;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Extern = Aaru.Devices.Windows.Extern;
|
|
|
|
|
using FileAccess = Aaru.Devices.Windows.FileAccess;
|
|
|
|
|
using FileAttributes = Aaru.Devices.Windows.FileAttributes;
|
|
|
|
|
using FileFlags = Aaru.Devices.Linux.FileFlags;
|
|
|
|
|
using FileMode = Aaru.Devices.Windows.FileMode;
|
|
|
|
|
using FileShare = Aaru.Devices.Windows.FileShare;
|
|
|
|
|
using Inquiry = Aaru.CommonTypes.Structs.Devices.SCSI.Inquiry;
|
2020-07-20 15:43:52 +01:00
|
|
|
using Marshal = System.Runtime.InteropServices.Marshal;
|
2020-02-27 00:33:26 +00:00
|
|
|
using PlatformID = Aaru.CommonTypes.Interop.PlatformID;
|
|
|
|
|
using VendorString = Aaru.Decoders.SecureDigital.VendorString;
|
|
|
|
|
|
|
|
|
|
namespace Aaru.Devices
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global"), SuppressMessage("ReSharper", "UnusedMember.Global"),
|
|
|
|
|
SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
|
|
|
|
|
public sealed partial class Device
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
/// <summary>Opens the device for sending direct commands</summary>
|
2015-10-12 20:08:56 +01:00
|
|
|
/// <param name="devicePath">Device path</param>
|
2015-10-12 19:55:00 +01:00
|
|
|
public Device(string devicePath)
|
|
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
PlatformId = DetectOS.GetRealPlatformID();
|
|
|
|
|
Timeout = 15;
|
|
|
|
|
Error = false;
|
2017-12-21 07:19:46 +00:00
|
|
|
IsRemovable = false;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
2020-07-22 13:20:25 +01:00
|
|
|
if(devicePath.StartsWith("dic://", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
devicePath.StartsWith("aaru://", StringComparison.OrdinalIgnoreCase))
|
2019-10-13 21:17:38 +01:00
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
devicePath =
|
|
|
|
|
devicePath.Substring(devicePath.StartsWith("dic://", StringComparison.OrdinalIgnoreCase) ? 6 : 7);
|
2020-03-01 06:02:40 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
string[] pieces = devicePath.Split('/');
|
|
|
|
|
string host = pieces[0];
|
2019-10-13 21:17:38 +01:00
|
|
|
devicePath = devicePath.Substring(host.Length);
|
|
|
|
|
|
2019-10-21 23:39:04 +01:00
|
|
|
_remote = new Remote.Remote(host);
|
2019-10-13 21:17:38 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
Error = !_remote.Open(devicePath, out int errno);
|
2019-10-13 22:29:09 +01:00
|
|
|
LastError = errno;
|
2019-10-13 21:17:38 +01:00
|
|
|
}
|
2019-10-13 22:29:09 +01:00
|
|
|
else
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
switch(PlatformId)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2019-10-13 22:29:09 +01:00
|
|
|
case PlatformID.Win32NT:
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
FileHandle = Extern.CreateFile(devicePath, FileAccess.GenericRead | FileAccess.GenericWrite,
|
2020-05-13 10:42:58 +00:00
|
|
|
FileShare.Read | FileShare.Write, IntPtr.Zero,
|
2020-02-29 18:03:35 +00:00
|
|
|
FileMode.OpenExisting, FileAttributes.Normal, IntPtr.Zero);
|
2015-10-13 01:45:07 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(((SafeFileHandle)FileHandle).IsInvalid)
|
2019-10-13 22:29:09 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
Error = true;
|
2019-10-13 22:29:09 +01:00
|
|
|
LastError = Marshal.GetLastWin32Error();
|
|
|
|
|
}
|
2015-10-12 19:55:00 +01:00
|
|
|
|
2019-10-13 22:29:09 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PlatformID.Linux:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2019-10-13 22:29:09 +01:00
|
|
|
FileHandle =
|
|
|
|
|
Linux.Extern.open(devicePath,
|
2020-01-09 15:02:21 +00:00
|
|
|
FileFlags.ReadWrite | FileFlags.NonBlocking | FileFlags.CreateNew);
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if((int)FileHandle < 0)
|
2018-04-02 23:09:18 +01:00
|
|
|
{
|
2019-10-13 22:29:09 +01:00
|
|
|
LastError = Marshal.GetLastWin32Error();
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(LastError == 13 ||
|
|
|
|
|
LastError == 30) // EACCES or EROFS
|
2019-10-13 22:29:09 +01:00
|
|
|
{
|
|
|
|
|
FileHandle = Linux.Extern.open(devicePath, FileFlags.Readonly | FileFlags.NonBlocking);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if((int)FileHandle < 0)
|
2019-10-13 22:29:09 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
Error = true;
|
2019-10-13 22:29:09 +01:00
|
|
|
LastError = Marshal.GetLastWin32Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-04-02 23:09:18 +01:00
|
|
|
{
|
2019-10-13 21:17:38 +01:00
|
|
|
Error = true;
|
2018-04-02 23:09:18 +01:00
|
|
|
}
|
2019-10-13 22:29:09 +01:00
|
|
|
|
|
|
|
|
LastError = Marshal.GetLastWin32Error();
|
2018-04-02 23:09:18 +01:00
|
|
|
}
|
2019-10-13 22:29:09 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PlatformID.FreeBSD:
|
|
|
|
|
{
|
|
|
|
|
FileHandle = FreeBSD.Extern.cam_open_device(devicePath, FreeBSD.FileFlags.ReadWrite);
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(((IntPtr)FileHandle).ToInt64() == 0)
|
2019-10-13 21:17:38 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
Error = true;
|
2019-10-13 22:29:09 +01:00
|
|
|
LastError = Marshal.GetLastWin32Error();
|
2019-10-13 21:17:38 +01:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
var camDevice = (CamDevice)Marshal.PtrToStructure((IntPtr)FileHandle, typeof(CamDevice));
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(StringHandlers.CToString(camDevice.SimName) == "ata")
|
2019-10-13 22:29:09 +01:00
|
|
|
throw new
|
2020-01-09 15:02:21 +00:00
|
|
|
DeviceException("Parallel ATA devices are not supported on FreeBSD due to upstream bug #224250.");
|
2017-12-10 21:00:20 +00:00
|
|
|
|
2019-10-13 22:29:09 +01:00
|
|
|
break;
|
2017-12-10 21:00:20 +00:00
|
|
|
}
|
2019-11-02 01:15:49 +00:00
|
|
|
default: throw new DeviceException($"Platform {PlatformId} not yet supported.");
|
2017-12-10 21:00:20 +00:00
|
|
|
}
|
2015-10-12 19:55:00 +01:00
|
|
|
}
|
2015-10-19 04:46:09 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(Error)
|
|
|
|
|
throw new DeviceException(LastError);
|
* DiscImageChef.CommonTypes/MediaTypeFromSCSI.cs:
* DiscImageChef.CommonTypes/DiscImageChef.CommonTypes.csproj:
Added method to calculate MediaType from SCSI parameters
(mode, density, medium type, device type, etc).
* DiscImageChef.Metadata/DeviceReport.cs:
Added command to guess drive and media parameters and output
an XML report of them.
* DiscImageChef/Commands/DeviceReport.cs:
* DiscImageChef.Metadata/DiscImageChef.Metadata.csproj:
Added command to guess drive and media parameters and output
an XML report of them.
* DiscImageChef/Commands/DumpMedia.cs:
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
* DiscImageChef/Core/Checksum.cs:
* DiscImageChef/Commands/CreateSidecar.cs:
Moved checksum generation to a separate class.
* CICMMetadata:
Added support for ADIP.
* DiscImageChef.CommonTypes/MediaType.cs:
Added parameters of UDO media.
Moved DataPlay outside of Iomega, as it's not from that
manufacturer.
Added missing Exatape media and corrected 160m XL one.
Added SyJet media.
Added all ECMA defined magneto-optical (sectors calculated
from specifications, unchecked).
Added PD media.
Added Imation 320Gb RDX.
Added generic USB flash drives.
* DiscImageChef.Decoders/SCSI/Enums.cs:
Make enumerations public.
* DiscImageChef.Decoders/SCSI/Inquiry.cs:
* DiscImageChef.Devices/Device/Constructor.cs:
Trim space padded strings on SCSI INQUIRY.
* DiscImageChef.Devices/Device/ScsiCommands/MMC.cs:
Added PREVENT ALLOW MEDIUM REMOVAL.
Added START STOP UNIT.
* DiscImageChef.Devices/Device/ScsiCommands/NEC.cs:
Rename NEC methods.
* DiscImageChef.Devices/Device/ScsiCommands/Pioneer.cs:
Corrected Pioneer transfer length calculation.
* DiscImageChef.Devices/Device/ScsiCommands/Plextor.cs:
Renamed Plextor methods.
* DiscImageChef.Devices/Device/ScsiCommands/SPC.cs:
Renamed SSC PREVENT ALLOW MEDIUM REMOVAL to uncollide with
MMC same name but different command.
* DiscImageChef.Devices/DiscImageChef.Devices.csproj:
Set platform target to x86 (does it really matter?).
* DiscImageChef.Devices/Linux/Command.cs:
Reduced allocation for readlink() to current kernel
MAX_PATH.
* DiscImageChef.Devices/Linux/Enums.cs:
Modified Linux ioctl to 32-bit. Works on 64-bit also. Solves
commands not working on 32-bit environments.
* DiscImageChef.DiscImages/ZZZRawImage.cs:
Changed ECMA-184 and ECMA-183 enums.
* DiscImageChef.Metadata/Dimensions.cs:
Added all ECMA defined magneto-opticals.
Added PD media.
Added 320Gb RDX.
Corrected Exatape 160m XL.
Added Exatape 22m and 28m.
* DiscImageChef.Metadata/MediaType.cs:
Added 356mm magneto-optical media.
Changed ECMA-184 and ECMA-183 enums.
Added USB generic flash drive.
* DiscImageChef/Commands/DeviceInfo.cs:
Corrected SCSI INQUIRY naming.
Corrected SCSI MODE SENSE (6) parameters.
Reduced SCSI MODE SENSE timeout, some devices just get stuck
with unsupported MODE SENSE commanda and must be left to
timeout.
Changed FUJITSU vendor string comparison.
* DiscImageChef/Commands/MediaInfo.cs:
Added method to calculate MediaType from SCSI parameters
(mode, density, medium type, device type, etc).
Changed some error WriteLine() to debug ones. Too much
verbosity.
Added DVD media type decoding from PFI.
Found a drive that dumps ADIP, enabling it again (not
decoded).
* DiscImageChef/Commands/MediaScan.cs:
Added option to generate ImgBurn compatible log to
media-scan command.
* DiscImageChef/DiscImageChef.csproj:
Moved checksum generation to a separate class.
Added command to guess drive and media parameters and output
an XML report of them.
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
* DiscImageChef/Main.cs:
Added command to guess drive and media parameters and output
an XML report of them.
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
* DiscImageChef/Options.cs:
Added command to guess drive and media parameters and output
an XML report of them.
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
Added option to generate ImgBurn compatible log to media-scan
command.
2016-01-31 08:05:56 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
Type = DeviceType.Unknown;
|
2017-12-21 14:30:38 +00:00
|
|
|
ScsiType = PeripheralDeviceTypes.UnknownDevice;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
|
|
|
|
byte[] ataBuf;
|
2017-09-07 16:47:06 +01:00
|
|
|
byte[] inqBuf = null;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(Error)
|
|
|
|
|
throw new DeviceException(LastError);
|
* DiscImageChef.CommonTypes/MediaTypeFromSCSI.cs:
* DiscImageChef.CommonTypes/DiscImageChef.CommonTypes.csproj:
Added method to calculate MediaType from SCSI parameters
(mode, density, medium type, device type, etc).
* DiscImageChef.Metadata/DeviceReport.cs:
Added command to guess drive and media parameters and output
an XML report of them.
* DiscImageChef/Commands/DeviceReport.cs:
* DiscImageChef.Metadata/DiscImageChef.Metadata.csproj:
Added command to guess drive and media parameters and output
an XML report of them.
* DiscImageChef/Commands/DumpMedia.cs:
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
* DiscImageChef/Core/Checksum.cs:
* DiscImageChef/Commands/CreateSidecar.cs:
Moved checksum generation to a separate class.
* CICMMetadata:
Added support for ADIP.
* DiscImageChef.CommonTypes/MediaType.cs:
Added parameters of UDO media.
Moved DataPlay outside of Iomega, as it's not from that
manufacturer.
Added missing Exatape media and corrected 160m XL one.
Added SyJet media.
Added all ECMA defined magneto-optical (sectors calculated
from specifications, unchecked).
Added PD media.
Added Imation 320Gb RDX.
Added generic USB flash drives.
* DiscImageChef.Decoders/SCSI/Enums.cs:
Make enumerations public.
* DiscImageChef.Decoders/SCSI/Inquiry.cs:
* DiscImageChef.Devices/Device/Constructor.cs:
Trim space padded strings on SCSI INQUIRY.
* DiscImageChef.Devices/Device/ScsiCommands/MMC.cs:
Added PREVENT ALLOW MEDIUM REMOVAL.
Added START STOP UNIT.
* DiscImageChef.Devices/Device/ScsiCommands/NEC.cs:
Rename NEC methods.
* DiscImageChef.Devices/Device/ScsiCommands/Pioneer.cs:
Corrected Pioneer transfer length calculation.
* DiscImageChef.Devices/Device/ScsiCommands/Plextor.cs:
Renamed Plextor methods.
* DiscImageChef.Devices/Device/ScsiCommands/SPC.cs:
Renamed SSC PREVENT ALLOW MEDIUM REMOVAL to uncollide with
MMC same name but different command.
* DiscImageChef.Devices/DiscImageChef.Devices.csproj:
Set platform target to x86 (does it really matter?).
* DiscImageChef.Devices/Linux/Command.cs:
Reduced allocation for readlink() to current kernel
MAX_PATH.
* DiscImageChef.Devices/Linux/Enums.cs:
Modified Linux ioctl to 32-bit. Works on 64-bit also. Solves
commands not working on 32-bit environments.
* DiscImageChef.DiscImages/ZZZRawImage.cs:
Changed ECMA-184 and ECMA-183 enums.
* DiscImageChef.Metadata/Dimensions.cs:
Added all ECMA defined magneto-opticals.
Added PD media.
Added 320Gb RDX.
Corrected Exatape 160m XL.
Added Exatape 22m and 28m.
* DiscImageChef.Metadata/MediaType.cs:
Added 356mm magneto-optical media.
Changed ECMA-184 and ECMA-183 enums.
Added USB generic flash drive.
* DiscImageChef/Commands/DeviceInfo.cs:
Corrected SCSI INQUIRY naming.
Corrected SCSI MODE SENSE (6) parameters.
Reduced SCSI MODE SENSE timeout, some devices just get stuck
with unsupported MODE SENSE commanda and must be left to
timeout.
Changed FUJITSU vendor string comparison.
* DiscImageChef/Commands/MediaInfo.cs:
Added method to calculate MediaType from SCSI parameters
(mode, density, medium type, device type, etc).
Changed some error WriteLine() to debug ones. Too much
verbosity.
Added DVD media type decoding from PFI.
Found a drive that dumps ADIP, enabling it again (not
decoded).
* DiscImageChef/Commands/MediaScan.cs:
Added option to generate ImgBurn compatible log to
media-scan command.
* DiscImageChef/DiscImageChef.csproj:
Moved checksum generation to a separate class.
Added command to guess drive and media parameters and output
an XML report of them.
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
* DiscImageChef/Main.cs:
Added command to guess drive and media parameters and output
an XML report of them.
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
* DiscImageChef/Options.cs:
Added command to guess drive and media parameters and output
an XML report of them.
Added preliminary command to dump media. Only SCSI for now.
CDs and tapes are not supported. Errors are blalanty
ignored. Options are incomplete. Not yet usable.
Added option to generate ImgBurn compatible log to media-scan
command.
2016-01-31 08:05:56 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
bool scsiSense = true;
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(_remote is null)
|
2017-12-23 20:04:36 +00:00
|
|
|
{
|
2019-10-16 20:07:24 +01:00
|
|
|
// Windows is answering SCSI INQUIRY for all device types so it needs to be detected first
|
2020-01-09 15:02:21 +00:00
|
|
|
switch(PlatformId)
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
var query = new StoragePropertyQuery();
|
2020-01-09 15:02:21 +00:00
|
|
|
query.PropertyId = StoragePropertyId.Device;
|
|
|
|
|
query.QueryType = StorageQueryType.Standard;
|
2019-10-16 20:07:24 +01:00
|
|
|
query.AdditionalParameters = new byte[1];
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
IntPtr descriptorPtr = Marshal.AllocHGlobal(1000);
|
|
|
|
|
byte[] descriptorB = new byte[1000];
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
uint returned = 0;
|
2020-01-09 15:02:21 +00:00
|
|
|
int error = 0;
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
bool hasError = !Extern.DeviceIoControlStorageQuery((SafeFileHandle)FileHandle,
|
2020-02-29 18:03:35 +00:00
|
|
|
WindowsIoctl.IoctlStorageQueryProperty,
|
|
|
|
|
ref query, (uint)Marshal.SizeOf(query),
|
|
|
|
|
descriptorPtr, 1000, ref returned,
|
|
|
|
|
IntPtr.Zero);
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(hasError)
|
|
|
|
|
error = Marshal.GetLastWin32Error();
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
Marshal.Copy(descriptorPtr, descriptorB, 0, 1000);
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!hasError &&
|
|
|
|
|
error == 0)
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2019-10-16 20:07:24 +01:00
|
|
|
var descriptor = new StorageDeviceDescriptor
|
|
|
|
|
{
|
2020-07-20 04:34:16 +01:00
|
|
|
Version = BitConverter.ToUInt32(descriptorB, 0),
|
|
|
|
|
Size = BitConverter.ToUInt32(descriptorB, 4),
|
|
|
|
|
DeviceType = descriptorB[8],
|
|
|
|
|
DeviceTypeModifier = descriptorB[9],
|
|
|
|
|
RemovableMedia = descriptorB[10] > 0,
|
|
|
|
|
CommandQueueing = descriptorB[11] > 0,
|
|
|
|
|
VendorIdOffset = BitConverter.ToInt32(descriptorB, 12),
|
|
|
|
|
ProductIdOffset = BitConverter.ToInt32(descriptorB, 16),
|
2019-10-16 20:07:24 +01:00
|
|
|
ProductRevisionOffset = BitConverter.ToInt32(descriptorB, 20),
|
2020-07-20 04:34:16 +01:00
|
|
|
SerialNumberOffset = BitConverter.ToInt32(descriptorB, 24),
|
|
|
|
|
BusType = (StorageBusType)BitConverter.ToUInt32(descriptorB, 28),
|
|
|
|
|
RawPropertiesLength = BitConverter.ToUInt32(descriptorB, 32)
|
2019-10-16 20:07:24 +01:00
|
|
|
};
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
descriptor.RawDeviceProperties = new byte[descriptor.RawPropertiesLength];
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
Array.Copy(descriptorB, 36, descriptor.RawDeviceProperties, 0,
|
2020-01-09 15:02:21 +00:00
|
|
|
descriptor.RawPropertiesLength);
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
switch(descriptor.BusType)
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
|
|
|
|
case StorageBusType.SCSI:
|
|
|
|
|
case StorageBusType.SSA:
|
|
|
|
|
case StorageBusType.Fibre:
|
|
|
|
|
case StorageBusType.iSCSI:
|
|
|
|
|
case StorageBusType.SAS:
|
|
|
|
|
Type = DeviceType.SCSI;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.FireWire:
|
|
|
|
|
IsFireWire = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
Type = DeviceType.SCSI;
|
|
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.USB:
|
|
|
|
|
IsUsb = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
Type = DeviceType.SCSI;
|
|
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.ATAPI:
|
2017-12-21 07:19:46 +00:00
|
|
|
Type = DeviceType.ATAPI;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.ATA:
|
|
|
|
|
case StorageBusType.SATA:
|
|
|
|
|
Type = DeviceType.ATA;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.MultiMediaCard:
|
|
|
|
|
Type = DeviceType.MMC;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.SecureDigital:
|
|
|
|
|
Type = DeviceType.SecureDigital;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.NVMe:
|
|
|
|
|
Type = DeviceType.NVMe;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
switch(Type)
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
|
|
|
|
case DeviceType.SCSI:
|
|
|
|
|
case DeviceType.ATAPI:
|
|
|
|
|
scsiSense = ScsiInquiry(out inqBuf, out _);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case DeviceType.ATA:
|
2020-01-09 15:02:21 +00:00
|
|
|
bool atapiSense = AtapiIdentify(out ataBuf, out _);
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!atapiSense)
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
|
|
|
|
Type = DeviceType.ATAPI;
|
2020-01-09 15:02:21 +00:00
|
|
|
Identify.IdentifyDevice? ataid = Identify.Decode(ataBuf);
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(ataid.HasValue)
|
|
|
|
|
scsiSense = ScsiInquiry(out inqBuf, out _);
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Manufacturer = "ATA";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-09-07 16:47:06 +01:00
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
Marshal.FreeHGlobal(descriptorPtr);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(Windows.Command.IsSdhci((SafeFileHandle)FileHandle))
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
byte[] sdBuffer = new byte[16];
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
LastError = Windows.Command.SendMmcCommand((SafeFileHandle)FileHandle, MmcCommands.SendCsd,
|
|
|
|
|
false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR2 | MmcFlags.ResponseR2 |
|
|
|
|
|
MmcFlags.CommandAc, 0, 16, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out bool sense);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!sense)
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedCsd = new byte[16];
|
|
|
|
|
Array.Copy(sdBuffer, 0, _cachedCsd, 0, 16);
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
sdBuffer = new byte[16];
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
LastError = Windows.Command.SendMmcCommand((SafeFileHandle)FileHandle, MmcCommands.SendCid,
|
|
|
|
|
false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR2 | MmcFlags.ResponseR2 |
|
|
|
|
|
MmcFlags.CommandAc, 0, 16, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out sense);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!sense)
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedCid = new byte[16];
|
|
|
|
|
Array.Copy(sdBuffer, 0, _cachedCid, 0, 16);
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
sdBuffer = new byte[8];
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
LastError = Windows.Command.SendMmcCommand((SafeFileHandle)FileHandle,
|
|
|
|
|
(MmcCommands)SecureDigitalCommands.SendScr,
|
|
|
|
|
false, true,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 |
|
|
|
|
|
MmcFlags.CommandAdtc, 0, 8, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out sense);
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!sense)
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedScr = new byte[8];
|
|
|
|
|
Array.Copy(sdBuffer, 0, _cachedScr, 0, 8);
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-27 20:43:50 +00:00
|
|
|
sdBuffer = new byte[4];
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
LastError = Windows.Command.SendMmcCommand((SafeFileHandle)FileHandle,
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedScr != null
|
2020-01-09 15:02:21 +00:00
|
|
|
? (MmcCommands)SecureDigitalCommands.
|
|
|
|
|
SendOperatingCondition
|
|
|
|
|
: MmcCommands.SendOpCond, false, true,
|
|
|
|
|
MmcFlags.ResponseSpiR3 | MmcFlags.ResponseR3 |
|
|
|
|
|
MmcFlags.CommandBcr, 0, 4, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out sense);
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!sense)
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedScr = new byte[4];
|
|
|
|
|
Array.Copy(sdBuffer, 0, _cachedScr, 0, 4);
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
case PlatformID.Linux:
|
2020-01-09 15:02:21 +00:00
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
2020-05-14 03:41:06 +01:00
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sg", StringComparison.Ordinal))
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2019-10-16 20:07:24 +01:00
|
|
|
scsiSense = ScsiInquiry(out inqBuf, out _);
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
// MultiMediaCard and SecureDigital go here
|
2020-01-09 15:02:21 +00:00
|
|
|
else if(devicePath.StartsWith("/dev/mmcblk", StringComparison.Ordinal))
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string devPath = devicePath.Substring(5);
|
|
|
|
|
|
|
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/csd"))
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/csd", out _cachedCsd);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(len == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedCsd = null;
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/cid"))
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/cid", out _cachedCid);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(len == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedCid = null;
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/scr"))
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/scr", out _cachedScr);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(len == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedScr = null;
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/ocr"))
|
2019-10-16 20:07:24 +01:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
int len = ConvertFromHexAscii("/sys/block/" + devPath + "/device/ocr", out _cachedOcr);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(len == 0)
|
2020-07-20 21:11:32 +01:00
|
|
|
_cachedOcr = null;
|
2019-10-16 20:07:24 +01:00
|
|
|
}
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
scsiSense = ScsiInquiry(out inqBuf, out _);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-16 20:07:24 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-10-21 23:39:04 +01:00
|
|
|
Type = _remote.GetDeviceType();
|
2019-10-17 22:15:01 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
switch(Type)
|
2019-10-17 22:15:01 +01:00
|
|
|
{
|
|
|
|
|
case DeviceType.ATAPI:
|
|
|
|
|
case DeviceType.SCSI:
|
|
|
|
|
scsiSense = ScsiInquiry(out inqBuf, out _);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-17 22:15:01 +01:00
|
|
|
break;
|
|
|
|
|
case DeviceType.SecureDigital:
|
|
|
|
|
case DeviceType.MMC:
|
2020-07-20 21:11:32 +01:00
|
|
|
if(!_remote.GetSdhciRegisters(out _cachedCsd, out _cachedCid, out _cachedOcr, out _cachedScr))
|
2019-10-17 22:15:01 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
Type = DeviceType.SCSI;
|
2019-10-17 22:15:01 +01:00
|
|
|
ScsiType = PeripheralDeviceTypes.DirectAccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
}
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2017-12-06 23:05:59 +00:00
|
|
|
#region SecureDigital / MultiMediaCard
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_cachedCid != null)
|
2017-12-06 23:05:59 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
ScsiType = PeripheralDeviceTypes.DirectAccess;
|
2017-12-21 07:19:46 +00:00
|
|
|
IsRemovable = false;
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_cachedScr != null)
|
2017-12-06 23:05:59 +00:00
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
Type = DeviceType.SecureDigital;
|
2020-07-20 21:11:32 +01:00
|
|
|
CID decoded = Decoders.SecureDigital.Decoders.DecodeCID(_cachedCid);
|
2017-12-21 14:30:38 +00:00
|
|
|
Manufacturer = VendorString.Prettify(decoded.Manufacturer);
|
2020-01-09 15:02:21 +00:00
|
|
|
Model = decoded.ProductName;
|
|
|
|
|
|
|
|
|
|
FirmwareRevision =
|
|
|
|
|
$"{(decoded.ProductRevision & 0xF0) >> 4:X2}.{decoded.ProductRevision & 0x0F:X2}";
|
|
|
|
|
|
2019-10-13 21:17:38 +01:00
|
|
|
Serial = $"{decoded.ProductSerialNumber}";
|
2017-12-06 23:05:59 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
Type = DeviceType.MMC;
|
2020-07-20 21:11:32 +01:00
|
|
|
Decoders.MMC.CID decoded = Decoders.MMC.Decoders.DecodeCID(_cachedCid);
|
2017-12-21 07:19:46 +00:00
|
|
|
Manufacturer = Decoders.MMC.VendorString.Prettify(decoded.Manufacturer);
|
2020-01-09 15:02:21 +00:00
|
|
|
Model = decoded.ProductName;
|
|
|
|
|
|
|
|
|
|
FirmwareRevision =
|
|
|
|
|
$"{(decoded.ProductRevision & 0xF0) >> 4:X2}.{decoded.ProductRevision & 0x0F:X2}";
|
|
|
|
|
|
2019-10-13 21:17:38 +01:00
|
|
|
Serial = $"{decoded.ProductSerialNumber}";
|
2017-09-28 19:14:50 +00:00
|
|
|
}
|
2020-05-13 10:42:58 +00:00
|
|
|
|
|
|
|
|
return;
|
2017-09-28 17:54:07 +00:00
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
#endregion SecureDigital / MultiMediaCard
|
2017-12-06 13:46:35 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
#region USB
|
2020-01-09 15:02:21 +00:00
|
|
|
if(_remote is null)
|
2017-12-23 20:04:36 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
switch(PlatformId)
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
|
|
|
|
case PlatformID.Linux:
|
2020-01-09 15:02:21 +00:00
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal))
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string devPath = devicePath.Substring(5);
|
|
|
|
|
|
|
|
|
|
if(Directory.Exists("/sys/block/" + devPath))
|
2019-10-18 22:44:02 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(resolvedLink))
|
2015-12-31 16:12:22 +00:00
|
|
|
{
|
2019-10-18 23:42:18 +01:00
|
|
|
resolvedLink = "/sys" + resolvedLink.Substring(2);
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
while(resolvedLink.Contains("usb"))
|
2017-12-21 06:06:19 +00:00
|
|
|
{
|
2019-10-18 23:42:18 +01:00
|
|
|
resolvedLink = Path.GetDirectoryName(resolvedLink);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(!File.Exists(resolvedLink + "/descriptors") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/idProduct") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/idVendor"))
|
|
|
|
|
continue;
|
2019-10-18 23:42:18 +01:00
|
|
|
|
|
|
|
|
var usbFs = new FileStream(resolvedLink + "/descriptors",
|
2020-01-09 15:02:21 +00:00
|
|
|
System.IO.FileMode.Open, System.IO.FileAccess.Read);
|
|
|
|
|
|
|
|
|
|
byte[] usbBuf = new byte[65536];
|
|
|
|
|
int usbCount = usbFs.Read(usbBuf, 0, 65536);
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbDescriptors = new byte[usbCount];
|
|
|
|
|
Array.Copy(usbBuf, 0, UsbDescriptors, 0, usbCount);
|
|
|
|
|
usbFs.Close();
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
var usbSr = new StreamReader(resolvedLink + "/idProduct");
|
|
|
|
|
string usbTemp = usbSr.ReadToEnd();
|
|
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
ushort.TryParse(usbTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2020-07-20 21:11:32 +01:00
|
|
|
out _usbProduct);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
usbSr.Close();
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/idVendor");
|
2019-10-18 23:42:18 +01:00
|
|
|
usbTemp = usbSr.ReadToEnd();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
ushort.TryParse(usbTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2020-07-20 21:11:32 +01:00
|
|
|
out _usbVendor);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
usbSr.Close();
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists(resolvedLink + "/manufacturer"))
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/manufacturer");
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbManufacturerString = usbSr.ReadToEnd().Trim();
|
|
|
|
|
usbSr.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists(resolvedLink + "/product"))
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/product");
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbProductString = usbSr.ReadToEnd().Trim();
|
|
|
|
|
usbSr.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists(resolvedLink + "/serial"))
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/serial");
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbSerialString = usbSr.ReadToEnd().Trim();
|
|
|
|
|
usbSr.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsUsb = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2015-12-31 16:12:22 +00:00
|
|
|
}
|
2019-10-18 22:44:02 +01:00
|
|
|
}
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
break;
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
Usb.UsbDevice usbDevice = null;
|
2017-12-06 19:30:03 +00:00
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
// I have to search for USB disks, floppies and CD-ROMs as separate device types
|
2020-01-09 15:02:21 +00:00
|
|
|
foreach(string devGuid in new[]
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
2019-10-27 20:43:20 +00:00
|
|
|
Usb.GuidDevinterfaceFloppy, Usb.GuidDevinterfaceCdrom, Usb.GuidDevinterfaceDisk,
|
|
|
|
|
Usb.GuidDevinterfaceTape
|
2019-10-18 23:42:18 +01:00
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
usbDevice = Usb.FindDrivePath(devicePath, devGuid);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(usbDevice != null)
|
|
|
|
|
break;
|
2019-10-18 23:42:18 +01:00
|
|
|
}
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(usbDevice != null)
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
UsbDescriptors = usbDevice.BinaryDescriptors;
|
2020-07-20 21:11:32 +01:00
|
|
|
_usbVendor = (ushort)usbDevice.DeviceDescriptor.idVendor;
|
|
|
|
|
_usbProduct = (ushort)usbDevice.DeviceDescriptor.idProduct;
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbManufacturerString = usbDevice.Manufacturer;
|
2020-01-09 15:02:21 +00:00
|
|
|
UsbProductString = usbDevice.Product;
|
|
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbSerialString =
|
2020-01-09 15:02:21 +00:00
|
|
|
usbDevice.
|
|
|
|
|
SerialNumber; // This is incorrect filled by Windows with SCSI/ATA serial number
|
2019-10-18 23:42:18 +01:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
IsUsb = false;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-18 23:42:18 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(_remote.GetUsbData(out byte[] remoteUsbDescriptors, out ushort remoteUsbVendor,
|
|
|
|
|
out ushort remoteUsbProduct, out string remoteUsbManufacturer,
|
|
|
|
|
out string remoteUsbProductString, out string remoteUsbSerial))
|
2019-10-18 23:42:18 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
IsUsb = true;
|
|
|
|
|
UsbDescriptors = remoteUsbDescriptors;
|
2020-07-20 21:11:32 +01:00
|
|
|
_usbVendor = remoteUsbVendor;
|
|
|
|
|
_usbProduct = remoteUsbProduct;
|
2019-10-18 23:42:18 +01:00
|
|
|
UsbManufacturerString = remoteUsbManufacturer;
|
2020-01-09 15:02:21 +00:00
|
|
|
UsbProductString = remoteUsbProductString;
|
|
|
|
|
UsbSerialString = remoteUsbSerial;
|
2019-10-18 23:42:18 +01:00
|
|
|
}
|
2015-12-31 16:12:22 +00:00
|
|
|
}
|
|
|
|
|
#endregion USB
|
|
|
|
|
|
2015-12-31 16:33:20 +00:00
|
|
|
#region FireWire
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!(_remote is null))
|
2015-12-31 16:33:20 +00:00
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
if(_remote.GetFireWireData(out _firewireVendor, out _firewireModel, out _firewireGuid,
|
2020-01-09 15:02:21 +00:00
|
|
|
out string remoteFireWireVendorName, out string remoteFireWireModelName))
|
2015-12-31 16:33:20 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
IsFireWire = true;
|
2019-10-19 00:08:33 +01:00
|
|
|
FireWireVendorName = remoteFireWireVendorName;
|
2020-01-09 15:02:21 +00:00
|
|
|
FireWireModelName = remoteFireWireModelName;
|
2019-10-19 00:08:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(PlatformId == PlatformID.Linux)
|
2019-10-19 00:08:33 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal))
|
2015-12-31 16:33:20 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string devPath = devicePath.Substring(5);
|
|
|
|
|
|
|
|
|
|
if(Directory.Exists("/sys/block/" + devPath))
|
2019-10-19 00:08:33 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
|
2019-10-19 00:08:33 +01:00
|
|
|
resolvedLink = "/sys" + resolvedLink.Substring(2);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(resolvedLink))
|
|
|
|
|
while(resolvedLink.Contains("firewire"))
|
2015-12-31 16:33:20 +00:00
|
|
|
{
|
2019-10-19 00:08:33 +01:00
|
|
|
resolvedLink = Path.GetDirectoryName(resolvedLink);
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!File.Exists(resolvedLink + "/model") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/vendor") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/guid"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var fwSr = new StreamReader(resolvedLink + "/model");
|
|
|
|
|
string fwTemp = fwSr.ReadToEnd();
|
|
|
|
|
|
2019-10-19 00:08:33 +01:00
|
|
|
uint.TryParse(fwTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2020-07-20 21:11:32 +01:00
|
|
|
out _firewireModel);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2015-12-31 16:33:20 +00:00
|
|
|
fwSr.Close();
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/vendor");
|
2019-10-19 00:08:33 +01:00
|
|
|
fwTemp = fwSr.ReadToEnd();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 00:08:33 +01:00
|
|
|
uint.TryParse(fwTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2020-07-20 21:11:32 +01:00
|
|
|
out _firewireVendor);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2015-12-31 16:33:20 +00:00
|
|
|
fwSr.Close();
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/guid");
|
2019-10-19 00:08:33 +01:00
|
|
|
fwTemp = fwSr.ReadToEnd();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 00:08:33 +01:00
|
|
|
ulong.TryParse(fwTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2020-07-20 21:11:32 +01:00
|
|
|
out _firewireGuid);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 00:08:33 +01:00
|
|
|
fwSr.Close();
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists(resolvedLink + "/model_name"))
|
2019-10-19 00:08:33 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/model_name");
|
2019-10-19 00:08:33 +01:00
|
|
|
FireWireModelName = fwSr.ReadToEnd().Trim();
|
|
|
|
|
fwSr.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(File.Exists(resolvedLink + "/vendor_name"))
|
2019-10-19 00:08:33 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/vendor_name");
|
2019-10-19 00:08:33 +01:00
|
|
|
FireWireVendorName = fwSr.ReadToEnd().Trim();
|
|
|
|
|
fwSr.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsFireWire = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 00:08:33 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-31 16:33:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 00:08:33 +01:00
|
|
|
// TODO: Implement for other operating systems
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
IsFireWire = false;
|
|
|
|
|
}
|
2019-10-13 21:17:38 +01:00
|
|
|
}
|
2015-12-31 16:33:20 +00:00
|
|
|
#endregion FireWire
|
|
|
|
|
|
2016-10-17 04:41:27 +01:00
|
|
|
#region PCMCIA
|
2020-01-09 15:02:21 +00:00
|
|
|
if(_remote is null)
|
2016-10-17 04:41:27 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(PlatformId == PlatformID.Linux)
|
2016-10-17 04:41:27 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal))
|
2016-10-17 04:41:27 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string devPath = devicePath.Substring(5);
|
|
|
|
|
|
|
|
|
|
if(Directory.Exists("/sys/block/" + devPath))
|
2019-10-19 01:10:06 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
|
2019-10-19 01:10:06 +01:00
|
|
|
resolvedLink = "/sys" + resolvedLink.Substring(2);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(resolvedLink))
|
|
|
|
|
while(resolvedLink.Contains("/sys/devices"))
|
2019-10-19 01:10:06 +01:00
|
|
|
{
|
|
|
|
|
resolvedLink = Path.GetDirectoryName(resolvedLink);
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!Directory.Exists(resolvedLink + "/pcmcia_socket"))
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
string[] subdirs = Directory.GetDirectories(resolvedLink + "/pcmcia_socket",
|
|
|
|
|
"pcmcia_socket*",
|
|
|
|
|
SearchOption.TopDirectoryOnly);
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(subdirs.Length <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
string possibleDir = Path.Combine(resolvedLink, "pcmcia_socket", subdirs[0]);
|
|
|
|
|
|
|
|
|
|
if(!File.Exists(possibleDir + "/card_type") ||
|
|
|
|
|
!File.Exists(possibleDir + "/cis"))
|
2019-10-19 01:10:06 +01:00
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2019-10-19 01:10:06 +01:00
|
|
|
var cisFs = new FileStream(possibleDir + "/cis", System.IO.FileMode.Open,
|
2020-01-09 15:02:21 +00:00
|
|
|
System.IO.FileAccess.Read);
|
|
|
|
|
|
|
|
|
|
byte[] cisBuf = new byte[65536];
|
|
|
|
|
int cisCount = cisFs.Read(cisBuf, 0, 65536);
|
2019-10-19 01:10:06 +01:00
|
|
|
Cis = new byte[cisCount];
|
|
|
|
|
Array.Copy(cisBuf, 0, Cis, 0, cisCount);
|
|
|
|
|
cisFs.Close();
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2019-10-19 01:10:06 +01:00
|
|
|
IsPcmcia = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 01:10:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2019-10-19 01:10:06 +01:00
|
|
|
// TODO: Implement for other operating systems
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
IsPcmcia = false;
|
|
|
|
|
}
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
2019-10-13 21:17:38 +01:00
|
|
|
else
|
|
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(_remote.GetPcmciaData(out byte[] cisBuf))
|
2019-10-19 01:10:06 +01:00
|
|
|
{
|
|
|
|
|
IsPcmcia = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
Cis = cisBuf;
|
2019-10-19 01:10:06 +01:00
|
|
|
}
|
2019-10-13 21:17:38 +01:00
|
|
|
}
|
2016-10-17 04:41:27 +01:00
|
|
|
#endregion PCMCIA
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!scsiSense)
|
2015-11-05 06:50:02 +00:00
|
|
|
{
|
2020-01-11 20:55:54 +00:00
|
|
|
Inquiry? inquiry = Inquiry.Decode(inqBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
Type = DeviceType.SCSI;
|
2020-01-09 15:02:21 +00:00
|
|
|
bool serialSense = ScsiInquiry(out inqBuf, out _, 0x80);
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!serialSense)
|
|
|
|
|
Serial = EVPD.DecodePage80(inqBuf);
|
|
|
|
|
|
|
|
|
|
if(inquiry.HasValue)
|
2015-11-05 06:50:02 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string tmp = StringHandlers.CToString(inquiry.Value.ProductRevisionLevel);
|
|
|
|
|
|
|
|
|
|
if(tmp != null)
|
|
|
|
|
FirmwareRevision = tmp.Trim();
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
tmp = StringHandlers.CToString(inquiry.Value.ProductIdentification);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(tmp != null)
|
|
|
|
|
Model = tmp.Trim();
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
tmp = StringHandlers.CToString(inquiry.Value.VendorIdentification);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
|
|
|
|
if(tmp != null)
|
|
|
|
|
Manufacturer = tmp.Trim();
|
|
|
|
|
|
2017-12-21 07:19:46 +00:00
|
|
|
IsRemovable = inquiry.Value.RMB;
|
2015-10-19 05:20:42 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
ScsiType = (PeripheralDeviceTypes)inquiry.Value.PeripheralDeviceType;
|
2015-10-19 05:11:28 +01:00
|
|
|
}
|
2015-11-05 06:50:02 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
bool atapiSense = AtapiIdentify(out ataBuf, out _);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(!atapiSense)
|
2015-10-19 05:11:28 +01:00
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
Type = DeviceType.ATAPI;
|
2020-01-09 15:02:21 +00:00
|
|
|
Identify.IdentifyDevice? ataId = Identify.Decode(ataBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(ataId.HasValue)
|
|
|
|
|
Serial = ataId.Value.SerialNumber;
|
2015-10-19 05:11:28 +01:00
|
|
|
}
|
2018-09-01 23:49:36 +01:00
|
|
|
|
|
|
|
|
LastError = 0;
|
2020-01-09 15:02:21 +00:00
|
|
|
Error = false;
|
2015-11-05 06:50:02 +00:00
|
|
|
}
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2020-01-31 23:13:11 +00:00
|
|
|
if((scsiSense && !(IsUsb || IsFireWire)) ||
|
2020-01-09 15:02:21 +00:00
|
|
|
Manufacturer == "ATA")
|
2015-11-05 06:50:02 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
bool ataSense = AtaIdentify(out ataBuf, out _);
|
|
|
|
|
|
|
|
|
|
if(!ataSense)
|
2015-10-19 05:11:28 +01:00
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
Type = DeviceType.ATA;
|
2020-01-09 15:02:21 +00:00
|
|
|
Identify.IdentifyDevice? ataid = Identify.Decode(ataBuf);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(ataid.HasValue)
|
2015-10-19 05:11:28 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
string[] separated = ataid.Value.Model.Split(' ');
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(separated.Length == 1)
|
2019-10-13 21:17:38 +01:00
|
|
|
{
|
|
|
|
|
Model = separated[0];
|
|
|
|
|
}
|
2015-11-05 06:50:02 +00:00
|
|
|
else
|
|
|
|
|
{
|
2017-12-21 07:19:46 +00:00
|
|
|
Manufacturer = separated[0];
|
2020-04-22 00:22:34 +01:00
|
|
|
Model = separated[^1];
|
2015-11-05 06:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
FirmwareRevision = ataid.Value.FirmwareRevision;
|
|
|
|
|
Serial = ataid.Value.SerialNumber;
|
2015-10-19 05:20:42 +01:00
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
ScsiType = PeripheralDeviceTypes.DirectAccess;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if((ushort)ataid.Value.GeneralConfiguration != 0x848A)
|
2017-12-21 07:19:46 +00:00
|
|
|
IsRemovable |=
|
2017-12-20 17:15:26 +00:00
|
|
|
(ataid.Value.GeneralConfiguration & Identify.GeneralConfigurationBit.Removable) ==
|
2017-12-19 20:33:03 +00:00
|
|
|
Identify.GeneralConfigurationBit.Removable;
|
2020-01-09 15:02:21 +00:00
|
|
|
else
|
|
|
|
|
IsCompactFlash = true;
|
2015-10-19 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(Type == DeviceType.Unknown)
|
2015-10-19 05:11:28 +01:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
Manufacturer = null;
|
|
|
|
|
Model = null;
|
|
|
|
|
FirmwareRevision = null;
|
|
|
|
|
Serial = null;
|
2015-10-19 05:11:28 +01:00
|
|
|
}
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(IsUsb)
|
2015-12-31 16:12:22 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(string.IsNullOrEmpty(Manufacturer))
|
|
|
|
|
Manufacturer = UsbManufacturerString;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrEmpty(Model))
|
|
|
|
|
Model = UsbProductString;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrEmpty(Serial))
|
|
|
|
|
Serial = UsbSerialString;
|
2018-04-02 23:09:18 +01:00
|
|
|
else
|
2020-07-20 06:09:42 +01:00
|
|
|
foreach(char c in Serial.Where(c => !char.IsControl(c)))
|
|
|
|
|
Serial = $"{(uint)c:X2}";
|
2015-12-31 16:12:22 +00:00
|
|
|
}
|
2015-12-31 16:33:20 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(IsFireWire)
|
2019-02-12 23:04:53 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
if(string.IsNullOrEmpty(Manufacturer))
|
|
|
|
|
Manufacturer = FireWireVendorName;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrEmpty(Model))
|
|
|
|
|
Model = FireWireModelName;
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrEmpty(Serial))
|
2020-07-20 21:11:32 +01:00
|
|
|
Serial = $"{_firewireGuid:X16}";
|
2019-02-12 23:04:53 +00:00
|
|
|
else
|
2020-07-20 06:09:42 +01:00
|
|
|
foreach(char c in Serial.Where(c => !char.IsControl(c)))
|
|
|
|
|
Serial = $"{(uint)c:X2}";
|
2019-02-12 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Some optical drives are not getting the correct serial, and IDENTIFY PACKET DEVICE is blocked without
|
|
|
|
|
// administrator privileges
|
2020-01-09 15:02:21 +00:00
|
|
|
if(ScsiType != PeripheralDeviceTypes.MultiMediaDevice)
|
|
|
|
|
return;
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
bool featureSense = GetConfiguration(out byte[] featureBuffer, out _, 0x0108, MmcGetConfigurationRt.Single,
|
|
|
|
|
Timeout, out _);
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(featureSense)
|
|
|
|
|
return;
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
Features.SeparatedFeatures features = Features.Separate(featureBuffer);
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(features.Descriptors?.Length != 1 ||
|
|
|
|
|
features.Descriptors[0].Code != 0x0108)
|
|
|
|
|
return;
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
Feature_0108? serialFeature = Features.Decode_0108(features.Descriptors[0].Data);
|
|
|
|
|
|
|
|
|
|
if(serialFeature is null)
|
|
|
|
|
return;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2019-02-12 23:04:53 +00:00
|
|
|
Serial = serialFeature.Value.Serial;
|
2015-10-12 19:55:00 +01:00
|
|
|
}
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
static int ConvertFromHexAscii(string file, out byte[] outBuf)
|
2017-09-28 19:14:50 +00:00
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
var sr = new StreamReader(file);
|
|
|
|
|
string ins = sr.ReadToEnd().Trim();
|
2020-06-05 23:30:17 +01:00
|
|
|
|
|
|
|
|
if(ins.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
ins = ins.Substring(2);
|
|
|
|
|
|
2017-09-28 19:14:50 +00:00
|
|
|
outBuf = new byte[ins.Length / 2];
|
2020-01-09 15:02:21 +00:00
|
|
|
int count = 0;
|
2017-09-28 19:14:50 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-01-09 15:02:21 +00:00
|
|
|
for(int i = 0; i < ins.Length; i += 2)
|
2017-09-28 19:14:50 +00:00
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
outBuf[i / 2] = Convert.ToByte(ins.Substring(i, 2), 16);
|
2017-09-28 19:14:50 +00:00
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-13 21:17:38 +01:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
count = 0;
|
|
|
|
|
}
|
2017-09-28 19:14:50 +00:00
|
|
|
|
|
|
|
|
sr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2017-09-28 19:14:50 +00:00
|
|
|
return count;
|
|
|
|
|
}
|
2015-10-12 19:55:00 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|