mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
REFACTOR: Reformat code.
This commit is contained in:
@@ -53,14 +53,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
/// <param name="direction">SCSI command transfer direction</param>
|
||||
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
||||
/// <param name="sense"><c>True</c> if SCSI error returned non-OK status and <paramref name="senseBuffer"/> contains SCSI sense</param>
|
||||
internal static int SendScsiCommand(SafeFileHandle fd, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer, uint timeout, ScsiIoctlDirection direction, out double duration, out bool sense)
|
||||
internal static int SendScsiCommand(SafeFileHandle fd, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer,
|
||||
uint timeout, ScsiIoctlDirection direction, out double duration,
|
||||
out bool sense)
|
||||
{
|
||||
senseBuffer = null;
|
||||
duration = 0;
|
||||
sense = false;
|
||||
|
||||
if(buffer == null)
|
||||
return -1;
|
||||
if(buffer == null) return -1;
|
||||
|
||||
ScsiPassThroughDirectAndSenseBuffer sptd_sb = new ScsiPassThroughDirectAndSenseBuffer();
|
||||
sptd_sb.sptd = new ScsiPassThroughDirect();
|
||||
@@ -82,12 +83,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
Marshal.Copy(buffer, 0, sptd_sb.sptd.DataBuffer, buffer.Length);
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
bool hasError = !Extern.DeviceIoControlScsi(fd, WindowsIoctl.IOCTL_SCSI_PASS_THROUGH_DIRECT, ref sptd_sb, (uint)Marshal.SizeOf(sptd_sb), ref sptd_sb,
|
||||
(uint)Marshal.SizeOf(sptd_sb), ref k, IntPtr.Zero);
|
||||
bool hasError = !Extern.DeviceIoControlScsi(fd, WindowsIoctl.IOCTL_SCSI_PASS_THROUGH_DIRECT, ref sptd_sb,
|
||||
(uint)Marshal.SizeOf(sptd_sb), ref sptd_sb,
|
||||
(uint)Marshal.SizeOf(sptd_sb), ref k, IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(hasError)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(hasError) error = Marshal.GetLastWin32Error();
|
||||
|
||||
Marshal.Copy(sptd_sb.sptd.DataBuffer, buffer, 0, buffer.Length);
|
||||
|
||||
@@ -103,15 +104,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
return error;
|
||||
}
|
||||
|
||||
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersCHS registers, out AtaErrorRegistersCHS errorRegisters,
|
||||
AtaProtocol protocol, ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersCHS registers,
|
||||
out AtaErrorRegistersCHS errorRegisters, AtaProtocol protocol,
|
||||
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
{
|
||||
duration = 0;
|
||||
sense = false;
|
||||
errorRegisters = new AtaErrorRegistersCHS();
|
||||
|
||||
if(buffer == null)
|
||||
return -1;
|
||||
if(buffer == null) return -1;
|
||||
|
||||
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
||||
|
||||
@@ -138,7 +139,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
dataBuffer = new byte[64 * 512]
|
||||
};
|
||||
|
||||
if (protocol == AtaProtocol.PioIn || protocol == AtaProtocol.UDmaIn || protocol == AtaProtocol.Dma)
|
||||
if(protocol == AtaProtocol.PioIn || protocol == AtaProtocol.UDmaIn || protocol == AtaProtocol.Dma)
|
||||
aptd_buf.aptd.AtaFlags = AtaFlags.DataIn;
|
||||
else if(protocol == AtaProtocol.PioOut || protocol == AtaProtocol.UDmaOut)
|
||||
aptd_buf.aptd.AtaFlags = AtaFlags.DataOut;
|
||||
@@ -163,12 +164,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
Array.Copy(buffer, 0, aptd_buf.dataBuffer, 0, buffer.Length);
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IOCTL_ATA_PASS_THROUGH, ref aptd_buf, (uint)Marshal.SizeOf(aptd_buf), ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref k, IntPtr.Zero);
|
||||
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IOCTL_ATA_PASS_THROUGH, ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref k, IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(sense)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(sense) error = Marshal.GetLastWin32Error();
|
||||
|
||||
Array.Copy(aptd_buf.dataBuffer, 0, buffer, 0, buffer.Length);
|
||||
|
||||
@@ -188,15 +189,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
return error;
|
||||
}
|
||||
|
||||
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersLBA28 registers, out AtaErrorRegistersLBA28 errorRegisters,
|
||||
AtaProtocol protocol, ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersLBA28 registers,
|
||||
out AtaErrorRegistersLBA28 errorRegisters, AtaProtocol protocol,
|
||||
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
{
|
||||
duration = 0;
|
||||
sense = false;
|
||||
errorRegisters = new AtaErrorRegistersLBA28();
|
||||
|
||||
if(buffer == null)
|
||||
return -1;
|
||||
if(buffer == null) return -1;
|
||||
|
||||
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
||||
|
||||
@@ -248,12 +249,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
Array.Copy(buffer, 0, aptd_buf.dataBuffer, 0, buffer.Length);
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IOCTL_ATA_PASS_THROUGH, ref aptd_buf, (uint)Marshal.SizeOf(aptd_buf), ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref k, IntPtr.Zero);
|
||||
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IOCTL_ATA_PASS_THROUGH, ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref k, IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(sense)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(sense) error = Marshal.GetLastWin32Error();
|
||||
|
||||
Array.Copy(aptd_buf.dataBuffer, 0, buffer, 0, buffer.Length);
|
||||
|
||||
@@ -273,15 +274,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
return error;
|
||||
}
|
||||
|
||||
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersLBA48 registers, out AtaErrorRegistersLBA48 errorRegisters,
|
||||
AtaProtocol protocol, ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersLBA48 registers,
|
||||
out AtaErrorRegistersLBA48 errorRegisters, AtaProtocol protocol,
|
||||
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
{
|
||||
duration = 0;
|
||||
sense = false;
|
||||
errorRegisters = new AtaErrorRegistersLBA48();
|
||||
|
||||
if(buffer == null)
|
||||
return -1;
|
||||
if(buffer == null) return -1;
|
||||
|
||||
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
||||
|
||||
@@ -293,14 +294,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
DataBuffer = (IntPtr)offsetForBuffer,
|
||||
Length = (ushort)Marshal.SizeOf(typeof(AtaPassThroughDirect)),
|
||||
DataTransferLength = (uint)buffer.Length,
|
||||
PreviousTaskFile = new AtaTaskFile
|
||||
{
|
||||
CylinderHigh = (byte)((registers.lbaHigh & 0xFF00) >> 8),
|
||||
CylinderLow = (byte)((registers.lbaMid & 0xFF00) >> 8),
|
||||
Features = (byte)((registers.feature & 0xFF00) >> 8),
|
||||
SectorCount = (byte)((registers.sectorCount & 0xFF00) >> 8),
|
||||
SectorNumber = (byte)((registers.lbaLow & 0xFF00) >> 8)
|
||||
},
|
||||
PreviousTaskFile =
|
||||
new AtaTaskFile
|
||||
{
|
||||
CylinderHigh = (byte)((registers.lbaHigh & 0xFF00) >> 8),
|
||||
CylinderLow = (byte)((registers.lbaMid & 0xFF00) >> 8),
|
||||
Features = (byte)((registers.feature & 0xFF00) >> 8),
|
||||
SectorCount = (byte)((registers.sectorCount & 0xFF00) >> 8),
|
||||
SectorNumber = (byte)((registers.lbaLow & 0xFF00) >> 8)
|
||||
},
|
||||
CurrentTaskFile = new AtaTaskFile
|
||||
{
|
||||
Command = registers.command,
|
||||
@@ -340,21 +342,25 @@ namespace DiscImageChef.Devices.Windows
|
||||
Array.Copy(buffer, 0, aptd_buf.dataBuffer, 0, buffer.Length);
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IOCTL_ATA_PASS_THROUGH, ref aptd_buf, (uint)Marshal.SizeOf(aptd_buf), ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref k, IntPtr.Zero);
|
||||
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IOCTL_ATA_PASS_THROUGH, ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref aptd_buf,
|
||||
(uint)Marshal.SizeOf(aptd_buf), ref k, IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(sense)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(sense) error = Marshal.GetLastWin32Error();
|
||||
|
||||
Array.Copy(aptd_buf.dataBuffer, 0, buffer, 0, buffer.Length);
|
||||
|
||||
duration = (end - start).TotalMilliseconds;
|
||||
|
||||
errorRegisters.sectorCount = (ushort)((aptd_buf.aptd.PreviousTaskFile.SectorCount << 8) + aptd_buf.aptd.CurrentTaskFile.SectorCount);
|
||||
errorRegisters.lbaLow = (ushort)((aptd_buf.aptd.PreviousTaskFile.SectorNumber << 8) + aptd_buf.aptd.CurrentTaskFile.SectorNumber);
|
||||
errorRegisters.lbaMid = (ushort)((aptd_buf.aptd.PreviousTaskFile.CylinderLow << 8) + aptd_buf.aptd.CurrentTaskFile.CylinderLow);
|
||||
errorRegisters.lbaHigh = (ushort)((aptd_buf.aptd.PreviousTaskFile.CylinderHigh << 8) + aptd_buf.aptd.CurrentTaskFile.CylinderHigh);
|
||||
errorRegisters.sectorCount = (ushort)((aptd_buf.aptd.PreviousTaskFile.SectorCount << 8) +
|
||||
aptd_buf.aptd.CurrentTaskFile.SectorCount);
|
||||
errorRegisters.lbaLow = (ushort)((aptd_buf.aptd.PreviousTaskFile.SectorNumber << 8) +
|
||||
aptd_buf.aptd.CurrentTaskFile.SectorNumber);
|
||||
errorRegisters.lbaMid = (ushort)((aptd_buf.aptd.PreviousTaskFile.CylinderLow << 8) +
|
||||
aptd_buf.aptd.CurrentTaskFile.CylinderLow);
|
||||
errorRegisters.lbaHigh = (ushort)((aptd_buf.aptd.PreviousTaskFile.CylinderHigh << 8) +
|
||||
aptd_buf.aptd.CurrentTaskFile.CylinderHigh);
|
||||
errorRegisters.command = aptd_buf.aptd.CurrentTaskFile.Command;
|
||||
errorRegisters.deviceHead = aptd_buf.aptd.CurrentTaskFile.DeviceHead;
|
||||
errorRegisters.error = aptd_buf.aptd.CurrentTaskFile.Error;
|
||||
@@ -365,15 +371,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
return error;
|
||||
}
|
||||
|
||||
internal static int SendIdeCommand(SafeFileHandle fd, AtaRegistersCHS registers, out AtaErrorRegistersCHS errorRegisters,
|
||||
AtaProtocol protocol, ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
internal static int SendIdeCommand(SafeFileHandle fd, AtaRegistersCHS registers,
|
||||
out AtaErrorRegistersCHS errorRegisters, AtaProtocol protocol,
|
||||
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
{
|
||||
duration = 0;
|
||||
sense = false;
|
||||
errorRegisters = new AtaErrorRegistersCHS();
|
||||
|
||||
if(buffer == null || buffer.Length > 512)
|
||||
return -1;
|
||||
if(buffer == null || buffer.Length > 512) return -1;
|
||||
|
||||
IdePassThroughDirect iptd = new IdePassThroughDirect
|
||||
{
|
||||
@@ -397,12 +403,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
Array.Copy(buffer, 0, iptd.DataBuffer, 0, buffer.Length);
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
sense = !Extern.DeviceIoControlIde(fd, WindowsIoctl.IOCTL_IDE_PASS_THROUGH, ref iptd, (uint)Marshal.SizeOf(iptd), ref iptd,
|
||||
(uint)Marshal.SizeOf(iptd), ref k, IntPtr.Zero);
|
||||
sense = !Extern.DeviceIoControlIde(fd, WindowsIoctl.IOCTL_IDE_PASS_THROUGH, ref iptd,
|
||||
(uint)Marshal.SizeOf(iptd), ref iptd, (uint)Marshal.SizeOf(iptd), ref k,
|
||||
IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(sense)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(sense) error = Marshal.GetLastWin32Error();
|
||||
|
||||
buffer = new byte[k - 12];
|
||||
Array.Copy(iptd.DataBuffer, 0, buffer, 0, buffer.Length);
|
||||
@@ -423,15 +429,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
return error;
|
||||
}
|
||||
|
||||
internal static int SendIdeCommand(SafeFileHandle fd, AtaRegistersLBA28 registers, out AtaErrorRegistersLBA28 errorRegisters,
|
||||
AtaProtocol protocol, ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
internal static int SendIdeCommand(SafeFileHandle fd, AtaRegistersLBA28 registers,
|
||||
out AtaErrorRegistersLBA28 errorRegisters, AtaProtocol protocol,
|
||||
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
||||
{
|
||||
duration = 0;
|
||||
sense = false;
|
||||
errorRegisters = new AtaErrorRegistersLBA28();
|
||||
|
||||
if(buffer == null)
|
||||
return -1;
|
||||
if(buffer == null) return -1;
|
||||
|
||||
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
||||
|
||||
@@ -457,12 +463,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
Array.Copy(buffer, 0, iptd.DataBuffer, 0, buffer.Length);
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
sense = !Extern.DeviceIoControlIde(fd, WindowsIoctl.IOCTL_IDE_PASS_THROUGH, ref iptd, (uint)Marshal.SizeOf(iptd), ref iptd,
|
||||
(uint)Marshal.SizeOf(iptd), ref k, IntPtr.Zero);
|
||||
sense = !Extern.DeviceIoControlIde(fd, WindowsIoctl.IOCTL_IDE_PASS_THROUGH, ref iptd,
|
||||
(uint)Marshal.SizeOf(iptd), ref iptd, (uint)Marshal.SizeOf(iptd), ref k,
|
||||
IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(sense)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(sense) error = Marshal.GetLastWin32Error();
|
||||
|
||||
buffer = new byte[k - 12];
|
||||
Array.Copy(iptd.DataBuffer, 0, buffer, 0, buffer.Length);
|
||||
@@ -486,13 +492,11 @@ namespace DiscImageChef.Devices.Windows
|
||||
internal static uint GetDeviceNumber(SafeFileHandle deviceHandle)
|
||||
{
|
||||
StorageDeviceNumber sdn = new StorageDeviceNumber();
|
||||
sdn.deviceNumber = - 1;
|
||||
sdn.deviceNumber = -1;
|
||||
uint k = 0;
|
||||
if(!Extern.DeviceIoControlGetDeviceNumber(deviceHandle, WindowsIoctl.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero,
|
||||
0, ref sdn, (uint)Marshal.SizeOf(sdn), ref k, IntPtr.Zero))
|
||||
{
|
||||
return uint.MaxValue;
|
||||
}
|
||||
if(!Extern.DeviceIoControlGetDeviceNumber(deviceHandle, WindowsIoctl.IOCTL_STORAGE_GET_DEVICE_NUMBER,
|
||||
IntPtr.Zero, 0, ref sdn, (uint)Marshal.SizeOf(sdn), ref k,
|
||||
IntPtr.Zero)) { return uint.MaxValue; }
|
||||
|
||||
return (uint)sdn.deviceNumber;
|
||||
}
|
||||
@@ -501,28 +505,27 @@ namespace DiscImageChef.Devices.Windows
|
||||
{
|
||||
uint devNumber = GetDeviceNumber(fd);
|
||||
|
||||
if(devNumber == uint.MaxValue)
|
||||
return null;
|
||||
if(devNumber == uint.MaxValue) return null;
|
||||
|
||||
SafeFileHandle hDevInfo = Extern.SetupDiGetClassDevs(ref Consts.GUID_DEVINTERFACE_DISK, IntPtr.Zero,
|
||||
IntPtr.Zero, DeviceGetClassFlags.Present | DeviceGetClassFlags.DeviceInterface);
|
||||
IntPtr.Zero,
|
||||
DeviceGetClassFlags.Present |
|
||||
DeviceGetClassFlags.DeviceInterface);
|
||||
|
||||
if(hDevInfo.IsInvalid)
|
||||
return null;
|
||||
if(hDevInfo.IsInvalid) return null;
|
||||
|
||||
uint index = 0;
|
||||
DeviceInterfaceData spdid = new DeviceInterfaceData();
|
||||
spdid.cbSize = Marshal.SizeOf(spdid);
|
||||
|
||||
|
||||
byte[] buffer;
|
||||
|
||||
|
||||
while(true)
|
||||
{
|
||||
buffer = new byte[2048];
|
||||
|
||||
|
||||
if(!Extern.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref Consts.GUID_DEVINTERFACE_DISK, index,
|
||||
ref spdid))
|
||||
break;
|
||||
ref spdid)) break;
|
||||
|
||||
uint size = 0;
|
||||
|
||||
@@ -530,13 +533,16 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
if(size > 0 && size < buffer.Length)
|
||||
{
|
||||
buffer[0] = (byte)(IntPtr.Size == 8 ? IntPtr.Size : IntPtr.Size + Marshal.SystemDefaultCharSize); // Funny...
|
||||
buffer[0] = (byte)(IntPtr.Size == 8
|
||||
? IntPtr.Size
|
||||
: IntPtr.Size + Marshal.SystemDefaultCharSize); // Funny...
|
||||
|
||||
IntPtr pspdidd = Marshal.AllocHGlobal(buffer.Length);
|
||||
Marshal.Copy(buffer, 0, pspdidd, buffer.Length);
|
||||
|
||||
bool result =
|
||||
Extern.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref spdid, pspdidd, size, ref size, IntPtr.Zero);
|
||||
Extern.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref spdid, pspdidd, size, ref size,
|
||||
IntPtr.Zero);
|
||||
|
||||
buffer = new byte[size];
|
||||
Marshal.Copy(pspdidd, buffer, 0, buffer.Length);
|
||||
@@ -546,7 +552,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
{
|
||||
string devicePath = Encoding.Unicode.GetString(buffer, 4, (int)size - 4);
|
||||
SafeFileHandle hDrive = Extern.CreateFile(devicePath, 0, FileShare.Read | FileShare.Write,
|
||||
IntPtr.Zero, FileMode.OpenExisting, 0, IntPtr.Zero);
|
||||
IntPtr.Zero, FileMode.OpenExisting, 0, IntPtr.Zero);
|
||||
|
||||
if(!hDrive.IsInvalid)
|
||||
{
|
||||
@@ -558,11 +564,11 @@ namespace DiscImageChef.Devices.Windows
|
||||
return devicePath;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Extern.CloseHandle(hDrive);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
@@ -575,8 +581,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
SffdiskQueryDeviceProtocolData queryData1 = new SffdiskQueryDeviceProtocolData();
|
||||
queryData1.size = (ushort)Marshal.SizeOf(queryData1);
|
||||
uint bytesReturned;
|
||||
Extern.DeviceIoControl(fd, WindowsIoctl.IOCTL_SFFDISK_QUERY_DEVICE_PROTOCOL, IntPtr.Zero, 0,
|
||||
ref queryData1, queryData1.size, out bytesReturned, IntPtr.Zero);
|
||||
Extern.DeviceIoControl(fd, WindowsIoctl.IOCTL_SFFDISK_QUERY_DEVICE_PROTOCOL, IntPtr.Zero, 0, ref queryData1,
|
||||
queryData1.size, out bytesReturned, IntPtr.Zero);
|
||||
return queryData1.protocolGuid.Equals(Consts.GUID_SFF_PROTOCOL_SD);
|
||||
}
|
||||
|
||||
@@ -598,8 +604,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
/// <param name="response">Response registers</param>
|
||||
/// <param name="blockSize">Size of block in bytes</param>
|
||||
internal static int SendMmcCommand(SafeFileHandle fd, MmcCommands command, bool write, bool isApplication,
|
||||
MmcFlags flags, uint argument, uint blockSize, uint blocks, ref byte[] buffer, out uint[] response,
|
||||
out double duration, out bool sense, uint timeout = 0)
|
||||
MmcFlags flags, uint argument, uint blockSize, uint blocks,
|
||||
ref byte[] buffer, out uint[] response, out double duration, out bool sense,
|
||||
uint timeout = 0)
|
||||
{
|
||||
SffdiskDeviceCommandData commandData = new SffdiskDeviceCommandData();
|
||||
SdCmdDescriptor commandDescriptor = new SdCmdDescriptor();
|
||||
@@ -610,7 +617,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
commandDescriptor.commandCode = (byte)command;
|
||||
commandDescriptor.cmdClass = isApplication ? SdCommandClass.AppCmd : SdCommandClass.Standard;
|
||||
commandDescriptor.transferDirection = write ? SdTransferDirection.Write : SdTransferDirection.Read;
|
||||
commandDescriptor.transferType = flags.HasFlag(MmcFlags.CommandADTC) ? SdTransferType.SingleBlock : SdTransferType.CmdOnly;
|
||||
commandDescriptor.transferType = flags.HasFlag(MmcFlags.CommandADTC)
|
||||
? SdTransferType.SingleBlock
|
||||
: SdTransferType.CmdOnly;
|
||||
commandDescriptor.responseType = 0;
|
||||
|
||||
if(flags.HasFlag(MmcFlags.Response_R1) || flags.HasFlag(MmcFlags.ResponseSPI_R1))
|
||||
@@ -625,10 +634,10 @@ namespace DiscImageChef.Devices.Windows
|
||||
commandDescriptor.responseType = SdResponseType.R4;
|
||||
if(flags.HasFlag(MmcFlags.Response_R5) || flags.HasFlag(MmcFlags.ResponseSPI_R5))
|
||||
commandDescriptor.responseType = SdResponseType.R5;
|
||||
if(flags.HasFlag(MmcFlags.Response_R6))
|
||||
commandDescriptor.responseType = SdResponseType.R6;
|
||||
if(flags.HasFlag(MmcFlags.Response_R6)) commandDescriptor.responseType = SdResponseType.R6;
|
||||
|
||||
byte[] command_b = new byte[commandData.size + commandData.protocolArgumentSize + commandData.deviceDataBufferSize];
|
||||
byte[] command_b = new byte[commandData.size + commandData.protocolArgumentSize +
|
||||
commandData.deviceDataBufferSize];
|
||||
IntPtr hBuf = Marshal.AllocHGlobal(command_b.Length);
|
||||
Marshal.StructureToPtr(commandData, hBuf, true);
|
||||
IntPtr descriptorOffset = new IntPtr(hBuf.ToInt32() + commandData.size);
|
||||
@@ -640,20 +649,19 @@ namespace DiscImageChef.Devices.Windows
|
||||
int error = 0;
|
||||
DateTime start = DateTime.Now;
|
||||
sense = !Extern.DeviceIoControl(fd, WindowsIoctl.IOCTL_SFFDISK_DEVICE_COMMAND, command_b,
|
||||
(uint)command_b.Length, command_b, (uint)command_b.Length, out bytesReturned, IntPtr.Zero);
|
||||
(uint)command_b.Length, command_b, (uint)command_b.Length,
|
||||
out bytesReturned, IntPtr.Zero);
|
||||
DateTime end = DateTime.Now;
|
||||
|
||||
if(sense)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(sense) error = Marshal.GetLastWin32Error();
|
||||
|
||||
buffer = new byte[blockSize * blocks];
|
||||
Buffer.BlockCopy(command_b, command_b.Length - buffer.Length, buffer, 0, buffer.Length);
|
||||
|
||||
|
||||
response = new uint[4];
|
||||
duration = (end - start).TotalMilliseconds;
|
||||
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -343,8 +343,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400,
|
||||
IOCTL_IDE_PASS_THROUGH = 0x4D028,
|
||||
IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x2D1080,
|
||||
IOCTL_SFFDISK_QUERY_DEVICE_PROTOCOL = 0x71E80,
|
||||
IOCTL_SFFDISK_DEVICE_COMMAND = 0x79E84,
|
||||
IOCTL_SFFDISK_QUERY_DEVICE_PROTOCOL = 0x71E80,
|
||||
IOCTL_SFFDISK_DEVICE_COMMAND = 0x79E84,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
@@ -423,7 +423,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
FileBackedVirtual = 0xF,
|
||||
NVMe = 0x11,
|
||||
}
|
||||
|
||||
|
||||
[Flags]
|
||||
enum DeviceGetClassFlags : uint
|
||||
{
|
||||
@@ -448,7 +448,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
/// </summary>
|
||||
DeviceInterface = 0x10,
|
||||
}
|
||||
|
||||
|
||||
public enum SdCommandClass : uint
|
||||
{
|
||||
Standard,
|
||||
@@ -492,10 +492,11 @@ namespace DiscImageChef.Devices.Windows
|
||||
UnlockChannel,
|
||||
DeviceCommand
|
||||
};
|
||||
|
||||
|
||||
static class Consts
|
||||
{
|
||||
public static Guid GUID_SFF_PROTOCOL_SD = new Guid("AD7536A8-D055-4C40-AA4D-96312DDB6B38");
|
||||
public static Guid GUID_DEVINTERFACE_DISK = new Guid(0x53F56307, 0xB6BF, 0x11D0, 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B);
|
||||
public static Guid GUID_DEVINTERFACE_DISK =
|
||||
new Guid(0x53F56307, 0xB6BF, 0x11D0, 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,123 +40,77 @@ namespace DiscImageChef.Devices.Windows
|
||||
static class Extern
|
||||
{
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
internal static extern SafeFileHandle CreateFile(
|
||||
[MarshalAs(UnmanagedType.LPTStr)] string filename,
|
||||
[MarshalAs(UnmanagedType.U4)] FileAccess access,
|
||||
[MarshalAs(UnmanagedType.U4)] FileShare share,
|
||||
IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
|
||||
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
|
||||
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
|
||||
IntPtr templateFile);
|
||||
internal static extern SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPTStr)] string filename,
|
||||
[MarshalAs(UnmanagedType.U4)] FileAccess access,
|
||||
[MarshalAs(UnmanagedType.U4)] FileShare share,
|
||||
IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
|
||||
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
|
||||
[MarshalAs(UnmanagedType.U4)]
|
||||
FileAttributes flagsAndAttributes, IntPtr templateFile);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControlScsi(
|
||||
SafeFileHandle hDevice,
|
||||
WindowsIoctl IoControlCode,
|
||||
ref ScsiPassThroughDirectAndSenseBuffer InBuffer,
|
||||
uint nInBufferSize,
|
||||
ref ScsiPassThroughDirectAndSenseBuffer OutBuffer,
|
||||
uint nOutBufferSize,
|
||||
ref uint pBytesReturned,
|
||||
IntPtr Overlapped
|
||||
);
|
||||
internal static extern bool DeviceIoControlScsi(SafeFileHandle hDevice, WindowsIoctl IoControlCode,
|
||||
ref ScsiPassThroughDirectAndSenseBuffer InBuffer,
|
||||
uint nInBufferSize,
|
||||
ref ScsiPassThroughDirectAndSenseBuffer OutBuffer,
|
||||
uint nOutBufferSize, ref uint pBytesReturned,
|
||||
IntPtr Overlapped);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControlAta(
|
||||
SafeFileHandle hDevice,
|
||||
WindowsIoctl IoControlCode,
|
||||
ref AtaPassThroughDirectWithBuffer InBuffer,
|
||||
uint nInBufferSize,
|
||||
ref AtaPassThroughDirectWithBuffer OutBuffer,
|
||||
uint nOutBufferSize,
|
||||
ref uint pBytesReturned,
|
||||
IntPtr Overlapped
|
||||
);
|
||||
internal static extern bool DeviceIoControlAta(SafeFileHandle hDevice, WindowsIoctl IoControlCode,
|
||||
ref AtaPassThroughDirectWithBuffer InBuffer, uint nInBufferSize,
|
||||
ref AtaPassThroughDirectWithBuffer OutBuffer,
|
||||
uint nOutBufferSize, ref uint pBytesReturned, IntPtr Overlapped);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControlStorageQuery(
|
||||
SafeFileHandle hDevice,
|
||||
WindowsIoctl IoControlCode,
|
||||
ref StoragePropertyQuery InBuffer,
|
||||
uint nInBufferSize,
|
||||
IntPtr OutBuffer,
|
||||
uint nOutBufferSize,
|
||||
ref uint pBytesReturned,
|
||||
IntPtr Overlapped
|
||||
);
|
||||
internal static extern bool DeviceIoControlStorageQuery(SafeFileHandle hDevice, WindowsIoctl IoControlCode,
|
||||
ref StoragePropertyQuery InBuffer, uint nInBufferSize,
|
||||
IntPtr OutBuffer, uint nOutBufferSize,
|
||||
ref uint pBytesReturned, IntPtr Overlapped);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControlIde(
|
||||
SafeFileHandle hDevice,
|
||||
WindowsIoctl IoControlCode,
|
||||
ref IdePassThroughDirect InBuffer,
|
||||
uint nInBufferSize,
|
||||
ref IdePassThroughDirect OutBuffer,
|
||||
uint nOutBufferSize,
|
||||
ref uint pBytesReturned,
|
||||
IntPtr Overlapped
|
||||
);
|
||||
internal static extern bool DeviceIoControlIde(SafeFileHandle hDevice, WindowsIoctl IoControlCode,
|
||||
ref IdePassThroughDirect InBuffer, uint nInBufferSize,
|
||||
ref IdePassThroughDirect OutBuffer, uint nOutBufferSize,
|
||||
ref uint pBytesReturned, IntPtr Overlapped);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControlGetDeviceNumber(
|
||||
SafeFileHandle hDevice,
|
||||
WindowsIoctl IoControlCode,
|
||||
IntPtr InBuffer,
|
||||
uint nInBufferSize,
|
||||
ref StorageDeviceNumber OutBuffer,
|
||||
uint nOutBufferSize,
|
||||
ref uint pBytesReturned,
|
||||
IntPtr Overlapped
|
||||
);
|
||||
internal static extern bool DeviceIoControlGetDeviceNumber(SafeFileHandle hDevice, WindowsIoctl IoControlCode,
|
||||
IntPtr InBuffer, uint nInBufferSize,
|
||||
ref StorageDeviceNumber OutBuffer,
|
||||
uint nOutBufferSize, ref uint pBytesReturned,
|
||||
IntPtr Overlapped);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControl(
|
||||
SafeFileHandle hDevice,
|
||||
WindowsIoctl IoControlCode,
|
||||
IntPtr InBuffer,
|
||||
uint nInBufferSize,
|
||||
ref SffdiskQueryDeviceProtocolData OutBuffer,
|
||||
uint nOutBufferSize,
|
||||
out uint pBytesReturned,
|
||||
IntPtr Overlapped
|
||||
);
|
||||
internal static extern bool DeviceIoControl(SafeFileHandle hDevice, WindowsIoctl IoControlCode, IntPtr InBuffer,
|
||||
uint nInBufferSize, ref SffdiskQueryDeviceProtocolData OutBuffer,
|
||||
uint nOutBufferSize, out uint pBytesReturned, IntPtr Overlapped);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)]
|
||||
internal static extern bool DeviceIoControl(SafeFileHandle hDevice, WindowsIoctl IoControlCode, byte[] InBuffer,
|
||||
uint nInBufferSize, byte[] OutBuffer, uint nOutBufferSize, out uint pBytesReturned, IntPtr Overlapped);
|
||||
uint nInBufferSize, byte[] OutBuffer, uint nOutBufferSize,
|
||||
out uint pBytesReturned, IntPtr Overlapped);
|
||||
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
|
||||
internal static extern SafeFileHandle SetupDiGetClassDevs(
|
||||
ref Guid ClassGuid,
|
||||
IntPtr Enumerator,
|
||||
IntPtr hwndParent,
|
||||
DeviceGetClassFlags Flags
|
||||
);
|
||||
|
||||
internal static extern SafeFileHandle SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator,
|
||||
IntPtr hwndParent, DeviceGetClassFlags Flags);
|
||||
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern bool SetupDiEnumDeviceInterfaces(
|
||||
SafeFileHandle hDevInfo,
|
||||
IntPtr devInfo,
|
||||
ref Guid interfaceClassGuid,
|
||||
uint memberIndex,
|
||||
ref DeviceInterfaceData deviceInterfaceData
|
||||
);
|
||||
|
||||
public static extern bool SetupDiEnumDeviceInterfaces(SafeFileHandle hDevInfo, IntPtr devInfo,
|
||||
ref Guid interfaceClassGuid, uint memberIndex,
|
||||
ref DeviceInterfaceData deviceInterfaceData);
|
||||
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern bool SetupDiGetDeviceInterfaceDetail(
|
||||
SafeFileHandle hDevInfo,
|
||||
ref DeviceInterfaceData deviceInterfaceData,
|
||||
IntPtr deviceInterfaceDetailData,
|
||||
UInt32 deviceInterfaceDetailDataSize,
|
||||
ref UInt32 requiredSize,
|
||||
IntPtr deviceInfoData
|
||||
);
|
||||
|
||||
public static extern bool SetupDiGetDeviceInterfaceDetail(SafeFileHandle hDevInfo,
|
||||
ref DeviceInterfaceData deviceInterfaceData,
|
||||
IntPtr deviceInterfaceDetailData,
|
||||
UInt32 deviceInterfaceDetailDataSize,
|
||||
ref UInt32 requiredSize, IntPtr deviceInfoData);
|
||||
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern bool SetupDiDestroyDeviceInfoList(SafeFileHandle hDevInfo);
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
internal static extern bool CloseHandle(SafeFileHandle hDevice);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -47,23 +47,21 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
try
|
||||
{
|
||||
ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
|
||||
ManagementObjectSearcher mgmtObjSearcher =
|
||||
new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
|
||||
ManagementObjectCollection objCol = mgmtObjSearcher.Get();
|
||||
|
||||
foreach (ManagementObject drive in objCol)
|
||||
DeviceIDs.Add((string)drive["DeviceID"]);
|
||||
foreach(ManagementObject drive in objCol) DeviceIDs.Add((string)drive["DeviceID"]);
|
||||
|
||||
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_TapeDrive");
|
||||
objCol = mgmtObjSearcher.Get();
|
||||
|
||||
foreach (ManagementObject drive in objCol)
|
||||
DeviceIDs.Add((string)drive["DeviceID"]);
|
||||
foreach(ManagementObject drive in objCol) DeviceIDs.Add((string)drive["DeviceID"]);
|
||||
|
||||
mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
|
||||
objCol = mgmtObjSearcher.Get();
|
||||
|
||||
foreach (ManagementObject drive in objCol)
|
||||
DeviceIDs.Add((string)drive["Drive"]);
|
||||
foreach(ManagementObject drive in objCol) DeviceIDs.Add((string)drive["Drive"]);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
@@ -76,15 +74,14 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
List<DeviceInfo> dev_list = new List<DeviceInfo>();
|
||||
|
||||
foreach (string devId in DeviceIDs)
|
||||
foreach(string devId in DeviceIDs)
|
||||
{
|
||||
string physId = devId;
|
||||
// TODO: This can be done better
|
||||
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;
|
||||
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;
|
||||
@@ -100,15 +97,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
uint returned = 0;
|
||||
int error = 0;
|
||||
|
||||
bool hasError = !Extern.DeviceIoControlStorageQuery(fd, WindowsIoctl.IOCTL_STORAGE_QUERY_PROPERTY, ref query, (uint)Marshal.SizeOf(query), descriptorPtr, 1000, ref returned, IntPtr.Zero);
|
||||
bool hasError = !Extern.DeviceIoControlStorageQuery(fd, WindowsIoctl.IOCTL_STORAGE_QUERY_PROPERTY,
|
||||
ref query, (uint)Marshal.SizeOf(query),
|
||||
descriptorPtr, 1000, ref returned, IntPtr.Zero);
|
||||
|
||||
if (hasError)
|
||||
error = Marshal.GetLastWin32Error();
|
||||
if(hasError) error = Marshal.GetLastWin32Error();
|
||||
|
||||
Marshal.Copy(descriptorPtr, descriptor_b, 0, 1000);
|
||||
|
||||
if (hasError && error != 0)
|
||||
continue;
|
||||
if(hasError && error != 0) continue;
|
||||
|
||||
StorageDeviceDescriptor descriptor = new StorageDeviceDescriptor();
|
||||
descriptor.Version = BitConverter.ToUInt32(descriptor_b, 0);
|
||||
@@ -116,7 +113,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
descriptor.DeviceType = descriptor_b[8];
|
||||
descriptor.DeviceTypeModifier = descriptor_b[9];
|
||||
descriptor.RemovableMedia = BitConverter.ToBoolean(descriptor_b, 10);
|
||||
descriptor.CommandQueueing= BitConverter.ToBoolean(descriptor_b, 11);
|
||||
descriptor.CommandQueueing = BitConverter.ToBoolean(descriptor_b, 11);
|
||||
descriptor.VendorIdOffset = BitConverter.ToUInt32(descriptor_b, 12);
|
||||
descriptor.ProductIdOffset = BitConverter.ToUInt32(descriptor_b, 16);
|
||||
descriptor.ProductRevisionOffset = BitConverter.ToUInt32(descriptor_b, 20);
|
||||
@@ -124,31 +121,31 @@ namespace DiscImageChef.Devices.Windows
|
||||
descriptor.BusType = (StorageBusType)BitConverter.ToUInt32(descriptor_b, 28);
|
||||
descriptor.RawPropertiesLength = BitConverter.ToUInt32(descriptor_b, 32);
|
||||
|
||||
DeviceInfo info = new DeviceInfo
|
||||
{
|
||||
path = physId,
|
||||
bus = descriptor.BusType.ToString()
|
||||
};
|
||||
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);
|
||||
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);
|
||||
// TODO: Get serial number of SCSI and USB devices, probably also FireWire (untested)
|
||||
if (descriptor.SerialNumberOffset > 0)
|
||||
info.serial = StringHandlers.CToString(descriptor_b, Encoding.ASCII, start: (int)descriptor.SerialNumberOffset);
|
||||
if(descriptor.SerialNumberOffset > 0)
|
||||
info.serial =
|
||||
StringHandlers.CToString(descriptor_b, Encoding.ASCII,
|
||||
start: (int)descriptor.SerialNumberOffset);
|
||||
|
||||
if (string.IsNullOrEmpty(info.vendor) || info.vendor == "ATA")
|
||||
if(string.IsNullOrEmpty(info.vendor) || info.vendor == "ATA")
|
||||
{
|
||||
string[] pieces = info.model.Split(' ');
|
||||
if (pieces.Length > 1)
|
||||
if(pieces.Length > 1)
|
||||
{
|
||||
info.vendor = pieces[0];
|
||||
info.model = info.model.Substring(pieces[0].Length + 1);
|
||||
}
|
||||
}
|
||||
|
||||
switch (descriptor.BusType)
|
||||
switch(descriptor.BusType)
|
||||
{
|
||||
case StorageBusType.SCSI:
|
||||
case StorageBusType.ATAPI:
|
||||
@@ -173,4 +170,4 @@ namespace DiscImageChef.Devices.Windows
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,22 +46,19 @@ namespace DiscImageChef.Devices.Windows
|
||||
public byte Lun;
|
||||
public byte CdbLength;
|
||||
public byte SenseInfoLength;
|
||||
[MarshalAs(UnmanagedType.U1)]
|
||||
public ScsiIoctlDirection DataIn;
|
||||
[MarshalAs(UnmanagedType.U1)] public ScsiIoctlDirection DataIn;
|
||||
public uint DataTransferLength;
|
||||
public uint TimeOutValue;
|
||||
public IntPtr DataBuffer;
|
||||
public uint SenseInfoOffset;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
|
||||
public byte[] Cdb;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] Cdb;
|
||||
};
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct ScsiPassThroughDirectAndSenseBuffer
|
||||
{
|
||||
public ScsiPassThroughDirect sptd;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
|
||||
public byte[] SenseBuf;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] SenseBuf;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
@@ -74,8 +71,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
/// <summary>
|
||||
/// Indicates transfer direction and kind of operation
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.U2)]
|
||||
public AtaFlags AtaFlags;
|
||||
[MarshalAs(UnmanagedType.U2)] public AtaFlags AtaFlags;
|
||||
/// <summary>
|
||||
/// Indicates IDE port or bus, set by driver
|
||||
/// </summary>
|
||||
@@ -123,49 +119,35 @@ namespace DiscImageChef.Devices.Windows
|
||||
{
|
||||
public AtaPassThroughDirect aptd;
|
||||
public uint filler;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64 * 512)]
|
||||
public byte[] dataBuffer;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64 * 512)] public byte[] dataBuffer;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
struct AtaTaskFile
|
||||
{
|
||||
// Fields for commands sent
|
||||
[FieldOffset(0)]
|
||||
public byte Features;
|
||||
[FieldOffset(6)]
|
||||
public byte Command;
|
||||
[FieldOffset(0)] public byte Features;
|
||||
[FieldOffset(6)] public byte Command;
|
||||
|
||||
// Fields on command return
|
||||
[FieldOffset(0)]
|
||||
public byte Error;
|
||||
[FieldOffset(6)]
|
||||
public byte Status;
|
||||
[FieldOffset(0)] public byte Error;
|
||||
[FieldOffset(6)] public byte Status;
|
||||
|
||||
// Common fields
|
||||
[FieldOffset(1)]
|
||||
public byte SectorCount;
|
||||
[FieldOffset(2)]
|
||||
public byte SectorNumber;
|
||||
[FieldOffset(3)]
|
||||
public byte CylinderLow;
|
||||
[FieldOffset(4)]
|
||||
public byte CylinderHigh;
|
||||
[FieldOffset(5)]
|
||||
public byte DeviceHead;
|
||||
[FieldOffset(7)]
|
||||
public byte Reserved;
|
||||
[FieldOffset(1)] public byte SectorCount;
|
||||
[FieldOffset(2)] public byte SectorNumber;
|
||||
[FieldOffset(3)] public byte CylinderLow;
|
||||
[FieldOffset(4)] public byte CylinderHigh;
|
||||
[FieldOffset(5)] public byte DeviceHead;
|
||||
[FieldOffset(7)] public byte Reserved;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct StoragePropertyQuery
|
||||
{
|
||||
[MarshalAs(UnmanagedType.U4)]
|
||||
public StoragePropertyId PropertyId;
|
||||
[MarshalAs(UnmanagedType.U4)]
|
||||
public StorageQueryType QueryType;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
|
||||
public byte[] AdditionalParameters;
|
||||
[MarshalAs(UnmanagedType.U4)] public StoragePropertyId PropertyId;
|
||||
[MarshalAs(UnmanagedType.U4)] public StorageQueryType QueryType;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public byte[] AdditionalParameters;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
@@ -182,10 +164,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
public uint Size;
|
||||
public byte DeviceType;
|
||||
public byte DeviceTypeModifier;
|
||||
[MarshalAs(UnmanagedType.U1)]
|
||||
public bool RemovableMedia;
|
||||
[MarshalAs(UnmanagedType.U1)]
|
||||
public bool CommandQueueing;
|
||||
[MarshalAs(UnmanagedType.U1)] public bool RemovableMedia;
|
||||
[MarshalAs(UnmanagedType.U1)] public bool CommandQueueing;
|
||||
public uint VendorIdOffset;
|
||||
public uint ProductIdOffset;
|
||||
public uint ProductRevisionOffset;
|
||||
@@ -209,8 +189,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
/// <summary>
|
||||
/// Data buffer
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
|
||||
public byte[] DataBuffer;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)] public byte[] DataBuffer;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
@@ -220,7 +199,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public int deviceNumber;
|
||||
public int partitionNumber;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct DeviceInfoData
|
||||
{
|
||||
@@ -229,7 +208,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public uint devInst;
|
||||
public IntPtr reserved;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct DeviceInterfaceData
|
||||
{
|
||||
@@ -238,7 +217,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public uint flags;
|
||||
private IntPtr reserved;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct USB_SETUP_PACKET
|
||||
{
|
||||
@@ -248,7 +227,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public short wIndex;
|
||||
public short wLength;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct USB_DESCRIPTOR_REQUEST
|
||||
{
|
||||
@@ -256,7 +235,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public USB_SETUP_PACKET SetupPacket;
|
||||
//public byte[] Data;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct SffdiskQueryDeviceProtocolData
|
||||
{
|
||||
@@ -264,7 +243,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public ushort reserved;
|
||||
public Guid protocolGuid;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct SffdiskDeviceCommandData
|
||||
{
|
||||
@@ -275,7 +254,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
public uint deviceDataBufferSize;
|
||||
public uint information;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct SdCmdDescriptor
|
||||
{
|
||||
@@ -285,5 +264,4 @@ namespace DiscImageChef.Devices.Windows
|
||||
public SdTransferType transferType;
|
||||
public SdResponseType responseType;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,7 +41,6 @@ namespace DiscImageChef.Devices.Windows
|
||||
public partial class Usb
|
||||
{
|
||||
#region "API Region"
|
||||
|
||||
// ********************** Constants ************************
|
||||
|
||||
const int GENERIC_WRITE = 0x40000000;
|
||||
@@ -373,19 +372,10 @@ namespace DiscImageChef.Devices.Windows
|
||||
//);
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
|
||||
static extern IntPtr SetupDiGetClassDevs( // 1st form using a ClassGUID
|
||||
ref Guid ClassGuid,
|
||||
int Enumerator,
|
||||
IntPtr hwndParent,
|
||||
int Flags
|
||||
);
|
||||
ref Guid ClassGuid, int Enumerator, IntPtr hwndParent, int Flags);
|
||||
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] // 2nd form uses an Enumerator
|
||||
static extern IntPtr SetupDiGetClassDevs(
|
||||
int ClassGuid,
|
||||
string Enumerator,
|
||||
IntPtr hwndParent,
|
||||
int Flags
|
||||
);
|
||||
static extern IntPtr SetupDiGetClassDevs(int ClassGuid, string Enumerator, IntPtr hwndParent, int Flags);
|
||||
|
||||
//BOOL SetupDiEnumDeviceInterfaces(
|
||||
// HDEVINFO DeviceInfoSet,
|
||||
@@ -395,13 +385,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
// PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
|
||||
//);
|
||||
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool SetupDiEnumDeviceInterfaces(
|
||||
IntPtr DeviceInfoSet,
|
||||
IntPtr DeviceInfoData,
|
||||
ref Guid InterfaceClassGuid,
|
||||
int MemberIndex,
|
||||
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData
|
||||
);
|
||||
static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData,
|
||||
ref Guid InterfaceClassGuid, int MemberIndex,
|
||||
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
|
||||
|
||||
//BOOL SetupDiGetDeviceInterfaceDetail(
|
||||
// HDEVINFO DeviceInfoSet,
|
||||
@@ -412,14 +398,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
// PSP_DEVINFO_DATA DeviceInfoData
|
||||
//);
|
||||
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool SetupDiGetDeviceInterfaceDetail(
|
||||
IntPtr DeviceInfoSet,
|
||||
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
|
||||
ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
|
||||
int DeviceInterfaceDetailDataSize,
|
||||
ref int RequiredSize,
|
||||
ref SP_DEVINFO_DATA DeviceInfoData
|
||||
);
|
||||
static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet,
|
||||
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
|
||||
ref SP_DEVICE_INTERFACE_DETAIL_DATA
|
||||
DeviceInterfaceDetailData,
|
||||
int DeviceInterfaceDetailDataSize, ref int RequiredSize,
|
||||
ref SP_DEVINFO_DATA DeviceInfoData);
|
||||
|
||||
//BOOL SetupDiGetDeviceRegistryProperty(
|
||||
// HDEVINFO DeviceInfoSet,
|
||||
@@ -431,15 +415,10 @@ namespace DiscImageChef.Devices.Windows
|
||||
// PDWORD RequiredSize
|
||||
//);
|
||||
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool SetupDiGetDeviceRegistryProperty(
|
||||
IntPtr DeviceInfoSet,
|
||||
ref SP_DEVINFO_DATA DeviceInfoData,
|
||||
int iProperty,
|
||||
ref int PropertyRegDataType,
|
||||
IntPtr PropertyBuffer,
|
||||
int PropertyBufferSize,
|
||||
ref int RequiredSize
|
||||
);
|
||||
static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData,
|
||||
int iProperty, ref int PropertyRegDataType,
|
||||
IntPtr PropertyBuffer, int PropertyBufferSize,
|
||||
ref int RequiredSize);
|
||||
|
||||
//BOOL SetupDiEnumDeviceInfo(
|
||||
// HDEVINFO DeviceInfoSet,
|
||||
@@ -447,19 +426,14 @@ namespace DiscImageChef.Devices.Windows
|
||||
// PSP_DEVINFO_DATA DeviceInfoData
|
||||
//);
|
||||
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool SetupDiEnumDeviceInfo(
|
||||
IntPtr DeviceInfoSet,
|
||||
int MemberIndex,
|
||||
ref SP_DEVINFO_DATA DeviceInfoData
|
||||
);
|
||||
static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex,
|
||||
ref SP_DEVINFO_DATA DeviceInfoData);
|
||||
|
||||
//BOOL SetupDiDestroyDeviceInfoList(
|
||||
// HDEVINFO DeviceInfoSet
|
||||
//);
|
||||
[DllImport("setupapi.dll", SetLastError = true)]
|
||||
static extern bool SetupDiDestroyDeviceInfoList(
|
||||
IntPtr DeviceInfoSet
|
||||
);
|
||||
static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
|
||||
|
||||
//WINSETUPAPI BOOL WINAPI SetupDiGetDeviceInstanceId(
|
||||
// IN HDEVINFO DeviceInfoSet,
|
||||
@@ -469,13 +443,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
// OUT PDWORD RequiredSize OPTIONAL
|
||||
//);
|
||||
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool SetupDiGetDeviceInstanceId(
|
||||
IntPtr DeviceInfoSet,
|
||||
ref SP_DEVINFO_DATA DeviceInfoData,
|
||||
StringBuilder DeviceInstanceId,
|
||||
int DeviceInstanceIdSize,
|
||||
out int RequiredSize
|
||||
);
|
||||
static extern bool SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData,
|
||||
StringBuilder DeviceInstanceId, int DeviceInstanceIdSize,
|
||||
out int RequiredSize);
|
||||
|
||||
//BOOL DeviceIoControl(
|
||||
// HANDLE hDevice,
|
||||
@@ -488,16 +458,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
// LPOVERLAPPED lpOverlapped
|
||||
//);
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool DeviceIoControl(
|
||||
IntPtr hDevice,
|
||||
int dwIoControlCode,
|
||||
IntPtr lpInBuffer,
|
||||
int nInBufferSize,
|
||||
IntPtr lpOutBuffer,
|
||||
int nOutBufferSize,
|
||||
out int lpBytesReturned,
|
||||
IntPtr lpOverlapped
|
||||
);
|
||||
static extern bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize,
|
||||
IntPtr lpOutBuffer, int nOutBufferSize, out int lpBytesReturned,
|
||||
IntPtr lpOverlapped);
|
||||
|
||||
//HANDLE CreateFile(
|
||||
// LPCTSTR lpFileName,
|
||||
@@ -509,24 +472,15 @@ namespace DiscImageChef.Devices.Windows
|
||||
// HANDLE hTemplateFile
|
||||
//);
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern IntPtr CreateFile(
|
||||
string lpFileName,
|
||||
int dwDesiredAccess,
|
||||
int dwShareMode,
|
||||
IntPtr lpSecurityAttributes,
|
||||
int dwCreationDisposition,
|
||||
int dwFlagsAndAttributes,
|
||||
IntPtr hTemplateFile
|
||||
);
|
||||
static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode,
|
||||
IntPtr lpSecurityAttributes, int dwCreationDisposition,
|
||||
int dwFlagsAndAttributes, IntPtr hTemplateFile);
|
||||
|
||||
//BOOL CloseHandle(
|
||||
// HANDLE hObject
|
||||
//);
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
static extern bool CloseHandle(
|
||||
IntPtr hObject
|
||||
);
|
||||
|
||||
static extern bool CloseHandle(IntPtr hObject);
|
||||
#endregion
|
||||
|
||||
//
|
||||
@@ -578,12 +532,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
int RegType = REG_SZ;
|
||||
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref RegType, ptrBuf,
|
||||
BUFFER_SIZE, ref RequiredSize))
|
||||
BUFFER_SIZE, ref RequiredSize))
|
||||
{
|
||||
host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf,
|
||||
BUFFER_SIZE, ref RequiredSize))
|
||||
BUFFER_SIZE, ref RequiredSize))
|
||||
{
|
||||
host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
@@ -591,7 +545,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
HostList.Add(host);
|
||||
}
|
||||
i++;
|
||||
} while(Success);
|
||||
}
|
||||
while(Success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
@@ -652,7 +607,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Open a handle to the Host Controller
|
||||
h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
int nBytesReturned;
|
||||
@@ -662,7 +617,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// get the Hub Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
HubName = (USB_ROOT_HUB_NAME)Marshal.PtrToStructure(ptrHubName, typeof(USB_ROOT_HUB_NAME));
|
||||
Root.HubDevicePath = @"\\.\" + HubName.RootHubName;
|
||||
@@ -672,7 +627,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Now let's open the Hub (based upon the HubName we got above)
|
||||
h2 = CreateFile(Root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
IntPtr.Zero);
|
||||
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
|
||||
@@ -683,10 +638,10 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// get the Hub Information
|
||||
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo,
|
||||
typeof(USB_NODE_INFORMATION));
|
||||
typeof(USB_NODE_INFORMATION));
|
||||
Root.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
|
||||
Root.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
|
||||
}
|
||||
@@ -790,7 +745,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Open a handle to the Hub device
|
||||
IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
|
||||
@@ -806,11 +761,13 @@ namespace DiscImageChef.Devices.Windows
|
||||
Marshal.StructureToPtr(NodeConnection, ptrNodeConnection, true);
|
||||
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes,
|
||||
ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
NodeConnection =
|
||||
(USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection,
|
||||
typeof(USB_NODE_CONNECTION_INFORMATION_EX));
|
||||
typeof(
|
||||
USB_NODE_CONNECTION_INFORMATION_EX
|
||||
));
|
||||
|
||||
// load up the USBPort class
|
||||
USBPort port = new USBPort();
|
||||
@@ -829,6 +786,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
PortList.Add(port);
|
||||
}
|
||||
}
|
||||
|
||||
Marshal.FreeHGlobal(ptrNodeConnection);
|
||||
CloseHandle(h);
|
||||
}
|
||||
@@ -897,10 +855,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
// return a down stream external hub
|
||||
public USBDevice GetDevice()
|
||||
{
|
||||
if(!PortIsDeviceConnected)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if(!PortIsDeviceConnected) { return null; }
|
||||
|
||||
USBDevice Device = new USBDevice();
|
||||
|
||||
// Copy over some values from the Port class
|
||||
@@ -911,7 +867,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Open a handle to the Hub device
|
||||
IntPtr h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
int nBytesReturned;
|
||||
@@ -938,7 +894,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// The location of the string descriptor is immediately after
|
||||
// the Request structure. Because this location is not "covered"
|
||||
@@ -947,7 +903,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
|
||||
USB_STRING_DESCRIPTOR StringDesc =
|
||||
(USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(USB_STRING_DESCRIPTOR));
|
||||
typeof(USB_STRING_DESCRIPTOR));
|
||||
Device.DeviceManufacturer = StringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
@@ -967,13 +923,13 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// the location of the string descriptor is immediately after the Request structure
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
|
||||
USB_STRING_DESCRIPTOR StringDesc =
|
||||
(USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(USB_STRING_DESCRIPTOR));
|
||||
typeof(USB_STRING_DESCRIPTOR));
|
||||
Device.DeviceProduct = StringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
@@ -993,13 +949,13 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ptrRequest, nBytes,
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
ptrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
// the location of the string descriptor is immediately after the Request structure
|
||||
IntPtr ptrStringDesc = new IntPtr(ptrRequest.ToInt32() + Marshal.SizeOf(Request));
|
||||
USB_STRING_DESCRIPTOR StringDesc =
|
||||
(USB_STRING_DESCRIPTOR)Marshal.PtrToStructure(ptrStringDesc,
|
||||
typeof(USB_STRING_DESCRIPTOR));
|
||||
typeof(USB_STRING_DESCRIPTOR));
|
||||
Device.DeviceSerialNumber = StringDesc.bString;
|
||||
}
|
||||
Marshal.FreeHGlobal(ptrRequest);
|
||||
@@ -1010,14 +966,14 @@ namespace DiscImageChef.Devices.Windows
|
||||
dcrRequest.ConnectionIndex = PortPortNumber;
|
||||
dcrRequest.SetupPacket.wValue = (short)((USB_CONFIGURATION_DESCRIPTOR_TYPE << 8));
|
||||
dcrRequest.SetupPacket.wLength = (short)(nBytes - Marshal.SizeOf(dcrRequest));
|
||||
dcrRequest.SetupPacket.wIndex = 0;
|
||||
dcrRequest.SetupPacket.wIndex = 0;
|
||||
// Geez, I wish C# had a Marshal.MemSet() method
|
||||
IntPtr dcrPtrRequest = Marshal.StringToHGlobalAuto(NullString);
|
||||
Marshal.StructureToPtr(dcrRequest, dcrPtrRequest, true);
|
||||
|
||||
// Use an IOCTL call to request the String Descriptor
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, dcrPtrRequest, nBytes,
|
||||
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
dcrPtrRequest, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
IntPtr ptrStringDesc = new IntPtr(dcrPtrRequest.ToInt32() + Marshal.SizeOf(dcrRequest));
|
||||
Device.BinaryDeviceDescriptors = new byte[nBytesReturned];
|
||||
@@ -1034,10 +990,12 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Use an IOCTL call to request the Driver Key Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ptrDriverKey, nBytes,
|
||||
ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
ptrDriverKey, nBytes, out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
DriverKey = (USB_NODE_CONNECTION_DRIVERKEY_NAME)Marshal.PtrToStructure(ptrDriverKey,
|
||||
typeof(USB_NODE_CONNECTION_DRIVERKEY_NAME));
|
||||
typeof(
|
||||
USB_NODE_CONNECTION_DRIVERKEY_NAME
|
||||
));
|
||||
Device.DeviceDriverKey = DriverKey.DriverKeyName;
|
||||
|
||||
// use the DriverKeyName to get the Device Description and Instance ID
|
||||
@@ -1053,10 +1011,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
// return a down stream external hub
|
||||
public USBHub GetHub()
|
||||
{
|
||||
if(!PortIsHub)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if(!PortIsHub) { return null; }
|
||||
|
||||
USBHub Hub = new USBHub();
|
||||
IntPtr h, h2;
|
||||
Hub.HubIsRootHub = false;
|
||||
@@ -1064,7 +1020,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Open a handle to the Host Controller
|
||||
h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
IntPtr.Zero);
|
||||
if(h.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
// Get the DevicePath for downstream hub
|
||||
@@ -1077,16 +1033,16 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// Use an IOCTL call to request the Node Name
|
||||
if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
NodeName = (USB_NODE_CONNECTION_NAME)Marshal.PtrToStructure(ptrNodeName,
|
||||
typeof(USB_NODE_CONNECTION_NAME));
|
||||
typeof(USB_NODE_CONNECTION_NAME));
|
||||
Hub.HubDevicePath = @"\\.\" + NodeName.NodeName;
|
||||
}
|
||||
|
||||
// Now let's open the Hub (based upon the HubName we got above)
|
||||
h2 = CreateFile(Hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
|
||||
IntPtr.Zero);
|
||||
IntPtr.Zero);
|
||||
if(h2.ToInt32() != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
|
||||
@@ -1097,10 +1053,10 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
// get the Hub Information
|
||||
if(DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes,
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
out nBytesReturned, IntPtr.Zero))
|
||||
{
|
||||
NodeInfo = (USB_NODE_INFORMATION)Marshal.PtrToStructure(ptrNodeInfo,
|
||||
typeof(USB_NODE_INFORMATION));
|
||||
typeof(USB_NODE_INFORMATION));
|
||||
Hub.HubIsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
|
||||
Hub.HubPortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
|
||||
}
|
||||
@@ -1193,7 +1149,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
{
|
||||
get { return DeviceSerialNumber; }
|
||||
}
|
||||
|
||||
|
||||
public byte[] BinaryDescriptors
|
||||
{
|
||||
get { return BinaryDeviceDescriptors; }
|
||||
@@ -1233,7 +1189,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
KeyName = "";
|
||||
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE,
|
||||
ref RequiredSize))
|
||||
ref RequiredSize))
|
||||
{
|
||||
KeyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
@@ -1242,19 +1198,22 @@ namespace DiscImageChef.Devices.Windows
|
||||
if(KeyName == DriverKeyName)
|
||||
{
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref RegType, ptrBuf,
|
||||
BUFFER_SIZE, ref RequiredSize))
|
||||
BUFFER_SIZE, ref RequiredSize))
|
||||
{
|
||||
ans = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
} while(Success);
|
||||
}
|
||||
while(Success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
@@ -1291,7 +1250,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
|
||||
KeyName = "";
|
||||
if(SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE,
|
||||
ref RequiredSize))
|
||||
ref RequiredSize))
|
||||
{
|
||||
KeyName = Marshal.PtrToStringAuto(ptrBuf);
|
||||
}
|
||||
@@ -1306,13 +1265,16 @@ namespace DiscImageChef.Devices.Windows
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
} while(Success);
|
||||
}
|
||||
while(Success);
|
||||
|
||||
Marshal.FreeHGlobal(ptrBuf);
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,14 +37,12 @@ using System.Runtime.InteropServices;
|
||||
// Copyright "Fort Hood TX", public domain, 2007
|
||||
namespace DiscImageChef.Devices.Windows
|
||||
{
|
||||
|
||||
//
|
||||
// A place for "higher level" related functions
|
||||
// You might not want to keep these in the USB class... your choice
|
||||
//
|
||||
public partial class Usb
|
||||
{
|
||||
|
||||
//
|
||||
// Get a list of all connected devices
|
||||
//
|
||||
@@ -52,10 +50,8 @@ namespace DiscImageChef.Devices.Windows
|
||||
{
|
||||
List<USBDevice> DevList = new List<USBDevice>();
|
||||
|
||||
foreach(USBController Controller in GetHostControllers())
|
||||
{
|
||||
ListHub(Controller.GetRootHub(), DevList);
|
||||
}
|
||||
foreach(USBController Controller in GetHostControllers()) { ListHub(Controller.GetRootHub(), DevList); }
|
||||
|
||||
return DevList;
|
||||
}
|
||||
|
||||
@@ -69,13 +65,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
// recursive
|
||||
ListHub(Port.GetHub(), DevList);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Port.IsDeviceConnected)
|
||||
{
|
||||
DevList.Add(Port.GetDevice());
|
||||
}
|
||||
}
|
||||
else { if(Port.IsDeviceConnected) { DevList.Add(Port.GetDevice()); } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,9 +79,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
foreach(USBController Controller in GetHostControllers())
|
||||
{
|
||||
SearchHubDriverKeyName(Controller.GetRootHub(), ref FoundDevice, DriverKeyName);
|
||||
if(FoundDevice != null)
|
||||
break;
|
||||
if(FoundDevice != null) break;
|
||||
}
|
||||
|
||||
return FoundDevice;
|
||||
}
|
||||
|
||||
@@ -130,9 +120,9 @@ namespace DiscImageChef.Devices.Windows
|
||||
foreach(USBController Controller in GetHostControllers())
|
||||
{
|
||||
SearchHubInstanceID(Controller.GetRootHub(), ref FoundDevice, InstanceID);
|
||||
if(FoundDevice != null)
|
||||
break;
|
||||
if(FoundDevice != null) break;
|
||||
}
|
||||
|
||||
return FoundDevice;
|
||||
}
|
||||
|
||||
@@ -185,11 +175,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
// IN ULONG ulFlags
|
||||
//);
|
||||
[DllImport("setupapi.dll")]
|
||||
static extern int CM_Get_Parent(
|
||||
out IntPtr pdnDevInst,
|
||||
IntPtr dnDevInst,
|
||||
int ulFlags
|
||||
);
|
||||
static extern int CM_Get_Parent(out IntPtr pdnDevInst, IntPtr dnDevInst, int ulFlags);
|
||||
|
||||
//CMAPI CONFIGRET WINAPI CM_Get_Device_ID(
|
||||
// IN DEVINST dnDevInst,
|
||||
@@ -198,12 +184,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
// IN ULONG ulFlags
|
||||
//);
|
||||
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
|
||||
static extern int CM_Get_Device_ID(
|
||||
IntPtr dnDevInst,
|
||||
IntPtr Buffer,
|
||||
int BufferLen,
|
||||
int ulFlags
|
||||
);
|
||||
static extern int CM_Get_Device_ID(IntPtr dnDevInst, IntPtr Buffer, int BufferLen, int ulFlags);
|
||||
|
||||
//
|
||||
// Find a device based upon a Drive Letter
|
||||
@@ -217,10 +198,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
// DriveLetter. We'll use this later to find a matching
|
||||
// DevicePath "symbolic name"
|
||||
int DevNum = GetDeviceNumber(@"\\.\" + DriveLetter.TrimEnd('\\'));
|
||||
if(DevNum < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if(DevNum < 0) { return null; }
|
||||
|
||||
return FindDeviceNumber(DevNum, deviceGuid);
|
||||
}
|
||||
@@ -234,10 +212,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
// DriveLetter. We'll use this later to find a matching
|
||||
// DevicePath "symbolic name"
|
||||
int DevNum = GetDeviceNumber(DrivePath);
|
||||
if(DevNum < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if(DevNum < 0) { return null; }
|
||||
|
||||
return FindDeviceNumber(DevNum, deviceGuid);
|
||||
}
|
||||
@@ -304,15 +279,14 @@ namespace DiscImageChef.Devices.Windows
|
||||
}
|
||||
}
|
||||
i++;
|
||||
} while(Success);
|
||||
}
|
||||
while(Success);
|
||||
|
||||
SetupDiDestroyDeviceInfoList(h);
|
||||
}
|
||||
|
||||
// Did we find an InterfaceID of a USB device?
|
||||
if(InstanceID.StartsWith("USB\\"))
|
||||
{
|
||||
FoundDevice = FindDeviceByInstanceID(InstanceID);
|
||||
}
|
||||
if(InstanceID.StartsWith("USB\\")) { FoundDevice = FindDeviceByInstanceID(InstanceID); }
|
||||
return FoundDevice;
|
||||
}
|
||||
|
||||
@@ -330,7 +304,7 @@ namespace DiscImageChef.Devices.Windows
|
||||
IntPtr ptrSdn = Marshal.AllocHGlobal(nBytes);
|
||||
|
||||
if(DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, ptrSdn, nBytes, out requiredSize,
|
||||
IntPtr.Zero))
|
||||
IntPtr.Zero))
|
||||
{
|
||||
Sdn = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(ptrSdn, typeof(STORAGE_DEVICE_NUMBER));
|
||||
// just my way of combining the relevant parts of the
|
||||
@@ -343,4 +317,4 @@ namespace DiscImageChef.Devices.Windows
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user