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

224 lines
8.5 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2017-12-19 19:33:46 +00:00
2022-03-07 07:36:44 +00:00
namespace Aaru.Devices.Windows;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Management;
2022-11-14 01:15:06 +00:00
using System.Runtime.Versioning;
using System.Text;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-29 18:03:35 +00:00
using Microsoft.Win32.SafeHandles;
2020-07-20 15:43:52 +01:00
using Marshal = System.Runtime.InteropServices.Marshal;
2022-11-14 01:15:06 +00:00
[SupportedOSPlatform("windows")]
2022-03-07 07:36:44 +00:00
static class ListDevices
{
2022-03-06 13:29:38 +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>
static string HexStringToString(string hex)
{
2022-03-06 13:29:38 +00:00
var result = new StringBuilder();
2022-03-18 01:32:22 +00:00
const string hexTable = "0123456789abcdef";
2022-03-07 07:36:44 +00:00
for(var i = 0; i < hex.Length / 2; i++)
2022-03-18 01:32:22 +00:00
result.Append((char)(16 * hexTable.IndexOf(hex[2 * i]) + hexTable.IndexOf(hex[2 * i + 1])));
2022-03-06 13:29:38 +00:00
return result.ToString();
}
2022-03-06 13:29:38 +00:00
/// <summary>Gets a list of all known storage devices on Windows</summary>
/// <returns>List of devices</returns>
[SuppressMessage("ReSharper", "RedundantCatchClause")]
internal static DeviceInfo[] GetList()
{
2022-03-07 07:36:44 +00:00
var deviceIDs = new List<string>();
2022-03-06 13:29:38 +00:00
try
{
var mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
2022-03-06 13:29:38 +00:00
ManagementObjectCollection objCol = mgmtObjSearcher.Get();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["DeviceID"]);
2022-03-06 13:29:38 +00:00
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_TapeDrive");
objCol = mgmtObjSearcher.Get();
2022-03-06 13:29:38 +00:00
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["DeviceID"]);
2022-03-06 13:29:38 +00:00
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
objCol = mgmtObjSearcher.Get();
2022-03-06 13:29:38 +00:00
deviceIDs.AddRange(from ManagementObject drive in objCol select (string)drive["Drive"]);
}
catch(Exception)
{
#if DEBUG
throw;
#else
return null;
2022-03-06 13:29:38 +00:00
#endif
}
2022-03-07 07:36:44 +00:00
var devList = new List<DeviceInfo>();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
foreach(string devId in deviceIDs)
{
if(devId is null)
continue;
2022-03-06 13:29:38 +00:00
string physId = devId;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// TODO: This can be done better
if(devId.Length == 2 &&
devId[1] == ':')
physId = "\\\\?\\" + devId;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
SafeFileHandle fd = Extern.CreateFile(physId, 0, FileShare.Read | FileShare.Write, IntPtr.Zero,
FileMode.OpenExisting, 0, IntPtr.Zero);
2022-03-06 13:29:38 +00:00
if(fd.IsInvalid)
continue;
2022-03-06 13:29:38 +00:00
var query = new StoragePropertyQuery
{
PropertyId = StoragePropertyId.Device,
QueryType = StorageQueryType.Standard,
AdditionalParameters = new byte[1]
};
2022-03-06 13:29:38 +00:00
//StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor();
//descriptor.RawDeviceProperties = new byte[16384];
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];
2022-03-06 13:29:38 +00:00
uint returned = 0;
2022-03-07 07:36:44 +00:00
var error = 0;
2022-03-07 07:36:44 +00:00
bool hasError = !Extern.DeviceIoControlStorageQuery(fd, WindowsIoctl.IoctlStorageQueryProperty, ref query,
(uint)Marshal.SizeOf(query), descriptorPtr, 1000,
ref returned, IntPtr.Zero);
2022-03-06 13:29:38 +00:00
if(hasError)
error = Marshal.GetLastWin32Error();
2022-03-06 13:29:38 +00:00
Marshal.Copy(descriptorPtr, descriptorB, 0, 1000);
2022-03-06 13:29:38 +00:00
if(hasError && error != 0)
continue;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
var 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)
};
var info = new DeviceInfo
{
Path = physId,
Bus = descriptor.BusType.ToString()
};
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(descriptor.VendorIdOffset > 0)
2022-03-07 07:36:44 +00:00
info.Vendor = StringHandlers.CToString(descriptorB, Encoding.ASCII, start: descriptor.VendorIdOffset);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(descriptor.ProductIdOffset > 0)
2022-03-07 07:36:44 +00:00
info.Model = StringHandlers.CToString(descriptorB, Encoding.ASCII, start: descriptor.ProductIdOffset);
2022-03-06 13:29:38 +00:00
// TODO: Get serial number of SCSI and USB devices, probably also FireWire (untested)
if(descriptor.SerialNumberOffset > 0)
{
info.Serial =
StringHandlers.CToString(descriptorB, Encoding.ASCII, start: descriptor.SerialNumberOffset);
2022-03-06 13:29:38 +00:00
// fix any serial numbers that are returned as hex-strings
if(Array.TrueForAll(info.Serial.ToCharArray(), c => "0123456789abcdef".IndexOf(c) >= 0) &&
info.Serial.Length == 40)
info.Serial = HexStringToString(info.Serial).Trim();
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(string.IsNullOrEmpty(info.Vendor) ||
info.Vendor == "ATA")
{
string[] pieces = info.Model?.Split(' ');
2022-03-06 13:29:38 +00:00
if(pieces?.Length > 1)
{
2022-03-06 13:29:38 +00:00
info.Vendor = pieces[0];
2022-11-14 01:15:06 +00:00
info.Model = info.Model[(pieces[0].Length + 1)..];
}
}
2022-03-06 13:29:38 +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:
case StorageBusType.SecureDigital:
case StorageBusType.MultiMediaCard:
info.Supported = true;
break;
}
2022-03-06 13:29:38 +00:00
Marshal.FreeHGlobal(descriptorPtr);
devList.Add(info);
}
2022-03-06 13:29:38 +00:00
DeviceInfo[] devices = devList.ToArray();
return devices;
}
2017-12-19 20:33:03 +00:00
}