Commit dfd911856d31fd91eb4e3c1edb1d691723c6edaf

Authored by Roberto Pereira
Committed by faqiang.zhu
1 parent f7d43073b3

ql-tipc: trusty_ipc: Change ipc polling to be per device

This allows ipc devices to provide service callbacks (e.g. rpmb) transparently
to the application instead of needing to have prior knowledge of the expected
request and having to poll the individual services' channels separately.

Change-Id: I3257ae5e429f4a0c279f070d750b56c5600c38d5

Showing 6 changed files with 55 additions and 80 deletions Side-by-side Diff

include/trusty/rpmb.h
... ... @@ -38,10 +38,6 @@
38 38 */
39 39 int rpmb_storage_proxy_init(struct trusty_ipc_dev *dev, void *rpmb_dev);
40 40 /*
41   - * Poll for and handle RPMB storange events. Returns one of trusty_err.
42   - */
43   -int rpmb_storage_proxy_poll(void);
44   -/*
45 41 * Shutdown RPMB storage proxy
46 42 *
47 43 * @dev: initialized with trusty_ipc_dev_create
include/trusty/trusty_ipc.h
... ... @@ -227,10 +227,10 @@
227 227 */
228 228 int trusty_ipc_close(struct trusty_ipc_chan *chan);
229 229 /*
230   - * Calls trusty_ipc_dev_get_event to poll for an event on @chan. Handles
231   - * event by calling appropriate callback. Returns nonnegative on success.
  230 + * Calls trusty_ipc_dev_get_event to poll @dev for events. Handles
  231 + * events by calling appropriate callbacks. Returns nonnegative on success.
232 232 */
233   -int trusty_ipc_poll_for_event(struct trusty_ipc_chan *chan);
  233 +int trusty_ipc_poll_for_event(struct trusty_ipc_dev *dev);
234 234 /*
235 235 * Calls trusty_ipc_dev_send to send a message. Returns a trusty_err.
236 236 *
lib/trusty/ql-tipc/avb.c
... ... @@ -75,10 +75,9 @@
75 75 * @resp: the response buffer
76 76 * @resp_size_p: pointer to the size of the response buffer. changed to the
77 77 actual size of the response read from the secure side
78   - * @handle_rpmb: true if the request is expected to invoke RPMB callbacks
79 78 */
80 79 static int avb_do_tipc(uint32_t cmd, void *req, uint32_t req_size, void *resp,
81   - uint32_t *resp_size_p, bool handle_rpmb)
  80 + uint32_t *resp_size_p)
82 81 {
83 82 int rc;
84 83 struct avb_message msg = { .cmd = cmd };
... ... @@ -94,16 +93,6 @@
94 93 return rc;
95 94 }
96 95  
97   - if (handle_rpmb) {
98   - /* handle any incoming RPMB requests */
99   - rc = rpmb_storage_proxy_poll();
100   - if (rc < 0) {
101   - trusty_error("%s: failed (%d) to get RPMB requests\n", __func__,
102   - rc);
103   - return rc;
104   - }
105   - }
106   -
107 96 uint32_t resp_size = resp_size_p ? *resp_size_p : 0;
108 97 rc = avb_read_response(&msg, cmd, resp, resp_size);
109 98 if (rc < 0) {
... ... @@ -128,7 +117,7 @@
128 117 struct avb_get_version_resp resp;
129 118 uint32_t resp_size = sizeof(resp);
130 119  
131   - rc = avb_do_tipc(AVB_GET_VERSION, NULL, 0, &resp, &resp_size, false);
  120 + rc = avb_do_tipc(AVB_GET_VERSION, NULL, 0, &resp, &resp_size);
132 121  
133 122 *version = resp.version;
134 123 return rc;
... ... @@ -190,7 +179,7 @@
190 179 uint32_t resp_size = sizeof(resp);
191 180  
192 181 rc = avb_do_tipc(READ_ROLLBACK_INDEX, &req, sizeof(req), &resp,
193   - &resp_size, true);
  182 + &resp_size);
