From 4a66443c85203a3c3c494c5e8ad247d925fa2c1e Mon Sep 17 00:00:00 2001 From: Greg Joyce Date: Thu, 10 Apr 2025 17:23:23 -0500 Subject: [PATCH] sed: better error messages for revert and password Use context to print better error messages for common revert and password errors. Signed-off-by: Greg Joyce --- plugins/sed/sed.c | 4 ++-- plugins/sed/sedopal_cmd.c | 41 +++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/plugins/sed/sed.c b/plugins/sed/sed.c index b4ac0377..3d6052a9 100644 --- a/plugins/sed/sed.c +++ b/plugins/sed/sed.c @@ -130,7 +130,7 @@ static int sed_opal_revert(int argc, char **argv, struct command *cmd, return err; err = sedopal_cmd_revert(dev->direct.fd); - if ((err != 0) && (err != -EOPNOTSUPP)) + if ((err != 0) && (err != -EOPNOTSUPP) && (err != EPERM)) fprintf(stderr, "revert: SED error - %s\n", sedopal_error_to_text(err)); @@ -190,7 +190,7 @@ static int sed_opal_password(int argc, char **argv, struct command *cmd, return err; err = sedopal_cmd_password(dev->direct.fd); - if (err != 0) + if ((err != 0) && (err != EPERM)) fprintf(stderr, "password: SED error - %s\n", sedopal_error_to_text(err)); diff --git a/plugins/sed/sedopal_cmd.c b/plugins/sed/sedopal_cmd.c index 8d931b9a..bbcf16b4 100644 --- a/plugins/sed/sedopal_cmd.c +++ b/plugins/sed/sedopal_cmd.c @@ -127,11 +127,15 @@ char *sedopal_get_password(char *prompt) return NULL; len = strlen(pass); - if (len < SEDOPAL_MIN_PASSWORD_LEN) + if (len < SEDOPAL_MIN_PASSWORD_LEN) { + fprintf(stderr, "Error: password is not long enough\n"); return NULL; + } - if (len > SEDOPAL_MAX_PASSWORD_LEN) + if (len > SEDOPAL_MAX_PASSWORD_LEN) { + fprintf(stderr, "Error: password is too long\n"); return NULL; + } return pass; } @@ -425,8 +429,12 @@ static int sedopal_revert_psid(int fd) rc = sedopal_set_key(&key); if (rc == 0) { rc = ioctl(fd, IOC_OPAL_PSID_REVERT_TPR, &key); - if (rc != 0) - fprintf(stderr, "PSID_REVERT_TPR rc %d\n", rc); + if (rc != 0) { + if (rc == EPERM) + fprintf(stderr, "Error: incorrect password\n"); + else + fprintf(stderr, "PSID_REVERT_TPR rc %d\n", rc); + } } return rc; @@ -457,6 +465,7 @@ int sedopal_cmd_revert(int fd) #ifdef IOC_OPAL_REVERT_LSP struct opal_revert_lsp revert_lsp; uint8_t locking_state; + char *revert = "LSP"; locking_state = sedopal_locking_state(fd); @@ -481,6 +490,7 @@ int sedopal_cmd_revert(int fd) rc = ioctl(fd, IOC_OPAL_REVERT_LSP, &revert_lsp); if (rc == 0) { + revert = "TPER"; /* * TPER must also be reverted. */ @@ -488,12 +498,20 @@ int sedopal_cmd_revert(int fd) if (rc != 0) fprintf(stderr, "Error: revert TPR - %d\n", rc); } + + if (rc != 0) { + if (rc == EPERM) + fprintf(stderr, "Error: incorrect password\n"); + else + fprintf(stderr, "Error: revert %s - %d\n", + revert, rc); + } #else rc = -EOPNOTSUPP; #endif } - if (rc != 0) + if ((rc != 0) && (rc != EPERM)) fprintf(stderr, "Error: failed reverting drive - %d\n", rc); return rc; @@ -533,7 +551,10 @@ int sedopal_cmd_password(int fd) */ rc = ioctl(fd, IOC_OPAL_SET_PW, &new_pw); if (rc != 0) { - fprintf(stderr, "Error: failed setting password - %d\n", rc); + if (rc == EPERM) + fprintf(stderr, "Error: incorrect password\n"); + else + fprintf(stderr, "Error: setting password - %d\n", rc); return rc; } @@ -542,8 +563,12 @@ int sedopal_cmd_password(int fd) * set sid password */ rc = ioctl(fd, IOC_OPAL_SET_SID_PW, &new_pw); - if (rc != 0) - fprintf(stderr, "Error: failed setting SID password - %d\n", rc); + if (rc != 0) { + if (rc == EPERM) + fprintf(stderr, "Error: incorrect password\n"); + else + fprintf(stderr, "Error: setting SID pw - %d\n", rc); + } #endif return rc; -- 2.49.0