From patchwork Tue Sep 25 14:40:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 147469 Delivered-To: patches@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp812453lji; Tue, 25 Sep 2018 07:40:37 -0700 (PDT) X-Received: by 2002:a19:1a93:: with SMTP id a141-v6mr1132416lfa.23.1537886437232; Tue, 25 Sep 2018 07:40:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537886437; cv=none; d=google.com; s=arc-20160816; b=ljbUyLMskh72V12+NQQMq3LOffSIscC8u72ERyrhxEHjNj61eHBGm8NdE/8t52AX8l JXEZgHw2JmuAs3ekWZj3IiGTE9fJ9g04NOATCuaObudeHld/GOv4KRd4B+dqyDdTqRbK CdwnY4sfm5lLSDa9cdBY8H/JMO8GKR33pDrnVhQebizD/j8qhdUFEvgpskOjtya9sCO0 sMDOiDTAKozdoB32akrOLASrYvqvTihBm4MeC6r8w3fmYO2jNiCX5VQzrTCe/pcc2fI4 wg08EiUZkzpkdmNAZxcEL+i4rfLRIivw2RJstvVdWbVTDPbGIWLbX0jl03CPcmBXRlDR sRgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8DMzPFTbewVd9IpffioMoRC7q3xjDjX2vzoyXEPw6bU=; b=VsB1qOmXOriZZPjYgRay3tE7gXdIiqqB7p1bVf2yktuCq3/vJL2gayv7BZjZU/LxOy ZLojKhGdgBZ08L2g8Z+4zUH1wRHqYHo37malMUaRB5C+fMxG7QFfaMf9Id35nl1roIP1 Xq2TjVQZeCx5Niyn5E98zZ5bhbeD0lRrCKJ+VnXIkj3gwgfIDy3DGtnWSxpAYdse6HaV KVFnvztVV6uKdJ3pqBgvXfQTgOiV1vmhQGUvfDzkoEi0MVXbfOOOHbn0Bn0+6Cwsbjy3 MjpO45ClgJFIX+0AivTocHX/ktmFzA5N6jCOhn0NMbTy1OgZyCMK/Nax6awAB4zWAoQd 7s9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CkADKFPd; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id l25-v6sor1149028lfc.38.2018.09.25.07.40.37 for (Google Transport Security); Tue, 25 Sep 2018 07:40:37 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CkADKFPd; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8DMzPFTbewVd9IpffioMoRC7q3xjDjX2vzoyXEPw6bU=; b=CkADKFPdfcNn6/MXbbWhwDWf1EUwKI2aqRZLoHU4GLuxBXTHd9OV8VhR3CH637+a+0 4xQLCOxf7V1xWfZ7hXidtHFEM0/DvtrIpVtjNwN3CntFR8KK0nxYSlWFu1PwaYenHLlX DJXOnmuDXT02LNe0sZ5VTvMcvZxRarfLtNX24= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8DMzPFTbewVd9IpffioMoRC7q3xjDjX2vzoyXEPw6bU=; b=HjHOSvcrEIRgWgp+SuD/COWQzmG1YNJoHEcbsticJxU/lIx1o2LpKRmd2f/aknZhlN ZyRbZORHRx7XWITFtTKbDt/ofry99f7O8nN7W4xLBECxlUaVoGvQIN5lZfw7RBKvdwC2 ZoBnFLn2voMXRkGlKQ88h9n3EACUg0dfRjKY2gosilbYkvgWLzT38NBa4zfUefNJw3cQ 3AddNbnMMJQayhEaDJstwd9K3SfTAY0HitMTvUeom3jh+DPuEMRB+ZxqUh9HXxLuMAB+ MlRwLElVDV/sacYUl6ZH2nd59JwGBOeyDneYQ4gUwo98gqiTqniNI261ME9yme3FJSRL hdkA== X-Gm-Message-State: ABuFfogjWRR6lGVOUefDOSX4lTpAPD9N6AJHOntFE7NFsoeAYS00Qc7D EiB25g7IsN7eahvsC6qxsmFW5UXT X-Google-Smtp-Source: ACcGV61k1MhCDTYqv4E+xIn66Mf0zRyMXU31RC+iuVUQDQr0j155RPOc3HbvoUFguPis82THHE8cOQ== X-Received: by 2002:a19:c7c2:: with SMTP id x185-v6mr1095112lff.123.1537886436835; Tue, 25 Sep 2018 07:40:36 -0700 (PDT) Return-Path: Received: from jax.urgonet (h-84-105.A175.priv.bahnhof.se. [79.136.84.105]) by smtp.gmail.com with ESMTPSA id g14-v6sm14483lja.96.2018.09.25.07.40.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 07:40:36 -0700 (PDT) From: Jens Wiklander To: u-boot@lists.denx.de Cc: Simon Glass , Igor Opaniuk , Tom Rini , Jaehoon Chung , Pierre Aubert , Albert Aribaud , Peter Griffin , Michal Simek , Jens Wiklander Subject: [PATCH v4 04/19] mmc: rpmb: add mmc_rpmb_route_frames() Date: Tue, 25 Sep 2018 16:40:08 +0200 Message-Id: <20180925144023.24555-5-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180925144023.24555-1-jens.wiklander@linaro.org> References: <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 Signed-off-by: Jens Wiklander --- drivers/mmc/rpmb.c | 160 +++++++++++++++++++++++++++++++++++++++++++++ include/mmc.h | 19 ++++++ 2 files changed, 179 insertions(+) -- 2.17.1 Reviewed-by: Simon Glass 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