[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH 16/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_
From: |
Alex Bennée |
Subject: |
[RFC PATCH 16/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_READ |
Date: |
Fri, 25 Sep 2020 13:51:44 +0100 |
The read command is a lot simpler to implement. However the spec does
specify you can only read a single block at a time so we limit it to
that.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
tools/vhost-user-rpmb/main.c | 52 ++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git a/tools/vhost-user-rpmb/main.c b/tools/vhost-user-rpmb/main.c
index a17c3b4bcc4e..49d4e00b24a9 100644
--- a/tools/vhost-user-rpmb/main.c
+++ b/tools/vhost-user-rpmb/main.c
@@ -414,6 +414,55 @@ static int vrpmb_handle_write(VuDev *dev, struct
virtio_rpmb_frame *frame)
return extra_frames;
}
+/*
+ * vrpmb_handle_read:
+ *
+ * Unlike the write operation we return a frame with the result of the
+ * read here. While the config specifies a maximum read count the spec
+ * is limited to a single read at a time.
+ */
+static struct virtio_rpmb_frame *
+vrpmb_handle_read(VuDev *dev, struct virtio_rpmb_frame *frame)
+{
+ VuRpmb *r = container_of(dev, VuRpmb, dev.parent);
+ size_t offset = be16toh(frame->address) * RPMB_BLOCK_SIZE;
+ uint16_t block_count = be16toh(frame->block_count);
+ struct virtio_rpmb_frame *resp = g_new0(struct virtio_rpmb_frame, 1);
+
+ resp->req_resp = htobe16(VIRTIO_RPMB_RESP_DATA_READ);
+ resp->address = frame->address;
+ resp->block_count = htobe16(1);
+
+ /*
+ * Run the checks from:
+ * 5.12.6.1.4 Device Requirements: Device Operation: Data Read
+ */
+ if (!r->key) {
+ g_warning("no key programmed");
+ resp->result = htobe16(VIRTIO_RPMB_RES_NO_AUTH_KEY);
+ } else if (block_count != 1) {
+ /*
+ * Despite the config the spec only allows for reading one
+ * block at a time: "If block count has not been set to 1 then
+ * VIRTIO_RPMB_RES_GENERAL_FAILURE SHOULD be responded as
+ * result."
+ */
+ resp->result = htobe16(VIRTIO_RPMB_RES_GENERAL_FAILURE);
+ } else if (offset > (r->virtio_config.capacity * (128 * KiB))) {
+ resp->result = htobe16(VIRTIO_RPMB_RES_ADDR_FAILURE);
+ } else {
+ void *blk = r->flash_map + offset;
+ g_debug("%s: reading block from %p (%zu)", __func__, blk, offset);
+ memcpy(resp->data, blk, RPMB_BLOCK_SIZE);
+ resp->result = htobe16(VIRTIO_RPMB_RES_OK);
+ }
+
+ /* Final housekeeping, copy nonce and calculate MAC */
+ memcpy(&resp->nonce, &frame->nonce, sizeof(frame->nonce));
+ vrpmb_update_mac_in_frame(r, resp);
+
+ return resp;
+}
/*
* Return the result of the last message. This is only valid if the
@@ -544,6 +593,9 @@ vrpmb_handle_ctrl(VuDev *dev, int qidx)
/* we can have multiple blocks handled */
n += vrpmb_handle_write(dev, f);
break;
+ case VIRTIO_RPMB_REQ_DATA_READ:
+ resp = vrpmb_handle_read(dev, f);
+ break;
default:
g_debug("un-handled request: %x", f->req_resp);
break;
--
2.20.1
- [RFC PATCH 08/19] tools/vhost-user-rpmb: connect to fd and instantiate basic run loop, (continued)
- [RFC PATCH 08/19] tools/vhost-user-rpmb: connect to fd and instantiate basic run loop, Alex Bennée, 2020/09/25
- [RFC PATCH 09/19] tools/vhost-user-rpmb: add a --verbose/debug flags for logging, Alex Bennée, 2020/09/25
- [RFC PATCH 11/19] tools/vhost-user-rpmb: add --flash-path for backing store, Alex Bennée, 2020/09/25
- [RFC PATCH 12/19] tools/vhost-user-rpmb: import hmac_sha256 functions, Alex Bennée, 2020/09/25
- [RFC PATCH 10/19] tools/vhost-user-rpmb: handle shutdown and SIGINT/SIGHUP cleanly, Alex Bennée, 2020/09/25
- [RFC PATCH 17/19] tools/vhost-user-rpmb: add key persistence, Alex Bennée, 2020/09/25
- [RFC PATCH 15/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_WRITE, Alex Bennée, 2020/09/25
- [RFC PATCH 16/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_READ,
Alex Bennée <=
- [RFC PATCH 18/19] tools/vhost-user-rpmb: allow setting of the write_count, Alex Bennée, 2020/09/25
- [RFC PATCH 13/19] tools/vhost-user-rpmb: implement the PROGRAM_KEY handshake, Alex Bennée, 2020/09/25
- [RFC PATCH 19/19] docs: add a man page for vhost-user-rpmb, Alex Bennée, 2020/09/25
- [RFC PATCH 14/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_GET_WRITE_COUNTER, Alex Bennée, 2020/09/25
- Re: [RFC PATCH 00/19] vhost-user-rpmb (Replay Protected Memory Block), no-reply, 2020/09/25