1 /*
2 * Copyright (C) 2022 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 #if defined(TRUSTY_USERSPACE)
18 #include <openssl/rand.h>
19 #include <trusty_ipc.h>
20 #else
21 #include <lib/rand/rand.h>
22 #endif
23
24 #include <binder/Common.h>
25 #include <binder/RpcTransportTipcTrusty.h>
26 #include <log/log.h>
27 #include <trusty_log.h>
28
29 #include "../OS.h"
30 #include "TrustyStatus.h"
31
32 #include <cstdarg>
33
34 using android::binder::borrowed_fd;
35 using android::binder::unique_fd;
36
37 namespace android::binder::os {
38
trace_begin(uint64_t,const char *)39 void trace_begin(uint64_t, const char*) {}
40
trace_end(uint64_t)41 void trace_end(uint64_t) {}
42
trace_int(uint64_t,const char *,int32_t)43 void trace_int(uint64_t, const char*, int32_t) {}
44
GetThreadId()45 uint64_t GetThreadId() {
46 return 0;
47 }
48
report_sysprop_change()49 bool report_sysprop_change() {
50 return false;
51 }
52
setNonBlocking(borrowed_fd)53 status_t setNonBlocking(borrowed_fd /*fd*/) {
54 // Trusty IPC syscalls are all non-blocking by default.
55 return OK;
56 }
57
getRandomBytes(uint8_t * data,size_t size)58 status_t getRandomBytes(uint8_t* data, size_t size) {
59 #if defined(TRUSTY_USERSPACE)
60 int res = RAND_bytes(data, size);
61 return res == 1 ? OK : UNKNOWN_ERROR;
62 #else
63 int res = rand_get_bytes(data, size);
64 return res == 0 ? OK : UNKNOWN_ERROR;
65 #endif // TRUSTY_USERSPACE
66 }
67
dupFileDescriptor(int oldFd,int * newFd)68 status_t dupFileDescriptor(int oldFd, int* newFd) {
69 int res = dup(oldFd);
70 if (res < 0) {
71 return statusFromTrusty(res);
72 }
73
74 *newFd = res;
75 return OK;
76 }
77
makeDefaultRpcTransportCtxFactory()78 std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() {
79 return RpcTransportCtxFactoryTipcTrusty::make();
80 }
81
sendMessageOnSocket(const RpcTransportFd &,iovec *,int,const std::vector<std::variant<unique_fd,borrowed_fd>> *)82 ssize_t sendMessageOnSocket(
83 const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
84 const std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) {
85 errno = ENOTSUP;
86 return -1;
87 }
88
receiveMessageFromSocket(const RpcTransportFd &,iovec *,int,std::vector<std::variant<unique_fd,borrowed_fd>> *)89 ssize_t receiveMessageFromSocket(
90 const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
91 std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) {
92 errno = ENOTSUP;
93 return -1;
94 }
95
96 } // namespace android::binder::os
97
__android_log_print(int prio,const char * tag,const char * fmt,...)98 LIBBINDER_EXPORTED int __android_log_print(int prio [[maybe_unused]], const char* tag,
99 const char* fmt, ...) {
100 #ifdef TRUSTY_USERSPACE
101 #define trusty_tlog _tlog
102 #define trusty_vtlog _vtlog
103 #else
104 // mapping taken from kernel trusty_log.h (TLOGx)
105 int kernelLogLevel;
106 if (prio <= ANDROID_LOG_DEBUG) {
107 kernelLogLevel = LK_DEBUGLEVEL_ALWAYS;
108 } else if (prio == ANDROID_LOG_INFO) {
109 kernelLogLevel = LK_DEBUGLEVEL_SPEW;
110 } else if (prio == ANDROID_LOG_WARN) {
111 kernelLogLevel = LK_DEBUGLEVEL_INFO;
112 } else if (prio == ANDROID_LOG_ERROR) {
113 kernelLogLevel = LK_DEBUGLEVEL_CRITICAL;
114 } else { /* prio >= ANDROID_LOG_FATAL */
115 kernelLogLevel = LK_DEBUGLEVEL_CRITICAL;
116 }
117 #if LK_DEBUGLEVEL_NO_ALIASES
118 auto LK_DEBUGLEVEL_kernelLogLevel = kernelLogLevel;
119 #endif
120
121 #define trusty_tlog(...) _tlog(kernelLogLevel, __VA_ARGS__)
122 #define trusty_vtlog(...) _vtlog(kernelLogLevel, __VA_ARGS__)
123 #endif
124
125 va_list args;
126 va_start(args, fmt);
127 trusty_tlog((tag[0] == '\0') ? "libbinder" : "libbinder-");
128 trusty_vtlog(fmt, args);
129 va_end(args);
130
131 return 1;
132 }
133