// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : Smart.cs // Author(s) : Natalia Portillo // // Component : ATA commands. // // --[ Description ] ---------------------------------------------------------- // // Contains S.M.A.R.T. commands. // // --[ License ] -------------------------------------------------------------- // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ namespace Aaru.Devices; using System; using Aaru.Console; using Aaru.Decoders.ATA; public sealed partial class Device { /// Disables S.M.A.R.T. /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartDisable(out AtaErrorRegistersLba28 statusRegisters, uint timeout, out double duration) { byte[] buffer = Array.Empty(); var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.Disable, LbaHigh = 0xC2, LbaMid = 0x4F }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART DISABLE OPERATIONS took {0} ms.", duration); return sense; } /// Enables auto-saving of S.M.A.R.T. attributes /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartEnableAttributeAutosave(out AtaErrorRegistersLba28 statusRegisters, uint timeout, out double duration) { byte[] buffer = Array.Empty(); var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.EnableDisableAttributeAutosave, LbaHigh = 0xC2, LbaMid = 0x4F, SectorCount = 0xF1 }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART ENABLE ATTRIBUTE AUTOSAVE took {0} ms.", duration); return sense; } /// Disables auto-saving of S.M.A.R.T. attributes /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartDisableAttributeAutosave(out AtaErrorRegistersLba28 statusRegisters, uint timeout, out double duration) { byte[] buffer = Array.Empty(); var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.EnableDisableAttributeAutosave, LbaHigh = 0xC2, LbaMid = 0x4F }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART DISABLE ATTRIBUTE AUTOSAVE took {0} ms.", duration); return sense; } /// Enables S.M.A.R.T. /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartEnable(out AtaErrorRegistersLba28 statusRegisters, uint timeout, out double duration) { byte[] buffer = Array.Empty(); var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.Enable, LbaHigh = 0xC2, LbaMid = 0x4F }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART ENABLE OPERATIONS took {0} ms.", duration); return sense; } /// Requests drive to execute offline immediate S.M.A.R.T. test /// Subcommand /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartExecuteOffLineImmediate(out AtaErrorRegistersLba28 statusRegisters, byte subcommand, uint timeout, out double duration) { byte[] buffer = Array.Empty(); var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.ExecuteOfflineImmediate, LbaHigh = 0xC2, LbaMid = 0x4F, LbaLow = subcommand }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART EXECUTE OFF-LINE IMMEDIATE took {0} ms.", duration); return sense; } /// Reads S.M.A.R.T. data /// Buffer containing data /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartReadData(out byte[] buffer, out AtaErrorRegistersLba28 statusRegisters, uint timeout, out double duration) { buffer = new byte[512]; var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.ReadData, LbaHigh = 0xC2, LbaMid = 0x4F }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART READ DATA took {0} ms.", duration); return sense; } /// Reads S.M.A.R.T. log /// Buffer containing log /// Returned status registers /// Log address /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartReadLog(out byte[] buffer, out AtaErrorRegistersLba28 statusRegisters, byte logAddress, uint timeout, out double duration) { buffer = new byte[512]; var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.ReadLog, LbaHigh = 0xC2, LbaMid = 0x4F, LbaLow = logAddress }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.PioIn, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART READ LOG took {0} ms.", duration); return sense; } /// Retrieves S.M.A.R.T. status /// Returned status registers /// Timeout to wait for command execution /// Time the device took to execute the command in milliseconds /// true if the device set an error condition, false otherwise public bool SmartReturnStatus(out AtaErrorRegistersLba28 statusRegisters, uint timeout, out double duration) { byte[] buffer = Array.Empty(); var registers = new AtaRegistersLba28 { Command = (byte)AtaCommands.Smart, Feature = (byte)AtaSmartSubCommands.ReturnStatus, LbaHigh = 0xC2, LbaMid = 0x4F }; LastError = SendAtaCommand(registers, out statusRegisters, AtaProtocol.NonData, AtaTransferRegister.NoTransfer, ref buffer, timeout, false, out duration, out bool sense); Error = LastError != 0; AaruConsole.DebugWriteLine("ATA Device", "SMART RETURN STATUS took {0} ms.", duration); return sense; } }