Remove OP-TEE dependency on trace functions
Introducing a custom trace component which replaces the one that comes
from the SP dev kit. The new component provides the same DMSG, IMSG,
EMSG macros for printing trace messages with different levels.
Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I5f9466c5a32fefeb9ef350b179dc22ee33324f7e
diff --git a/components/common/trace/component.cmake b/components/common/trace/component.cmake
new file mode 100644
index 0000000..c591832
--- /dev/null
+++ b/components/common/trace/component.cmake
@@ -0,0 +1,20 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+if (NOT DEFINED TGT)
+ message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+
+target_include_directories(${TGT}
+ PUBLIC
+ "${CMAKE_CURRENT_LIST_DIR}/include"
+ )
+
+target_sources(${TGT} PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/trace.c"
+ )
diff --git a/components/common/trace/include/trace.h b/components/common/trace/include/trace.h
new file mode 100644
index 0000000..f89411b
--- /dev/null
+++ b/components/common/trace/include/trace.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef TRACE_H_
+#define TRACE_H_
+
+#include "compiler.h"
+
+#define TRACE_LEVEL_NONE (0)
+#define TRACE_LEVEL_ERROR (1)
+#define TRACE_LEVEL_INFO (2)
+#define TRACE_LEVEL_DEBUG (3)
+
+#ifndef TRACE_LEVEL
+#define TRACE_LEVEL TRACE_LEVEL_ERROR
+#endif /* TRACE_LEVEL */
+
+/**
+ * no_trace_printf will be optimized out becase of the 'if (0)' but all the
+ * checks will still run against the format string and the parameters.
+ */
+#define no_trace_printf(func, line, level, fmt, ...) \
+ do { \
+ if (0) { \
+ trace_printf(func, line, level, fmt, ##__VA_ARGS__); \
+ } \
+ } while (0)
+
+void trace_puts(const char *str);
+void trace_printf(const char *func, int line, int level, const char *fmt, ...) __printf(4, 5);
+
+#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
+#define EMSG(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
+#else
+#define EMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_ERROR */
+
+#if TRACE_LEVEL >= TRACE_LEVEL_INFO
+#define IMSG(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
+#else
+#define IMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_INFO */
+
+#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
+#define DMSG(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
+#else
+#define DMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_DEBUG */
+
+#endif /* TRACE_H_ */
diff --git a/components/common/trace/trace.c b/components/common/trace/trace.c
new file mode 100644
index 0000000..bc7f0a9
--- /dev/null
+++ b/components/common/trace/trace.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include "trace.h"
+#include <stdarg.h>
+#include <stdio.h>
+
+#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
+#ifndef TRACE_PREFIX
+#error TRACE_PREFIX must be defined
+#endif /* TRACE_PREFIX */
+
+void trace_printf(const char *func, int line, int level, const char *fmt, ...)
+{
+ char buffer[256];
+ char level_char = 0;
+ int offset = 0;
+ va_list ap;
+ static const char levels[] = {'E', 'I', 'D'};
+
+ if (TRACE_LEVEL_ERROR <= level && TRACE_LEVEL_DEBUG >= level)
+ level_char = levels[level - TRACE_LEVEL_ERROR];
+ else
+ level_char = '?';
+
+ offset = snprintf(buffer, sizeof(buffer), "%c/" TRACE_PREFIX ": %s:%d ",
+ level_char, func, line);
+
+ if (offset < sizeof(buffer)) {
+ va_start(ap, fmt);
+ vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, ap);
+ va_end(ap);
+ }
+
+ trace_puts(buffer);
+}
+#endif
diff --git a/deployments/attestation/opteesp/CMakeLists.txt b/deployments/attestation/opteesp/CMakeLists.txt
index 92508eb..49bba4c 100644
--- a/deployments/attestation/opteesp/CMakeLists.txt
+++ b/deployments/attestation/opteesp/CMakeLists.txt
@@ -21,6 +21,7 @@
add_executable(attestation)
target_include_directories(attestation PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
set(SP_UUID "a1baf155-8876-4695-8f7c-54955e8db974")
+set(TRACE_PREFIX "ATT" CACHE STRING "Trace prefix")
# Include SP DEV KIT interface
@@ -44,6 +45,8 @@
BASE_DIR ${TS_ROOT}
COMPONENTS
"components/common/tlv"
+ "components/common/trace"
+ "components/common/utils"
"components/common/endian"
"components/common/uuid"
"components/config/ramstore"
diff --git a/deployments/crypto/opteesp/CMakeLists.txt b/deployments/crypto/opteesp/CMakeLists.txt
index 8ada74e..a9b1b90 100644
--- a/deployments/crypto/opteesp/CMakeLists.txt
+++ b/deployments/crypto/opteesp/CMakeLists.txt
@@ -21,6 +21,7 @@
add_executable(crypto-sp)
target_include_directories(crypto-sp PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
set(SP_UUID "d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c0")
+set(TRACE_PREFIX "CRYPTO" CACHE STRING "Trace prefix")
# Include SP DEV KIT interface
@@ -38,6 +39,8 @@
BASE_DIR ${TS_ROOT}
COMPONENTS
"components/common/tlv"
+ "components/common/trace"
+ "components/common/utils"
"components/config/ramstore"
"components/config/loader/sp"
"components/messaging/ffa/libsp"
diff --git a/deployments/env-test/opteesp/CMakeLists.txt b/deployments/env-test/opteesp/CMakeLists.txt
index d2c5447..d75f74f 100644
--- a/deployments/env-test/opteesp/CMakeLists.txt
+++ b/deployments/env-test/opteesp/CMakeLists.txt
@@ -22,6 +22,7 @@
add_executable(env-test)
target_include_directories(env-test PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
set(SP_UUID "33c75baf-ac6a-4fe4-8ac7-e9909bee2d17")
+set(TRACE_PREFIX "ENVTEST" CACHE STRING "Trace prefix")
# Include SP DEV KIT interface
@@ -38,6 +39,8 @@
add_components(TARGET "env-test"
BASE_DIR ${TS_ROOT}
COMPONENTS
+ "components/common/trace"
+ "components/common/utils"
"components/config/loader/sp"
"components/messaging/ffa/libsp"
"components/rpc/ffarpc/endpoint"
diff --git a/deployments/internal-trusted-storage/opteesp/CMakeLists.txt b/deployments/internal-trusted-storage/opteesp/CMakeLists.txt
index c86dcc4..25d910b 100644
--- a/deployments/internal-trusted-storage/opteesp/CMakeLists.txt
+++ b/deployments/internal-trusted-storage/opteesp/CMakeLists.txt
@@ -18,6 +18,7 @@
add_executable(internal-trusted-storage)
target_include_directories(internal-trusted-storage PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
set(SP_UUID "dc1eef48-b17a-4ccf-ac8b-dfcff7711b14")
+set(TRACE_PREFIX "ITS" CACHE STRING "Trace prefix")
# Include SP DEV KIT interface
@@ -31,6 +32,8 @@
add_components(TARGET "internal-trusted-storage"
BASE_DIR ${TS_ROOT}
COMPONENTS
+ components/common/trace
+ components/common/utils
components/messaging/ffa/libsp
components/rpc/ffarpc/endpoint
components/rpc/common/interface
diff --git a/deployments/protected-storage/opteesp/CMakeLists.txt b/deployments/protected-storage/opteesp/CMakeLists.txt
index b152519..e7f154a 100644
--- a/deployments/protected-storage/opteesp/CMakeLists.txt
+++ b/deployments/protected-storage/opteesp/CMakeLists.txt
@@ -18,6 +18,7 @@
add_executable(protected-storage)
target_include_directories(protected-storage PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
set(SP_UUID "751bf801-3dde-4768-a514-0f10aeed1790")
+set(TRACE_PREFIX "PS" CACHE STRING "Trace prefix")
# Include SP DEV KIT interface
@@ -31,6 +32,8 @@
add_components(TARGET "protected-storage"
BASE_DIR ${TS_ROOT}
COMPONENTS
+ components/common/trace
+ components/common/utils
components/messaging/ffa/libsp
components/rpc/ffarpc/endpoint
components/rpc/common/interface
diff --git a/deployments/se-proxy/opteesp/CMakeLists.txt b/deployments/se-proxy/opteesp/CMakeLists.txt
index 4e2069a..23a5c62 100644
--- a/deployments/se-proxy/opteesp/CMakeLists.txt
+++ b/deployments/se-proxy/opteesp/CMakeLists.txt
@@ -40,6 +40,8 @@
add_components(TARGET "se-proxy"
BASE_DIR ${TS_ROOT}
COMPONENTS
+ "components/common/trace"
+ "components/common/utils"
"protocols/rpc/common/packed-c"
"protocols/service/secure_storage/packed-c"
"protocols/service/crypto/protobuf"
diff --git a/deployments/sfs-demo/opteesp/CMakeLists.txt b/deployments/sfs-demo/opteesp/CMakeLists.txt
index acb1116..05b338a 100644
--- a/deployments/sfs-demo/opteesp/CMakeLists.txt
+++ b/deployments/sfs-demo/opteesp/CMakeLists.txt
@@ -17,6 +17,7 @@
project(trusted-services LANGUAGES C ASM)
add_executable(sfs-demo)
set(SP_UUID "01109cf8-e5ca-446f-9b55-f3cdc65110c8")
+set(TRACE_PREFIX "SFSDEMO" CACHE STRING "Trace prefix")
# Include SP DEV KIT interface
@@ -30,6 +31,8 @@
add_components(TARGET "sfs-demo"
BASE_DIR ${TS_ROOT}
COMPONENTS
+ components/common/trace
+ components/common/utils
components/messaging/ffa/libsp
components/rpc/common/interface
components/rpc/common/caller
diff --git a/deployments/smm-gateway/opteesp/CMakeLists.txt b/deployments/smm-gateway/opteesp/CMakeLists.txt
index f9f9721..c3f2674 100644
--- a/deployments/smm-gateway/opteesp/CMakeLists.txt
+++ b/deployments/smm-gateway/opteesp/CMakeLists.txt
@@ -40,6 +40,8 @@
add_components(TARGET "smm-gateway"
BASE_DIR ${TS_ROOT}
COMPONENTS
+ "components/common/trace"
+ "components/common/utils"
"components/common/uuid"
"components/config/ramstore"
"components/config/loader/sp"
diff --git a/environments/opteesp/component.cmake b/environments/opteesp/component.cmake
index f7e391f..bd6e036 100644
--- a/environments/opteesp/component.cmake
+++ b/environments/opteesp/component.cmake
@@ -11,9 +11,23 @@
target_sources(${TGT} PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/libsp_entry.c"
+ "${CMAKE_CURRENT_LIST_DIR}/sp_trace.c"
)
target_include_directories(${TGT}
- PUBLIC
- "${CMAKE_CURRENT_LIST_DIR}/include"
- )
+ PUBLIC
+ "${CMAKE_CURRENT_LIST_DIR}/include"
+ )
+
+if (NOT DEFINED TRACE_PREFIX)
+ set(TRACE_PREFIX "SP" CACHE STRING "Trace prefix")
+endif()
+
+if (NOT DEFINED TRACE_LEVEL)
+ set(TRACE_LEVEL "TRACE_LEVEL_ERROR" CACHE STRING "Trace level")
+endif()
+
+target_compile_definitions(${TGT} PRIVATE
+ TRACE_LEVEL=${TRACE_LEVEL}
+ TRACE_PREFIX="${TRACE_PREFIX}"
+)
diff --git a/environments/opteesp/libsp_entry.c b/environments/opteesp/libsp_entry.c
index 683b37c..68f410b 100644
--- a/environments/opteesp/libsp_entry.c
+++ b/environments/opteesp/libsp_entry.c
@@ -18,13 +18,6 @@
sp_main((struct ffa_init_info *)a0);
}
-void optee_sp_log_puts(const char *str)
-{
- struct ffa_params resp;
-
- ffa_svc(0xdeadbeef, (uintptr_t)str, 0, 0, 0, 0, 0, 0, &resp);
-}
-
void __noreturn optee_sp_panic(void)
{
while (1)
diff --git a/environments/opteesp/sp_trace.c b/environments/opteesp/sp_trace.c
new file mode 100644
index 0000000..6ac0ddc
--- /dev/null
+++ b/environments/opteesp/sp_trace.c
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include "trace.h"
+#include "ffa_internal_api.h"
+
+#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
+
+void trace_puts(const char *str)
+{
+ struct ffa_params resp;
+
+ ffa_svc(0xdeadbeef, (uintptr_t)str, 0, 0, 0, 0, 0, 0, &resp);
+}
+
+#endif /* TRACE_LEVEL >= TRACE_LEVEL_ERROR */
diff --git a/external/Spdevkit/FindSpdevkit.cmake b/external/Spdevkit/FindSpdevkit.cmake
index 4a5df37..dc81cbc 100644
--- a/external/Spdevkit/FindSpdevkit.cmake
+++ b/external/Spdevkit/FindSpdevkit.cmake
@@ -1,5 +1,5 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -115,7 +115,6 @@
${SP_DEV_KIT_SRC_DIR}/sp_assert.c
${SP_DEV_KIT_SRC_DIR}/sp_entry.c
${SP_DEV_KIT_SRC_DIR}/sp_header.c
- ${SP_DEV_KIT_SRC_DIR}/sp_trace.c
)
target_include_directories(sp_header