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
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:53 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2015-10-12 19:55:00 +01:00
|
|
|
// ****************************************************************************/
|
2016-07-28 18:13:49 +01:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
namespace Aaru.Devices;
|
|
|
|
|
|
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;
|
2021-09-14 20:44:06 +01:00
|
|
|
using Aaru.Devices.Linux;
|
2020-02-27 00:33:26 +00:00
|
|
|
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 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;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>Implements a device or media containing drive</summary>
|
|
|
|
|
[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
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>Opens the device for sending direct commands</summary>
|
|
|
|
|
/// <param name="devicePath">Device path</param>
|
2022-03-26 17:46:59 +00:00
|
|
|
public static Device Create(string devicePath)
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
var dev = new Device();
|
|
|
|
|
|
|
|
|
|
dev.PlatformId = DetectOS.GetRealPlatformID();
|
|
|
|
|
dev.Timeout = 15;
|
|
|
|
|
dev.Error = false;
|
|
|
|
|
dev.IsRemovable = false;
|
|
|
|
|
dev._devicePath = devicePath;
|
2015-10-12 19:55:00 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Uri aaruUri;
|
2020-03-01 06:02:40 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
aaruUri = new Uri(devicePath);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception)
|
|
|
|
|
{
|
|
|
|
|
// Ignore, treat as local path below
|
|
|
|
|
aaruUri = null;
|
|
|
|
|
}
|
2019-10-13 21:17:38 +01:00
|
|
|
|
2022-03-16 11:47:00 +00:00
|
|
|
if(aaruUri?.Scheme is "dic" or "aaru")
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
devicePath = aaruUri.AbsolutePath;
|
2019-10-13 21:17:38 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(devicePath.StartsWith('/'))
|
|
|
|
|
devicePath = devicePath.Substring(1);
|
2020-10-24 15:05:38 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(devicePath.StartsWith("dev", StringComparison.Ordinal))
|
|
|
|
|
devicePath = $"/{devicePath}";
|
2021-06-08 01:04:46 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._remote = new Remote.Remote(aaruUri);
|
2021-06-22 20:23:21 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Error = !dev._remote.Open(devicePath, out int errno);
|
|
|
|
|
dev.LastError = errno;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
switch(dev.PlatformId)
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case PlatformID.Win32NT:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FileHandle = Extern.CreateFile(devicePath, FileAccess.GenericRead | FileAccess.GenericWrite,
|
|
|
|
|
FileShare.Read | FileShare.Write, IntPtr.Zero,
|
|
|
|
|
FileMode.OpenExisting, FileAttributes.Normal, IntPtr.Zero);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(((SafeFileHandle)dev.FileHandle).IsInvalid)
|
2015-10-12 19:55:00 +01:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Error = true;
|
|
|
|
|
dev.LastError = Marshal.GetLastWin32Error();
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2015-10-13 01:45:07 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PlatformID.Linux:
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FileHandle =
|
2022-03-06 13:29:38 +00:00
|
|
|
Linux.Extern.open(devicePath,
|
|
|
|
|
FileFlags.ReadWrite | FileFlags.NonBlocking | FileFlags.CreateNew);
|
2015-10-12 19:55:00 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if((int)dev.FileHandle < 0)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = Marshal.GetLastWin32Error();
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.LastError is 13 or 30) // EACCES or EROFS
|
2018-04-02 23:09:18 +01:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FileHandle = Linux.Extern.open(devicePath, FileFlags.Readonly | FileFlags.NonBlocking);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if((int)dev.FileHandle < 0)
|
2018-04-02 23:09:18 +01:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Error = true;
|
|
|
|
|
dev.LastError = Marshal.GetLastWin32Error();
|
2018-04-02 23:09:18 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Error = true;
|
2019-10-13 22:29:09 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = Marshal.GetLastWin32Error();
|
2019-10-13 22:29:09 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
break;
|
2017-12-10 21:00:20 +00:00
|
|
|
}
|
2022-03-26 17:46:59 +00:00
|
|
|
default: throw new DeviceException($"Platform {dev.PlatformId} not supported.");
|
2015-10-12 19:55:00 +01:00
|
|
|
}
|
2015-10-19 04:46:09 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.Error)
|
|
|
|
|
throw new DeviceException(dev.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
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Seems ioctl(2) does not allow the atomicity needed
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._remote is null)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.PlatformId == PlatformID.Linux)
|
2020-12-10 13:53:16 +00:00
|
|
|
_readMultipleBlockCannotSetBlockCount = true;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2022-03-26 17:46:59 +00:00
|
|
|
else if(dev._remote.ServerOperatingSystem == "Linux")
|
2022-03-06 13:29:38 +00:00
|
|
|
_readMultipleBlockCannotSetBlockCount = true;
|
2020-12-10 13:53:16 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.Unknown;
|
|
|
|
|
dev.ScsiType = PeripheralDeviceTypes.UnknownDevice;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
byte[] ataBuf;
|
|
|
|
|
byte[] inqBuf = null;
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.Error)
|
|
|
|
|
throw new DeviceException(dev.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
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
var scsiSense = true;
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._remote is null)
|
2022-03-07 07:36:44 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Windows is answering SCSI INQUIRY for all device types so it needs to be detected first
|
2022-03-26 17:46:59 +00:00
|
|
|
switch(dev.PlatformId)
|
2017-12-23 20:04:36 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
var query = new StoragePropertyQuery();
|
|
|
|
|
query.PropertyId = StoragePropertyId.Device;
|
|
|
|
|
query.QueryType = StorageQueryType.Standard;
|
|
|
|
|
query.AdditionalParameters = new byte[1];
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
IntPtr descriptorPtr = Marshal.AllocHGlobal(1000);
|
2022-03-07 07:36:44 +00:00
|
|
|
var descriptorB = new byte[1000];
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
uint returned = 0;
|
2022-03-07 07:36:44 +00:00
|
|
|
var error = 0;
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
bool hasError = !Extern.DeviceIoControlStorageQuery((SafeFileHandle)dev.FileHandle,
|
2022-03-06 13:29:38 +00:00
|
|
|
WindowsIoctl.IoctlStorageQueryProperty,
|
|
|
|
|
ref query, (uint)Marshal.SizeOf(query),
|
2022-03-07 07:36:44 +00:00
|
|
|
descriptorPtr, 1000, ref returned, IntPtr.Zero);
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(hasError)
|
|
|
|
|
error = Marshal.GetLastWin32Error();
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Marshal.Copy(descriptorPtr, descriptorB, 0, 1000);
|
2017-09-07 16:47:06 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!hasError &&
|
|
|
|
|
error == 0)
|
|
|
|
|
{
|
|
|
|
|
var descriptor = new StorageDeviceDescriptor
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00: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),
|
|
|
|
|
ProductRevisionOffset = BitConverter.ToInt32(descriptorB, 20),
|
|
|
|
|
SerialNumberOffset = BitConverter.ToInt32(descriptorB, 24),
|
|
|
|
|
BusType = (StorageBusType)BitConverter.ToUInt32(descriptorB, 28),
|
|
|
|
|
RawPropertiesLength = BitConverter.ToUInt32(descriptorB, 32)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
descriptor.RawDeviceProperties = new byte[descriptor.RawPropertiesLength];
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
Array.Copy(descriptorB, 36, descriptor.RawDeviceProperties, 0, descriptor.RawPropertiesLength);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
switch(descriptor.BusType)
|
|
|
|
|
{
|
|
|
|
|
case StorageBusType.SCSI:
|
|
|
|
|
case StorageBusType.SSA:
|
|
|
|
|
case StorageBusType.Fibre:
|
|
|
|
|
case StorageBusType.iSCSI:
|
|
|
|
|
case StorageBusType.SAS:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.SCSI;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.FireWire:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsFireWire = true;
|
|
|
|
|
dev.Type = DeviceType.SCSI;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.USB:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsUsb = true;
|
|
|
|
|
dev.Type = DeviceType.SCSI;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.ATAPI:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.ATAPI;
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.ATA:
|
|
|
|
|
case StorageBusType.SATA:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.ATA;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.MultiMediaCard:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.MMC;
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.SecureDigital:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.SecureDigital;
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case StorageBusType.NVMe:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.NVMe;
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
2017-09-07 16:47:06 +01:00
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
switch(dev.Type)
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case DeviceType.SCSI:
|
|
|
|
|
case DeviceType.ATAPI:
|
2022-03-26 17:46:59 +00:00
|
|
|
scsiSense = dev.ScsiInquiry(out inqBuf, out _);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case DeviceType.ATA:
|
2022-03-26 17:46:59 +00:00
|
|
|
bool atapiSense = dev.AtapiIdentify(out ataBuf, out _);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!atapiSense)
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.ATAPI;
|
2022-03-06 13:29:38 +00:00
|
|
|
Identify.IdentifyDevice? ataid = Identify.Decode(ataBuf);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(ataid.HasValue)
|
2022-03-26 17:46:59 +00:00
|
|
|
scsiSense = dev.ScsiInquiry(out inqBuf, out _);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Manufacturer = "ATA";
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Marshal.FreeHGlobal(descriptorPtr);
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(Windows.Command.IsSdhci((SafeFileHandle)dev.FileHandle))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
var sdBuffer = new byte[16];
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = Windows.Command.SendMmcCommand((SafeFileHandle)dev.FileHandle,
|
|
|
|
|
MmcCommands.SendCsd, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR2 | MmcFlags.ResponseR2 |
|
|
|
|
|
MmcFlags.CommandAc, 0, 16, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out bool sense);
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!sense)
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedCsd = new byte[16];
|
|
|
|
|
Array.Copy(sdBuffer, 0, dev._cachedCsd, 0, 16);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
sdBuffer = new byte[16];
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = Windows.Command.SendMmcCommand((SafeFileHandle)dev.FileHandle,
|
|
|
|
|
MmcCommands.SendCid, false, false,
|
|
|
|
|
MmcFlags.ResponseSpiR2 | MmcFlags.ResponseR2 |
|
|
|
|
|
MmcFlags.CommandAc, 0, 16, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out sense);
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!sense)
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedCid = new byte[16];
|
|
|
|
|
Array.Copy(sdBuffer, 0, dev._cachedCid, 0, 16);
|
2017-12-06 23:05:59 +00:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
sdBuffer = new byte[8];
|
|
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = Windows.Command.SendMmcCommand((SafeFileHandle)dev.FileHandle,
|
|
|
|
|
(MmcCommands)SecureDigitalCommands.SendScr,
|
|
|
|
|
false, true,
|
|
|
|
|
MmcFlags.ResponseSpiR1 | MmcFlags.ResponseR1 |
|
|
|
|
|
MmcFlags.CommandAdtc, 0, 8, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out sense);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(!sense)
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedScr = new byte[8];
|
|
|
|
|
Array.Copy(sdBuffer, 0, dev._cachedScr, 0, 8);
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
sdBuffer = new byte[4];
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = Windows.Command.SendMmcCommand((SafeFileHandle)dev.FileHandle,
|
|
|
|
|
dev._cachedScr != null
|
|
|
|
|
? (MmcCommands)SecureDigitalCommands.
|
|
|
|
|
SendOperatingCondition
|
|
|
|
|
: MmcCommands.SendOpCond, false, true,
|
|
|
|
|
MmcFlags.ResponseSpiR3 | MmcFlags.ResponseR3 |
|
|
|
|
|
MmcFlags.CommandBcr, 0, 4, 1, ref sdBuffer,
|
|
|
|
|
out _, out _, out sense);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!sense)
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedScr = new byte[4];
|
|
|
|
|
Array.Copy(sdBuffer, 0, dev._cachedScr, 0, 4);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case PlatformID.Linux:
|
|
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sg", StringComparison.Ordinal))
|
2022-03-26 17:46:59 +00:00
|
|
|
scsiSense = dev.ScsiInquiry(out inqBuf, out _);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// MultiMediaCard and SecureDigital go here
|
|
|
|
|
else if(devicePath.StartsWith("/dev/mmcblk", StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
string devPath = devicePath.Substring(5);
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/csd"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
int len = ConvertFromFileHexAscii("/sys/block/" + devPath + "/device/csd",
|
|
|
|
|
out dev._cachedCsd);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(len == 0)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedCsd = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-10-16 20:07:24 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/cid"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
int len = ConvertFromFileHexAscii("/sys/block/" + devPath + "/device/cid",
|
|
|
|
|
out dev._cachedCid);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(len == 0)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedCid = null;
|
2017-12-21 04:43:29 +00:00
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/scr"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
int len = ConvertFromFileHexAscii("/sys/block/" + devPath + "/device/scr",
|
|
|
|
|
out dev._cachedScr);
|
2019-10-17 22:15:01 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(len == 0)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedScr = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists("/sys/block/" + devPath + "/device/ocr"))
|
2019-10-17 22:15:01 +01:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
int len = ConvertFromFileHexAscii("/sys/block/" + devPath + "/device/ocr",
|
|
|
|
|
out dev._cachedOcr);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(len == 0)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev._cachedOcr = null;
|
2019-10-17 22:15:01 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-10-17 22:15:01 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2022-03-26 17:46:59 +00:00
|
|
|
scsiSense = dev.ScsiInquiry(out inqBuf, out _);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
break;
|
2017-12-06 23:05:59 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = dev._remote.GetDeviceType();
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
switch(dev.Type)
|
2017-12-06 23:05:59 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case DeviceType.ATAPI:
|
|
|
|
|
case DeviceType.SCSI:
|
2022-03-26 17:46:59 +00:00
|
|
|
scsiSense = dev.ScsiInquiry(out inqBuf, out _);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case DeviceType.SecureDigital:
|
|
|
|
|
case DeviceType.MMC:
|
2022-03-26 17:46:59 +00:00
|
|
|
if(!dev._remote.GetSdhciRegisters(out dev._cachedCsd, out dev._cachedCid, out dev._cachedOcr,
|
|
|
|
|
out dev._cachedScr))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.SCSI;
|
|
|
|
|
dev.ScsiType = PeripheralDeviceTypes.DirectAccess;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
#region SecureDigital / MultiMediaCard
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._cachedCid != null)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.ScsiType = PeripheralDeviceTypes.DirectAccess;
|
|
|
|
|
dev.IsRemovable = false;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._cachedScr != null)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.SecureDigital;
|
|
|
|
|
CID decoded = Decoders.DecodeCID(dev._cachedCid);
|
|
|
|
|
dev.Manufacturer = VendorString.Prettify(decoded.Manufacturer);
|
|
|
|
|
dev.Model = decoded.ProductName;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FirmwareRevision =
|
|
|
|
|
$"{(decoded.ProductRevision & 0xF0) >> 4:X2}.{decoded.ProductRevision & 0x0F:X2}";
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Serial = $"{decoded.ProductSerialNumber}";
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.MMC;
|
|
|
|
|
Aaru.Decoders.MMC.CID decoded = Aaru.Decoders.MMC.Decoders.DecodeCID(dev._cachedCid);
|
|
|
|
|
dev.Manufacturer = Aaru.Decoders.MMC.VendorString.Prettify(decoded.Manufacturer);
|
|
|
|
|
dev.Model = decoded.ProductName;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FirmwareRevision =
|
|
|
|
|
$"{(decoded.ProductRevision & 0xF0) >> 4:X2}.{decoded.ProductRevision & 0x0F:X2}";
|
2020-05-13 10:42:58 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Serial = $"{decoded.ProductSerialNumber}";
|
2017-09-28 17:54:07 +00:00
|
|
|
}
|
2017-12-06 13:46:35 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
return dev;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
#endregion SecureDigital / MultiMediaCard
|
|
|
|
|
|
|
|
|
|
#region USB
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._remote is null)
|
|
|
|
|
switch(dev.PlatformId)
|
2017-12-23 20:04:36 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case PlatformID.Linux:
|
|
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
string devPath = devicePath.Substring(5);
|
|
|
|
|
|
|
|
|
|
if(Directory.Exists("/sys/block/" + devPath))
|
2017-12-21 04:43:29 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!string.IsNullOrEmpty(resolvedLink))
|
2019-10-18 22:44:02 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
resolvedLink = "/sys" + resolvedLink.Substring(2);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(resolvedLink.Contains("usb"))
|
2015-12-31 16:12:22 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
resolvedLink = Path.GetDirectoryName(resolvedLink);
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!File.Exists(resolvedLink + "/descriptors") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/idProduct") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/idVendor"))
|
|
|
|
|
continue;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
var usbFs = new FileStream(resolvedLink + "/descriptors", System.IO.FileMode.Open,
|
|
|
|
|
System.IO.FileAccess.Read);
|
2019-10-18 23:42:18 +01:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
var usbBuf = new byte[65536];
|
|
|
|
|
int usbCount = usbFs.Read(usbBuf, 0, 65536);
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.UsbDescriptors = new byte[usbCount];
|
|
|
|
|
Array.Copy(usbBuf, 0, dev.UsbDescriptors, 0, usbCount);
|
2022-03-06 13:29:38 +00:00
|
|
|
usbFs.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var usbSr = new StreamReader(resolvedLink + "/idProduct");
|
|
|
|
|
string usbTemp = usbSr.ReadToEnd();
|
2019-10-18 23:42:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ushort.TryParse(usbTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2022-03-26 17:46:59 +00:00
|
|
|
out dev._usbProduct);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
usbSr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/idVendor");
|
|
|
|
|
usbTemp = usbSr.ReadToEnd();
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ushort.TryParse(usbTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2022-03-26 17:46:59 +00:00
|
|
|
out dev._usbVendor);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
usbSr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists(resolvedLink + "/manufacturer"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/manufacturer");
|
|
|
|
|
dev.UsbManufacturerString = usbSr.ReadToEnd().Trim();
|
2017-12-21 06:06:19 +00:00
|
|
|
usbSr.Close();
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(File.Exists(resolvedLink + "/product"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/product");
|
|
|
|
|
dev.UsbProductString = usbSr.ReadToEnd().Trim();
|
2022-03-06 13:29:38 +00:00
|
|
|
usbSr.Close();
|
|
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists(resolvedLink + "/serial"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
usbSr = new StreamReader(resolvedLink + "/serial");
|
|
|
|
|
dev.UsbSerialString = usbSr.ReadToEnd().Trim();
|
2022-03-06 13:29:38 +00:00
|
|
|
usbSr.Close();
|
2019-10-18 23:42:18 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsUsb = true;
|
2022-03-06 13:29:38 +00: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
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
Usb.UsbDevice usbDevice = null;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// I have to search for USB disks, floppies and CD-ROMs as separate device types
|
|
|
|
|
foreach(string devGuid in new[]
|
|
|
|
|
{
|
2022-03-18 01:32:22 +00:00
|
|
|
Usb.GUID_DEVINTERFACE_FLOPPY, Usb.GUID_DEVINTERFACE_CDROM, Usb.GUID_DEVINTERFACE_DISK,
|
|
|
|
|
Usb.GUID_DEVINTERFACE_TAPE
|
2022-03-06 13:29:38 +00:00
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
usbDevice = Usb.FindDrivePath(devicePath, devGuid);
|
2017-12-21 04:43:29 +00:00
|
|
|
|
2020-01-09 15:02:21 +00:00
|
|
|
if(usbDevice != null)
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2018-04-02 23:09:18 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(usbDevice != null)
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.UsbDescriptors = usbDevice.BinaryDescriptors;
|
|
|
|
|
dev._usbVendor = (ushort)usbDevice._deviceDescriptor.idVendor;
|
|
|
|
|
dev._usbProduct = (ushort)usbDevice._deviceDescriptor.idProduct;
|
|
|
|
|
dev.UsbManufacturerString = usbDevice.Manufacturer;
|
|
|
|
|
dev.UsbProductString = usbDevice.Product;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.UsbSerialString =
|
2022-03-07 07:36:44 +00:00
|
|
|
usbDevice.SerialNumber; // This is incorrect filled by Windows with SCSI/ATA serial number
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsUsb = false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
break;
|
2019-10-18 23:42:18 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._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
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsUsb = true;
|
|
|
|
|
dev.UsbDescriptors = remoteUsbDescriptors;
|
|
|
|
|
dev._usbVendor = remoteUsbVendor;
|
|
|
|
|
dev._usbProduct = remoteUsbProduct;
|
|
|
|
|
dev.UsbManufacturerString = remoteUsbManufacturer;
|
|
|
|
|
dev.UsbProductString = remoteUsbProductString;
|
|
|
|
|
dev.UsbSerialString = remoteUsbSerial;
|
2015-12-31 16:12:22 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
#endregion USB
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
#region FireWire
|
2022-03-26 17:46:59 +00:00
|
|
|
if(!(dev._remote is null))
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._remote.GetFireWireData(out dev._firewireVendor, out dev._firewireModel, out dev._firewireGuid,
|
|
|
|
|
out string remoteFireWireVendorName, out string remoteFireWireModelName))
|
2015-12-31 16:33:20 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsFireWire = true;
|
|
|
|
|
dev.FireWireVendorName = remoteFireWireVendorName;
|
|
|
|
|
dev.FireWireModelName = remoteFireWireModelName;
|
2019-10-19 00:08:33 +01:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.PlatformId == PlatformID.Linux)
|
2019-10-19 00:08:33 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
if(devicePath.StartsWith("/dev/sd", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/sr", StringComparison.Ordinal) ||
|
|
|
|
|
devicePath.StartsWith("/dev/st", StringComparison.Ordinal))
|
2019-10-19 00:08:33 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
string devPath = devicePath.Substring(5);
|
|
|
|
|
|
|
|
|
|
if(Directory.Exists("/sys/block/" + devPath))
|
2015-12-31 16:33:20 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
|
|
|
|
|
resolvedLink = "/sys" + resolvedLink.Substring(2);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!string.IsNullOrEmpty(resolvedLink))
|
|
|
|
|
while(resolvedLink.Contains("firewire"))
|
|
|
|
|
{
|
|
|
|
|
resolvedLink = Path.GetDirectoryName(resolvedLink);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!File.Exists(resolvedLink + "/model") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/vendor") ||
|
|
|
|
|
!File.Exists(resolvedLink + "/guid"))
|
|
|
|
|
continue;
|
2019-10-19 00:08:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var fwSr = new StreamReader(resolvedLink + "/model");
|
|
|
|
|
string fwTemp = fwSr.ReadToEnd();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
uint.TryParse(fwTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2022-03-26 17:46:59 +00:00
|
|
|
out dev._firewireModel);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fwSr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/vendor");
|
|
|
|
|
fwTemp = fwSr.ReadToEnd();
|
2015-12-31 16:33:20 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
uint.TryParse(fwTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2022-03-26 17:46:59 +00:00
|
|
|
out dev._firewireVendor);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fwSr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/guid");
|
|
|
|
|
fwTemp = fwSr.ReadToEnd();
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ulong.TryParse(fwTemp, NumberStyles.HexNumber, CultureInfo.InvariantCulture,
|
2022-03-26 17:46:59 +00:00
|
|
|
out dev._firewireGuid);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fwSr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists(resolvedLink + "/model_name"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/model_name");
|
|
|
|
|
dev.FireWireModelName = fwSr.ReadToEnd().Trim();
|
2019-10-19 00:08:33 +01:00
|
|
|
fwSr.Close();
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-10-19 00:08:33 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(File.Exists(resolvedLink + "/vendor_name"))
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
fwSr = new StreamReader(resolvedLink + "/vendor_name");
|
|
|
|
|
dev.FireWireVendorName = fwSr.ReadToEnd().Trim();
|
2022-03-06 13:29:38 +00:00
|
|
|
fwSr.Close();
|
|
|
|
|
}
|
2019-10-19 00:08:33 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsFireWire = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2015-12-31 16:33:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// TODO: Implement for other operating systems
|
|
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsFireWire = false;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
#endregion FireWire
|
2015-12-31 16:33:20 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
#region PCMCIA
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._remote is null)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.PlatformId == PlatformID.Linux)
|
2016-10-17 04:41:27 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +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
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
string devPath = devicePath.Substring(5);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(Directory.Exists("/sys/block/" + devPath))
|
|
|
|
|
{
|
|
|
|
|
string resolvedLink = Linux.Command.ReadLink("/sys/block/" + devPath);
|
|
|
|
|
resolvedLink = "/sys" + resolvedLink.Substring(2);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!string.IsNullOrEmpty(resolvedLink))
|
|
|
|
|
while(resolvedLink.Contains("/sys/devices"))
|
|
|
|
|
{
|
|
|
|
|
resolvedLink = Path.GetDirectoryName(resolvedLink);
|
2016-10-17 04:41:27 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!Directory.Exists(resolvedLink + "/pcmcia_socket"))
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string[] subdirs = Directory.GetDirectories(resolvedLink + "/pcmcia_socket",
|
|
|
|
|
"pcmcia_socket*",
|
|
|
|
|
SearchOption.TopDirectoryOnly);
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(subdirs.Length <= 0)
|
|
|
|
|
continue;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string possibleDir = Path.Combine(resolvedLink, "pcmcia_socket", subdirs[0]);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!File.Exists(possibleDir + "/card_type") ||
|
|
|
|
|
!File.Exists(possibleDir + "/cis"))
|
|
|
|
|
continue;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var cisFs = new FileStream(possibleDir + "/cis", System.IO.FileMode.Open,
|
|
|
|
|
System.IO.FileAccess.Read);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
var cisBuf = new byte[65536];
|
|
|
|
|
int cisCount = cisFs.Read(cisBuf, 0, 65536);
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Cis = new byte[cisCount];
|
|
|
|
|
Array.Copy(cisBuf, 0, dev.Cis, 0, cisCount);
|
2022-03-06 13:29:38 +00:00
|
|
|
cisFs.Close();
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsPcmcia = true;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2016-10-17 04:41:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
// TODO: Implement for other operating systems
|
2019-10-13 21:17:38 +01:00
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsPcmcia = false;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev._remote.GetPcmciaData(out byte[] cisBuf))
|
2015-11-05 06:50:02 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsPcmcia = true;
|
|
|
|
|
dev.Cis = cisBuf;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion PCMCIA
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!scsiSense)
|
|
|
|
|
{
|
|
|
|
|
Inquiry? inquiry = Inquiry.Decode(inqBuf);
|
2016-04-19 02:11:47 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.SCSI;
|
|
|
|
|
bool serialSense = dev.ScsiInquiry(out inqBuf, out _, 0x80);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!serialSense)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Serial = EVPD.DecodePage80(inqBuf);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(inquiry.HasValue)
|
|
|
|
|
{
|
|
|
|
|
string tmp = StringHandlers.CToString(inquiry.Value.ProductRevisionLevel);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(tmp != null)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FirmwareRevision = tmp.Trim();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
tmp = StringHandlers.CToString(inquiry.Value.ProductIdentification);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(tmp != null)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Model = tmp.Trim();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
tmp = StringHandlers.CToString(inquiry.Value.VendorIdentification);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(tmp != null)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Manufacturer = tmp.Trim();
|
2015-10-19 05:20:42 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsRemovable = inquiry.Value.RMB;
|
2015-11-05 06:50:02 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.ScsiType = (PeripheralDeviceTypes)inquiry.Value.PeripheralDeviceType;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
bool atapiSense = dev.AtapiIdentify(out ataBuf, out _);
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(!atapiSense)
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.ATAPI;
|
2022-03-06 13:29:38 +00:00
|
|
|
Identify.IdentifyDevice? ataId = Identify.Decode(ataBuf);
|
2018-09-01 23:49:36 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(ataId.HasValue)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Serial = ataId.Value.SerialNumber;
|
2015-11-05 06:50:02 +00:00
|
|
|
}
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.LastError = 0;
|
|
|
|
|
dev.Error = false;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(scsiSense && !(dev.IsUsb || dev.IsFireWire) ||
|
|
|
|
|
dev.Manufacturer == "ATA")
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
bool ataSense = dev.AtaIdentify(out ataBuf, out _);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(!ataSense)
|
2015-11-05 06:50:02 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Type = DeviceType.ATA;
|
2022-03-06 13:29:38 +00:00
|
|
|
Identify.IdentifyDevice? ataid = Identify.Decode(ataBuf);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(ataid.HasValue)
|
2015-10-19 05:11:28 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
string[] separated = ataid.Value.Model.Split(' ');
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(separated.Length == 1)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Model = separated[0];
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
|
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Manufacturer = separated[0];
|
|
|
|
|
dev.Model = separated[^1];
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2015-11-05 06:50:02 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.FirmwareRevision = ataid.Value.FirmwareRevision;
|
|
|
|
|
dev.Serial = ataid.Value.SerialNumber;
|
2015-10-19 05:20:42 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.ScsiType = PeripheralDeviceTypes.DirectAccess;
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if((ushort)ataid.Value.GeneralConfiguration != 0x848A)
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsRemovable |=
|
2022-03-06 13:29:38 +00:00
|
|
|
(ataid.Value.GeneralConfiguration & Identify.GeneralConfigurationBit.Removable) ==
|
|
|
|
|
Identify.GeneralConfigurationBit.Removable;
|
|
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.IsCompactFlash = true;
|
2015-10-19 05:11:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2015-10-19 05:11:28 +01:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.Type == DeviceType.Unknown)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
dev.Manufacturer = null;
|
|
|
|
|
dev.Model = null;
|
|
|
|
|
dev.FirmwareRevision = null;
|
|
|
|
|
dev.Serial = null;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2015-12-31 16:12:22 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.IsUsb)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(string.IsNullOrEmpty(dev.Manufacturer))
|
|
|
|
|
dev.Manufacturer = dev.UsbManufacturerString;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(string.IsNullOrEmpty(dev.Model))
|
|
|
|
|
dev.Model = dev.UsbProductString;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(string.IsNullOrEmpty(dev.Serial))
|
|
|
|
|
dev.Serial = dev.UsbSerialString;
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
foreach(char c in dev.Serial.Where(c => !char.IsControl(c)))
|
|
|
|
|
dev.Serial = $"{dev.Serial}{c:X2}";
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2015-12-31 16:33:20 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.IsFireWire)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2022-03-26 17:46:59 +00:00
|
|
|
if(string.IsNullOrEmpty(dev.Manufacturer))
|
|
|
|
|
dev.Manufacturer = dev.FireWireVendorName;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(string.IsNullOrEmpty(dev.Model))
|
|
|
|
|
dev.Model = dev.FireWireModelName;
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
if(string.IsNullOrEmpty(dev.Serial))
|
|
|
|
|
dev.Serial = $"{dev._firewireGuid:X16}";
|
2022-03-06 13:29:38 +00:00
|
|
|
else
|
2022-03-26 17:46:59 +00:00
|
|
|
foreach(char c in dev.Serial.Where(c => !char.IsControl(c)))
|
|
|
|
|
dev.Serial = $"{dev.Serial}{c:X2}";
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Some optical drives are not getting the correct serial, and IDENTIFY PACKET DEVICE is blocked without
|
|
|
|
|
// administrator privileges
|
2022-03-26 17:46:59 +00:00
|
|
|
if(dev.ScsiType != PeripheralDeviceTypes.MultiMediaDevice)
|
|
|
|
|
return dev;
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
bool featureSense = dev.GetConfiguration(out byte[] featureBuffer, out _, 0x0108, MmcGetConfigurationRt.Single,
|
|
|
|
|
dev.Timeout, out _);
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(featureSense)
|
2022-03-26 17:46:59 +00:00
|
|
|
return dev;
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Features.SeparatedFeatures features = Features.Separate(featureBuffer);
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(features.Descriptors?.Length != 1 ||
|
|
|
|
|
features.Descriptors[0].Code != 0x0108)
|
2022-03-26 17:46:59 +00:00
|
|
|
return dev;
|
2019-02-12 23:04:53 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
Feature_0108? serialFeature = Features.Decode_0108(features.Descriptors[0].Data);
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(serialFeature is null)
|
2022-03-26 17:46:59 +00:00
|
|
|
return dev;
|
|
|
|
|
|
|
|
|
|
dev.Serial = serialFeature.Value.Serial;
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
return dev;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2022-03-26 17:46:59 +00:00
|
|
|
Device() {}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
static int ConvertFromFileHexAscii(string file, out byte[] outBuf)
|
|
|
|
|
{
|
|
|
|
|
var sr = new StreamReader(file);
|
|
|
|
|
string ins = sr.ReadToEnd().Trim();
|
2020-06-05 23:30:17 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
int count = Helpers.Marshal.ConvertFromHexAscii(ins, out outBuf);
|
2017-09-28 19:14:50 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
sr.Close();
|
2020-01-09 15:02:21 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return count;
|
2015-10-12 19:55:00 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|