From: Jens Wiklander <jens.wiklander@linaro.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4 04/19] mmc: rpmb: add mmc_rpmb_route_frames()
Date: Tue, 25 Sep 2018 16:40:08 +0200 [thread overview]
Message-ID: <20180925144023.24555-5-jens.wiklander@linaro.org> (raw)
In-Reply-To: <20180925144023.24555-1-jens.wiklander@linaro.org>
Adds mmc_rpmb_route_frames() to route RPMB data frames from/to an
external entity.
Tested-by: Igor Opaniuk <igor.opaniuk@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
---
drivers/mmc/rpmb.c | 160 +++++++++++++++++++++++++++++++++++++++++++++
include/mmc.h | 19 ++++++
2 files changed, 179 insertions(+)
diff --git a/drivers/mmc/rpmb.c b/drivers/mmc/rpmb.c
index dfbdb0deb107..908f19208955 100644
--- a/drivers/mmc/rpmb.c
+++ b/drivers/mmc/rpmb.c
@@ -321,3 +321,163 @@ int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk,
}
return i;
}
+
+static int send_write_mult_block(struct mmc *mmc, const struct s_rpmb *frm,
+ unsigned short cnt)
+{
+ struct mmc_cmd cmd = {
+ .cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK,
+ .resp_type = MMC_RSP_R1b,
+ };
+ struct mmc_data data = {
+ .src = (const void *)frm,
+ .blocks = cnt,
+ .blocksize = sizeof(*frm),
+ .flags = MMC_DATA_WRITE,
+ };
+
+ return mmc_send_cmd(mmc, &cmd, &data);
+}
+
+static int send_read_mult_block(struct mmc *mmc, struct s_rpmb *frm,
+ unsigned short cnt)
+{
+ struct mmc_cmd cmd = {
+ .cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK,
+ .resp_type = MMC_RSP_R1,
+ };
+ struct mmc_data data = {
+ .dest = (void *)frm,
+ .blocks = cnt,
+ .blocksize = sizeof(*frm),
+ .flags = MMC_DATA_READ,
+ };
+
+ return mmc_send_cmd(mmc, &cmd, &data);
+}
+
+static int rpmb_route_write_req(struct mmc *mmc, struct s_rpmb *req,
+ unsigned short req_cnt, struct s_rpmb *rsp,
+ unsigned short rsp_cnt)
+{
+ int ret;
+
+ /*
+ * Send the write request.
+ */
+ ret = mmc_set_blockcount(mmc, req_cnt, true);
+ if (ret)
+ return ret;
+
+ ret = send_write_mult_block(mmc, req, req_cnt);
+ if (ret)
+ return ret;
+
+ /*
+ * Read the result of the request.
+ */
+ ret = mmc_set_blockcount(mmc, 1, false);
+ if (ret)
+ return ret;
+
+ memset(rsp, 0, sizeof(*rsp));
+ rsp->request = cpu_to_be16(RPMB_REQ_STATUS);
+ ret = send_write_mult_block(mmc, rsp, 1);
+ if (ret)
+ return ret;
+
+ ret = mmc_set_blockcount(mmc, 1, false);
+ if (ret)
+ return ret;
+
+ return send_read_mult_block(mmc, rsp, 1);
+}
+
+static int rpmb_route_read_req(struct mmc *mmc, struct s_rpmb *req,
+ unsigned short req_cnt, struct s_rpmb *rsp,
+ unsigned short rsp_cnt)
+{
+ int ret;
+
+ /*
+ * Send the read request.
+ */
+ ret = mmc_set_blockcount(mmc, 1, false);
+ if (ret)
+ return ret;
+
+ ret = send_write_mult_block(mmc, req, 1);
+ if (ret)
+ return ret;
+
+ /*
+ * Read the result of the request.
+ */
+
+ ret = mmc_set_blockcount(mmc, rsp_cnt, false);
+ if (ret)
+ return ret;
+
+ return send_read_mult_block(mmc, rsp, rsp_cnt);
+}
+
+static int rpmb_route_frames(struct mmc *mmc, struct s_rpmb *req,
+ unsigned short req_cnt, struct s_rpmb *rsp,
+ unsigned short rsp_cnt)
+{
+ unsigned short n;
+
+ /*
+ * If multiple request frames are provided, make sure that all are
+ * of the same type.
+ */
+ for (n = 1; n < req_cnt; n++)
+ if (req[n].request != req->request)
+ return -EINVAL;
+
+ switch (be16_to_cpu(req->request)) {
+ case RPMB_REQ_KEY:
+ if (req_cnt != 1 || rsp_cnt != 1)
+ return -EINVAL;
+ return rpmb_route_write_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+ case RPMB_REQ_WRITE_DATA:
+ if (!req_cnt || rsp_cnt != 1)
+ return -EINVAL;
+ return rpmb_route_write_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+ case RPMB_REQ_WCOUNTER:
+ if (req_cnt != 1 || rsp_cnt != 1)
+ return -EINVAL;
+ return rpmb_route_read_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+ case RPMB_REQ_READ_DATA:
+ if (req_cnt != 1 || !req_cnt)
+ return -EINVAL;
+ return rpmb_route_read_req(mmc, req, req_cnt, rsp, rsp_cnt);
+
+ default:
+ debug("Unsupported message type: %d\n",
+ be16_to_cpu(req->request));
+ return -EINVAL;
+ }
+}
+
+int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen,
+ void *rsp, unsigned long rsplen)
+{
+ /*
+ * Whoever crafted the data supplied to this function knows how to
+ * format the PRMB frames and which response is expected. If
+ * there's some unexpected mismatch it's more helpful to report an
+ * error immediately than trying to guess what was the intention
+ * and possibly just delay an eventual error which will be harder
+ * to track down.
+ */
+
+ if (reqlen % sizeof(struct s_rpmb) || rsplen % sizeof(struct s_rpmb))
+ return -EINVAL;
+
+ return rpmb_route_frames(mmc, req, reqlen / sizeof(struct s_rpmb),
+ rsp, rsplen / sizeof(struct s_rpmb));
+}
diff --git a/include/mmc.h b/include/mmc.h
index df4255b828a7..aa2499020cab 100644
--- a/include/mmc.h
+++ b/include/mmc.h
@@ -748,6 +748,25 @@ int mmc_rpmb_read(struct mmc *mmc, void *addr, unsigned short blk,
unsigned short cnt, unsigned char *key);
int mmc_rpmb_write(struct mmc *mmc, void *addr, unsigned short blk,
unsigned short cnt, unsigned char *key);
+
+/**
+ * mmc_rpmb_route_frames() - route RPMB data frames
+ * @mmc Pointer to a MMC device struct
+ * @req Request data frames
+ * @reqlen Length of data frames in bytes
+ * @rsp Supplied buffer for response data frames
+ * @rsplen Length of supplied buffer for response data frames
+ *
+ * The RPMB data frames are routed to/from some external entity, for
+ * example a Trusted Exectuion Environment in an arm TrustZone protected
+ * secure world. It's expected that it's the external entity who is in
+ * control of the RPMB key.
+ *
+ * Returns 0 on success, < 0 on error.
+ */
+int mmc_rpmb_route_frames(struct mmc *mmc, void *req, unsigned long reqlen,
+ void *rsp, unsigned long rsplen);
+
#ifdef CONFIG_CMD_BKOPS_ENABLE
int mmc_set_bkops_enable(struct mmc *mmc);
#endif
--
2.17.1
next prev parent reply other threads:[~2018-09-25 14:40 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-25 14:40 [U-Boot] [PATCH v4 00/19] AVB using OP-TEE Jens Wiklander
2018-09-25 14:40 ` [U-Boot] [PATCH v4 01/19] dm: fdt: scan for devices under /firmware too Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 02/19] cmd: avb read_rb: print rb_idx in hexadecimal Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 03/19] cmd: avb: print error message if command fails Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` Jens Wiklander [this message]
2018-10-02 11:21 ` [U-Boot] [PATCH v4 04/19] mmc: rpmb: add mmc_rpmb_route_frames() Simon Glass
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 05/19] Add UCLASS_TEE for Trusted Execution Environment Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 06/19] dt/bindings: add bindings for optee Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot,v4,06/19] " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 07/19] tee: add OP-TEE driver Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot,v4,07/19] " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 08/19] Documentation: tee uclass and op-tee driver Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 09/19] arm: dt: hikey: Add optee node Jens Wiklander
2018-10-08 1:40 ` [U-Boot] [U-Boot,v4,09/19] " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 10/19] optee: support routing of rpmb data frames to mmc Jens Wiklander
2018-10-02 11:21 ` Simon Glass
2018-10-08 1:40 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 11/19] tee: optee: support AVB trusted application Jens Wiklander
2018-10-02 11:21 ` Simon Glass
2018-10-08 1:41 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 12/19] sandbox: dt: add sandbox_tee node Jens Wiklander
2018-10-08 1:41 ` [U-Boot] [U-Boot,v4,12/19] " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 13/19] sandbox: imply CONFIG_TEE (TEE uclass) Jens Wiklander
2018-10-08 1:41 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 14/19] tee: add sandbox driver Jens Wiklander
2018-10-02 11:21 ` Simon Glass
2018-10-08 1:41 ` [U-Boot] [U-Boot,v4,14/19] " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 15/19] test: tee: test TEE uclass Jens Wiklander
2018-10-08 1:41 ` [U-Boot] [U-Boot,v4,15/19] " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 16/19] avb_verify: support using OP-TEE TA AVB Jens Wiklander
2018-10-08 1:41 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 17/19] test_avb: Update pymark.buildconfigspec information for the AVB tests Jens Wiklander
2018-10-08 1:41 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 18/19] avb_verify: support sandbox configuration Jens Wiklander
2018-10-02 11:21 ` Simon Glass
2018-10-08 1:41 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-25 14:40 ` [U-Boot] [PATCH v4 19/19] Kconfig: sandbox: enable cmd_avb and dependencies Jens Wiklander
2018-10-08 1:41 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-10-02 11:21 ` [U-Boot] [PATCH v4 00/19] AVB using OP-TEE Simon Glass
2018-10-08 7:18 ` Jens Wiklander
2018-10-09 3:40 ` Simon Glass
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=20180925144023.24555-5-jens.wiklander@linaro.org \
--to=jens.wiklander@linaro.org \
--cc=u-boot@lists.denx.de \
/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.