blob: 2356eef4e683c35b786273124ec078dcf881d2ec [file] [log] [blame]
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -08001/*
Tri Vo19b62a52021-02-16 11:51:26 -08002 * Copyright 2021, The Android Open Source Project
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -08003 *
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#include "TrustyApp.h"
18
Tri Vo19b62a52021-02-16 11:51:26 -080019#include <BufferAllocator/BufferAllocator.h>
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080020#include <android-base/logging.h>
Tri Vo19b62a52021-02-16 11:51:26 -080021#include <sys/mman.h>
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080022#include <sys/uio.h>
23#include <trusty/tipc.h>
24
Tri Vo19b62a52021-02-16 11:51:26 -080025#define countof(arr) (sizeof(arr) / sizeof(arr[0]))
26
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080027namespace android {
28namespace trusty {
Tri Voabd86f82021-02-19 22:09:22 -080029namespace confirmationui {
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080030
Tri Vo19b62a52021-02-16 11:51:26 -080031using ::android::base::unique_fd;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080032
Tri Vo19b62a52021-02-16 11:51:26 -080033ssize_t TrustyApp::TrustyRpc(const uint8_t* obegin, const uint8_t* oend, uint8_t* ibegin,
34 uint8_t* iend) {
35 uint32_t olen = oend - obegin;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080036
Tri Vo19b62a52021-02-16 11:51:26 -080037 if (olen > shm_len_) {
38 LOG(ERROR) << AT << "request message too long to fit in shared memory";
39 return -1;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080040 }
41
Tri Vo19b62a52021-02-16 11:51:26 -080042 memcpy(shm_base_, obegin, olen);
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080043
Tri Vo19b62a52021-02-16 11:51:26 -080044 confirmationui_hdr hdr = {
45 .cmd = CONFIRMATIONUI_CMD_MSG,
46 };
47 confirmationui_msg_args args = {
48 .msg_len = olen,
49 };
50 iovec iov[] = {
51 {
52 .iov_base = &hdr,
53 .iov_len = sizeof(hdr),
54 },
55 {
56 .iov_base = &args,
57 .iov_len = sizeof(args),
58 },
59 };
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080060
Tri Vo19b62a52021-02-16 11:51:26 -080061 int rc = tipc_send(handle_, iov, countof(iov), NULL, 0);
62 if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) {
63 LOG(ERROR) << AT << "failed to send MSG request";
64 return -1;
65 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080066
Tri Vo19b62a52021-02-16 11:51:26 -080067 rc = readv(handle_, iov, countof(iov));
68 if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) {
69 LOG(ERROR) << AT << "failed to receive MSG response";
70 return -1;
71 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080072
Tri Vo19b62a52021-02-16 11:51:26 -080073 if (hdr.cmd != (CONFIRMATIONUI_CMD_MSG | CONFIRMATIONUI_RESP_BIT)) {
74 LOG(ERROR) << AT << "unknown response command: " << hdr.cmd;
75 return -1;
76 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080077
Tri Vo19b62a52021-02-16 11:51:26 -080078 uint32_t ilen = iend - ibegin;
79 if (args.msg_len > ilen) {
80 LOG(ERROR) << AT << "response message too long to fit in return buffer";
81 return -1;
82 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080083
Tri Vo19b62a52021-02-16 11:51:26 -080084 memcpy(ibegin, shm_base_, args.msg_len);
85
86 return args.msg_len;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080087}
88
89TrustyApp::TrustyApp(const std::string& path, const std::string& appname)
90 : handle_(kInvalidHandle) {
Tri Vo19b62a52021-02-16 11:51:26 -080091 unique_fd tipc_handle(tipc_connect(path.c_str(), appname.c_str()));
92 if (tipc_handle < 0) {
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080093 LOG(ERROR) << AT << "failed to connect to Trusty TA \"" << appname << "\" using dev:"
94 << "\"" << path << "\"";
Tri Vo19b62a52021-02-16 11:51:26 -080095 return;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080096 }
Tri Vo19b62a52021-02-16 11:51:26 -080097
Kalesh Singh623d1402023-09-06 11:32:53 -070098 uint32_t shm_len = CONFIRMATIONUI_MAX_MSG_SIZE;
Tri Vo19b62a52021-02-16 11:51:26 -080099 BufferAllocator allocator;
100 unique_fd dma_buf(allocator.Alloc("system", shm_len));
101 if (dma_buf < 0) {
102 LOG(ERROR) << AT << "failed to allocate shared memory buffer";
103 return;
104 }
105
Tri Vo19b62a52021-02-16 11:51:26 -0800106 confirmationui_hdr hdr = {
107 .cmd = CONFIRMATIONUI_CMD_INIT,
108 };
109 confirmationui_init_req args = {
110 .shm_len = shm_len,
111 };
112 iovec iov[] = {
113 {
114 .iov_base = &hdr,
115 .iov_len = sizeof(hdr),
116 },
117 {
118 .iov_base = &args,
119 .iov_len = sizeof(args),
120 },
121 };
122 trusty_shm shm = {
123 .fd = dma_buf,
124 .transfer = TRUSTY_SHARE,
125 };
126
127 int rc = tipc_send(tipc_handle, iov, 2, &shm, 1);
128 if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) {
129 LOG(ERROR) << AT << "failed to send INIT request";
130 return;
131 }
132
133 rc = read(tipc_handle, &hdr, sizeof(hdr));
134 if (rc != static_cast<int>(sizeof(hdr))) {
135 LOG(ERROR) << AT << "failed to receive INIT response";
136 return;
137 }
138
139 if (hdr.cmd != (CONFIRMATIONUI_CMD_INIT | CONFIRMATIONUI_RESP_BIT)) {
140 LOG(ERROR) << AT << "unknown response command: " << hdr.cmd;
141 return;
142 }
143
144 void* shm_base = mmap(0, shm_len, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf, 0);
145 if (shm_base == MAP_FAILED) {
146 LOG(ERROR) << AT << "failed to mmap() shared memory buffer";
147 return;
148 }
149
150 handle_ = std::move(tipc_handle);
151 shm_base_ = shm_base;
152 shm_len_ = shm_len;
153
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800154 LOG(INFO) << AT << "succeeded to connect to Trusty TA \"" << appname << "\"";
155}
Tri Vo19b62a52021-02-16 11:51:26 -0800156
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800157TrustyApp::~TrustyApp() {
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800158 LOG(INFO) << "Done shutting down TrustyApp";
159}
160
Tri Voabd86f82021-02-19 22:09:22 -0800161} // namespace confirmationui
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800162} // namespace trusty
163} // namespace android