blob: cf1ce77e6e19e68ea5fb2300fcfb48d4d32675e9 [file] [log] [blame]
Jens Wiklander232cfd62018-09-25 16:40:14 +02001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (c) 2018 Linaro Limited
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <log.h>
9#include <tee.h>
10#include <mmc.h>
11
12#include "optee_msg.h"
13#include "optee_private.h"
14
15/*
16 * Request and response definitions must be in sync with the secure side of
17 * OP-TEE.
18 */
19
20/* Request */
21struct rpmb_req {
22 u16 cmd;
23#define RPMB_CMD_DATA_REQ 0x00
24#define RPMB_CMD_GET_DEV_INFO 0x01
25 u16 dev_id;
26 u16 block_count;
27 /* Optional data frames (rpmb_data_frame) follow */
28};
29
30#define RPMB_REQ_DATA(req) ((void *)((struct rpmb_req *)(req) + 1))
31
32/* Response to device info request */
33struct rpmb_dev_info {
34 u8 cid[16];
35 u8 rpmb_size_mult; /* EXT CSD-slice 168: RPMB Size */
36 u8 rel_wr_sec_c; /* EXT CSD-slice 222: Reliable Write Sector */
37 /* Count */
38 u8 ret_code;
39#define RPMB_CMD_GET_DEV_INFO_RET_OK 0x00
40#define RPMB_CMD_GET_DEV_INFO_RET_ERROR 0x01
41};
42
43static void release_mmc(struct optee_private *priv)
44{
45 int rc;
46
47 if (!priv->rpmb_mmc)
48 return;
49
50 rc = blk_select_hwpart_devnum(IF_TYPE_MMC, priv->rpmb_dev_id,
51 priv->rpmb_original_part);
52 if (rc)
53 debug("%s: blk_select_hwpart_devnum() failed: %d\n",
54 __func__, rc);
55
56 priv->rpmb_mmc = NULL;
57}
58
59static struct mmc *get_mmc(struct optee_private *priv, int dev_id)
60{
61 struct mmc *mmc;
62 int rc;
63
64 if (priv->rpmb_mmc && priv->rpmb_dev_id == dev_id)
65 return priv->rpmb_mmc;
66
67 release_mmc(priv);
68
69 mmc = find_mmc_device(dev_id);
70 if (!mmc) {
71 debug("Cannot find RPMB device\n");
72 return NULL;
73 }
74 if (!(mmc->version & MMC_VERSION_MMC)) {
75 debug("Device id %d is not an eMMC device\n", dev_id);
76 return NULL;
77 }
78 if (mmc->version < MMC_VERSION_4_41) {
79 debug("Device id %d: RPMB not supported before version 4.41\n",
80 dev_id);
81 return NULL;
82 }
83
84 priv->rpmb_original_part = mmc_get_blk_desc(mmc)->hwpart;
85
86 rc = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_id, MMC_PART_RPMB);
87 if (rc) {
88 debug("Device id %d: cannot select RPMB partition: %d\n",
89 dev_id, rc);
90 return NULL;
91 }
92
93 priv->rpmb_mmc = mmc;
94 priv->rpmb_dev_id = dev_id;
95 return mmc;
96}
97
98static u32 rpmb_get_dev_info(u16 dev_id, struct rpmb_dev_info *info)
99{
100 struct mmc *mmc = find_mmc_device(dev_id);
Jorge Ramirez-Ortiz2464b222019-11-26 17:19:34 +0100101 int i;
Jens Wiklander232cfd62018-09-25 16:40:14 +0200102
103 if (!mmc)
104 return TEE_ERROR_ITEM_NOT_FOUND;
105
106 if (!mmc->ext_csd)
107 return TEE_ERROR_GENERIC;
108
Jorge Ramirez-Ortiz2464b222019-11-26 17:19:34 +0100109 for (i = 0; i < ARRAY_SIZE(mmc->cid); i++)
110 ((u32 *) info->cid)[i] = cpu_to_be32(mmc->cid[i]);
111
Jens Wiklander232cfd62018-09-25 16:40:14 +0200112 info->rel_wr_sec_c = mmc->ext_csd[222];
113 info->rpmb_size_mult = mmc->ext_csd[168];
114 info->ret_code = RPMB_CMD_GET_DEV_INFO_RET_OK;
115
116 return TEE_SUCCESS;
117}
118
119static u32 rpmb_process_request(struct optee_private *priv, void *req,
120 ulong req_size, void *rsp, ulong rsp_size)
121{
122 struct rpmb_req *sreq = req;
123 struct mmc *mmc;
124
125 if (req_size < sizeof(*sreq))
126 return TEE_ERROR_BAD_PARAMETERS;
127
128 switch (sreq->cmd) {
129 case RPMB_CMD_DATA_REQ:
130 mmc = get_mmc(priv, sreq->dev_id);
131 if (!mmc)
132 return TEE_ERROR_ITEM_NOT_FOUND;
133 if (mmc_rpmb_route_frames(mmc, RPMB_REQ_DATA(req),
134 req_size - sizeof(struct rpmb_req),
135 rsp, rsp_size))
136 return TEE_ERROR_BAD_PARAMETERS;
137 return TEE_SUCCESS;
138
139 case RPMB_CMD_GET_DEV_INFO:
140 if (req_size != sizeof(struct rpmb_req) ||
141 rsp_size != sizeof(struct rpmb_dev_info)) {
142 debug("Invalid req/rsp size\n");
143 return TEE_ERROR_BAD_PARAMETERS;
144 }
145 return rpmb_get_dev_info(sreq->dev_id, rsp);
146
147 default:
148 debug("Unsupported RPMB command: %d\n", sreq->cmd);
149 return TEE_ERROR_BAD_PARAMETERS;
150 }
151}
152
153void optee_suppl_cmd_rpmb(struct udevice *dev, struct optee_msg_arg *arg)
154{
155 struct tee_shm *req_shm;
156 struct tee_shm *rsp_shm;
157 void *req_buf;
158 void *rsp_buf;
159 ulong req_size;
160 ulong rsp_size;
161
162 if (arg->num_params != 2 ||
163 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ||
164 arg->params[1].attr != OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT) {
165 arg->ret = TEE_ERROR_BAD_PARAMETERS;
166 return;
167 }
168
169 req_shm = (struct tee_shm *)(ulong)arg->params[0].u.rmem.shm_ref;
170 req_buf = (u8 *)req_shm->addr + arg->params[0].u.rmem.offs;
171 req_size = arg->params[0].u.rmem.size;
172
173 rsp_shm = (struct tee_shm *)(ulong)arg->params[1].u.rmem.shm_ref;
174 rsp_buf = (u8 *)rsp_shm->addr + arg->params[1].u.rmem.offs;
175 rsp_size = arg->params[1].u.rmem.size;
176
177 arg->ret = rpmb_process_request(dev_get_priv(dev), req_buf, req_size,
178 rsp_buf, rsp_size);
179}
180
181void optee_suppl_rpmb_release(struct udevice *dev)
182{
183 release_mmc(dev_get_priv(dev));
184}