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;
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ListDevices
|
|
|
|
|
|
{
|
|
|
|
|
|
public 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");
|
2017-08-22 03:40:43 +01:00
|
|
|
|
ManagementObjectCollection objCol = mgmtObjSearcher.Get();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(ManagementObject drive in objCol) DeviceIDs.Add((string)drive["DeviceID"]);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_TapeDrive");
|
|
|
|
|
|
objCol = mgmtObjSearcher.Get();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(ManagementObject drive in objCol) DeviceIDs.Add((string)drive["DeviceID"]);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
|
|
|
|
|
|
objCol = mgmtObjSearcher.Get();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
foreach(ManagementObject drive in objCol) DeviceIDs.Add((string)drive["Drive"]);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
#else
|
|
|
|
|
|
return null;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<DeviceInfo> dev_list = new List<DeviceInfo>();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +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
|
|
|
|
|
|
|
|
|
|
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[] descriptor_b = new byte[1000];
|
|
|
|
|
|
|
|
|
|
|
|
uint returned = 0;
|
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
bool hasError = !Extern.DeviceIoControlStorageQuery(fd, WindowsIoctl.IOCTL_STORAGE_QUERY_PROPERTY,
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(descriptorPtr, descriptor_b, 0, 1000);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(hasError && error != 0) continue;
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
|
|
|
|
|
StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor();
|
|
|
|
|
|
descriptor.Version = BitConverter.ToUInt32(descriptor_b, 0);
|
|
|
|
|
|
descriptor.Size = BitConverter.ToUInt32(descriptor_b, 4);
|
|
|
|
|
|
descriptor.DeviceType = descriptor_b[8];
|
|
|
|
|
|
descriptor.DeviceTypeModifier = descriptor_b[9];
|
|
|
|
|
|
descriptor.RemovableMedia = BitConverter.ToBoolean(descriptor_b, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
descriptor.CommandQueueing = BitConverter.ToBoolean(descriptor_b, 11);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
descriptor.VendorIdOffset = BitConverter.ToUInt32(descriptor_b, 12);
|
|
|
|
|
|
descriptor.ProductIdOffset = BitConverter.ToUInt32(descriptor_b, 16);
|
|
|
|
|
|
descriptor.ProductRevisionOffset = BitConverter.ToUInt32(descriptor_b, 20);
|
|
|
|
|
|
descriptor.SerialNumberOffset = BitConverter.ToUInt32(descriptor_b, 24);
|
|
|
|
|
|
descriptor.BusType = (StorageBusType)BitConverter.ToUInt32(descriptor_b, 28);
|
|
|
|
|
|
descriptor.RawPropertiesLength = BitConverter.ToUInt32(descriptor_b, 32);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
DeviceInfo info = new DeviceInfo {path = physId, bus = descriptor.BusType.ToString()};
|
|
|
|
|
|
|
|
|
|
|
|
if(descriptor.VendorIdOffset > 0)
|
|
|
|
|
|
info.vendor =
|
|
|
|
|
|
StringHandlers.CToString(descriptor_b, Encoding.ASCII, start: (int)descriptor.VendorIdOffset);
|
|
|
|
|
|
if(descriptor.ProductIdOffset > 0)
|
|
|
|
|
|
info.model =
|
|
|
|
|
|
StringHandlers.CToString(descriptor_b, Encoding.ASCII, start: (int)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-19 20:33:03 +00:00
|
|
|
|
if(descriptor.SerialNumberOffset > 0)
|
|
|
|
|
|
info.serial =
|
|
|
|
|
|
StringHandlers.CToString(descriptor_b, Encoding.ASCII,
|
|
|
|
|
|
start: (int)descriptor.SerialNumberOffset);
|
2017-08-22 03:40:43 +01:00
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(string.IsNullOrEmpty(info.vendor) || info.vendor == "ATA")
|
2017-08-22 03:40:43 +01: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
|
|
|
|
{
|
|
|
|
|
|
info.vendor = pieces[0];
|
|
|
|
|
|
info.model = info.model.Substring(pieces[0].Length + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
info.supported = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Marshal.FreeHGlobal(descriptorPtr);
|
|
|
|
|
|
dev_list.Add(info);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceInfo[] devices = dev_list.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|