Skip to content

Commit a9b25b4

Browse files
nonapodeDavid Kozub
authored andcommitted
block: sed-opal: ioctl for writing to shadow mbr
Allow modification of the shadow mbr. If the shadow mbr is not marked as done, this data will be presented read only as the device content. Only after marking the shadow mbr as done and unlocking a locking range the actual content is accessible. Co-authored-by: David Kozub <zub@linux.fjfi.cvut.cz> Signed-off-by: Jonas Rabenstein <jonas.rabenstein@studium.uni-erlangen.de> Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz> Reviewed-by: Scott Bauer <sbauer@plzdonthack.me> Reviewed-by: Jon Derrick <jonathan.derrick@intel.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent c988844 commit a9b25b4

3 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎block/sed-opal.c‎

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#define IO_BUFFER_LENGTH 2048
2727
#define MAX_TOKS 64
2828

29+
/* Number of bytes needed by cmd_finalize. */
30+
#define CMD_FINALIZE_BYTES_NEEDED 7
31+
2932
struct opal_step {
3033
int (*fn)(struct opal_dev *dev, void *data);
3134
void *data;
@@ -523,12 +526,17 @@ static int opal_discovery0_step(struct opal_dev *dev)
523526
return execute_step(dev, &discovery0_step, 0);
524527
}
525528

