All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Kozub <zub@linux.fjfi.cvut.cz>
To: Jens Axboe <axboe@kernel.dk>,
	Jonathan Derrick <jonathan.derrick@intel.com>,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Jonas Rabenstein <jonas.rabenstein@studium.uni-erlangen.de>
Subject: [PATCH v2 13/16] block: sed-opal: check size of shadow mbr
Date: Thu, 17 Jan 2019 22:31:53 +0100	[thread overview]
Message-ID: <1547760716-7304-14-git-send-email-zub@linux.fjfi.cvut.cz> (raw)
In-Reply-To: <1547760716-7304-1-git-send-email-zub@linux.fjfi.cvut.cz>

From: Jonas Rabenstein <jonas.rabenstein@studium.uni-erlangen.de>

Check whether the shadow mbr does fit in the provided space on the
target. Also a proper firmware should handle this case and return an
error we may prevent problems or even damage with crappy firmwares.

Signed-off-by: Jonas Rabenstein <jonas.rabenstein@studium.uni-erlangen.de>
---
 block/opal_proto.h | 16 ++++++++++++++++
 block/sed-opal.c   | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/block/opal_proto.h b/block/opal_proto.h
index b6e352cfe982..5e8df3245eb0 100644
--- a/block/opal_proto.h
+++ b/block/opal_proto.h
@@ -106,6 +106,7 @@ enum opal_uid {
 	OPAL_ENTERPRISE_BANDMASTER0_UID,
 	OPAL_ENTERPRISE_ERASEMASTER_UID,
 	/* tables */
+	OPAL_TABLE_TABLE,
 	OPAL_LOCKINGRANGE_GLOBAL,
 	OPAL_LOCKINGRANGE_ACE_RDLOCKED,
 	OPAL_LOCKINGRANGE_ACE_WRLOCKED,
@@ -160,6 +161,21 @@ enum opal_token {
 	OPAL_STARTCOLUMN = 0x03,
 	OPAL_ENDCOLUMN = 0x04,
 	OPAL_VALUES = 0x01,
+	/* table table */
+	OPAL_TABLE_UID = 0x00,
+	OPAL_TABLE_NAME = 0x01,
+	OPAL_TABLE_COMMON = 0x02,
+	OPAL_TABLE_TEMPLATE = 0x03,
+	OPAL_TABLE_KIND = 0x04,
+	OPAL_TABLE_COLUMN = 0x05,
+	OPAL_TABLE_COLUMNS = 0x06,
+	OPAL_TABLE_ROWS = 0x07,
+	OPAL_TABLE_ROWS_FREE = 0x08,
+	OPAL_TABLE_ROW_BYTES = 0x09,
+	OPAL_TABLE_LASTID = 0x0A,
+	OPAL_TABLE_MIN = 0x0B,
+	OPAL_TABLE_MAX = 0x0C,
+
 	/* authority table */
 	OPAL_PIN = 0x03,
 	/* locking tokens */
diff --git a/block/sed-opal.c b/block/sed-opal.c
index 034cb940b34a..ad52091531f2 100644
--- a/block/sed-opal.c
+++ b/block/sed-opal.c
@@ -139,6 +139,8 @@ static const u8 opaluid[][OPAL_UID_LENGTH] = {
 
 	/* tables */
 
+	[OPAL_TABLE_TABLE]
+		{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01 },
 	[OPAL_LOCKINGRANGE_GLOBAL] =
 		{ 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
 	[OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
@@ -1120,6 +1122,29 @@ static int generic_get_column(struct opal_dev *dev, const u8 *table,
 	return finalize_and_send(dev, parse_and_check_status);
 }
 
+/*
+ * see TCG SAS 5.3.2.3 for a description of the available columns
+ *
+ * the result is provided in dev->resp->tok[4]
+ */
+static int generic_get_table_info(struct opal_dev *dev, enum opal_uid table,
+				  u64 column)
+{
+	u8 uid[OPAL_UID_LENGTH];
+	const unsigned int half = OPAL_UID_LENGTH/2;
+
+	/* sed-opal UIDs can be split in two halves:
+	 *  first:  actual table index
+	 *  second: relative index in the table
+	 * so we have to get the first half of the OPAL_TABLE_TABLE and use the
+	 * first part of the target table as relative index into that table
+	 */
+	memcpy(uid, opaluid[OPAL_TABLE_TABLE], half);
+	memcpy(uid+half, opaluid[table], half);
+
+	return generic_get_column(dev, uid, column);
+}
+
 static int gen_key(struct opal_dev *dev, void *data)
 {
 	u8 uid[OPAL_UID_LENGTH];
@@ -1535,6 +1560,20 @@ static int write_shadow_mbr(struct opal_dev *dev, void *data)
 	u64 len;
 	int err = 0;
 
+	/* do we fit in the available shadow mbr space? */
+	err = generic_get_table_info(dev, OPAL_MBR, OPAL_TABLE_ROWS);
+	if (err) {
+		pr_debug("MBR: could not get shadow size\n");
+		return err;
+	}
+
+	len = response_get_u64(&dev->parsed, 4);
+	if (shadow->offset + shadow->size > len) {
+		pr_debug("MBR: does not fit in shadow (%llu vs. %llu)\n",
+			 shadow->offset + shadow->size, len);
+		return -ENOSPC;
+	}
+
 	/* do the actual transmission(s) */
 	src = (u8 *) shadow->data;
 	while (off < shadow->size) {
-- 
2.20.1


  parent reply	other threads:[~2019-01-17 21:34 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-17 21:31 [PATCH v2 00/16] block: sed-opal: support shadow MBR done flag and write David Kozub
2019-01-17 21:31 ` [PATCH v2 01/16] block: sed-opal: fix typos and formatting David Kozub
2019-01-17 21:31 ` [PATCH v2 02/16] block: sed-opal: use correct macro for method length David Kozub
2019-01-17 21:31 ` [PATCH v2 03/16] block: sed-opal: unify space check in add_token_* David Kozub
2019-01-17 21:31 ` [PATCH v2 04/16] block: sed-opal: close parameter list in cmd_finalize David Kozub
2019-01-17 21:31 ` [PATCH v2 05/16] block: sed-opal: unify cmd start David Kozub
2019-01-17 21:31 ` [PATCH v2 06/16] block: sed-opal: unify error handling of responses David Kozub
2019-01-17 21:31 ` [PATCH v2 07/16] block: sed-opal: reuse response_get_token to decrease code duplication David Kozub
2019-01-17 21:31 ` [PATCH v2 08/16] block: sed-opal: print failed function address David Kozub
2019-01-17 21:31 ` [PATCH v2 09/16] block: sed-opal: split generation of bytestring header and content David Kozub
2019-01-17 21:31 ` [PATCH v2 10/16] block: sed-opal: add ioctl for done-mark of shadow mbr David Kozub
2019-01-17 21:31 ` [PATCH v2 11/16] block: sed-opal: ioctl for writing to " David Kozub
2019-01-19 17:15   ` Scott Bauer
2019-01-20 10:27     ` David Kozub
2019-01-20 21:20       ` Scott Bauer
2019-01-17 21:31 ` [PATCH v2 12/16] block: sed-opal: unify retrieval of table columns David Kozub
2019-01-17 21:31 ` David Kozub [this message]
2019-01-19 17:29   ` [PATCH v2 13/16] block: sed-opal: check size of shadow mbr Scott Bauer
2019-01-17 21:31 ` [PATCH v2 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
2019-01-17 21:31 ` [PATCH v2 15/16] block: sed-opal: don't repeat opal_discovery0 in each steps array David Kozub
2019-01-19 17:46   ` Scott Bauer
2019-01-20 20:23     ` David Kozub
2019-01-17 21:31 ` [PATCH v2 16/16] block: sed-opal: rename next to execute_steps David Kozub
2019-01-19 17:53 ` [PATCH v2 00/16] block: sed-opal: support shadow MBR done flag and write Scott Bauer
2019-01-20 20:28   ` David Kozub

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1547760716-7304-14-git-send-email-zub@linux.fjfi.cvut.cz \
    --to=zub@linux.fjfi.cvut.cz \
    --cc=axboe@kernel.dk \
    --cc=jonas.rabenstein@studium.uni-erlangen.de \
    --cc=jonathan.derrick@intel.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.