2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2015-10-12 06:39:31 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Command.cs
|
2016-07-28 18:13:49 +01:00
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2015-10-12 06:39:31 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Component : Windows direct device access.
|
2015-10-12 06:39:31 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// Contains a high level representation of the Windows syscalls used to
|
|
|
|
|
// directly interface devices.
|
2015-10-12 06:39:31 +01:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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
|
2015-10-12 06:39:31 +01:00
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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.
|
2015-10-12 06:39:31 +01:00
|
|
|
//
|
2016-07-28 18:13:49 +01:00
|
|
|
// 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/>.
|
2015-10-12 06:39:31 +01:00
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2015-10-12 06:39:31 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-12-22 03:13:43 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2015-10-12 06:25:49 +01:00
|
|
|
using System.Runtime.InteropServices;
|
2017-12-06 13:46:35 +00:00
|
|
|
using System.Text;
|
2017-08-21 04:30:10 +01:00
|
|
|
using DiscImageChef.Decoders.ATA;
|
2017-12-19 19:33:46 +00:00
|
|
|
using Microsoft.Win32.SafeHandles;
|
2015-10-12 06:25:49 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Devices.Windows
|
|
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
[SuppressMessage("ReSharper", "UnusedParameter.Global")]
|
2015-10-12 06:25:49 +01:00
|
|
|
static class Command
|
|
|
|
|
{
|
2015-10-12 20:08:56 +01:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends a SCSI command
|
2015-10-12 20:08:56 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="cdb">SCSI CDB</param>
|
|
|
|
|
/// <param name="buffer">Buffer for SCSI command response</param>
|
|
|
|
|
/// <param name="senseBuffer">Buffer with the SCSI sense</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="direction">SCSI command transfer direction</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// <param name="sense">
|
|
|
|
|
/// <c>True</c> if SCSI error returned non-OK status and <paramref name="senseBuffer" /> contains SCSI
|
|
|
|
|
/// sense
|
|
|
|
|
/// </param>
|
2017-12-19 20:33:03 +00:00
|
|
|
internal static int SendScsiCommand(SafeFileHandle fd, byte[] cdb, ref byte[] buffer, out byte[] senseBuffer,
|
|
|
|
|
uint timeout, ScsiIoctlDirection direction, out double duration,
|
|
|
|
|
out bool sense)
|
2015-10-12 06:25:49 +01:00
|
|
|
{
|
|
|
|
|
senseBuffer = null;
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(buffer == null) return -1;
|
2015-10-12 06:25:49 +01:00
|
|
|
|
2017-12-22 03:13:43 +00:00
|
|
|
ScsiPassThroughDirectAndSenseBuffer sptdSb = new ScsiPassThroughDirectAndSenseBuffer
|
|
|
|
|
{
|
|
|
|
|
SenseBuf = new byte[32],
|
|
|
|
|
sptd = new ScsiPassThroughDirect
|
|
|
|
|
{
|
|
|
|
|
Cdb = new byte[16],
|
|
|
|
|
CdbLength = (byte)cdb.Length,
|
|
|
|
|
SenseInfoLength = 32,
|
|
|
|
|
DataIn = direction,
|
|
|
|
|
DataTransferLength = (uint)buffer.Length,
|
|
|
|
|
TimeOutValue = timeout,
|
|
|
|
|
DataBuffer = Marshal.AllocHGlobal(buffer.Length)
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-12-20 17:15:26 +00:00
|
|
|
sptdSb.sptd.Length = (ushort)Marshal.SizeOf(sptdSb.sptd);
|
|
|
|
|
sptdSb.sptd.SenseInfoOffset = (uint)Marshal.SizeOf(sptdSb.sptd);
|
2017-12-22 03:13:43 +00:00
|
|
|
Array.Copy(cdb, sptdSb.sptd.Cdb, cdb.Length);
|
2015-10-12 06:25:49 +01:00
|
|
|
|
|
|
|
|
uint k = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(buffer, 0, sptdSb.sptd.DataBuffer, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2015-10-12 06:25:49 +01:00
|
|
|
DateTime start = DateTime.Now;
|
2017-12-20 17:15:26 +00:00
|
|
|
bool hasError = !Extern.DeviceIoControlScsi(fd, WindowsIoctl.IoctlScsiPassThroughDirect, ref sptdSb,
|
|
|
|
|
(uint)Marshal.SizeOf(sptdSb), ref sptdSb,
|
|
|
|
|
(uint)Marshal.SizeOf(sptdSb), ref k, IntPtr.Zero);
|
2015-10-12 06:25:49 +01:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(hasError) error = Marshal.GetLastWin32Error();
|
2015-10-12 06:25:49 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(sptdSb.sptd.DataBuffer, buffer, 0, buffer.Length);
|
2015-10-12 06:25:49 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
sense |= sptdSb.sptd.ScsiStatus != 0;
|
2015-10-12 06:25:49 +01:00
|
|
|
|
|
|
|
|
senseBuffer = new byte[32];
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(sptdSb.SenseBuf, senseBuffer, 32);
|
2015-10-12 06:25:49 +01:00
|
|
|
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.FreeHGlobal(sptdSb.sptd.DataBuffer);
|
2015-12-30 11:45:27 +00:00
|
|
|
|
2015-10-12 06:25:49 +01:00
|
|
|
return error;
|
|
|
|
|
}
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends an ATA command in CHS mode
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="buffer">Buffer for SCSI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA error returned non-OK status</param>
|
|
|
|
|
/// <param name="registers">Registers to send to drive</param>
|
|
|
|
|
/// <param name="errorRegisters">Registers returned by drive</param>
|
|
|
|
|
/// <param name="protocol">ATA protocol to use</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersChs registers,
|
|
|
|
|
out AtaErrorRegistersChs errorRegisters, AtaProtocol protocol,
|
2017-12-19 20:33:03 +00:00
|
|
|
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters = new AtaErrorRegistersChs();
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(buffer == null) return -1;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-09-07 16:48:25 +01:00
|
|
|
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
AtaPassThroughDirectWithBuffer aptdBuf = new AtaPassThroughDirectWithBuffer
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
2017-09-07 16:48:25 +01:00
|
|
|
aptd = new AtaPassThroughDirect
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
2017-09-07 16:48:25 +01:00
|
|
|
TimeOutValue = timeout,
|
|
|
|
|
DataBuffer = (IntPtr)offsetForBuffer,
|
|
|
|
|
Length = (ushort)Marshal.SizeOf(typeof(AtaPassThroughDirect)),
|
|
|
|
|
DataTransferLength = (uint)buffer.Length,
|
|
|
|
|
PreviousTaskFile = new AtaTaskFile(),
|
|
|
|
|
CurrentTaskFile = new AtaTaskFile
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
Command = registers.Command,
|
|
|
|
|
CylinderHigh = registers.CylinderHigh,
|
|
|
|
|
CylinderLow = registers.CylinderLow,
|
|
|
|
|
DeviceHead = registers.DeviceHead,
|
|
|
|
|
Features = registers.Feature,
|
|
|
|
|
SectorCount = registers.SectorCount,
|
|
|
|
|
SectorNumber = registers.Sector
|
2017-12-21 02:52:12 +00:00
|
|
|
}
|
2017-09-07 16:48:25 +01:00
|
|
|
},
|
|
|
|
|
dataBuffer = new byte[64 * 512]
|
2017-08-21 04:30:10 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-23 20:04:36 +00:00
|
|
|
switch(protocol)
|
|
|
|
|
{
|
2017-12-21 04:43:29 +00:00
|
|
|
case AtaProtocol.PioIn:
|
|
|
|
|
case AtaProtocol.UDmaIn:
|
2017-12-23 20:04:36 +00:00
|
|
|
case AtaProtocol.Dma:
|
|
|
|
|
aptdBuf.aptd.AtaFlags = AtaFlags.DataIn;
|
2017-12-21 04:43:29 +00:00
|
|
|
break;
|
|
|
|
|
case AtaProtocol.PioOut:
|
2017-12-23 20:04:36 +00:00
|
|
|
case AtaProtocol.UDmaOut:
|
|
|
|
|
aptdBuf.aptd.AtaFlags = AtaFlags.DataOut;
|
2017-12-21 04:43:29 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
switch(protocol)
|
|
|
|
|
{
|
|
|
|
|
case AtaProtocol.Dma:
|
|
|
|
|
case AtaProtocol.DmaQueued:
|
2017-12-20 17:15:26 +00:00
|
|
|
case AtaProtocol.FpDma:
|
2017-08-21 04:30:10 +01:00
|
|
|
case AtaProtocol.UDmaIn:
|
|
|
|
|
case AtaProtocol.UDmaOut:
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.AtaFlags |= AtaFlags.Dma;
|
2017-08-21 04:30:10 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 16:48:25 +01:00
|
|
|
// Unknown if needed
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.AtaFlags |= AtaFlags.DrdyRequired;
|
2017-09-07 16:48:25 +01:00
|
|
|
|
2017-08-21 04:30:10 +01:00
|
|
|
uint k = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(buffer, 0, aptdBuf.dataBuffer, 0, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
DateTime start = DateTime.Now;
|
2017-12-20 17:15:26 +00:00
|
|
|
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IoctlAtaPassThrough, ref aptdBuf,
|
|
|
|
|
(uint)Marshal.SizeOf(aptdBuf), ref aptdBuf,
|
|
|
|
|
(uint)Marshal.SizeOf(aptdBuf), ref k, IntPtr.Zero);
|
2017-08-21 04:30:10 +01:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sense) error = Marshal.GetLastWin32Error();
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(aptdBuf.dataBuffer, 0, buffer, 0, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.CylinderHigh = aptdBuf.aptd.CurrentTaskFile.CylinderHigh;
|
|
|
|
|
errorRegisters.CylinderLow = aptdBuf.aptd.CurrentTaskFile.CylinderLow;
|
|
|
|
|
errorRegisters.DeviceHead = aptdBuf.aptd.CurrentTaskFile.DeviceHead;
|
|
|
|
|
errorRegisters.Error = aptdBuf.aptd.CurrentTaskFile.Error;
|
|
|
|
|
errorRegisters.Sector = aptdBuf.aptd.CurrentTaskFile.SectorNumber;
|
|
|
|
|
errorRegisters.SectorCount = aptdBuf.aptd.CurrentTaskFile.SectorCount;
|
|
|
|
|
errorRegisters.Status = aptdBuf.aptd.CurrentTaskFile.Status;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
sense = errorRegisters.Error != 0 || (errorRegisters.Status & 0xA5) != 0;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends an ATA command in 28-bit LBA mode
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="buffer">Buffer for SCSI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA error returned non-OK status</param>
|
|
|
|
|
/// <param name="registers">Registers to send to drive</param>
|
|
|
|
|
/// <param name="errorRegisters">Registers returned by drive</param>
|
|
|
|
|
/// <param name="protocol">ATA protocol to use</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersLba28 registers,
|
|
|
|
|
out AtaErrorRegistersLba28 errorRegisters, AtaProtocol protocol,
|
2017-12-19 20:33:03 +00:00
|
|
|
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters = new AtaErrorRegistersLba28();
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(buffer == null) return -1;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-09-07 16:48:25 +01:00
|
|
|
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
AtaPassThroughDirectWithBuffer aptdBuf = new AtaPassThroughDirectWithBuffer
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
2017-09-07 16:48:25 +01:00
|
|
|
aptd = new AtaPassThroughDirect
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
2017-09-07 16:48:25 +01:00
|
|
|
TimeOutValue = timeout,
|
|
|
|
|
DataBuffer = (IntPtr)offsetForBuffer,
|
|
|
|
|
Length = (ushort)Marshal.SizeOf(typeof(AtaPassThroughDirect)),
|
2017-09-10 18:15:27 +01:00
|
|
|
DataTransferLength = (uint)buffer.Length,
|
2017-09-07 16:48:25 +01:00
|
|
|
PreviousTaskFile = new AtaTaskFile(),
|
|
|
|
|
CurrentTaskFile = new AtaTaskFile
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
Command = registers.Command,
|
|
|
|
|
CylinderHigh = registers.LbaHigh,
|
|
|
|
|
CylinderLow = registers.LbaMid,
|
|
|
|
|
DeviceHead = registers.DeviceHead,
|
|
|
|
|
Features = registers.Feature,
|
|
|
|
|
SectorCount = registers.SectorCount,
|
|
|
|
|
SectorNumber = registers.LbaLow
|
2017-12-21 02:52:12 +00:00
|
|
|
}
|
2017-09-07 16:48:25 +01:00
|
|
|
},
|
|
|
|
|
dataBuffer = new byte[64 * 512]
|
2017-08-21 04:30:10 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-23 20:04:36 +00:00
|
|
|
switch(protocol)
|
|
|
|
|
{
|
2017-12-21 04:43:29 +00:00
|
|
|
case AtaProtocol.PioIn:
|
|
|
|
|
case AtaProtocol.UDmaIn:
|
2017-12-23 20:04:36 +00:00
|
|
|
case AtaProtocol.Dma:
|
|
|
|
|
aptdBuf.aptd.AtaFlags = AtaFlags.DataIn;
|
2017-12-21 04:43:29 +00:00
|
|
|
break;
|
|
|
|
|
case AtaProtocol.PioOut:
|
2017-12-23 20:04:36 +00:00
|
|
|
case AtaProtocol.UDmaOut:
|
|
|
|
|
aptdBuf.aptd.AtaFlags = AtaFlags.DataOut;
|
2017-12-21 04:43:29 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
switch(protocol)
|
|
|
|
|
{
|
|
|
|
|
case AtaProtocol.Dma:
|
|
|
|
|
case AtaProtocol.DmaQueued:
|
2017-12-20 17:15:26 +00:00
|
|
|
case AtaProtocol.FpDma:
|
2017-08-21 04:30:10 +01:00
|
|
|
case AtaProtocol.UDmaIn:
|
|
|
|
|
case AtaProtocol.UDmaOut:
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.AtaFlags |= AtaFlags.Dma;
|
2017-08-21 04:30:10 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 16:48:25 +01:00
|
|
|
// Unknown if needed
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.AtaFlags |= AtaFlags.DrdyRequired;
|
2017-09-07 16:48:25 +01:00
|
|
|
|
2017-08-21 04:30:10 +01:00
|
|
|
uint k = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(buffer, 0, aptdBuf.dataBuffer, 0, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
DateTime start = DateTime.Now;
|
2017-12-20 17:15:26 +00:00
|
|
|
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IoctlAtaPassThrough, ref aptdBuf,
|
|
|
|
|
(uint)Marshal.SizeOf(aptdBuf), ref aptdBuf,
|
|
|
|
|
(uint)Marshal.SizeOf(aptdBuf), ref k, IntPtr.Zero);
|
2017-08-21 04:30:10 +01:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sense) error = Marshal.GetLastWin32Error();
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(aptdBuf.dataBuffer, 0, buffer, 0, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.LbaHigh = aptdBuf.aptd.CurrentTaskFile.CylinderHigh;
|
|
|
|
|
errorRegisters.LbaMid = aptdBuf.aptd.CurrentTaskFile.CylinderLow;
|
|
|
|
|
errorRegisters.DeviceHead = aptdBuf.aptd.CurrentTaskFile.DeviceHead;
|
|
|
|
|
errorRegisters.Error = aptdBuf.aptd.CurrentTaskFile.Error;
|
|
|
|
|
errorRegisters.LbaLow = aptdBuf.aptd.CurrentTaskFile.SectorNumber;
|
|
|
|
|
errorRegisters.SectorCount = aptdBuf.aptd.CurrentTaskFile.SectorCount;
|
|
|
|
|
errorRegisters.Status = aptdBuf.aptd.CurrentTaskFile.Status;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
sense = errorRegisters.Error != 0 || (errorRegisters.Status & 0xA5) != 0;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends an ATA command in 48-bit LBA mode
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="buffer">Buffer for SCSI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA error returned non-OK status</param>
|
|
|
|
|
/// <param name="registers">Registers to send to drive</param>
|
|
|
|
|
/// <param name="errorRegisters">Registers returned by drive</param>
|
|
|
|
|
/// <param name="protocol">ATA protocol to use</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
internal static int SendAtaCommand(SafeFileHandle fd, AtaRegistersLba48 registers,
|
|
|
|
|
out AtaErrorRegistersLba48 errorRegisters, AtaProtocol protocol,
|
2017-12-19 20:33:03 +00:00
|
|
|
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters = new AtaErrorRegistersLba48();
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(buffer == null) return -1;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-09-07 16:48:25 +01:00
|
|
|
uint offsetForBuffer = (uint)(Marshal.SizeOf(typeof(AtaPassThroughDirect)) + Marshal.SizeOf(typeof(uint)));
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
AtaPassThroughDirectWithBuffer aptdBuf = new AtaPassThroughDirectWithBuffer
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
2017-09-07 16:48:25 +01:00
|
|
|
aptd = new AtaPassThroughDirect
|
2017-08-21 04:30:10 +01:00
|
|
|
{
|
2017-09-07 16:48:25 +01:00
|
|
|
TimeOutValue = timeout,
|
|
|
|
|
DataBuffer = (IntPtr)offsetForBuffer,
|
|
|
|
|
Length = (ushort)Marshal.SizeOf(typeof(AtaPassThroughDirect)),
|
2017-09-10 18:15:27 +01:00
|
|
|
DataTransferLength = (uint)buffer.Length,
|
2017-12-19 20:33:03 +00:00
|
|
|
PreviousTaskFile =
|
|
|
|
|
new AtaTaskFile
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
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)
|
2017-12-19 20:33:03 +00:00
|
|
|
},
|
2017-09-07 16:48:25 +01:00
|
|
|
CurrentTaskFile = new AtaTaskFile
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
Command = registers.Command,
|
|
|
|
|
CylinderHigh = (byte)(registers.LbaHigh & 0xFF),
|
|
|
|
|
CylinderLow = (byte)(registers.LbaMid & 0xFF),
|
|
|
|
|
DeviceHead = registers.DeviceHead,
|
|
|
|
|
Features = (byte)(registers.Feature & 0xFF),
|
|
|
|
|
SectorCount = (byte)(registers.SectorCount & 0xFF),
|
|
|
|
|
SectorNumber = (byte)(registers.LbaLow & 0xFF)
|
2017-12-21 02:52:12 +00:00
|
|
|
}
|
2017-08-21 04:30:10 +01:00
|
|
|
},
|
2017-09-07 16:48:25 +01:00
|
|
|
dataBuffer = new byte[64 * 512]
|
2017-08-21 04:30:10 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-23 20:04:36 +00:00
|
|
|
switch(protocol)
|
|
|
|
|
{
|
2017-12-21 04:43:29 +00:00
|
|
|
case AtaProtocol.PioIn:
|
|
|
|
|
case AtaProtocol.UDmaIn:
|
2017-12-23 20:04:36 +00:00
|
|
|
case AtaProtocol.Dma:
|
|
|
|
|
aptdBuf.aptd.AtaFlags = AtaFlags.DataIn;
|
2017-12-21 04:43:29 +00:00
|
|
|
break;
|
|
|
|
|
case AtaProtocol.PioOut:
|
2017-12-23 20:04:36 +00:00
|
|
|
case AtaProtocol.UDmaOut:
|
|
|
|
|
aptdBuf.aptd.AtaFlags = AtaFlags.DataOut;
|
2017-12-21 04:43:29 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
switch(protocol)
|
|
|
|
|
{
|
|
|
|
|
case AtaProtocol.Dma:
|
|
|
|
|
case AtaProtocol.DmaQueued:
|
2017-12-20 17:15:26 +00:00
|
|
|
case AtaProtocol.FpDma:
|
2017-08-21 04:30:10 +01:00
|
|
|
case AtaProtocol.UDmaIn:
|
|
|
|
|
case AtaProtocol.UDmaOut:
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.AtaFlags |= AtaFlags.Dma;
|
2017-08-21 04:30:10 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 16:48:25 +01:00
|
|
|
// Unknown if needed
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.AtaFlags |= AtaFlags.DrdyRequired;
|
2017-09-07 16:48:25 +01:00
|
|
|
|
2017-08-21 04:30:10 +01:00
|
|
|
uint k = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(buffer, 0, aptdBuf.dataBuffer, 0, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
DateTime start = DateTime.Now;
|
2017-12-20 17:15:26 +00:00
|
|
|
sense = !Extern.DeviceIoControlAta(fd, WindowsIoctl.IoctlAtaPassThrough, ref aptdBuf,
|
|
|
|
|
(uint)Marshal.SizeOf(aptdBuf), ref aptdBuf,
|
|
|
|
|
(uint)Marshal.SizeOf(aptdBuf), ref k, IntPtr.Zero);
|
2017-08-21 04:30:10 +01:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sense) error = Marshal.GetLastWin32Error();
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Array.Copy(aptdBuf.dataBuffer, 0, buffer, 0, buffer.Length);
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.SectorCount = (ushort)((aptdBuf.aptd.PreviousTaskFile.SectorCount << 8) +
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.CurrentTaskFile.SectorCount);
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.LbaLow = (ushort)((aptdBuf.aptd.PreviousTaskFile.SectorNumber << 8) +
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.CurrentTaskFile.SectorNumber);
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.LbaMid = (ushort)((aptdBuf.aptd.PreviousTaskFile.CylinderLow << 8) +
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.CurrentTaskFile.CylinderLow);
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.LbaHigh = (ushort)((aptdBuf.aptd.PreviousTaskFile.CylinderHigh << 8) +
|
2017-12-20 17:15:26 +00:00
|
|
|
aptdBuf.aptd.CurrentTaskFile.CylinderHigh);
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.DeviceHead = aptdBuf.aptd.CurrentTaskFile.DeviceHead;
|
|
|
|
|
errorRegisters.Error = aptdBuf.aptd.CurrentTaskFile.Error;
|
|
|
|
|
errorRegisters.Status = aptdBuf.aptd.CurrentTaskFile.Status;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
sense = errorRegisters.Error != 0 || (errorRegisters.Status & 0xA5) != 0;
|
2017-08-21 04:30:10 +01:00
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2017-09-10 23:20:59 +01:00
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends an ATA command in CHS mode using undocumented Windows XP ioctl
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="buffer">Buffer for SCSI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA error returned non-OK status</param>
|
|
|
|
|
/// <param name="registers">Registers to send to drive</param>
|
|
|
|
|
/// <param name="errorRegisters">Registers returned by drive</param>
|
|
|
|
|
/// <param name="protocol">ATA protocol to use</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
internal static int SendIdeCommand(SafeFileHandle fd, AtaRegistersChs registers,
|
|
|
|
|
out AtaErrorRegistersChs errorRegisters, AtaProtocol protocol,
|
2017-12-19 20:33:03 +00:00
|
|
|
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
2017-09-10 23:20:59 +01:00
|
|
|
{
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters = new AtaErrorRegistersChs();
|
2017-09-10 23:20:59 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(buffer == null || buffer.Length > 512) return -1;
|
2017-09-10 23:20:59 +01:00
|
|
|
|
|
|
|
|
IdePassThroughDirect iptd = new IdePassThroughDirect
|
|
|
|
|
{
|
|
|
|
|
CurrentTaskFile = new AtaTaskFile
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
Command = registers.Command,
|
|
|
|
|
CylinderHigh = registers.CylinderHigh,
|
|
|
|
|
CylinderLow = registers.CylinderLow,
|
|
|
|
|
DeviceHead = registers.DeviceHead,
|
|
|
|
|
Features = registers.Feature,
|
|
|
|
|
SectorCount = registers.SectorCount,
|
|
|
|
|
SectorNumber = registers.Sector
|
2017-09-10 23:20:59 +01:00
|
|
|
},
|
|
|
|
|
DataBufferSize = 512,
|
2017-12-21 02:52:12 +00:00
|
|
|
DataBuffer = new byte[512]
|
2017-09-10 23:20:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
uint k = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
|
|
Array.Copy(buffer, 0, iptd.DataBuffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
DateTime start = DateTime.Now;
|
2017-12-20 17:15:26 +00:00
|
|
|
sense = !Extern.DeviceIoControlIde(fd, WindowsIoctl.IoctlIdePassThrough, ref iptd,
|
2017-12-19 20:33:03 +00:00
|
|
|
(uint)Marshal.SizeOf(iptd), ref iptd, (uint)Marshal.SizeOf(iptd), ref k,
|
|
|
|
|
IntPtr.Zero);
|
2017-09-10 23:20:59 +01:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sense) error = Marshal.GetLastWin32Error();
|
2017-09-10 23:20:59 +01:00
|
|
|
|
|
|
|
|
buffer = new byte[k - 12];
|
|
|
|
|
Array.Copy(iptd.DataBuffer, 0, buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.CylinderHigh = iptd.CurrentTaskFile.CylinderHigh;
|
|
|
|
|
errorRegisters.CylinderLow = iptd.CurrentTaskFile.CylinderLow;
|
|
|
|
|
errorRegisters.DeviceHead = iptd.CurrentTaskFile.DeviceHead;
|
|
|
|
|
errorRegisters.Error = iptd.CurrentTaskFile.Error;
|
|
|
|
|
errorRegisters.Sector = iptd.CurrentTaskFile.SectorNumber;
|
|
|
|
|
errorRegisters.SectorCount = iptd.CurrentTaskFile.SectorCount;
|
|
|
|
|
errorRegisters.Status = iptd.CurrentTaskFile.Status;
|
2017-09-10 23:20:59 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
sense = errorRegisters.Error != 0 || (errorRegisters.Status & 0xA5) != 0;
|
2017-09-10 23:20:59 +01:00
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends an ATA command in 28-bit LBA mode using undocumented Windows XP ioctl
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>0 if no error occurred, otherwise, errno</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="buffer">Buffer for SCSI command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if ATA error returned non-OK status</param>
|
|
|
|
|
/// <param name="registers">Registers to send to drive</param>
|
|
|
|
|
/// <param name="errorRegisters">Registers returned by drive</param>
|
|
|
|
|
/// <param name="protocol">ATA protocol to use</param>
|
2017-12-22 02:04:18 +00:00
|
|
|
internal static int SendIdeCommand(SafeFileHandle fd, AtaRegistersLba28 registers,
|
|
|
|
|
out AtaErrorRegistersLba28 errorRegisters, AtaProtocol protocol,
|
2017-12-19 20:33:03 +00:00
|
|
|
ref byte[] buffer, uint timeout, out double duration, out bool sense)
|
2017-09-10 23:20:59 +01:00
|
|
|
{
|
|
|
|
|
duration = 0;
|
|
|
|
|
sense = false;
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters = new AtaErrorRegistersLba28();
|
2017-09-10 23:20:59 +01:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(buffer == null) return -1;
|
2017-09-10 23:20:59 +01:00
|
|
|
|
|
|
|
|
IdePassThroughDirect iptd = new IdePassThroughDirect
|
|
|
|
|
{
|
|
|
|
|
CurrentTaskFile = new AtaTaskFile
|
|
|
|
|
{
|
2017-12-22 02:04:18 +00:00
|
|
|
Command = registers.Command,
|
|
|
|
|
CylinderHigh = registers.LbaHigh,
|
|
|
|
|
CylinderLow = registers.LbaMid,
|
|
|
|
|
DeviceHead = registers.DeviceHead,
|
|
|
|
|
Features = registers.Feature,
|
|
|
|
|
SectorCount = registers.SectorCount,
|
|
|
|
|
SectorNumber = registers.LbaLow
|
2017-09-10 23:20:59 +01:00
|
|
|
},
|
|
|
|
|
DataBufferSize = 512,
|
2017-12-21 02:52:12 +00:00
|
|
|
DataBuffer = new byte[512]
|
2017-09-10 23:20:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
uint k = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
|
|
Array.Copy(buffer, 0, iptd.DataBuffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
DateTime start = DateTime.Now;
|
2017-12-20 17:15:26 +00:00
|
|
|
sense = !Extern.DeviceIoControlIde(fd, WindowsIoctl.IoctlIdePassThrough, ref iptd,
|
2017-12-19 20:33:03 +00:00
|
|
|
(uint)Marshal.SizeOf(iptd), ref iptd, (uint)Marshal.SizeOf(iptd), ref k,
|
|
|
|
|
IntPtr.Zero);
|
2017-09-10 23:20:59 +01:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sense) error = Marshal.GetLastWin32Error();
|
2017-09-10 23:20:59 +01:00
|
|
|
|
|
|
|
|
buffer = new byte[k - 12];
|
|
|
|
|
Array.Copy(iptd.DataBuffer, 0, buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
errorRegisters.LbaHigh = iptd.CurrentTaskFile.CylinderHigh;
|
|
|
|
|
errorRegisters.LbaMid = iptd.CurrentTaskFile.CylinderLow;
|
|
|
|
|
errorRegisters.DeviceHead = iptd.CurrentTaskFile.DeviceHead;
|
|
|
|
|
errorRegisters.Error = iptd.CurrentTaskFile.Error;
|
|
|
|
|
errorRegisters.LbaLow = iptd.CurrentTaskFile.SectorNumber;
|
|
|
|
|
errorRegisters.SectorCount = iptd.CurrentTaskFile.SectorCount;
|
|
|
|
|
errorRegisters.Status = iptd.CurrentTaskFile.Status;
|
2017-09-10 23:20:59 +01:00
|
|
|
|
2017-12-22 02:04:18 +00:00
|
|
|
sense = errorRegisters.Error != 0 || (errorRegisters.Status & 0xA5) != 0;
|
2017-09-10 23:20:59 +01:00
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2017-12-06 13:46:35 +00:00
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Gets the device number for a specified handle
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="deviceHandle">Device handle</param>
|
|
|
|
|
/// <returns>Device number</returns>
|
2017-12-22 03:13:43 +00:00
|
|
|
static uint GetDeviceNumber(SafeFileHandle deviceHandle)
|
2017-12-06 13:46:35 +00:00
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
StorageDeviceNumber sdn = new StorageDeviceNumber {deviceNumber = -1};
|
2017-12-06 13:46:35 +00:00
|
|
|
uint k = 0;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(!Extern.DeviceIoControlGetDeviceNumber(deviceHandle, WindowsIoctl.IoctlStorageGetDeviceNumber,
|
2017-12-19 20:33:03 +00:00
|
|
|
IntPtr.Zero, 0, ref sdn, (uint)Marshal.SizeOf(sdn), ref k,
|
2017-12-20 23:07:46 +00:00
|
|
|
IntPtr.Zero)) return uint.MaxValue;
|
2017-12-06 13:46:35 +00:00
|
|
|
|
|
|
|
|
return (uint)sdn.deviceNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Gets the internal device path for a specified handle
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fd">Device handle</param>
|
|
|
|
|
/// <returns>Device path</returns>
|
2017-12-06 13:46:35 +00:00
|
|
|
internal static string GetDevicePath(SafeFileHandle fd)
|
|
|
|
|
{
|
|
|
|
|
uint devNumber = GetDeviceNumber(fd);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(devNumber == uint.MaxValue) return null;
|
2017-12-06 13:46:35 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
SafeFileHandle hDevInfo = Extern.SetupDiGetClassDevs(ref Consts.GuidDevinterfaceDisk, IntPtr.Zero,
|
2017-12-19 20:33:03 +00:00
|
|
|
IntPtr.Zero,
|
|
|
|
|
DeviceGetClassFlags.Present |
|
|
|
|
|
DeviceGetClassFlags.DeviceInterface);
|
2017-12-06 13:46:35 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(hDevInfo.IsInvalid) return null;
|
2017-12-06 13:46:35 +00:00
|
|
|
|
|
|
|
|
uint index = 0;
|
|
|
|
|
DeviceInterfaceData spdid = new DeviceInterfaceData();
|
|
|
|
|
spdid.cbSize = Marshal.SizeOf(spdid);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-06 13:46:35 +00:00
|
|
|
while(true)
|
|
|
|
|
{
|
2017-12-22 03:13:43 +00:00
|
|
|
byte[] buffer = new byte[2048];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(!Extern.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref Consts.GuidDevinterfaceDisk, index,
|
2017-12-19 20:33:03 +00:00
|
|
|
ref spdid)) break;
|
2017-12-06 13:46:35 +00:00
|
|
|
|
|
|
|
|
uint size = 0;
|
|
|
|
|
|
|
|
|
|
Extern.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref spdid, IntPtr.Zero, 0, ref size, IntPtr.Zero);
|
|
|
|
|
|
|
|
|
|
if(size > 0 && size < buffer.Length)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
buffer[0] = (byte)(IntPtr.Size == 8
|
|
|
|
|
? IntPtr.Size
|
|
|
|
|
: IntPtr.Size + Marshal.SystemDefaultCharSize); // Funny...
|
2017-12-06 13:46:35 +00:00
|
|
|
|
|
|
|
|
IntPtr pspdidd = Marshal.AllocHGlobal(buffer.Length);
|
|
|
|
|
Marshal.Copy(buffer, 0, pspdidd, buffer.Length);
|
|
|
|
|
|
|
|
|
|
bool result =
|
2017-12-19 20:33:03 +00:00
|
|
|
Extern.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref spdid, pspdidd, size, ref size,
|
|
|
|
|
IntPtr.Zero);
|
2017-12-06 13:46:35 +00:00
|
|
|
|
|
|
|
|
buffer = new byte[size];
|
|
|
|
|
Marshal.Copy(pspdidd, buffer, 0, buffer.Length);
|
|
|
|
|
Marshal.FreeHGlobal(pspdidd);
|
|
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
|
{
|
|
|
|
|
string devicePath = Encoding.Unicode.GetString(buffer, 4, (int)size - 4);
|
|
|
|
|
SafeFileHandle hDrive = Extern.CreateFile(devicePath, 0, FileShare.Read | FileShare.Write,
|
2017-12-19 20:33:03 +00:00
|
|
|
IntPtr.Zero, FileMode.OpenExisting, 0, IntPtr.Zero);
|
2017-12-06 13:46:35 +00:00
|
|
|
|
|
|
|
|
if(!hDrive.IsInvalid)
|
|
|
|
|
{
|
|
|
|
|
uint newDeviceNumber = GetDeviceNumber(hDrive);
|
|
|
|
|
|
|
|
|
|
if(newDeviceNumber == devNumber)
|
|
|
|
|
{
|
|
|
|
|
Extern.CloseHandle(hDrive);
|
|
|
|
|
return devicePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-06 13:46:35 +00:00
|
|
|
Extern.CloseHandle(hDrive);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-06 13:46:35 +00:00
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Extern.SetupDiDestroyDeviceInfoList(hDevInfo);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2017-12-23 02:32:02 +00:00
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Returns true if the specified handle is controlled by a SFFDISK (aka SDHCI) driver
|
2017-12-23 02:32:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fd">Device handle</param>
|
|
|
|
|
/// <returns><c>true</c> if SDHCI, false otherwise</returns>
|
2017-12-06 23:05:59 +00:00
|
|
|
internal static bool IsSdhci(SafeFileHandle fd)
|
|
|
|
|
{
|
|
|
|
|
SffdiskQueryDeviceProtocolData queryData1 = new SffdiskQueryDeviceProtocolData();
|
|
|
|
|
queryData1.size = (ushort)Marshal.SizeOf(queryData1);
|
2017-12-20 17:15:26 +00:00
|
|
|
Extern.DeviceIoControl(fd, WindowsIoctl.IoctlSffdiskQueryDeviceProtocol, IntPtr.Zero, 0, ref queryData1,
|
2017-12-22 03:13:43 +00:00
|
|
|
queryData1.size, out _, IntPtr.Zero);
|
2017-12-20 17:15:26 +00:00
|
|
|
return queryData1.protocolGuid.Equals(Consts.GuidSffProtocolSd);
|
2017-12-06 23:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-23 20:04:36 +00:00
|
|
|
/// Sends a MMC/SD command
|
2017-12-06 23:05:59 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The result of the command.</returns>
|
|
|
|
|
/// <param name="fd">File handle</param>
|
|
|
|
|
/// <param name="command">MMC/SD opcode</param>
|
|
|
|
|
/// <param name="buffer">Buffer for MMC/SD command response</param>
|
|
|
|
|
/// <param name="timeout">Timeout in seconds</param>
|
|
|
|
|
/// <param name="duration">Time it took to execute the command in milliseconds</param>
|
|
|
|
|
/// <param name="sense"><c>True</c> if MMC/SD returned non-OK status</param>
|
|
|
|
|
/// <param name="write"><c>True</c> if data is sent from host to card</param>
|
|
|
|
|
/// <param name="isApplication"><c>True</c> if command should be preceded with CMD55</param>
|
|
|
|
|
/// <param name="flags">Flags indicating kind and place of response</param>
|
|
|
|
|
/// <param name="blocks">How many blocks to transfer</param>
|
|
|
|
|
/// <param name="argument">Command argument</param>
|
|
|
|
|
/// <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,
|
2017-12-19 20:33:03 +00:00
|
|
|
MmcFlags flags, uint argument, uint blockSize, uint blocks,
|
|
|
|
|
ref byte[] buffer, out uint[] response, out double duration, out bool sense,
|
|
|
|
|
uint timeout = 0)
|
2017-12-06 23:05:59 +00:00
|
|
|
{
|
|
|
|
|
SffdiskDeviceCommandData commandData = new SffdiskDeviceCommandData();
|
|
|
|
|
SdCmdDescriptor commandDescriptor = new SdCmdDescriptor();
|
|
|
|
|
commandData.size = (ushort)Marshal.SizeOf(commandData);
|
|
|
|
|
commandData.command = SffdiskDcmd.DeviceCommand;
|
|
|
|
|
commandData.protocolArgumentSize = (ushort)Marshal.SizeOf(commandDescriptor);
|
|
|
|
|
commandData.deviceDataBufferSize = blockSize * blocks;
|
|
|
|
|
commandDescriptor.commandCode = (byte)command;
|
|
|
|
|
commandDescriptor.cmdClass = isApplication ? SdCommandClass.AppCmd : SdCommandClass.Standard;
|
|
|
|
|
commandDescriptor.transferDirection = write ? SdTransferDirection.Write : SdTransferDirection.Read;
|
2017-12-20 17:15:26 +00:00
|
|
|
commandDescriptor.transferType = flags.HasFlag(MmcFlags.CommandAdtc)
|
2017-12-19 20:33:03 +00:00
|
|
|
? SdTransferType.SingleBlock
|
|
|
|
|
: SdTransferType.CmdOnly;
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = 0;
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR1) || flags.HasFlag(MmcFlags.ResponseSpiR1))
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = SdResponseType.R1;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR1B) || flags.HasFlag(MmcFlags.ResponseSpiR1B))
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = SdResponseType.R1b;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR2) || flags.HasFlag(MmcFlags.ResponseSpiR2))
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = SdResponseType.R2;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR3) || flags.HasFlag(MmcFlags.ResponseSpiR3))
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = SdResponseType.R3;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR4) || flags.HasFlag(MmcFlags.ResponseSpiR4))
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = SdResponseType.R4;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR5) || flags.HasFlag(MmcFlags.ResponseSpiR5))
|
2017-12-06 23:05:59 +00:00
|
|
|
commandDescriptor.responseType = SdResponseType.R5;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(flags.HasFlag(MmcFlags.ResponseR6)) commandDescriptor.responseType = SdResponseType.R6;
|
2017-12-06 23:05:59 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] commandB = new byte[commandData.size + commandData.protocolArgumentSize +
|
2017-12-23 20:04:36 +00:00
|
|
|
commandData.deviceDataBufferSize];
|
2017-12-20 17:15:26 +00:00
|
|
|
IntPtr hBuf = Marshal.AllocHGlobal(commandB.Length);
|
2017-12-06 23:05:59 +00:00
|
|
|
Marshal.StructureToPtr(commandData, hBuf, true);
|
|
|
|
|
IntPtr descriptorOffset = new IntPtr(hBuf.ToInt32() + commandData.size);
|
|
|
|
|
Marshal.StructureToPtr(commandDescriptor, descriptorOffset, true);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(hBuf, commandB, 0, commandB.Length);
|
2017-12-06 23:05:59 +00:00
|
|
|
Marshal.FreeHGlobal(hBuf);
|
|
|
|
|
|
|
|
|
|
int error = 0;
|
|
|
|
|
DateTime start = DateTime.Now;
|
2017-12-23 20:04:36 +00:00
|
|
|
sense = !Extern.DeviceIoControl(fd, WindowsIoctl.IoctlSffdiskDeviceCommand, commandB, (uint)commandB.Length,
|
|
|
|
|
commandB, (uint)commandB.Length, out _, IntPtr.Zero);
|
2017-12-06 23:05:59 +00:00
|
|
|
DateTime end = DateTime.Now;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(sense) error = Marshal.GetLastWin32Error();
|
2017-12-06 23:05:59 +00:00
|
|
|
|
|
|
|
|
buffer = new byte[blockSize * blocks];
|
2017-12-20 17:15:26 +00:00
|
|
|
Buffer.BlockCopy(commandB, commandB.Length - buffer.Length, buffer, 0, buffer.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-06 23:05:59 +00:00
|
|
|
response = new uint[4];
|
|
|
|
|
duration = (end - start).TotalMilliseconds;
|
|
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2015-10-12 06:25:49 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|