From 75ec1d3d92be0665fcdc2e91c1897212071bb0da Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 14 Jul 2020 02:45:27 +0100 Subject: [PATCH] Add ATA commands for lock, unlock and eject. --- Aaru.Devices/Device/AtaCommands/AtaCHS.cs | 81 +++++++++++++++++++---- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/Aaru.Devices/Device/AtaCommands/AtaCHS.cs b/Aaru.Devices/Device/AtaCommands/AtaCHS.cs index 0b8f7e12b..4ca16764a 100644 --- a/Aaru.Devices/Device/AtaCommands/AtaCHS.cs +++ b/Aaru.Devices/Device/AtaCommands/AtaCHS.cs @@ -99,8 +99,7 @@ namespace Aaru.Devices var registers = new AtaRegistersChs { SectorCount = count, CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), - CylinderLow = (byte)((cylinder & 0xFF) / 0x1), DeviceHead = (byte)(head & 0x0F), - Sector = sector, + CylinderLow = (byte)((cylinder & 0xFF) / 0x1), DeviceHead = (byte)(head & 0x0F), Sector = sector, Command = retry ? (byte)AtaCommands.ReadDmaRetry : (byte)AtaCommands.ReadDma }; @@ -149,8 +148,7 @@ namespace Aaru.Devices var registers = new AtaRegistersChs { Command = retry ? (byte)AtaCommands.ReadRetry : (byte)AtaCommands.Read, SectorCount = count, - CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), - CylinderLow = (byte)((cylinder & 0xFF) / 0x1), + CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), CylinderLow = (byte)((cylinder & 0xFF) / 0x1), DeviceHead = (byte)(head & 0x0F), Sector = sector }; @@ -177,10 +175,8 @@ namespace Aaru.Devices var registers = new AtaRegistersChs { Command = retry ? (byte)AtaCommands.ReadLongRetry : (byte)AtaCommands.ReadLong, SectorCount = 1, - CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), - CylinderLow = (byte)((cylinder & 0xFF) / 0x1), - DeviceHead = (byte)(head & 0x0F), - Sector = sector + CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), CylinderLow = (byte)((cylinder & 0xFF) / 0x1), + DeviceHead = (byte)(head & 0x0F), Sector = sector }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn, @@ -202,8 +198,7 @@ namespace Aaru.Devices var registers = new AtaRegistersChs { Command = (byte)AtaCommands.Seek, CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), - CylinderLow = (byte)((cylinder & 0xFF) / 0x1), - DeviceHead = (byte)(head & 0x0F), Sector = sector + CylinderLow = (byte)((cylinder & 0xFF) / 0x1), DeviceHead = (byte)(head & 0x0F), Sector = sector }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, @@ -229,10 +224,8 @@ namespace Aaru.Devices var registers = new AtaRegistersChs { Command = (byte)AtaCommands.SetFeatures, CylinderHigh = (byte)((cylinder & 0xFF00) / 0x100), - CylinderLow = (byte)((cylinder & 0xFF) / 0x1), - DeviceHead = (byte)(head & 0x0F), Sector = sector, - SectorCount = sectorCount, - Feature = (byte)feature + CylinderLow = (byte)((cylinder & 0xFF) / 0x1), DeviceHead = (byte)(head & 0x0F), Sector = sector, + SectorCount = sectorCount, Feature = (byte)feature }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, @@ -245,5 +238,65 @@ namespace Aaru.Devices return sense; } + + public bool DoorLock(out AtaErrorRegistersChs statusRegisters, uint timeout, out double duration) + { + byte[] buffer = new byte[0]; + + var registers = new AtaRegistersChs + { + Command = (byte)AtaCommands.DoorLock + }; + + LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, + AtaTransferRegister.NoTransfer, ref buffer, timeout, true, out duration, + out bool sense); + + Error = LastError != 0; + + AaruConsole.DebugWriteLine("ATA Device", "DOOR LOCK took {0} ms.", duration); + + return sense; + } + + public bool DoorUnlock(out AtaErrorRegistersChs statusRegisters, uint timeout, out double duration) + { + byte[] buffer = new byte[0]; + + var registers = new AtaRegistersChs + { + Command = (byte)AtaCommands.DoorUnLock + }; + + LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, + AtaTransferRegister.NoTransfer, ref buffer, timeout, true, out duration, + out bool sense); + + Error = LastError != 0; + + AaruConsole.DebugWriteLine("ATA Device", "DOOR UNLOCK took {0} ms.", duration); + + return sense; + } + + public bool MediaEject(out AtaErrorRegistersChs statusRegisters, uint timeout, out double duration) + { + byte[] buffer = new byte[0]; + + var registers = new AtaRegistersChs + { + Command = (byte)AtaCommands.MediaEject + }; + + LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, + AtaTransferRegister.NoTransfer, ref buffer, timeout, true, out duration, + out bool sense); + + Error = LastError != 0; + + AaruConsole.DebugWriteLine("ATA Device", "MEDIA EJECT took {0} ms.", duration); + + return sense; + } } } \ No newline at end of file