From d46897645c8545638983e414b695a6a901074b86 Mon Sep 17 00:00:00 2001 From: itycodes Date: Wed, 29 May 2024 06:19:13 +0200 Subject: [PATCH 1/4] Add ATA command Security Unlock --- smartmontools/atacmds.h | 1 + smartmontools/ataprint.cpp | 16 +++++++++++++--- smartmontools/ataprint.h | 1 + smartmontools/smartctl.cpp | 3 +++ smartmontools/smartd.cpp | 6 ++++++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/smartmontools/atacmds.h b/smartmontools/atacmds.h index 9e813776..b914cc64 100644 --- a/smartmontools/atacmds.h +++ b/smartmontools/atacmds.h @@ -55,6 +55,7 @@ typedef enum { #define ATA_IDLE 0xe3 #define ATA_SMART_CMD 0xb0 #define ATA_SECURITY_FREEZE_LOCK 0xf5 +#define ATA_SECURITY_UNLOCK 0xf2 #ifndef ATA_SET_FEATURES #define ATA_SET_FEATURES 0xef #endif diff --git a/smartmontools/ataprint.cpp b/smartmontools/ataprint.cpp index 4efb03a7..40edd33f 100644 --- a/smartmontools/ataprint.cpp +++ b/smartmontools/ataprint.cpp @@ -3754,7 +3754,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options) if ( options.smart_disable || options.smart_enable || options.smart_auto_save_disable || options.smart_auto_save_enable || options.smart_auto_offl_disable || options.smart_auto_offl_enable - || options.set_aam || options.set_apm || options.set_lookahead + || options.set_aam || options.set_apm || options.set_lookahead || options.set_security_unlock || options.set_wcache || options.set_security_freeze || options.set_standby || options.sct_wcache_reorder_set || options.sct_wcache_sct_set || options.set_dsn) pout("=== START OF ENABLE/DISABLE COMMANDS SECTION ===\n"); @@ -3878,6 +3878,16 @@ int ataPrintMain (ata_device * device, const ata_print_options & options) pout("ATA Security set to frozen mode\n"); } + // Unlock ATA security + if (options.set_security_unlock) { + if (!ata_nodata_command(device, ATA_SECURITY_UNLOCK)) { + pout("ATA SECURITY UNLOCK failed: %s\n", device->get_errmsg()); + returnval |= FAILSMART; + } + else + pout("ATA Security set to unlocked\n"); + } + // Set standby timer unless immediate standby is also requested if (options.set_standby && !options.set_standby_now) { if (!ata_nodata_command(device, ATA_IDLE, options.set_standby-1)) { @@ -4000,8 +4010,8 @@ int ataPrintMain (ata_device * device, const ata_print_options & options) || options.smart_auto_save_disable || options.smart_auto_save_enable || options.smart_auto_offl_disable || options.smart_auto_offl_enable || options.set_aam || options.set_apm || options.set_lookahead - || options.set_wcache || options.set_security_freeze || options.set_standby - || options.sct_wcache_reorder_set || options.set_dsn) + || options.set_wcache || options.set_security_freeze || options.set_security_unlock + || options.set_standby || options.sct_wcache_reorder_set || options.set_dsn) pout("\n"); // START OF READ-ONLY OPTIONS APART FROM -V and -i diff --git a/smartmontools/ataprint.h b/smartmontools/ataprint.h index 64b74570..ff35dead 100644 --- a/smartmontools/ataprint.h +++ b/smartmontools/ataprint.h @@ -93,6 +93,7 @@ struct ata_print_options bool set_standby_now = false; // set drive to standby bool get_security = false; // print ATA security status bool set_security_freeze = false; // Freeze ATA security + bool set_security_unlock = false; // Unlock ATA security bool get_wcache = false; // print write cache status int set_wcache = 0; // disable(-1), enable(1) write cache bool sct_wcache_reorder_get = false; // print write cache reordering status diff --git a/smartmontools/smartctl.cpp b/smartmontools/smartctl.cpp index 67d1beef..4fd5915e 100644 --- a/smartmontools/smartctl.cpp +++ b/smartmontools/smartctl.cpp @@ -1077,6 +1077,9 @@ static int parse_options(int argc, char** argv, const char * & type, else if (!get && !strcmp(optarg, "security-freeze")) { ataopts.set_security_freeze = true; } + else if(!get && !strcmp(optarg, "security-unlock")) { + ataopts.set_security_unlock = true; + } else if (!get && !strcmp(optarg, "standby,now")) { ataopts.set_standby_now = true; scsiopts.set_standby_now = true; diff --git a/smartmontools/smartd.cpp b/smartmontools/smartd.cpp index 34f2cde2..9e4ae2eb 100644 --- a/smartmontools/smartd.cpp +++ b/smartmontools/smartd.cpp @@ -435,6 +435,7 @@ struct dev_config int set_lookahead{}; // disable(-1), enable(1) read look-ahead int set_standby{}; // set(1..255->0..254) standby timer bool set_security_freeze{}; // Freeze ATA security + bool set_security_unlock{}; // Unlock ATA security int set_wcache{}; // disable(-1), enable(1) write cache int set_dsn{}; // disable(0x2), enable(0x1) DSN @@ -2341,6 +2342,11 @@ static int ATADeviceScan(dev_config & cfg, dev_state & state, ata_device * atade format_set_result_msg(msg, "Security freeze", ata_nodata_command(atadev, ATA_SECURITY_FREEZE_LOCK)); + + if (cfg.set_security_unlock) + format_set_result_msg(msg, "Security unlock", + ata_nodata_command(atadev, ATA_SECURITY_UNLOCK)); + if (cfg.set_standby) format_set_result_msg(msg, "Standby", ata_nodata_command(atadev, ATA_IDLE, cfg.set_standby-1), cfg.set_standby, true); -- 2.45.1