• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define TLOG_TAG "smc-ipc"
18 
19 #include <lib/smc/smc_ipc.h>
20 #include <lib/tipc/tipc.h>
21 #include <trusty_log.h>
22 #include <uapi/err.h>
23 
smc_read_response(handle_t channel,struct smc_msg * msg)24 int smc_read_response(handle_t channel, struct smc_msg* msg) {
25     int rc;
26     uevent_t event;
27     int32_t err;
28     size_t msg_len = sizeof(struct smc_msg);
29     size_t resp_len = sizeof(struct smc_response);
30     STATIC_ASSERT(sizeof(struct smc_response) ==
31                   sizeof(struct smc_msg) + sizeof(err));
32 
33     rc = wait(channel, &event, INFINITE_TIME);
34     if (rc != NO_ERROR) {
35         TLOGD("%s: failed (%d) waiting for response\n", __func__, rc);
36         goto err;
37     }
38 
39     rc = tipc_recv2(channel, msg_len, msg, msg_len, &err, sizeof(err));
40     if (rc != (int)resp_len) {
41         TLOGD("%s: failed (%d) to read message. Expected to read %zu bytes.\n",
42               __func__, rc, msg_len);
43         if (rc >= 0)
44             rc = ERR_BAD_LEN;
45     } else if (err != NO_ERROR) {
46         TLOGE("%s: smc call failed (%d).\n", __func__, rc);
47         rc = err;
48     } else {
49         rc = msg_len; /* success - return number of bytes in smc_msg */
50     }
51 
52 err:
53     return rc;
54 }
55 
smc_send_request(handle_t channel,struct smc_msg * msg)56 int smc_send_request(handle_t channel, struct smc_msg* msg) {
57     size_t msg_len = sizeof(struct smc_msg);
58     int rc = tipc_send1(channel, msg, msg_len);
59     if (rc != (int)msg_len) {
60         TLOGD("%s: failed (%d) to send message. Expected to send %zu bytes.\n",
61               __func__, rc, msg_len);
62         if (rc >= 0)
63             rc = ERR_BAD_LEN;
64     }
65     return rc;
66 }
67