blob: 0e84b19c672fba97f37b75becb9bcd4687c92681 [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"
Tri Vo19b62a52021-02-16 11:51:26 -080018#include "TrustyIpc.h"
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080019
Tri Vo19b62a52021-02-16 11:51:26 -080020#include <BufferAllocator/BufferAllocator.h>
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080021#include <android-base/logging.h>
Tri Vo19b62a52021-02-16 11:51:26 -080022#include <sys/mman.h>
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080023#include <sys/uio.h>
24#include <trusty/tipc.h>
25
Tri Vo19b62a52021-02-16 11:51:26 -080026#define countof(arr) (sizeof(arr) / sizeof(arr[0]))
27
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080028namespace android {
29namespace trusty {
30
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 -080033static inline uintptr_t RoundPageUp(uintptr_t val) {
34 return (val + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080035}
36
Tri Vo19b62a52021-02-16 11:51:26 -080037ssize_t TrustyApp::TrustyRpc(const uint8_t* obegin, const uint8_t* oend, uint8_t* ibegin,
38 uint8_t* iend) {
39 uint32_t olen = oend - obegin;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080040
Tri Vo19b62a52021-02-16 11:51:26 -080041 if (olen > shm_len_) {
42 LOG(ERROR) << AT << "request message too long to fit in shared memory";
43 return -1;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080044 }
45
Tri Vo19b62a52021-02-16 11:51:26 -080046 memcpy(shm_base_, obegin, olen);
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080047
Tri Vo19b62a52021-02-16 11:51:26 -080048 confirmationui_hdr hdr = {
49 .cmd = CONFIRMATIONUI_CMD_MSG,
50 };
51 confirmationui_msg_args args = {
52 .msg_len = olen,
53 };
54 iovec iov[] = {
55 {
56 .iov_base = &hdr,
57 .iov_len = sizeof(hdr),
58 },
59 {
60 .iov_base = &args,
61 .iov_len = sizeof(args),
62 },
63 };
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080064
Tri Vo19b62a52021-02-16 11:51:26 -080065 int rc = tipc_send(handle_, iov, countof(iov), NULL, 0);
66 if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) {
67 LOG(ERROR) << AT << "failed to send MSG request";
68 return -1;
69 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080070
Tri Vo19b62a52021-02-16 11:51:26 -080071 rc = readv(handle_, iov, countof(iov));
72 if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) {
73 LOG(ERROR) << AT << "failed to receive MSG response";
74 return -1;
75 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080076
Tri Vo19b62a52021-02-16 11:51:26 -080077 if (hdr.cmd != (CONFIRMATIONUI_CMD_MSG | CONFIRMATIONUI_RESP_BIT)) {
78 LOG(ERROR) << AT << "unknown response command: " << hdr.cmd;
79 return -1;
80 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080081
Tri Vo19b62a52021-02-16 11:51:26 -080082 uint32_t ilen = iend - ibegin;
83 if (args.msg_len > ilen) {
84 LOG(ERROR) << AT << "response message too long to fit in return buffer";
85 return -1;
86 }
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080087
Tri Vo19b62a52021-02-16 11:51:26 -080088 memcpy(ibegin, shm_base_, args.msg_len);
89
90 return args.msg_len;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080091}
92
93TrustyApp::TrustyApp(const std::string& path, const std::string& appname)
94 : handle_(kInvalidHandle) {
Tri Vo19b62a52021-02-16 11:51:26 -080095 unique_fd tipc_handle(tipc_connect(path.c_str(), appname.c_str()));
96 if (tipc_handle < 0) {
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -080097 LOG(ERROR) << AT << "failed to connect to Trusty TA \"" << appname << "\" using dev:"
98 << "\"" << path << "\"";
Tri Vo19b62a52021-02-16 11:51:26 -080099 return;
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800100 }
Tri Vo19b62a52021-02-16 11:51:26 -0800101
102 uint32_t shm_len = RoundPageUp(CONFIRMATIONUI_MAX_MSG_SIZE);
103 BufferAllocator allocator;
104 unique_fd dma_buf(allocator.Alloc("system", shm_len));
105 if (dma_buf < 0) {
106 LOG(ERROR) << AT << "failed to allocate shared memory buffer";
107 return;
108 }
109
110 if (dma_buf < 0) {
111 LOG(ERROR) << AT << "failed to allocate shared memory buffer";
112 return;
113 }
114
115 confirmationui_hdr hdr = {
116 .cmd = CONFIRMATIONUI_CMD_INIT,
117 };
118 confirmationui_init_req args = {
119 .shm_len = shm_len,
120 };
121 iovec iov[] = {
122 {
123 .iov_base = &hdr,
124 .iov_len = sizeof(hdr),
125 },
126 {
127 .iov_base = &args,
128 .iov_len = sizeof(args),
129 },
130 };
131 trusty_shm shm = {
132 .fd = dma_buf,
133 .transfer = TRUSTY_SHARE,
134 };
135
136 int rc = tipc_send(tipc_handle, iov, 2, &shm, 1);
137 if (rc != static_cast<int>(sizeof(hdr) + sizeof(args))) {
138 LOG(ERROR) << AT << "failed to send INIT request";
139 return;
140 }
141
142 rc = read(tipc_handle, &hdr, sizeof(hdr));
143 if (rc != static_cast<int>(sizeof(hdr))) {
144 LOG(ERROR) << AT << "failed to receive INIT response";
145 return;
146 }
147
148 if (hdr.cmd != (CONFIRMATIONUI_CMD_INIT | CONFIRMATIONUI_RESP_BIT)) {
149 LOG(ERROR) << AT << "unknown response command: " << hdr.cmd;
150 return;
151 }
152
153 void* shm_base = mmap(0, shm_len, PROT_READ | PROT_WRITE, MAP_SHARED, dma_buf, 0);
154 if (shm_base == MAP_FAILED) {
155 LOG(ERROR) << AT << "failed to mmap() shared memory buffer";
156 return;
157 }
158
159 handle_ = std::move(tipc_handle);
160 shm_base_ = shm_base;
161 shm_len_ = shm_len;
162
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800163 LOG(INFO) << AT << "succeeded to connect to Trusty TA \"" << appname << "\"";
164}
Tri Vo19b62a52021-02-16 11:51:26 -0800165
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800166TrustyApp::~TrustyApp() {
Janis Danisevskis8fe0cfb2020-01-13 14:24:32 -0800167 LOG(INFO) << "Done shutting down TrustyApp";
168}
169
170} // namespace trusty
171} // namespace android