Skip to content

Commit 128f0d7

Browse files
committed
sed: Add plugin for basic SED Opal operations
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>
1 parent 209a384 commit 128f0d7

8 files changed

+865
-0
lines changed

meson.build

+23
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ conf.set10(
153153
cc.get_id() == 'clang',
154154
description: 'Is compiler warning about unused static line function?'
155155
)
156+
conf.set10(
157+
'HAVE_SED_OPAL',
158+
cc.compiles(
159+
'''#include <linux/sed-opal.h>''',
160+
name: 'linux/sed-opal.h'
161+
162+
),
163+
description: 'Is linux/sed-opa.h include-able?'
164+
)
165+
conf.set10(
166+
'HAVE_KEY_TYPE',
167+
cc.compiles(
168+
'''
169+
#include <linux/sed-opal.h>
170+
int main(void) {
171+
struct opal_key key;
172+
key.key_type = OPAL_INCLUDED;
173+
}
174+
''',
175+
name: 'key_type'
176+
),
177+
description: 'Does struct opal_key have a key_type field?'
178+
)
156179

157180
if cc.has_function_attribute('fallthrough')
158181
conf.set('fallthrough', '__attribute__((__fallthrough__))')

plugins/meson.build

+3
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ if json_c_dep.found()
2929
]
3030
subdir('solidigm')
3131
subdir('ocp')
32+
if conf.has('HAVE_SED_OPAL')
33+
subdir('sed')
34+
endif
3235
endif

plugins/sed/meson.build

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sources += [
2+
'plugins/sed/sed.c',
3+
'plugins/sed/sedopal_cmd.c',
4+
]