529+
static size_t remaining_size(struct opal_dev *cmd)
530+
{
531+
return IO_BUFFER_LENGTH - cmd->pos;
532+
}
533+
526534
static bool can_add(int *err, struct opal_dev *cmd, size_t len)
527535
{
528536
if (*err)
529537
return false;
530538

531-
if (len > IO_BUFFER_LENGTH || cmd->pos > IO_BUFFER_LENGTH - len) {
539+
if (remaining_size(cmd) < len) {
532540
pr_debug("Error adding %zu bytes: end of buffer.\n", len);
533541
*err = -ERANGE;
534542
return false;
@@ -674,7 +682,11 @@ static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
674682
struct opal_header *hdr;
675683
int err = 0;
676684

677-
/* close the parameter list opened from cmd_start */
685+
/*
686+
* Close the parameter list opened from cmd_start.
687+
* The number of bytes added must be equal to
688+
* CMD_FINALIZE_BYTES_NEEDED.
689+
*/
678690
add_token_u8(&err, cmd, OPAL_ENDLIST);
679691

680692
add_token_u8(&err, cmd, OPAL_ENDOFDATA);
@@ -1536,6 +1548,58 @@ static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
15361548
return finalize_and_send(dev, parse_and_check_status);
15371549
}
15381550

1551+
static int write_shadow_mbr(struct opal_dev *dev, void *data)
1552+
{
1553+
struct opal_shadow_mbr *shadow = data;
1554+
const u8 __user *src;
1555+
u8 *dst;
1556+
size_t off = 0;
1557+
u64 len;
1558+
int err = 0;
1559+
1560+
/* do the actual transmission(s) */
1561+
src = (u8 __user *)(uintptr_t)shadow->data;
1562+
while (off < shadow->size) {
1563+
err = cmd_start(dev, opaluid[OPAL_MBR], opalmethod[OPAL_SET]);
1564+
add_token_u8(&err, dev, OPAL_STARTNAME);
1565+
add_token_u8(&err, dev, OPAL_WHERE);
1566+
add_token_u64(&err, dev, shadow->offset + off);
1567+
add_token_u8(&err, dev, OPAL_ENDNAME);
1568+
1569+
add_token_u8(&err, dev, OPAL_STARTNAME);
1570+
add_token_u8(&err, dev, OPAL_VALUES);
1571+
1572+
/*
1573+
* The bytestring header is either 1 or 2 bytes, so assume 2.
1574+
* There also needs to be enough space to accommodate the
1575+
* trailing OPAL_ENDNAME (1 byte) and tokens added by
1576+
* cmd_finalize.
1577+
*/
1578+
len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
1579+
(size_t)(shadow->size - off));
1580+
pr_debug("MBR: write bytes %zu+%llu/%llu\n",
1581+
off, len, shadow->size);
1582+
1583+
dst = add_bytestring_header(&err, dev, len);
1584+
if (!dst)
1585+
break;
1586+
if (copy_from_user(dst, src + off, len))
1587+
err = -EFAULT;
1588+
dev->pos += len;
1589+
1590+
add_token_u8(&err, dev, OPAL_ENDNAME);
1591+
if (err)
1592+
break;
1593+
1594+
err = finalize_and_send(dev, parse_and_check_status);
1595+
if (err)
1596+
break;
1597+
1598+
off += len;
1599+
}
1600+
return err;
1601+
}
1602+
15391603
static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
15401604
struct opal_dev *dev)
15411605
{
@@ -2013,6 +2077,26 @@ static int opal_set_mbr_done(struct opal_dev *dev,
20132077
return ret;
20142078
}
20152079

2080+
static int opal_write_shadow_mbr(struct opal_dev *dev,
2081+
struct opal_shadow_mbr *info)
2082+
{
2083+
const struct opal_step mbr_steps[] = {
2084+
{ start_admin1LSP_opal_session, &info->key },
2085+
{ write_shadow_mbr, info },
2086+
{ end_opal_session, }
2087+
};
2088+
int ret;
2089+
2090+
if (info->size == 0)
2091+
return 0;
2092+
2093+
mutex_lock(&dev->dev_lock);
2094+
setup_opal_dev(dev);
2095+
ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2096+
mutex_unlock(&dev->dev_lock);
2097+
return ret;
2098+
}
2099+
20162100
static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
20172101
{
20182102
struct opal_suspend_data *suspend;
@@ -2337,6 +2421,9 @@ int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
23372421
case IOC_OPAL_MBR_DONE:
23382422
ret = opal_set_mbr_done(dev, p);
23392423
break;
2424+
case IOC_OPAL_WRITE_SHADOW_MBR:
2425+
ret = opal_write_shadow_mbr(dev, p);
2426+
break;
23402427
case IOC_OPAL_ERASE_LR:
23412428
ret = opal_erase_locking_range(dev, p);
23422429
break;

‎include/linux/sed-opal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static inline bool is_sed_ioctl(unsigned int cmd)
4141
case IOC_OPAL_SECURE_ERASE_LR:
4242
case IOC_OPAL_PSID_REVERT_TPR:
4343
case IOC_OPAL_MBR_DONE:
44+
case IOC_OPAL_WRITE_SHADOW_MBR:
4445
return true;
4546
}
4647
return false;

‎include/uapi/linux/sed-opal.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ struct opal_mbr_done {
106106
__u8 __align[7];
107107
};
108108

109+
struct opal_shadow_mbr {
110+
struct opal_key key;
111+
const __u64 data;
112+
__u64 offset;
113+
__u64 size;
114+
};
115+
109116
#define IOC_OPAL_SAVE _IOW('p', 220, struct opal_lock_unlock)
110117
#define IOC_OPAL_LOCK_UNLOCK _IOW('p', 221, struct opal_lock_unlock)
111118
#define IOC_OPAL_TAKE_OWNERSHIP _IOW('p', 222, struct opal_key)
@@ -120,5 +127,6 @@ struct opal_mbr_done {
120127
#define IOC_OPAL_SECURE_ERASE_LR _IOW('p', 231, struct opal_session_info)
121128
#define IOC_OPAL_PSID_REVERT_TPR _IOW('p', 232, struct opal_key)
122129
#define IOC_OPAL_MBR_DONE _IOW('p', 233, struct opal_mbr_done)
130+
#define IOC_OPAL_WRITE_SHADOW_MBR _IOW('p', 234, struct opal_shadow_mbr)
123131

124132
#endif /* _UAPI_SED_OPAL_H */

0 commit comments

Comments
 (0)