2017-08-22 03:40:43 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : ListDevices.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Windows direct device access.
|
2017-08-22 03:40:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Gets a list of known physical devices.
|
2017-08-22 03:40:43 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-08-22 03:40:43 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
|
2017-08-22 03:40:43 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2017-12-22 03:13:43 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2017-12-21 07:08:26 +00:00
|
|
|
|
using System.Linq;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
using System.Management;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using Microsoft.Win32.SafeHandles;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Devices.Windows
|
|
|
|
|
|
{
|
2017-12-20 02:08:37 +00:00
|
|
|
|
static class ListDevices
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
2017-12-23 02:32:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a hex dump string to the ASCII string it represents
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hex">Hex dump</param>
|
|
|
|
|
|
/// <returns>Decoded string</returns>
|
2017-12-22 03:13:43 +00:00
|
|
|
|
static string HexStringToString(string hex)
|
2017-12-20 20:10:28 +01:00
|
|
|
|
{
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
2017-12-22 03:13:43 +00:00
|
|
|
|
const string HEXTABLE = "0123456789abcdef";
|
2017-12-20 20:10:28 +01:00
|
|
|
|
|
2017-12-22 03:13:43 +00:00
|
|
|
|
for(int i = 0; i < hex.Length / 2; i++) result.Append((char)(16 * HEXTABLE.IndexOf(hex[2 * i]) + HEXTABLE.IndexOf(hex[2 * i + 1])));
|
2017-12-20 20:10:28 +01:00
|
|
|
|
|
|
|
|
|
|
return result.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a list of all known storage devices on Windows
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>List of devices</returns>
|
2017-12-22 03:13:43 +00:00
|
|
|
|
[SuppressMessage("ReSharper", "RedundantCatchClause")]
|
2017-12-20 02:08:37 +00:00
|
|
|
|
internal static DeviceInfo[] GetList()
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
List<string> deviceIDs = new List<string>();
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
ManagementObjectSearcher mgmtObjSearcher =
|
|
|
|
|
|
new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
|
2017-08-22 03:40:43 +01:00
|
|
|
|
ManagementObjectCollection objCol = mgmtObjSearcher.Get();
|
|
|
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
|
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["DeviceID"]);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_TapeDrive");
|
|
|
|
|
|
objCol = mgmtObjSearcher.Get();
|
|
|
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
|
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["DeviceID"]);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
|
|
|
|
|
|
objCol = mgmtObjSearcher.Get();
|
|
|
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
|
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["Drive"]);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
}
|
2017-12-22 03:13:43 +00:00
|
|
|
|
catch(Exception)
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
#if DEBUG
|
2017-12-21 17:14:49 +00:00
|
|
|
|
throw;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
#else
|
|
|
|
|
|
return null;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
List<DeviceInfo> devList = new List<DeviceInfo>();
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
foreach(string devId in deviceIDs)
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
string physId = devId;
|
|
|
|
|
|
// TODO: This can be done better
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(devId.Length == 2 && devId[1] == ':') physId = "\\\\?\\" + devId;
|
|
|
|
|
|
SafeFileHandle fd = Extern.CreateFile(physId, 0, FileShare.Read | FileShare.Write, IntPtr.Zero,
|
|
|
|
|
|
FileMode.OpenExisting, 0, IntPtr.Zero);
|
|
|
|
|
|
if(fd.IsInvalid) continue;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-22 03:13:43 +00:00
|
|
|
|
StoragePropertyQuery query = new StoragePropertyQuery
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyId = StoragePropertyId.Device,
|
|
|
|
|
|
QueryType = StorageQueryType.Standard,
|
|
|
|
|
|
AdditionalParameters = new byte[1]
|
|
|
|
|
|
};
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
//StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor();
|
|
|
|
|
|
//descriptor.RawDeviceProperties = new byte[16384];
|
|
|
|
|
|
|
|
|
|
|
|
IntPtr descriptorPtr = Marshal.AllocHGlobal(1000);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
byte[] descriptorB = new byte[1000];
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
uint returned = 0;
|
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
bool hasError = !Extern.DeviceIoControlStorageQuery(fd, WindowsIoctl.IoctlStorageQueryProperty,
|
2017-12-19 20:33:03 +00:00
|
|
|
|
ref query, (uint)Marshal.SizeOf(query),
|
|
|
|
|
|
descriptorPtr, 1000, ref returned, IntPtr.Zero);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(hasError) error = Marshal.GetLastWin32Error();
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
Marshal.Copy(descriptorPtr, descriptorB, 0, 1000);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(hasError && error != 0) continue;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-22 03:13:43 +00:00
|
|
|
|
StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor
|
|
|
|
|
|
{
|
|
|
|
|
|
Version = BitConverter.ToUInt32(descriptorB, 0),
|
|
|
|
|
|
Size = BitConverter.ToUInt32(descriptorB, 4),
|
|
|
|
|
|
DeviceType = descriptorB[8],
|
|
|
|
|
|
DeviceTypeModifier = descriptorB[9],
|
|
|
|
|
|
RemovableMedia = BitConverter.ToBoolean(descriptorB, 10),
|
|
|
|
|
|
CommandQueueing = BitConverter.ToBoolean(descriptorB, 11),
|
|
|
|
|
|
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)
|
|
|
|
|
|
};
|
2017-12-20 17:15:26 +00:00
|
|
|
|
|
|
|
|
|
|
DeviceInfo info = new DeviceInfo {Path = physId, Bus = descriptor.BusType.ToString()};
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
|
|
if(descriptor.VendorIdOffset > 0)
|
2017-12-20 17:15:26 +00:00
|
|
|
|
info.Vendor =
|
2017-12-21 14:30:38 +00:00
|
|
|
|
StringHandlers.CToString(descriptorB, Encoding.ASCII, start: descriptor.VendorIdOffset);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(descriptor.ProductIdOffset > 0)
|
2017-12-20 17:15:26 +00:00
|
|
|
|
info.Model =
|
2017-12-21 14:30:38 +00:00
|
|
|
|
StringHandlers.CToString(descriptorB, Encoding.ASCII, start: descriptor.ProductIdOffset);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
// TODO: Get serial number of SCSI and USB devices, probably also FireWire (untested)
|
2017-12-20 20:41:52 +01:00
|
|
|
|
if(descriptor.SerialNumberOffset > 0)
|
2017-12-20 20:10:28 +01:00
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
info.Serial =
|
|
|
|
|
|
StringHandlers.CToString(descriptorB, Encoding.ASCII,
|
2017-12-21 14:30:38 +00:00
|
|
|
|
start: descriptor.SerialNumberOffset);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-20 20:10:28 +01:00
|
|
|
|
// fix any serial numbers that are returned as hex-strings
|
2017-12-21 14:30:38 +00:00
|
|
|
|
if(Array.TrueForAll(info.Serial.ToCharArray(), c => "0123456789abcdef".IndexOf(c) >= 0)
|
2017-12-20 23:07:46 +00:00
|
|
|
|
&& info.Serial.Length == 40) info.Serial = HexStringToString(info.Serial).Trim();
|
2017-12-20 20:10:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
if(string.IsNullOrEmpty(info.Vendor) || info.Vendor == "ATA")
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
string[] pieces = info.Model.Split(' ');
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(pieces.Length > 1)
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
|
info.Vendor = pieces[0];
|
|
|
|
|
|
info.Model = info.Model.Substring(pieces[0].Length + 1);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
switch(descriptor.BusType)
|
2017-08-22 03:40:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
case StorageBusType.SCSI:
|
|
|
|
|
|
case StorageBusType.ATAPI:
|
|
|
|
|
|
case StorageBusType.ATA:
|
|
|
|
|
|
case StorageBusType.FireWire:
|
|
|
|
|
|
case StorageBusType.SSA:
|
|
|
|
|
|
case StorageBusType.Fibre:
|
|
|
|
|
|
case StorageBusType.USB:
|
|
|
|
|
|
case StorageBusType.iSCSI:
|
|
|
|
|
|
case StorageBusType.SAS:
|
|
|
|
|
|
case StorageBusType.SATA:
|
2017-12-20 17:15:26 +00:00
|
|
|
|
info.Supported = true;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Marshal.FreeHGlobal(descriptorPtr);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
devList.Add(info);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
|
DeviceInfo[] devices = devList.ToArray();
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|