From 6c76795c7ac7abb532ff27844483a4c1171ce724 Mon Sep 17 00:00:00 2001 From: Alexander Usyskin Date: Sun, 28 Dec 2025 11:54:33 +0200 Subject: [PATCH 1/4] MeTee: add TeeCancelIO API Add API to stop all in-progress transactions. It is used to sync all threads before disconnect. Signed-off-by: Alexander Usyskin --- include/metee.h | 5 +++++ include/meteepp.h | 6 ++++++ src/Windows/metee_win.c | 40 +++++++++++++++++++++++++++++----------- src/linux/metee_linux.c | 26 ++++++++++++++++++++++---- src/uefi/metee_efi.c | 4 ++++ 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/include/metee.h b/include/metee.h index 9968d55..e1793a1 100644 --- a/include/metee.h +++ b/include/metee.h @@ -308,6 +308,11 @@ TEESTATUS TEEAPI TeeGetTRC(IN PTEEHANDLE handle, OUT uint32_t* trc_val); */ void TEEAPI TeeDisconnect(IN PTEEHANDLE handle); +/*! Try to stop all in-process reads and writes + * \param handle The handle of the session + */ +void TEEAPI TeeCancelIO(IN PTEEHANDLE handle); + /*! Returns handle of TEE device * Obtains HECI device handle on Windows and mei device file descriptor on Linux * \param handle The handle of the session. diff --git a/include/meteepp.h b/include/meteepp.h index e2c28ef..09258ba 100644 --- a/include/meteepp.h +++ b/include/meteepp.h @@ -192,6 +192,12 @@ namespace intel { TeeDisconnect(&_handle); } + /*! Try to stop all in-process reads and writes */ + void cancel_io() + { + TeeCancelIO(&_handle); + } + /*! Connects to the TEE driver and starts a session */ void connect() { diff --git a/src/Windows/metee_win.c b/src/Windows/metee_win.c index 4b0b069..ead5a9f 100644 --- a/src/Windows/metee_win.c +++ b/src/Windows/metee_win.c @@ -531,20 +531,11 @@ TEESTATUS TEEAPI TeeGetTRC(IN PTEEHANDLE handle, OUT uint32_t* trc_val) return status; } -VOID TEEAPI TeeDisconnect(IN PTEEHANDLE handle) +static void __TeeCancelIO(PTEEHANDLE handle) { struct METEE_WIN_IMPL *impl_handle = to_int(handle); DWORD ret; - if (NULL == handle) { - return; - } - - FUNC_ENTRY(handle); - if (NULL == impl_handle) { - goto Cleanup; - } - if (CancelIoEx(impl_handle->handle, NULL)) { HANDLE handles[MAX_EVT]; for (size_t i = 0; i < MAX_EVT; i++) { @@ -556,6 +547,33 @@ VOID TEEAPI TeeDisconnect(IN PTEEHANDLE handle) ret, GetLastError()); } } +} + +void TEEAPI TeeCancelIO(IN PTEEHANDLE handle) +{ + struct METEE_WIN_IMPL *impl_handle = to_int(handle); + + FUNC_ENTRY(handle); + if (NULL != impl_handle) { + __TeeCancelIO(handle); + } + FUNC_EXIT(handle, TEE_SUCCESS); +} + +VOID TEEAPI TeeDisconnect(IN PTEEHANDLE handle) +{ + struct METEE_WIN_IMPL *impl_handle = to_int(handle); + + if (NULL == handle) { + return; + } + + FUNC_ENTRY(handle); + if (NULL == impl_handle) { + goto Cleanup; + } + + __TeeCancelIO(handle); for (size_t i = 0; i < MAX_EVT; i++) { if (impl_handle->evt[i]) { if (impl_handle->evt[i]->hEvent) @@ -570,7 +588,7 @@ VOID TEEAPI TeeDisconnect(IN PTEEHANDLE handle) handle->handle = NULL; Cleanup: - FUNC_EXIT(handle, 0); + FUNC_EXIT(handle, TEE_SUCCESS); } TEE_DEVICE_HANDLE TEEAPI TeeGetDeviceHandle(IN PTEEHANDLE handle) diff --git a/src/linux/metee_linux.c b/src/linux/metee_linux.c index 476e7b9..6720d92 100644 --- a/src/linux/metee_linux.c +++ b/src/linux/metee_linux.c @@ -488,10 +488,30 @@ TEESTATUS TEEAPI TeeGetTRC(IN PTEEHANDLE handle, OUT uint32_t* trc_val) return status; } +static void __TeeCancelIO(PTEEHANDLE handle) +{ + struct metee_linux_intl* intl = to_intl(handle); + const char buf[] = "X"; + + if (write(intl->cancel_pipe[1], buf, sizeof(buf)) < 0) { + ERRPRINT(handle, "Pipe write failed\n"); + } +} + +void TEEAPI TeeCancelIO(IN PTEEHANDLE handle) +{ + struct metee_linux_intl* intl = to_intl(handle); + + FUNC_ENTRY(handle); + if (intl) { + __TeeCancelIO(handle); + } + FUNC_EXIT(handle, TEE_SUCCESS); +} + void TEEAPI TeeDisconnect(PTEEHANDLE handle) { struct metee_linux_intl *intl = to_intl(handle); - const char buf[] = "X"; if (!handle) { return; @@ -499,9 +519,7 @@ void TEEAPI TeeDisconnect(PTEEHANDLE handle) FUNC_ENTRY(handle); if (intl) { - if (write(intl->cancel_pipe[1], buf, sizeof(buf)) < 0) { - ERRPRINT(handle, "Pipe write failed\n"); - } + __TeeCancelIO(handle); mei_deinit(&intl->me); close(intl->cancel_pipe[0]); close(intl->cancel_pipe[1]); diff --git a/src/uefi/metee_efi.c b/src/uefi/metee_efi.c index 22c2d8f..d6659d3 100644 --- a/src/uefi/metee_efi.c +++ b/src/uefi/metee_efi.c @@ -851,4 +851,8 @@ TEESTATUS TEEAPI TeeGetKind(IN PTEEHANDLE handle, IN OUT char *kind, IN OUT size End: FUNC_EXIT(handle, status); return status; +} + +void TEEAPI TeeCancelIO(IN PTEEHANDLE handle) +{ } \ No newline at end of file From 8074606b1570c4bb30486acd466e33aa65114cf1 Mon Sep 17 00:00:00 2001 From: Alexander Usyskin Date: Wed, 31 Dec 2025 09:18:13 +0200 Subject: [PATCH 2/4] MeTee: Linux: import libmei 1.8.1 Build for Android using Android NDK Signed-off-by: Alexander Usyskin --- src/linux/mei.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/linux/mei.c b/src/linux/mei.c index d48245f..21cdee0 100644 --- a/src/linux/mei.c +++ b/src/linux/mei.c @@ -25,8 +25,12 @@ *****************************************************************************/ #ifdef ANDROID #define LOG_TAG "libmei" -#include -#define mei_msg(_me, fmt, ARGS...) ALOGV_IF((_me->log_level >= MEI_LOG_LEVEL_VERBOSE), fmt, ##ARGS) +#include +#define mei_msg(_me, fmt, ARGS...) \ +((_me->log_level >= MEI_LOG_LEVEL_VERBOSE) \ +? (void)ALOGV(fmt, ##ARGS) \ +: (void)0) + #define mei_err(_me, fmt, ARGS...) ALOGE(fmt, ##ARGS) #ifdef DEBUG static inline void __dump_buffer(const char *buf) From c50466e2b62fb9fdebd21c0e96993f90a2c8c49b Mon Sep 17 00:00:00 2001 From: "Briskman, Yitzhak" Date: Thu, 11 Dec 2025 11:04:00 +0200 Subject: [PATCH 3/4] MeTee: Linu: build for Android using Android NDK Android NDK provides log_macros for logging. Replacing using cutils log with android NDK logging Signed-off-by: Briskman, Yitzhak Signed-off-by: Alexander Usyskin --- include/helpers.h | 7 +++---- linux.cmake | 3 +++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/helpers.h b/include/helpers.h index 8838456..7be7a8f 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -44,10 +44,9 @@ extern "C" { // For debugging //#define LOG_NDEBUG 0 #define LOG_TAG "metee" - #include - #define DebugPrintMe(fmt, ...) ALOGV_IF(true, fmt, ##__VA_ARGS__) - #define ErrorPrintMe(fmt, ...) ALOGE_IF(true, fmt, ##__VA_ARGS__) - + #include + #define DebugPrintMe(fmt, ...) ALOGV(fmt, ##__VA_ARGS__) + #define ErrorPrintMe(fmt, ...) ALOGE(fmt, ##__VA_ARGS__) #ifdef LOG_NDEBUG #define TEE_DEFAULT_LOG_LEVEL TEE_LOG_LEVEL_VERBOSE #else diff --git a/linux.cmake b/linux.cmake index 6a8b38a..377eb45 100644 --- a/linux.cmake +++ b/linux.cmake @@ -69,3 +69,6 @@ if(NOT CONSOLE_OUTPUT) endif() target_compile_definitions(${PROJECT_NAME} PRIVATE -D_GNU_SOURCE) +if(ANDROID) + target_link_libraries(${PROJECT_NAME} PRIVATE log) +endif() \ No newline at end of file From 2e56fb0abce1edeea205fdb31c05d7d40c9f3819 Mon Sep 17 00:00:00 2001 From: Alexander Usyskin Date: Wed, 31 Dec 2025 14:05:53 +0200 Subject: [PATCH 4/4] MeTee: bump version to 6.2.0 Update CHANGELOG.md Signed-off-by: Alexander Usyskin --- CHANGELOG.md | 9 +++++++++ VERSION | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9cd615..1ed2158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [6.2.0] + +### Changed + - Linux: build for Android using Android NDK + - Linux: import libmei 1.8.1 + +### Added + - add TeeCancelIO API + ## [6.1.0] ### Fixed diff --git a/VERSION b/VERSION index dfda3e0..6abaeb2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -6.1.0 +6.2.0