Skip to content

Commit

Permalink
sed: Add plugin for basic SED Opal operations
Browse files Browse the repository at this point in the history
A new plugin 'sed' is developed to provide basic SED Opal CLI
operations. These include:
        discover        Discover drive locking features
        intialize       Initialize a drive for SED Opal
        password        Change the authorization key
        revert          Revert drive to SED Opal disabled
        lock            Lock a SED Opal drive
        unlock          Unlock a SED Opal drive

Signed-off-by: Greg Joyce <gjoyce@linux.ibm.com>
  • Loading branch information
Greg Joyce authored and igaw committed Feb 8, 2024
1 parent d088264 commit 83aad43
Show file tree
Hide file tree
Showing 8 changed files with 865 additions and 0 deletions.
23 changes: 23 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ conf.set10(
cc.get_id() == 'clang',
description: 'Is compiler warning about unused static line function?'
)
conf.set10(
'HAVE_SED_OPAL',
cc.compiles(
'''#include <linux/sed-opal.h>''',
name: 'linux/sed-opal.h'

),
description: 'Is linux/sed-opa.h include-able?'
)
conf.set10(
'HAVE_KEY_TYPE',
cc.compiles(
'''
#include <linux/sed-opal.h>
int main(void) {
struct opal_key key;
key.key_type = OPAL_INCLUDED;
}
''',
name: 'key_type'
),
description: 'Does struct opal_key have a key_type field?'
)

if cc.has_function_attribute('fallthrough')
conf.set('fallthrough', '__attribute__((__fallthrough__))')
Expand Down
3 changes: 3 additions & 0 deletions plugins/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ if json_c_dep.found()
]
subdir('solidigm')
subdir('ocp')
if conf.has('HAVE_SED_OPAL')
subdir('sed')
endif
endif
4 changes: 4 additions & 0 deletions plugins/sed/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources += [
'plugins/sed/sed.c',
'plugins/sed/sedopal_cmd.c',
]
178 changes: 178 additions & 0 deletions plugins/sed/sed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <inttypes.h>
#include <linux/fs.h>
#include <sys/stat.h>

#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include "nvme-print.h"
#include "sedopal_cmd.h"
#include <linux/sed-opal.h>

#define CREATE_CMD
#include "sed.h"

OPT_ARGS(no_opts) = {
OPT_END()
};

OPT_ARGS(key_opts) = {
OPT_FLAG("ask-key", 'k', &sedopal_ask_key,
"prompt for SED authentication key"),
OPT_END()
};

OPT_ARGS(revert_opts) = {
OPT_FLAG("destructive", 'e', &sedopal_destructive_revert,
"destructive revert"),
OPT_FLAG("psid", 'p', &sedopal_psid_revert, "PSID revert"),
OPT_END()
};


/*
* Open the NVMe device specified on the command line. It must be the
* NVMe block device (e.g. /dev/nvme0n1).
*/
static int sed_opal_open_device(struct nvme_dev **dev, int argc, char **argv,
const char *desc, struct argconfig_commandline_options *opts)
{
int err;

err = parse_and_open(dev, argc, argv, desc, opts);
if (err)
return err;

if (!S_ISBLK((*dev)->direct.stat.st_mode)) {
fprintf(stderr,
"ERROR : The NVMe block device must be specified\n");
err = -EINVAL;
dev_close(*dev);
}

return err;
}

static int sed_opal_discover(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
int err;
const char *desc = "Query SED device and display locking features";
struct nvme_dev *dev;

err = sed_opal_open_device(&dev, argc, argv, desc, no_opts);
if (err)
return err;

err = sedopal_cmd_discover(dev->direct.fd);

dev_close(dev);
return err;
}

static int sed_opal_initialize(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
int err;
const char *desc = "Initialize a SED device for locking";
struct nvme_dev *dev;

err = sed_opal_open_device(&dev, argc, argv, desc, no_opts);
if (err)
return err;

err = sedopal_cmd_initialize(dev->direct.fd);
if (err != 0)
fprintf(stderr, "initialize: SED error - %s\n",
sedopal_error_to_text(err));

dev_close(dev);
return err;
}

static int sed_opal_revert(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
int err;
const char *desc = "Revert a SED device from locking state";
struct nvme_dev *dev;

err = sed_opal_open_device(&dev, argc, argv, desc, revert_opts);
if (err)
return err;

err = sedopal_cmd_revert(dev->direct.fd);
if (err != 0)
fprintf(stderr, "revert: SED error - %s\n",
sedopal_error_to_text(err));

dev_close(dev);
return err;
}

static int sed_opal_lock(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
int err;
const char *desc = "Lock a SED device";
struct nvme_dev *dev;

err = sed_opal_open_device(&dev, argc, argv, desc, key_opts);
if (err)
return err;

err = sedopal_cmd_lock(dev->direct.fd);
if (err != 0)
fprintf(stderr, "lock: SED error - %s\n",
sedopal_error_to_text(err));

dev_close(dev);
return err;
}

static int sed_opal_unlock(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
int err;
const char *desc = "Unlock a SED device";
struct nvme_dev *dev;

err = sed_opal_open_device(&dev, argc, argv, desc, key_opts);
if (err)
return err;

err = sedopal_cmd_unlock(dev->direct.fd);
if (err != 0)
fprintf(stderr, "unlock: SED error - %s\n",
sedopal_error_to_text(err));

dev_close(dev);
return err;
}

static int sed_opal_password(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
int err;
const char *desc = "Change the locking password of a SED device";
struct nvme_dev *dev;

err = sed_opal_open_device(&dev, argc, argv, desc, no_opts);
if (err)
return err;

err = sedopal_cmd_password(dev->direct.fd);
if (err != 0)
fprintf(stderr, "password: SED error - %s\n",
sedopal_error_to_text(err));

dev_close(dev);
return err;
}
19 changes: 19 additions & 0 deletions plugins/sed/sed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#undef CMD_INC_FILE
#define CMD_INC_FILE plugins/sed/sed

#include "cmd.h"
#include <linux/sed-opal.h>

PLUGIN(NAME("sed", "SED Opal Command Set", NVME_VERSION),
COMMAND_LIST(
ENTRY("discover", "Discover SED Opal Locking Features", sed_opal_discover, "1")
ENTRY("initialize", "Initialize a SED Opal Device for locking", sed_opal_initialize)
ENTRY("revert", "Revert a SED Opal Device from locking", sed_opal_revert)
ENTRY("lock", "Lock a SED Opal Device", sed_opal_lock)
ENTRY("unlock", "Unlock a SED Opal Device", sed_opal_unlock)
ENTRY("password", "Change the SED Opal Device password", sed_opal_password)
)
);

#include "define_cmd.h"
Loading

0 comments on commit 83aad43

Please sign in to comment.