194 183  
195 184 *value = resp.value;
196 185 return rc;
... ... @@ -204,7 +193,7 @@
204 193 uint32_t resp_size = sizeof(resp);
205 194  
206 195 rc = avb_do_tipc(WRITE_ROLLBACK_INDEX, &req, sizeof(req), &resp,
207   - &resp_size, true);
  196 + &resp_size);
208 197 return rc;
209 198 }
210 199  
... ... @@ -213,7 +202,7 @@
213 202 uint8_t resp_buf[AVB_MAX_BUFFER_LENGTH];
214 203 uint32_t resp_size = AVB_MAX_BUFFER_LENGTH;
215 204 int rc = avb_do_tipc(READ_PERMANENT_ATTRIBUTES, NULL, 0, resp_buf,
216   - &resp_size, true);
  205 + &resp_size);
217 206 if (rc != 0) {
218 207 return rc;
219 208 }
220 209  
221 210  
222 211  
... ... @@ -227,25 +216,25 @@
227 216  
228 217 int trusty_write_permanent_attributes(uint8_t *attributes, uint32_t size)
229 218 {
230   - return avb_do_tipc(WRITE_PERMANENT_ATTRIBUTES, attributes, size, NULL, NULL,
231   - true);
  219 + return avb_do_tipc(WRITE_PERMANENT_ATTRIBUTES, attributes, size, NULL,
  220 + NULL);
232 221 }
233 222  
234 223 int trusty_read_lock_state(uint8_t *lock_state)
235 224 {
236 225 uint32_t resp_size = sizeof(*lock_state);
237 226 return avb_do_tipc(READ_LOCK_STATE, NULL, 0, lock_state,
238   - &resp_size, true);
  227 + &resp_size);
239 228 }
240 229  
241 230 int trusty_write_lock_state(uint8_t lock_state)
242 231 {
243 232 return avb_do_tipc(WRITE_LOCK_STATE, &lock_state, sizeof(lock_state), NULL,
244   - NULL, true);
  233 + NULL);
