blob: ba9e457a75305e883c65afff433a969499aa95d4 [file] [log] [blame]
Andrei Homescu74a54452021-12-10 05:30:21 +00001/*
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
Ayrton Munoz04137372022-08-03 17:05:09 -040017#if defined(TRUSTY_USERSPACE)
Andrei Homescu74a54452021-12-10 05:30:21 +000018#include <openssl/rand.h>
Andrei Homescu1c18a802022-08-17 04:59:01 +000019#include <trusty_ipc.h>
Ayrton Munoz04137372022-08-03 17:05:09 -040020#else
21#include <lib/rand/rand.h>
22#endif
Andrei Homescu74a54452021-12-10 05:30:21 +000023
Frederick Maylef7b65d12024-05-14 16:55:14 -070024#include <binder/Common.h>
Andrei Homescu024727b2022-08-24 23:54:59 +000025#include <binder/RpcTransportTipcTrusty.h>
Tomasz Wasilczykc7c22ea2023-11-30 20:13:44 -080026#include <log/log.h>
27#include <trusty_log.h>
Andrei Homescu024727b2022-08-24 23:54:59 +000028
Andrei Homescu74a54452021-12-10 05:30:21 +000029#include "../OS.h"
Andrei Homescu1c18a802022-08-17 04:59:01 +000030#include "TrustyStatus.h"
Andrei Homescu74a54452021-12-10 05:30:21 +000031
Tomasz Wasilczykc7c22ea2023-11-30 20:13:44 -080032#include <cstdarg>
33
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070034using android::binder::borrowed_fd;
35using android::binder::unique_fd;
36
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000037namespace android::binder::os {
Andrei Homescu74a54452021-12-10 05:30:21 +000038
Tomasz Wasilczykdaf727a2023-11-21 13:50:09 -080039void trace_begin(uint64_t, const char*) {}
40
41void trace_end(uint64_t) {}
42
Tomasz Wasilczyka9e451a2024-05-22 12:18:42 -070043void trace_int(uint64_t, const char*, int32_t) {}
44
Pawan Wagh0d6e60f2024-09-26 00:08:10 +000045uint64_t get_trace_enabled_tags() {
46 return 0;
47}
48
Tomasz Wasilczykdaf727a2023-11-21 13:50:09 -080049uint64_t GetThreadId() {
50 return 0;
51}
52
53bool report_sysprop_change() {
54 return false;
55}
56
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070057status_t setNonBlocking(borrowed_fd /*fd*/) {
Andrei Homescu74a54452021-12-10 05:30:21 +000058 // Trusty IPC syscalls are all non-blocking by default.
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070059 return OK;
Andrei Homescu74a54452021-12-10 05:30:21 +000060}
61
62status_t getRandomBytes(uint8_t* data, size_t size) {
Ayrton Munoz04137372022-08-03 17:05:09 -040063#if defined(TRUSTY_USERSPACE)
Andrei Homescu74a54452021-12-10 05:30:21 +000064 int res = RAND_bytes(data, size);
65 return res == 1 ? OK : UNKNOWN_ERROR;
Ayrton Munoz04137372022-08-03 17:05:09 -040066#else
67 int res = rand_get_bytes(data, size);
68 return res == 0 ? OK : UNKNOWN_ERROR;
69#endif // TRUSTY_USERSPACE
Andrei Homescu74a54452021-12-10 05:30:21 +000070}
71
Andrei Homescu1c18a802022-08-17 04:59:01 +000072status_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;
Andrei Homescu24ad36e2022-08-04 01:33:33 +000080}
81
Andrei Homescu024727b2022-08-24 23:54:59 +000082std::unique_ptr<RpcTransportCtxFactory> makeDefaultRpcTransportCtxFactory() {
83 return RpcTransportCtxFactoryTipcTrusty::make();
84}
85
David Brazdilc3964f62022-10-24 23:06:14 +010086ssize_t sendMessageOnSocket(
David Brazdil21c887c2022-09-23 12:25:18 +010087 const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070088 const std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) {
David Brazdil21c887c2022-09-23 12:25:18 +010089 errno = ENOTSUP;
90 return -1;
91}
92
David Brazdilc3964f62022-10-24 23:06:14 +010093ssize_t receiveMessageFromSocket(
David Brazdil21c887c2022-09-23 12:25:18 +010094 const RpcTransportFd& /* socket */, iovec* /* iovs */, int /* niovs */,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070095 std::vector<std::variant<unique_fd, borrowed_fd>>* /* ancillaryFds */) {
David Brazdil21c887c2022-09-23 12:25:18 +010096 errno = ENOTSUP;
97 return -1;
98}
99
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +0000100} // namespace android::binder::os
Tomasz Wasilczykc7c22ea2023-11-30 20:13:44 -0800101
Frederick Maylef7b65d12024-05-14 16:55:14 -0700102LIBBINDER_EXPORTED int __android_log_print(int prio [[maybe_unused]], const char* tag,
103 const char* fmt, ...) {
Tomasz Wasilczykc7c22ea2023-11-30 20:13:44 -0800104#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}