1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #if defined(TRUSTY_USERSPACE)
18*38e8c45fSAndroid Build Coastguard Worker #include <openssl/rand.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <trusty_ipc.h>
20*38e8c45fSAndroid Build Coastguard Worker #else
21*38e8c45fSAndroid Build Coastguard Worker #include <lib/rand/rand.h>
22*38e8c45fSAndroid Build Coastguard Worker #endif
23*38e8c45fSAndroid Build Coastguard Worker
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/Common.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcTransportTipcTrusty.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <trusty_log.h>
28*38e8c45fSAndroid Build Coastguard Worker
29*38e8c45fSAndroid Build Coastguard Worker #include "../OS.h"
30*38e8c45fSAndroid Build Coastguard Worker #include "TrustyStatus.h"
31*38e8c45fSAndroid Build Coastguard Worker
32*38e8c45fSAndroid Build Coastguard Worker #include <cstdarg>
33*38e8c45fSAndroid Build Coastguard Worker
34*38e8c45fSAndroid Build Coastguard Worker using android::binder::borrowed_fd;
35*38e8c45fSAndroid Build Coastguard Worker using android::binder::unique_fd;
36*38e8c45fSAndroid Build Coastguard Worker
37*38e8c45fSAndroid Build Coastguard Worker namespace android::binder::os {
38*38e8c45fSAndroid Build Coastguard Worker
trace_begin(uint64_t,const char *)39*38e8c45fSAndroid Build Coastguard Worker void trace_begin(uint64_t, const char*) {}
40*38e8c45fSAndroid Build Coastguard Worker
trace_end(uint64_t)41*38e8c45fSAndroid Build Coastguard Worker void trace_end(uint64_t) {}
42*38e8c45fSAndroid Build Coastguard Worker
trace_int(uint64_t,const char *,int32_t)43*38e8c45fSAndroid Build Coastguard Worker void trace_int(uint64_t, const char*, int32_t) {}
44*38e8c45fSAndroid Build Coastguard Worker
get_trace_enabled_tags()45*38e8c45fSAndroid Build Coastguard Worker uint64_t get_trace_enabled_tags() {
46*38e8c45fSAndroid Build Coastguard Worker return 0;
47*38e8c45fSAndroid Build Coastguard Worker }
48*38e8c45fSAndroid Build Coastguard Worker
GetThreadId()49*38e8c45fSAndroid Build Coastguard Worker uint64_t GetThreadId() {
50*38e8c45fSAndroid Build Coastguard Worker return 0;
51*38e8c45fSAndroid Build Coastguard Worker }
52*38e8c45fSAndroid Build Coastguard Worker
report_sysprop_change()53*38e8c45fSAndroid Build Coastguard Worker bool report_sysprop_change() {
54*38e8c45fSAndroid Build Coastguard Worker return false;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker
setNonBlocking(borrowed_fd)57*38e8c45fSAndroid Build Coastguard Worker status_t setNonBlocking(borrowed_fd /*fd*/) {
58*38e8c45fSAndroid Build Coastguard Worker // Trusty IPC syscalls are all non-blocking by default.
59*38e8c45fSAndroid Build Coastguard Worker return OK;
60*38e8c45fSAndroid Build Coastguard Worker }
61*38e8c45fSAndroid Build Coastguard Worker
getRandomBytes(uint8_t * data,size_t size)62*38e8c45fSAndroid Build Coastguard Worker status_t getRandomBytes(uint8_t* data, size_t size) {
63*38e8c45fSAndroid Build Coastguard Worker #if defined(TRUSTY_USERSPACE)
64*38e8c45fSAndroid Build Coastguard Worker int res = RAND_bytes(data, size);
65*38e8c45fSAndroid Build Coastguard Worker return res == 1 ? OK : UNKNOWN_ERROR;
66*38e8c45fSAndroid Build Coastguard Worker #else
67*38e8c45fSAndroid Build Coastguard Worker int res = rand_get_bytes(data, size);
68*38e8c45fSAndroid Build Coastguard Worker return res == 0 ? OK : UNKNOWN_ERROR;
69*38e8c45fSAndroid Build Coastguard Worker #endif // TRUSTY_USERSPACE
70*38e8c45fSAndroid Build Coastguard Worker }
71*38e8c45fSAndroid Build Coastguard Worker
dupFileDescriptor(int oldFd,int * newFd)72*38e8c45fSAndroid Build Coastguard Worker status_t dupFileDescriptor(int oldFd, int* newFd) {
73*38e8c45fSAndroid Build Coastguard Worker int res = dup(oldFd);
74*38e8c45fSAndroid Build Coastguard Worker if (res < 0) {
75*38e8c45fSAndroid Build Coastguard Worker return statusFromTrusty(res);
76*38e8c45fSAndroid Build Coastguard Worker }
77*38e8c45fSAndroid Build Coastguard Worker
78*38e8c45fSAndroid Build Coastguard Worker *newFd = res;
79*38e8c45fSAndroid Build Coastguard Worker return OK;
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker
makeDefaultRpcTransportCtxFactory()82*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() {
83*38e8c45fSAndroid Build Coastguard Worker return RpcTransportCtxFactoryTipcTrusty::make();
84*38e8c45fSAndroid Build Coastguard Worker }
85*38e8c45fSAndroid Build Coastguard Worker
sendMessageOnSocket(const RpcTransportFd &,iovec *,int,const std::vector<std::variant<unique_fd,borrowed_fd>> *)86*38e8c45fSAndroid Build Coastguard Worker ssize_t sendMessageOnSocket(
87*38e8c45fSAndroid Build Coastguard Worker const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
88*38e8c45fSAndroid Build Coastguard Worker const std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) {
89*38e8c45fSAndroid Build Coastguard Worker errno = ENOTSUP;
90*38e8c45fSAndroid Build Coastguard Worker return -1;
91*38e8c45fSAndroid Build Coastguard Worker }
92*38e8c45fSAndroid Build Coastguard Worker
receiveMessageFromSocket(const RpcTransportFd &,iovec *,int,std::vector<std::variant<unique_fd,borrowed_fd>> *)93*38e8c45fSAndroid Build Coastguard Worker ssize_t receiveMessageFromSocket(
94*38e8c45fSAndroid Build Coastguard Worker const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
95*38e8c45fSAndroid Build Coastguard Worker std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) {
96*38e8c45fSAndroid Build Coastguard Worker errno = ENOTSUP;
97*38e8c45fSAndroid Build Coastguard Worker return -1;
98*38e8c45fSAndroid Build Coastguard Worker }
99*38e8c45fSAndroid Build Coastguard Worker
100*38e8c45fSAndroid Build Coastguard Worker } // namespace android::binder::os
101*38e8c45fSAndroid Build Coastguard Worker
__android_log_print(int prio,const char * tag,const char * fmt,...)102*38e8c45fSAndroid Build Coastguard Worker LIBBINDER_EXPORTED int __android_log_print(int prio [[maybe_unused]], const char* tag,
103*38e8c45fSAndroid Build Coastguard Worker const char* fmt, ...) {
104*38e8c45fSAndroid Build Coastguard Worker #ifdef TRUSTY_USERSPACE
105*38e8c45fSAndroid Build Coastguard Worker #define trusty_tlog _tlog
106*38e8c45fSAndroid Build Coastguard Worker #define trusty_vtlog _vtlog
107*38e8c45fSAndroid Build Coastguard Worker #else
108*38e8c45fSAndroid Build Coastguard Worker // mapping taken from kernel trusty_log.h (TLOGx)
109*38e8c45fSAndroid Build Coastguard Worker int kernelLogLevel;
110*38e8c45fSAndroid Build Coastguard Worker if (prio <= ANDROID_LOG_DEBUG) {
111*38e8c45fSAndroid Build Coastguard Worker kernelLogLevel = LK_DEBUGLEVEL_ALWAYS;
112*38e8c45fSAndroid Build Coastguard Worker } else if (prio == ANDROID_LOG_INFO) {
113*38e8c45fSAndroid Build Coastguard Worker kernelLogLevel = LK_DEBUGLEVEL_SPEW;
114*38e8c45fSAndroid Build Coastguard Worker } else if (prio == ANDROID_LOG_WARN) {
115*38e8c45fSAndroid Build Coastguard Worker kernelLogLevel = LK_DEBUGLEVEL_INFO;
116*38e8c45fSAndroid Build Coastguard Worker } else if (prio == ANDROID_LOG_ERROR) {
117*38e8c45fSAndroid Build Coastguard Worker kernelLogLevel = LK_DEBUGLEVEL_CRITICAL;
118*38e8c45fSAndroid Build Coastguard Worker } else { /* prio >= ANDROID_LOG_FATAL */
119*38e8c45fSAndroid Build Coastguard Worker kernelLogLevel = LK_DEBUGLEVEL_CRITICAL;
120*38e8c45fSAndroid Build Coastguard Worker }
121*38e8c45fSAndroid Build Coastguard Worker #if LK_DEBUGLEVEL_NO_ALIASES
122*38e8c45fSAndroid Build Coastguard Worker auto LK_DEBUGLEVEL_kernelLogLevel = kernelLogLevel;
123*38e8c45fSAndroid Build Coastguard Worker #endif
124*38e8c45fSAndroid Build Coastguard Worker
125*38e8c45fSAndroid Build Coastguard Worker #define trusty_tlog(...) _tlog(kernelLogLevel, __VA_ARGS__)
126*38e8c45fSAndroid Build Coastguard Worker #define trusty_vtlog(...) _vtlog(kernelLogLevel, __VA_ARGS__)
127*38e8c45fSAndroid Build Coastguard Worker #endif
128*38e8c45fSAndroid Build Coastguard Worker
129*38e8c45fSAndroid Build Coastguard Worker va_list args;
130*38e8c45fSAndroid Build Coastguard Worker va_start(args, fmt);
131*38e8c45fSAndroid Build Coastguard Worker trusty_tlog((tag[0] == '\0') ? "libbinder" : "libbinder-");
132*38e8c45fSAndroid Build Coastguard Worker trusty_vtlog(fmt, args);
133*38e8c45fSAndroid Build Coastguard Worker va_end(args);
134*38e8c45fSAndroid Build Coastguard Worker
135*38e8c45fSAndroid Build Coastguard Worker return 1;
136*38e8c45fSAndroid Build Coastguard Worker }
137