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