Files
Aaru/DiscImageChef.Devices/Windows/ListDevices.cs

190 lines
8.2 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : ListDevices.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Windows direct device access.
//
// --[ Description ] ----------------------------------------------------------
//
// Gets a list of known physical devices.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
2017-12-19 19:33:46 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
2017-12-19 19:33:46 +00:00
using Microsoft.Win32.SafeHandles;
namespace DiscImageChef.Devices.Windows
{
static class ListDevices
{
internal static string HexStringToString(string hex)
{
StringBuilder result = new StringBuilder();
const string hextable = "0123456789abcdef";
2017-12-20 23:07:46 +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])));
return result.ToString();
}
internal static DeviceInfo[] GetList()
{
List<string> deviceIDs = new List<string>();
try
{
2017-12-19 20:33:03 +00:00
ManagementObjectSearcher mgmtObjSearcher =
new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
ManagementObjectCollection objCol = mgmtObjSearcher.Get();
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["DeviceID"]);
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_TapeDrive");
objCol = mgmtObjSearcher.Get();
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["DeviceID"]);
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
objCol = mgmtObjSearcher.Get();
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["Drive"]);
}
catch(Exception ex)
{
#if DEBUG
2017-12-21 17:14:49 +00:00
throw;
#else
return null;
#endif
}
List<DeviceInfo> devList = new List<DeviceInfo>();
foreach(string devId in deviceIDs)
{
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;
StoragePropertyQuery query = new StoragePropertyQuery();
query.PropertyId = StoragePropertyId.Device;
query.QueryType = StorageQueryType.Standard;
query.AdditionalParameters = new byte[1];
//StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor();
//descriptor.RawDeviceProperties = new byte[16384];
IntPtr descriptorPtr = Marshal.AllocHGlobal(1000);
byte[] descriptorB = new byte[1000];
uint returned = 0;
int error = 0;
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-12-19 20:33:03 +00:00
if(hasError) error = Marshal.GetLastWin32Error();
Marshal.Copy(descriptorPtr, descriptorB, 0, 1000);
2017-12-19 20:33:03 +00:00
if(hasError && error != 0) continue;
StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor();
descriptor.Version = BitConverter.ToUInt32(descriptorB, 0);
descriptor.Size = BitConverter.ToUInt32(descriptorB, 4);
descriptor.DeviceType = descriptorB[8];
descriptor.DeviceTypeModifier = descriptorB[9];
descriptor.RemovableMedia = BitConverter.ToBoolean(descriptorB, 10);
descriptor.CommandQueueing = BitConverter.ToBoolean(descriptorB, 11);
2017-12-20 18:41:50 +01:00
descriptor.VendorIdOffset = BitConverter.ToInt32(descriptorB, 12);
descriptor.ProductIdOffset = BitConverter.ToInt32(descriptorB, 16);
descriptor.ProductRevisionOffset = BitConverter.ToInt32(descriptorB, 20);
descriptor.SerialNumberOffset = BitConverter.ToInt32(descriptorB, 24);
descriptor.BusType = (StorageBusType)BitConverter.ToUInt32(descriptorB, 28);
descriptor.RawPropertiesLength = BitConverter.ToUInt32(descriptorB, 32);
DeviceInfo info = new DeviceInfo {Path = physId, Bus = descriptor.BusType.ToString()};
2017-12-19 20:33:03 +00:00
if(descriptor.VendorIdOffset > 0)
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)
info.Model =
2017-12-21 14:30:38 +00:00
StringHandlers.CToString(descriptorB, Encoding.ASCII, start: descriptor.ProductIdOffset);
// 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)
{
info.Serial =
StringHandlers.CToString(descriptorB, Encoding.ASCII,
2017-12-21 14:30:38 +00:00
start: descriptor.SerialNumberOffset);
// 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();
}
if(string.IsNullOrEmpty(info.Vendor) || info.Vendor == "ATA")
{
string[] pieces = info.Model.Split(' ');
2017-12-19 20:33:03 +00:00
if(pieces.Length > 1)
{
info.Vendor = pieces[0];
info.Model = info.Model.Substring(pieces[0].Length + 1);
}
}
2017-12-19 20:33:03 +00:00
switch(descriptor.BusType)
{
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:
info.Supported = true;
break;
}
Marshal.FreeHGlobal(descriptorPtr);
devList.Add(info);
}
DeviceInfo[] devices = devList.ToArray();
return devices;
}
}
2017-12-19 20:33:03 +00:00
}