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