plugins/sed/sed.c

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include <fcntl.h>
3+
#include <errno.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <unistd.h>
7+
#include <stdbool.h>
8+
#include <inttypes.h>
9+
#include <linux/fs.h>
10+
#include <sys/stat.h>
11+
12+
#include "common.h"
13+
#include "nvme.h"
14+
#include "libnvme.h"
15+
#include "nvme-print.h"
16+
#include "sedopal_cmd.h"
17+
#include <linux/sed-opal.h>
18+
19+
#define CREATE_CMD
20+
#include "sed.h"
21+
22+
OPT_ARGS(no_opts) = {
23+
OPT_END()
24+
};
25+
26+
OPT_ARGS(key_opts) = {
27+
OPT_FLAG("ask-key", 'k', &sedopal_ask_key,
28+
"prompt for SED authentication key"),
29+
OPT_END()
30+
};
31+
32+
OPT_ARGS(revert_opts) = {
33+
OPT_FLAG("destructive", 'e', &sedopal_destructive_revert,
34+
"destructive revert"),
35+
OPT_FLAG("psid", 'p', &sedopal_psid_revert, "PSID revert"),
36+
OPT_END()
37+
};
38+
39+
40+
/*
41+
* Open the NVMe device specified on the command line. It must be the
42+
* NVMe block device (e.g. /dev/nvme0n1).
43+
*/
44+
static int sed_opal_open_device(struct nvme_dev **dev, int argc, char **argv,
45+
const char *desc, struct argconfig_commandline_options *opts)
46+
{
47+
int err;
48+
49+
err = parse_and_open(dev, argc, argv, desc, opts);
50+
if (err)
51+
return err;
52+
53+
if (!S_ISBLK((*dev)->direct.stat.st_mode)) {
54+
fprintf(stderr,
55+
"ERROR : The NVMe block device must be specified\n");
56+
err = -EINVAL;
57+
dev_close(*dev);
58+
}
59+
60+
return err;
61+
}
62+
63+
static int sed_opal_discover(int argc, char **argv, struct command *cmd,
64+
struct plugin *plugin)
65+
{
66+
int err;
67+
const char *desc = "Query SED device and display locking features";
68+
struct nvme_dev *dev;
69+
70+
err = sed_opal_open_device(&dev, argc, argv, desc, no_opts);
71+
if (err)
72+
return err;
73+
74+
err = sedopal_cmd_discover(dev->direct.fd);
75+
76+
dev_close(dev);
77+
return err;
78+
}
79+
80+
static int sed_opal_initialize(int argc, char **argv, struct command *cmd,
81+
struct plugin *plugin)
82+
{
83+
int err;
84+
const char *desc = "Initialize a SED device for locking";
85+
struct nvme_dev *dev;
86+
87+
err = sed_opal_open_device(&dev, argc, argv, desc, no_opts);
88+
if (err)
89+
return err;
90+
91+
err = sedopal_cmd_initialize(dev->direct.fd);
92+
if (err != 0)
93+
fprintf(stderr, "initialize: SED error - %s\n",
94+
sedopal_error_to_text(err));
95+
96+
dev_close(dev);
97+
return err;
98+
}
99+
100+
static int sed_opal_revert(int argc, char **argv, struct command *cmd,
101+
struct plugin *plugin)
102+
{
103+
int err;
104+
const char *desc = "Revert a SED device from locking state";
105+
struct nvme_dev *dev;
106+
107+
err = sed_opal_open_device(&dev, argc, argv, desc, revert_opts);
108+
if (err)
109+
return err;
110+
111+
err = sedopal_cmd_revert(dev->direct.fd);
112+
if (err != 0)
113+
fprintf(stderr, "revert: SED error - %s\n",
114+
sedopal_error_to_text(err));
115+
116+
dev_close(dev);
117+
return err;
118+
}
119+
120+
static int sed_opal_lock(int argc, char **argv, struct command *cmd,
121+
struct plugin *plugin)
122+
{
123+
int err;
124+
const char *desc = "Lock a SED device";
125+
struct nvme_dev *dev;
126+
127+
err = sed_opal_open_device(&dev, argc, argv, desc, key_opts);
128+
if (err)
129+
return err;
130+
131+
err = sedopal_cmd_lock(dev->direct.fd);
132+
if (err != 0)
133+
fprintf(stderr, "lock: SED error - %s\n",
134+
sedopal_error_to_text(err));
135+
136+
dev_close(dev);
137+
return err;
138+
}
139+
140+
static int sed_opal_unlock(int argc, char **argv, struct command *cmd,
141+
struct plugin *plugin)
142+
{
143+
int err;
144+
const char *desc = "Unlock a SED device";
145+
struct nvme_dev *dev;
146+
147+
err = sed_opal_open_device(&dev, argc, argv, desc, key_opts);
148+
if (err)
149+
return err;
150+
151+
err = sedopal_cmd_unlock(dev->direct.fd);
152+
if (err != 0)
153+
fprintf(stderr, "unlock: SED error - %s\n",
154+
sedopal_error_to_text(err));
155+
156+
dev_close(dev);
157+
return err;
158+
}
159+
160+
static int sed_opal_password(int argc, char **argv, struct command *cmd,
161+
struct plugin *plugin)
162+
{
163+
int err;
164+
const char *desc = "Change the locking password of a SED device";
165+
struct nvme_dev *dev;
166+
167+
err = sed_opal_open_device(&dev, argc, argv, desc, no_opts);
168+
if (err)
169+
return err;
170+
171+
err = sedopal_cmd_password(dev->direct.fd);
172+
if (err != 0)
173+
fprintf(stderr, "password: SED error - %s\n",
174+
sedopal_error_to_text(err));
175+
176+
dev_close(dev);
177+
return err;
178+
}

plugins/sed/sed.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
#undef CMD_INC_FILE
3+
#define CMD_INC_FILE plugins/sed/sed
4+
5+
#include "cmd.h"
6+
#include <linux/sed-opal.h>
7+
8+
PLUGIN(NAME("sed", "SED Opal Command Set", NVME_VERSION),
9+
COMMAND_LIST(
10+
ENTRY("discover", "Discover SED Opal Locking Features", sed_opal_discover, "1")
11+
ENTRY("initialize", "Initialize a SED Opal Device for locking", sed_opal_initialize)
12+
ENTRY("revert", "Revert a SED Opal Device from locking", sed_opal_revert)
13+
ENTRY("lock", "Lock a SED Opal Device", sed_opal_lock)
14+
ENTRY("unlock", "Unlock a SED Opal Device", sed_opal_unlock)
15+
ENTRY("password", "Change the SED Opal Device password", sed_opal_password)
16+
)
17+
);
18+
19+
#include "define_cmd.h"

0 commit comments

Comments
 (0)