245 234 }
246 235  
247 236 int trusty_lock_boot_state(void)
248 237 {
249   - return avb_do_tipc(LOCK_BOOT_STATE, NULL, 0, NULL, NULL, false);
  238 + return avb_do_tipc(LOCK_BOOT_STATE, NULL, 0, NULL, NULL);
250 239 }
lib/trusty/ql-tipc/ipc.c
... ... @@ -57,14 +57,15 @@
57 57  
58 58 chan->complete = 0;
59 59 for (;;) {
60   - rc = trusty_ipc_poll_for_event(chan);
  60 + rc = trusty_ipc_poll_for_event(chan->dev);
61 61 if (rc < 0)
62 62 return rc;
63 63  
64 64 if (chan->complete)
65 65 break;
66 66  
67   - trusty_ipc_dev_idle(chan->dev);
  67 + if (rc == TRUSTY_EVENT_NONE)
  68 + trusty_ipc_dev_idle(chan->dev);
68 69 }
69 70  
70 71 return chan->complete;
71 72  
72 73  
73 74  
74 75  
... ... @@ -185,29 +186,30 @@
185 186 trusty_assert(chan->dev);
186 187 trusty_assert(chan->handle);
187 188  
188   -Again:
189   - rc = trusty_ipc_dev_recv(chan->dev, chan->handle, iovs, iovs_cnt);
190   - if (rc == TRUSTY_ERR_NO_MSG) {
191   - if (wait) {
192   - rc = wait_for_reply(chan);
193   - if (rc < 0) {
194   - trusty_error("%s: wait to reply failed (%d)\n", __func__, rc);
195   - return rc;
196   - }
197   - goto Again;
  189 + if (wait) {
  190 + rc = wait_for_reply(chan);
  191 + if (rc < 0) {
  192 + trusty_error("%s: wait to reply failed (%d)\n", __func__, rc);
  193 + return rc;
198 194 }
199 195 }
200 196  
  197 + rc = trusty_ipc_dev_recv(chan->dev, chan->handle, iovs, iovs_cnt);
  198 + if (rc < 0)
  199 + trusty_error("%s: ipc recv failed (%d)\n", __func__, rc);
  200 +
201 201 return rc;
202 202 }
203 203  
204   -int trusty_ipc_poll_for_event(struct trusty_ipc_chan *chan)
  204 +int trusty_ipc_poll_for_event(struct trusty_ipc_dev *ipc_dev)
205 205 {
206 206 int rc;
207 207 struct trusty_ipc_event evt;
208   - trusty_assert(chan && chan->ops);
  208 + struct trusty_ipc_chan *chan;
209 209  
210   - rc = trusty_ipc_dev_get_event(chan->dev, chan->handle, &evt);
  210 + trusty_assert(dev);
  211 +
  212 + rc = trusty_ipc_dev_get_event(ipc_dev, 0, &evt);
211 213 if (rc) {
212 214 trusty_error("%s: get event failed (%d)\n", __func__, rc);
213 215 return rc;
... ... @@ -218,6 +220,9 @@
218 220 trusty_debug("%s: no event\n", __func__);
219 221 return TRUSTY_EVENT_NONE;
220 222 }
  223 +
  224 + chan = (struct trusty_ipc_chan *)(uintptr_t)evt.cookie;
  225 + trusty_assert(chan && chan->ops);
221 226  
222 227 /* check if we have raw event handler */
223 228 if (chan->ops->on_raw_event) {
lib/trusty/ql-tipc/keymaster.c
... ... @@ -185,9 +185,8 @@
185 185 * caller expects an additional data buffer to be returned from the secure
186 186 * side.
187 187 */
188   -static int km_do_tipc(uint32_t cmd, bool handle_rpmb, void* req,
189   - uint32_t req_len, void* resp_data,
190   - uint32_t* resp_data_len)
  188 +static int km_do_tipc(uint32_t cmd, void* req, uint32_t req_len,
  189 + void* resp_data, uint32_t* resp_data_len)
191 190 {
192 191 int rc = TRUSTY_ERR_GENERIC;
193 192 struct km_no_response resp_header;
... ... @@ -198,15 +197,6 @@
198 197 return rc;
199 198 }
200 199  
201   - if (handle_rpmb) {
202   - /* handle any incoming RPMB requests */
203   - rc = rpmb_storage_proxy_poll();
204   - if (rc < 0) {
205   - trusty_error("%s: failed (%d) to get RPMB requests\n", __func__, rc);
206   - return rc;
207   - }
208   - }
209   -
210 200 if (!resp_data) {
211 201 rc = km_read_raw_response(cmd, &resp_header, sizeof(resp_header));
212 202 } else {
... ... @@ -343,7 +333,7 @@
343 333 trusty_error("failed (%d) to serialize request\n", rc);
344 334 goto end;
345 335 }
346   - rc = km_do_tipc(KM_SET_BOOT_PARAMS, false, req, req_size, NULL, NULL);
  336 + rc = km_do_tipc(KM_SET_BOOT_PARAMS, req, req_size, NULL, NULL);
347 337  
348 338 end:
349 339 if (req) {
... ... @@ -369,7 +359,7 @@
369 359 trusty_error("failed (%d) to serialize request\n", rc);
370 360 goto end;
371 361 }
372   - rc = km_do_tipc(cmd, true, req, req_size, NULL, NULL);
  362 + rc = km_do_tipc(cmd, req, req_size, NULL, NULL);
373 363  
374 364 end:
375 365 if (req) {
... ... @@ -393,7 +383,7 @@
393 383 trusty_error("failed (%d) to serialize request\n", rc);
394 384 goto end;
395 385 }
396   - rc = km_do_tipc(cmd, false, req, req_size, resp_data, resp_data_size);
  386 + rc = km_do_tipc(cmd, req, req_size, resp_data, resp_data_size);
397 387  
398 388 end:
399 389 if (req) {
... ... @@ -445,7 +435,7 @@
445 435  
446 436 /* Tell the Trusty Keymaster TA the size of CA Response message */
447 437 begin_req.ca_response_size = ca_response_size;
448   - rc = km_do_tipc(KM_ATAP_SET_CA_RESPONSE_BEGIN, false, &begin_req,
  438 + rc = km_do_tipc(KM_ATAP_SET_CA_RESPONSE_BEGIN, &begin_req,
449 439 sizeof(begin_req), NULL, NULL);
450 440 if (rc != TRUSTY_ERR_NONE) {
451 441 return rc;
... ... @@ -464,7 +454,7 @@
464 454 }
465 455  
466 456 /* Tell Trusty Keymaster to parse the CA Response message */
467   - return km_do_tipc(KM_ATAP_SET_CA_RESPONSE_FINISH, true, NULL, 0, NULL, NULL);
  457 + return km_do_tipc(KM_ATAP_SET_CA_RESPONSE_FINISH, NULL, 0, NULL, NULL);
468 458 }
469 459  
470 460  
... ... @@ -473,7 +463,7 @@
473 463 *uuid_p = (char*) trusty_calloc(1, kUuidSize);
474 464  
475 465 uint32_t response_size = kUuidSize;
476   - int rc = km_do_tipc(KM_ATAP_READ_UUID, true, NULL, 0, *uuid_p,
  466 + int rc = km_do_tipc(KM_ATAP_READ_UUID, NULL, 0, *uuid_p,
477 467 &response_size);
478 468 if (rc < 0) {
479 469 trusty_error("failed to read uuid: %d\n", rc);
lib/trusty/ql-tipc/rpmb_proxy.c
... ... @@ -300,10 +300,20 @@
300 300 /* override default ops */
301 301 proxy_chan.ops = &proxy_ops;
302 302  
303   - rc = rpmb_storage_proxy_poll();
304   - if (rc < 0) {
305   - return rc;
  303 + do {
  304 + /* Check for RPMB events */
  305 + rc = trusty_ipc_poll_for_event(proxy_chan.dev);
  306 + if (rc < 0) {
  307 + trusty_error("%s: failed (%d) to get rpmb event\n", __func__, rc);
  308 + return rc;
  309 + }
  310 +
  311 + if (proxy_chan.handle == INVALID_IPC_HANDLE) {
  312 + trusty_error("%s: unexpected proxy channel close\n");
  313 + return TRUSTY_ERR_CHANNEL_CLOSED;
  314 + }
306 315 }
  316 + while (rc != TRUSTY_EVENT_NONE);
307 317  
308 318 /* mark as initialized */
309 319 initialized = true;
310 320  
... ... @@ -311,24 +321,9 @@
311 321 return TRUSTY_ERR_NONE;
312 322 }
313 323  
314   -int rpmb_storage_proxy_poll(void)
315   -{
316   - int rc = 0;
317   - while ((rc != TRUSTY_EVENT_NONE) && (proxy_chan.handle != INVALID_IPC_HANDLE)){
318   - /* Check for RPMB events */
319   - rc = trusty_ipc_poll_for_event(&proxy_chan);
320   - if (rc < 0) {
321   - trusty_error("%s: failed (%d) to get rpmb event\n", __func__, rc);
322   - return rc;
323   - }
324   - }
325   - return (proxy_chan.handle)? TRUSTY_ERR_NONE : TRUSTY_ERR_CHANNEL_CLOSED;
326   -}
327   -
328 324 void rpmb_storage_proxy_shutdown(struct trusty_ipc_dev *dev)
329 325 {
330   - if (!initialized)
331   - return; /* nothing to do */
  326 + trusty_assert(initialized);
332 327  
333 328 /* close channel */
334 329 trusty_ipc_close(&proxy_chan);