Opal: Add opal_create_key function

Change-Id: Id1705636e25fe3ad90ff60a57aca7b1e4c2ef687
Signed-off-by: Chunyang Hui <chunyang.hui@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/453972
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Paul Luse <paul.e.luse@intel.com>
This commit is contained in:
Chunyang Hui 2019-05-10 23:04:29 +08:00 committed by Changpeng Liu
parent 9f988238fc
commit dd26583316
2 changed files with 56 additions and 15 deletions

View File

@ -138,6 +138,20 @@ enum spdk_opal_user {
OPAL_USER9 = 0x09,
};
enum spdk_opal_locking_range {
OPAL_LOCKING_RANGE_GLOBAL = 0x0,
OPAL_LOCKING_RANGE_1,
OPAL_LOCKING_RANGE_2,
OPAL_LOCKING_RANGE_3,
OPAL_LOCKING_RANGE_4,
OPAL_LOCKING_RANGE_5,
OPAL_LOCKING_RANGE_6,
OPAL_LOCKING_RANGE_7,
OPAL_LOCKING_RANGE_8,
OPAL_LOCKING_RANGE_9,
OPAL_LOCKING_RANGE_10,
};
struct spdk_opal_dev;
struct spdk_opal_dev *spdk_opal_init_dev(void *dev_handler);

View File

@ -612,6 +612,39 @@ opal_set_comid(struct spdk_opal_dev *dev, uint16_t comid)
hdr->com_packet.extended_comid[1] = 0;
}
static int
opal_create_key(struct spdk_opal_key **opal_key, const char *passwd,
enum spdk_opal_locking_range locking_range)
{
struct spdk_opal_key *_opal_key;
int len;
if (passwd == NULL || passwd[0] == '\0') {
SPDK_ERRLOG("Password is empty. Create key failed\n");
return -EINVAL;
}
len = strlen(passwd);
if (len >= OPAL_KEY_MAX) {
SPDK_ERRLOG("Password too long. Create key failed\n");
return -EINVAL;
}
_opal_key = calloc(1, sizeof(struct spdk_opal_key));
if (!_opal_key) {
SPDK_ERRLOG("Memory allocation failed for spdk_opal_key\n");
return -ENOMEM;
}
_opal_key->key_len = len;
memcpy(_opal_key->key, passwd, _opal_key->key_len);
_opal_key->locking_range = locking_range;
*opal_key = _opal_key;
return 0;
}
static void
opal_check_tper(struct spdk_opal_dev *dev, const void *data)
{
@ -1092,16 +1125,13 @@ opal_set_sid_cpin_pin(struct spdk_opal_dev *dev, void *data)
{
uint8_t cpin_uid[OPAL_UID_LENGTH];
const char *new_passwd = data;
struct spdk_opal_key *opal_key;
struct spdk_opal_key *opal_key = NULL;
int ret;
opal_key = calloc(1, sizeof(struct spdk_opal_key));
if (!opal_key) {
SPDK_ERRLOG("Memory allocation failed for spdk_opal_key\n");
return -ENOMEM;
ret = opal_create_key(&opal_key, new_passwd, OPAL_LOCKING_RANGE_GLOBAL);
if (ret != 0) {
return ret;
}
opal_key->key_len = strlen(new_passwd);
memcpy(opal_key->key, new_passwd, opal_key->key_len);
dev->dev_key = opal_key;
memcpy(cpin_uid, spdk_opal_uid[UID_C_PIN_SID], OPAL_UID_LENGTH);
@ -1252,19 +1282,16 @@ int
spdk_opal_cmd_revert_tper(struct spdk_opal_dev *dev, const char *passwd)
{
int ret;
struct spdk_opal_key *opal_key;
struct spdk_opal_key *opal_key = NULL;
if (!dev || dev->supported == false) {
return -ENODEV;
}
opal_key = calloc(1, sizeof(struct spdk_opal_key));
if (!opal_key) {
SPDK_ERRLOG("Memory allocation failed for spdk_opal_key\n");
return -ENOMEM;
ret = opal_create_key(&opal_key, passwd, OPAL_LOCKING_RANGE_GLOBAL);
if (ret != 0) {
return ret;
}
opal_key->key_len = strlen(passwd);
memcpy(opal_key->key, passwd, opal_key->key_len);
dev->dev_key = opal_key;
pthread_mutex_lock(&dev->mutex_lock);