diff --git a/CHANGELOG.md b/CHANGELOG.md index d4b71bf..47ad826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +## [6.2.4] + +### Fixed + - EFI: fix print specifiers for callback2 + +### Changed + - samples: metee_gsc: enable EFI build + - samples: metee_basic: enable EFI build + +### Added + - EFI: add support for CSC target + - EFI: add DEFINE_GUID + - EFI: add library configure + ## [6.2.3] ### Fixed diff --git a/CMakeLists.txt b/CMakeLists.txt index d57d0f5..521da59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,11 +18,13 @@ include(GNUInstallDirs) set(LICENSE Apache) include(version.cmake) -if(WIN32) +if(EFI) + include(uefi.cmake) +elseif(WIN32) include(win32.cmake) -else(WIN32) +else() include(linux.cmake) -endif(WIN32) +endif() set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER include/metee.h) set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${TEE_VERSION_STRING}) diff --git a/MeTeePkg/MeTeePkg.dec.in b/MeTeePkg/MeTeePkg.dec.in new file mode 100644 index 0000000..ddfe10c --- /dev/null +++ b/MeTeePkg/MeTeePkg.dec.in @@ -0,0 +1,18 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (C) 2026 Intel Corporation + +[Defines] + DEC_SPECIFICATION = 0x00010005 + PACKAGE_NAME = MeTeePkg + PACKAGE_GUID = 8e6a6715-9abc-4043-88ef-9e39c6f63e0f + PACKAGE_VERSION = 0.1 + +[Includes] + @METEE_INCLUDE_RELPATH@ + +[LibraryClasses] + ## @libraryclass Provides MeTee interface functions + MeTeeLib|include/metee.h + +[Guids] + gMeTeePkgTokenSpaceGuid = { 0x8e6a6715, 0x9abc, 0x4043, { 0x88, 0xef, 0x9e, 0x39, 0xc6, 0xf6, 0x3e, 0x0f }} diff --git a/MeTeePkg/MeTeePkg.dsc.in b/MeTeePkg/MeTeePkg.dsc.in new file mode 100644 index 0000000..bfb5f94 --- /dev/null +++ b/MeTeePkg/MeTeePkg.dsc.in @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2024-2026 Intel Corporation +# +[Defines] + DSC_SPECIFICATION = 0x0001001A + PLATFORM_GUID = A4E6FE89-05EF-44DC-89F1-4EC07B513B10 + PLATFORM_VERSION = 0.01 + PLATFORM_NAME = MeTeePkg + SKUID_IDENTIFIER = DEFAULT + SUPPORTED_ARCHITECTURES = X64 + BUILD_TARGETS = DEBUG|RELEASE + OUTPUT_DIRECTORY = BINARY_OUTPUT + +!include MdePkg/MdeLibs.dsc.inc + +[LibraryClasses] +UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf +UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf +DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf +PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf +RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf +PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf +BaseLib|MdePkg/Library/BaseLib/BaseLib.inf +DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf +BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf +UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf +MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf +UefiLib|MdePkg/Library/UefiLib/UefiLib.inf + +DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf +PciLib|MdePkg/Library/BasePciLibCf8/BasePciLibCf8.inf + +IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf +PciCf8Lib|MdePkg/Library/BasePciCf8Lib/BasePciCf8Lib.inf +HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf +PciSegmentLib|MdePkg/Library/BasePciSegmentLibPci/BasePciSegmentLibPci.inf + +MeTeeLib|MeTeePkg/MeTeeLibrary/MeTeeLibrary.inf + +[Components] +MeTeePkg/MeTeeLibrary/MeTeeLibrary.inf + +@METEE_SAMPLES@ + +[PcdsFixedAtBuild] +gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000043 diff --git a/VERSION b/VERSION index bee9433..42cc526 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -6.2.3 +6.2.4 diff --git a/include/helpers.h b/include/helpers.h index 65cb541..b4ead3c 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -17,10 +17,15 @@ extern "C" { #ifdef EFI #define DEBUG_PRINT_ME_PREFIX_INTERNAL "TEELIB: (%a:%a():%d) " + #ifdef METEE_EFI_STDLIB_SUPPORT + #define DEBUG_PRINT_ME_PREFIX_EXTERNAL "TEELIB: (%s:%s():%d) " + #else + #define DEBUG_PRINT_ME_PREFIX_EXTERNAL "TEELIB: (%a:%a():%d) " + #endif /* METEE_EFI_STDLIB_SUPPORT */ #else /* EFI */ #define DEBUG_PRINT_ME_PREFIX_INTERNAL "TEELIB: (%s:%s():%d) " + #define DEBUG_PRINT_ME_PREFIX_EXTERNAL "TEELIB: (%s:%s():%d) " #endif /* EFI */ -#define DEBUG_PRINT_ME_PREFIX_EXTERNAL "TEELIB: (%s:%s():%d) " #ifdef _WIN32 #include diff --git a/include/metee.h b/include/metee.h index e1793a1..bb61805 100644 --- a/include/metee.h +++ b/include/metee.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright (C) 2014-2025 Intel Corporation + * Copyright (C) 2014-2026 Intel Corporation */ /*! \file metee.h * \brief metee library API @@ -39,9 +39,13 @@ extern "C" { #define TEE_DEVICE_HANDLE void * #define TEE_INVALID_DEVICE_HANDLE ((void*)-1) + #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ + const GUID name \ + = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } + // when calling TeeInitFull // TEE_DEVICE_TYPE_BDF - HECI device Bus Device Function - + #else /* _WIN32 */ #ifndef METEE_DLL #define METEE_DLL_API diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index c53f8e5..e917896 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -1,5 +1,5 @@ # SPDX-License-Identifier: Apache-2.0 -# Copyright (C) 2014-2024 Intel Corporation +# Copyright (C) 2014-2026 Intel Corporation cmake_minimum_required(VERSION 3.15) project(metee_sample) @@ -14,6 +14,13 @@ add_executable(metee-gsc metee_gsc.c) target_link_libraries(metee-gsc metee) install(TARGETS metee-gsc RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +if(EFI) + configure_file ( + "${PROJECT_SOURCE_DIR}/MeTeeGsc.inf.in" + "${CMAKE_BINARY_DIR}/MeTeePkg/Samples/MeTeeGsc/MeTeeGsc.inf" + ) +endif(EFI) + if(UNIX) add_executable(metee-connect metee_connect.c meiuuid.c) @@ -30,10 +37,17 @@ add_executable(metee-basic metee_basic.c) target_link_libraries(metee-basic metee) install(TARGETS metee-basic RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +if(EFI) + configure_file ( + "${PROJECT_SOURCE_DIR}/MeTeeBasic.inf.in" + "${CMAKE_BINARY_DIR}/MeTeePkg/Samples/MeTeeBasic/MeTeeBasic.inf" + ) +endif(EFI) + add_executable(meteepp-basic meteepp_basic.cpp) target_link_libraries(meteepp-basic metee) install(TARGETS meteepp-basic RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) if(BUILD_SHARED_LIBS AND WIN32) add_subdirectory(csharp) -endif(BUILD_SHARED_LIBS AND WIN32) \ No newline at end of file +endif(BUILD_SHARED_LIBS AND WIN32) diff --git a/samples/MeTeeBasic.inf.in b/samples/MeTeeBasic.inf.in new file mode 100644 index 0000000..a563eb1 --- /dev/null +++ b/samples/MeTeeBasic.inf.in @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (C) 2026 Intel Corporation + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MeTeeBasic + FILE_GUID = 1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = @TEE_VERSION_STRING@ + ENTRY_POINT = UefiMain + +[Sources] + ../samples/metee_basic.c + ../samples/MeTeeBasicMain.c + +[Packages] + MdePkg/MdePkg.dec + MeTeePkg/MeTeePkg.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiLib + BaseLib + BaseMemoryLib + MemoryAllocationLib + MeTeeLib + +[BuildOptions] + MSFT:*_*_*_CC_FLAGS = /GS- /D EFI /U _WIN32 /W4 /WX /wd4201 + GCC:*_*_*_CC_FLAGS = -D EFI \ No newline at end of file diff --git a/samples/MeTeeBasicMain.c b/samples/MeTeeBasicMain.c new file mode 100644 index 0000000..8b6b250 --- /dev/null +++ b/samples/MeTeeBasicMain.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2026 Intel Corporation + */ + +#include +#include + +extern int main(int argc, char* argv[]); + +/** + UEFI application entry point which has an interface similar to a + standard C main function. + + @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point executed successfully. + @retval other Some error occurred when executing this entry point. + +**/ +EFI_STATUS +EFIAPI +UefiMain( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + int ret; + + (void)ImageHandle; + (void)SystemTable; + + ret = main(0, NULL); + + return (ret == 0) ? EFI_SUCCESS : EFI_DEVICE_ERROR; +} \ No newline at end of file diff --git a/samples/MeTeeGsc.inf.in b/samples/MeTeeGsc.inf.in new file mode 100644 index 0000000..8cab8df --- /dev/null +++ b/samples/MeTeeGsc.inf.in @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (C) 2026 Intel Corporation + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = MeTeeGsc + FILE_GUID = 3c4d5e6f-7a8b-9c0d-1e2f-3a4b5c6d7e8f + MODULE_TYPE = UEFI_APPLICATION + VERSION_STRING = @TEE_VERSION_STRING@ + ENTRY_POINT = UefiMain + +[Sources] + ../samples/metee_gsc.c + ../samples/MeTeeGscMain.c + +[Packages] + MdePkg/MdePkg.dec + MeTeePkg/MeTeePkg.dec + StdLib/StdLib.dec + +[LibraryClasses] + UefiApplicationEntryPoint + UefiLib + BaseLib + BaseMemoryLib + MemoryAllocationLib + MeTeeLib + +[BuildOptions] + MSFT:*_*_*_CC_FLAGS = /GS- /D EFI /U _WIN32 /W4 /WX /wd4201 /D _CRT_SECURE_NO_WARNINGS + GCC:*_*_*_CC_FLAGS = -D EFI \ No newline at end of file diff --git a/samples/MeTeeGscMain.c b/samples/MeTeeGscMain.c new file mode 100644 index 0000000..8b6b250 --- /dev/null +++ b/samples/MeTeeGscMain.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright (C) 2026 Intel Corporation + */ + +#include +#include + +extern int main(int argc, char* argv[]); + +/** + UEFI application entry point which has an interface similar to a + standard C main function. + + @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point executed successfully. + @retval other Some error occurred when executing this entry point. + +**/ +EFI_STATUS +EFIAPI +UefiMain( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + int ret; + + (void)ImageHandle; + (void)SystemTable; + + ret = main(0, NULL); + + return (ret == 0) ? EFI_SUCCESS : EFI_DEVICE_ERROR; +} \ No newline at end of file diff --git a/samples/metee_basic.c b/samples/metee_basic.c index 851fd19..7d591c6 100644 --- a/samples/metee_basic.c +++ b/samples/metee_basic.c @@ -1,12 +1,35 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright (C) 2024-2025 Intel Corporation + * Copyright (C) 2024-2026 Intel Corporation */ #include #include -#ifdef __linux__ +#ifdef WIN32 +#define metee_basic_print(...) printf(__VA_ARGS__) +#define metee_basic_err(...) fprintf(stderr, __VA_ARGS__) +#define metee_basic_malloc(size) malloc(size) +#define metee_basic_free(ptr) free(ptr) +#define metee_basic_sleep(ms) Sleep(ms) +#elif __linux__ #include -#endif /* __linux__ */ +#define metee_basic_print(...) printf(__VA_ARGS__) +#define metee_basic_err(...) fprintf(stderr, __VA_ARGS__) +#define metee_basic_malloc(size) malloc(size) +#define metee_basic_free(ptr) free(ptr) +#define metee_basic_sleep(ms) usleep((ms)*1000) +#elif defined(EFI) +#include +#include +#include +#include +#include + +#define metee_basic_print(...) AsciiPrint(__VA_ARGS__) +#define metee_basic_err(...) AsciiPrint(__VA_ARGS__) +#define metee_basic_malloc(size) AllocatePool(size) +#define metee_basic_free(ptr) FreePool(ptr) +#define metee_basic_sleep(ms) gBS->Stall((ms)*1000) +#endif #include @@ -61,6 +84,7 @@ int main(int argc, char* argv[]) .type = TEE_DEVICE_TYPE_NONE, .data.path = NULL }; + int retry = CONNECT_RETRIES; size_t written = 0; struct mkhi_fwver_req req; @@ -71,15 +95,15 @@ int main(int argc, char* argv[]) status = TeeInitFull(&handle, &MEI_MKHIF, addr, TEE_LOG_LEVEL_VERBOSE, NULL); if (!TEE_IS_SUCCESS(status)) { - fprintf(stderr, "TeeInitFull failed with status = %u\n", status); + metee_basic_err("TeeInitFull failed with status = %u\n", status); return 1; } status = TeeGetKind(&handle, kind, &kind_size); if (!TEE_IS_SUCCESS(status)) { - fprintf(stderr, "TeeGetKind failed with status = %u\n", status); + metee_basic_err("TeeGetKind failed with status = %u\n", status); } else { - printf("Tee device kind is %s\n", kind); + metee_basic_print("Tee device kind is %s\n", kind); } while (retry--) { @@ -87,27 +111,23 @@ int main(int argc, char* argv[]) if (status != TEE_BUSY && status != TEE_UNABLE_TO_COMPLETE_OPERATION) /* windows return this error on busy */ break; - fprintf(stderr, "Client is busy, retrying\n"); -#ifdef WIN32 - Sleep(2000); -#else - sleep(2); -#endif /* WIN32 */ + metee_basic_err("Client is busy, retrying\n"); + metee_basic_sleep(2000); } switch (status) { case TEE_SUCCESS: break; case TEE_CLIENT_NOT_FOUND: - fprintf(stderr, "TeeConnect failed with status = %u (Client not found)\n", status); + metee_basic_err("TeeConnect failed with status = %u (Client not found)\n", status); goto out; default: - fprintf(stderr, "TeeConnect failed with status = %u\n", status); + metee_basic_err("TeeConnect failed with status = %u\n", status); goto out; } if (TeeGetMaxMsgLen(&handle) == 0) { - fprintf(stderr, "Client reported zero MTU. Aborting.\n"); + metee_basic_err("Client reported zero MTU. Aborting.\n"); goto out; } @@ -120,55 +140,55 @@ int main(int argc, char* argv[]) status = TeeWrite(&handle, &req, sizeof(req), &written, MKHI_TIMEOUT); if (!TEE_IS_SUCCESS(status)) { - fprintf(stderr, "TeeWrite failed with status = %u\n", status); + metee_basic_err("TeeWrite failed with status = %u\n", status); goto out; } if (written != sizeof(req)) { - fprintf(stderr, "TeeWrite failed written = %zu\n", written); + metee_basic_err("TeeWrite failed written = %zu\n", written); status = TEE_INTERNAL_ERROR; goto out; } /* Read */ - read_buf = (uint8_t*)malloc(TeeGetMaxMsgLen(&handle)); + read_buf = (uint8_t*)metee_basic_malloc(TeeGetMaxMsgLen(&handle)); if (!read_buf) { - fprintf(stderr, "malloc failed\n"); + metee_basic_err("malloc failed\n"); status = TEE_INTERNAL_ERROR; goto out; } status = TeeRead(&handle, read_buf, TeeGetMaxMsgLen(&handle), &written, MKHI_TIMEOUT); if (!TEE_IS_SUCCESS(status)) { - fprintf(stderr, "TeeWrite failed with status = %u\n", status); + metee_basic_err("TeeRead failed with status = %u\n", status); goto out; } rsp = (struct mkhi_fwver_rsp*)read_buf; if (written < sizeof(struct mkhi_msg_hdr)) { - fprintf(stderr, "Returned less then header = %zu\n", written); + metee_basic_err("Returned less than header = %zu\n", written); status = TEE_INTERNAL_ERROR; goto out; } if (written < sizeof(struct mkhi_fwver_rsp)) { - fprintf(stderr, "Returned less then response = %zu\n", written); + metee_basic_err("Returned less than response = %zu\n", written); status = TEE_INTERNAL_ERROR; goto out; } if (rsp->header.Result) { - fprintf(stderr, "Result = %u\n", rsp->header.Result); + metee_basic_err("Result = %u\n", rsp->header.Result); status = TEE_INTERNAL_ERROR; goto out; } - printf("Version: %u.%u.%u.%u\n", + metee_basic_print("Version: %u.%u.%u.%u\n", rsp->version.code.major, rsp->version.code.minor, rsp->version.code.hotFix, rsp->version.code.buildNo); out: TeeDisconnect(&handle); if (read_buf) - free(read_buf); + metee_basic_free(read_buf); return TEE_IS_SUCCESS(status) ? 0 : 1; } diff --git a/samples/metee_gsc.c b/samples/metee_gsc.c index 404e5e2..942f342 100644 --- a/samples/metee_gsc.c +++ b/samples/metee_gsc.c @@ -1,25 +1,67 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright (C) 2020-2025 Intel Corporation + * Copyright (C) 2020-2026 Intel Corporation */ -#include -#include -#include -#include #include +#include +#include #include #include -#ifndef BIT -#define BIT(n) 1 << (n) -#endif /* BIT */ +#ifdef WIN32 +#include +#include +#include +#include -#include -#ifdef _WIN32 typedef SSIZE_T ssize_t; -#else +#define metee_gsc_print(...) printf(__VA_ARGS__) +#define metee_gsc_err(...) fprintf(stderr, __VA_ARGS__) +#define metee_gsc_malloc(size) malloc(size) +#define metee_gsc_free(ptr) free(ptr) +#define metee_gsc_sleep(ms) Sleep(ms) +static void mk_host_if_log(bool is_error, const char* data) { + fprintf((is_error) ? stderr : stdout, "LIB: %s", data); +} +#elif __linux__ +#include +#include +#include +#include #include -#endif /* _WIN32 */ + +#define metee_gsc_print(...) printf(__VA_ARGS__) +#define metee_gsc_err(...) fprintf(stderr, __VA_ARGS__) +#define metee_gsc_malloc(size) malloc(size) +#define metee_gsc_free(ptr) free(ptr) +#define metee_gsc_sleep(ms) usleep((ms)*1000) +static void mk_host_if_log(bool is_error, const char* data) { + fprintf((is_error) ? stderr : stdout, "LIB: %s", data); +} +#elif defined(EFI) +#include +#include +#include +#include +#include +#include +#include + +#define metee_gsc_print(...) AsciiPrint(__VA_ARGS__) +#define metee_gsc_err(...) AsciiPrint(__VA_ARGS__) +#define metee_gsc_malloc(size) AllocatePool(size) +#define metee_gsc_free(ptr) FreePool(ptr) +#define metee_gsc_sleep(ms) gBS->Stall((ms)*1000) +#define strlen AsciiStrLen +#define memcpy CopyMem +#define strcmp AsciiStrCmp +static inline void* memset(void *s, int c, size_t n) { + return SetMem(s, n, (UINT8)c); +} +static void mk_host_if_log(bool is_error, const char* data) { + AsciiPrint("LIB: %a\n", data); +} +#endif #pragma pack(1) struct gsc_fwu_heci_header { @@ -86,7 +128,7 @@ const char *mkhi_status(uint32_t status) MKH_STATUS(GSC_FWU_STATUS_INVALID_PARAMS); MKH_STATUS(GSC_FWU_STATUS_FAILURE); default: - fprintf(stderr, "unknown 0x%08X\n", status); + metee_gsc_err("unknown 0x%08X\n", status); return "unknown"; } #undef MKH_STATUS @@ -112,7 +154,7 @@ static void mk_host_if_fw_status(struct mk_host_if *acmd) for (uint32_t i = 0; i < 6; i++) if (TeeFWStatus(&acmd->mei_cl, i, &fwStatus) == TEE_SUCCESS) - printf("FW Status[%u] = 0x%08X\n", i, fwStatus); + metee_gsc_print("FW Status[%u] = 0x%08X\n", i, fwStatus); } static bool mk_host_if_is_gscfi(struct mk_host_if *acmd) @@ -132,11 +174,6 @@ static bool mk_host_if_connect(struct mk_host_if *acmd) return acmd->initialized; } -static void mk_host_if_log(bool is_error, const char* data) -{ - fprintf((is_error) ? stderr : stdout, "LIB: %s", data); -} - static bool mk_host_if_init(struct mk_host_if *acmd, const GUID *guid, bool reconnect, bool verbose) { @@ -148,6 +185,17 @@ static bool mk_host_if_init(struct mk_host_if *acmd, const GUID *guid, .data.guid = &GUID_DEVINTERFACE_HECI_GSC_CHILD } }; +#elif defined(EFI) +#define ADDR_NUM 1 + struct tee_device_address addr[ADDR_NUM] = { + { + .type = TEE_DEVICE_TYPE_BDF, + .data.bdf = { + .value = { 0x0, 0x15, 0x0, 0x0}, + .hw_type = HECI_HW_TYPE_GFX_CSC + } + } + }; #else #define ADDR_NUM 3 struct tee_device_address addr[ADDR_NUM] = { @@ -193,12 +241,12 @@ static uint32_t mkhi_verify_response_header(struct gsc_fwu_heci_header *msg, str { bool match = true; if (msg->command_id != resp->command_id) { - printf("Mismatch Command; Req-Command = %d , Resp-Command = %d\n", + metee_gsc_print("Mismatch Command; Req-Command = %d , Resp-Command = %d\n", msg->command_id, resp->command_id); match = false; } if (resp->is_response != 1) { - printf("Wrong IsResponse; Resp-IsResponse = %d\n", resp->is_response); + metee_gsc_print("Wrong IsResponse; Resp-IsResponse = %d\n", resp->is_response); match = false; } @@ -206,7 +254,7 @@ static uint32_t mkhi_verify_response_header(struct gsc_fwu_heci_header *msg, str } static uint32_t mk_host_if_call(struct mk_host_if *acmd, - const unsigned char *command, ssize_t command_sz, + const unsigned char *command, size_t command_sz, uint8_t **read_buf, uint32_t rcmd, unsigned int expected_sz) { @@ -214,6 +262,7 @@ static uint32_t mk_host_if_call(struct mk_host_if *acmd, size_t out_buf_sz; size_t written; TEESTATUS status; + uint32_t ret; struct gsc_fwu_heci_response *msg_hdr; int count = 0; @@ -221,13 +270,13 @@ static uint32_t mk_host_if_call(struct mk_host_if *acmd, if (in_buf_sz == 0) { if (acmd->verbose) - fprintf(stderr, "mkhif: client reproted zero MTU.\n"); + metee_gsc_err("mkhif: client reproted zero MTU.\n"); return GSC_FWU_STATUS_FAILURE; } - *read_buf = (uint8_t *)malloc(in_buf_sz); + *read_buf = (uint8_t *)metee_gsc_malloc(in_buf_sz); if (*read_buf == NULL) return GSC_FWU_STATUS_FAILURE; - memset(*read_buf, 0, in_buf_sz); + memset((void*)*read_buf, 0, in_buf_sz); msg_hdr = (struct gsc_fwu_heci_response *)*read_buf; while (count++ < 2) { @@ -244,13 +293,12 @@ static uint32_t mk_host_if_call(struct mk_host_if *acmd, if (status) return GSC_FWU_STATUS_FAILURE; - status = msg_hdr->status; if (acmd->verbose) - fprintf(stderr, "mkhif: message header read status = %d\n", status); + metee_gsc_err("mkhif: message header read status = %u\n", msg_hdr->status); - status = mkhi_verify_response_header((struct gsc_fwu_heci_header *)command, &msg_hdr->header); - if (status != GSC_FWU_STATUS_SUCCESS) - return status; + ret = mkhi_verify_response_header((struct gsc_fwu_heci_header *)command, &msg_hdr->header); + if (ret != GSC_FWU_STATUS_SUCCESS) + return ret; if (expected_sz && expected_sz != out_buf_sz) { return GSC_FWU_STATUS_FAILURE; @@ -260,7 +308,7 @@ static uint32_t mk_host_if_call(struct mk_host_if *acmd, static void printf_if_fw_version(struct gsc_fwu_external_version *version) { - printf("Firmware Version %c%c%c%c.%d.%d\n", + metee_gsc_print("Firmware Version %c%c%c%c.%d.%d\n", version->project[0], version->project[1], version->project[2], version->project[3], version->hotfix, version->build); @@ -333,12 +381,12 @@ static uint32_t mk_host_if_fw_version_resp(struct mk_host_if *acmd) static void usage(const char *p) { - fprintf(stderr, "Usage: %s [-hv] [-e ] [-i ] [-b M.m.f.b] [-r] [-s ] [-k ]\n", p); - fprintf(stderr, " -h help\n"); - fprintf(stderr, " -v verbose\n"); - fprintf(stderr, " -i iterate n times\n"); - fprintf(stderr, " -r reconnect if failed to write\n"); - fprintf(stderr, " -k timeout between iterations in microseconds (default: 0)\n"); + metee_gsc_err("Usage: %s [-hv] [-e ] [-i ] [-b M.m.f.b] [-r] [-s ] [-k ]\n", p); + metee_gsc_err(" -h help\n"); + metee_gsc_err(" -v verbose\n"); + metee_gsc_err(" -i iterate n times\n"); + metee_gsc_err(" -r reconnect if failed to write\n"); + metee_gsc_err(" -k timeout between iterations in microseconds (default: 0)\n"); } int main(int argc, char *argv[]) @@ -358,11 +406,10 @@ int main(int argc, char *argv[]) bool reconnect = false; unsigned long iter_timeout = 0; -#ifdef _WIN32 +#if defined(_WIN32) || defined(EFI) verbose = true; reconnect = true; #else - size_t echo_size; extern char *optarg; int opt; @@ -419,14 +466,10 @@ int main(int argc, char *argv[]) for (i = 0; i < iterations ; i++) { if (iter_timeout && i > 0) { - printf("Sleeping for %lu microseconds ...\n", iter_timeout); -#ifdef _WIN32 - Sleep(iter_timeout/1000); -#else - usleep(iter_timeout); -#endif /* _WIN32_ */ + metee_gsc_print("Sleeping for %lu microseconds ...\n", iter_timeout); + metee_gsc_sleep(iter_timeout / 1000); } - printf("Running version test %d...\n", i); + metee_gsc_print("Running version test %d...\n", i); memset(&version, 0, sizeof(version)); ret = mk_host_if_fw_version(&acmd, &version); if (ret != GSC_FWU_STATUS_SUCCESS) @@ -440,6 +483,6 @@ int main(int argc, char *argv[]) out: mk_host_if_deinit(&acmd); - printf("STATUS %s\n", mkhi_status(ret)); + metee_gsc_print("STATUS %s\n", mkhi_status(ret)); return ret; } diff --git a/src/uefi/MeTeeLibrary.inf.in b/src/uefi/MeTeeLibrary.inf.in new file mode 100644 index 0000000..01062ad --- /dev/null +++ b/src/uefi/MeTeeLibrary.inf.in @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2024-2026 Intel Corporation +# +[Defines] + INF_VERSION = 1.25 + BASE_NAME = MeTeeLib + FILE_GUID = 5AC8222F-1511-4642-8484-5B20FF7F60DB + MODULE_TYPE = BASE + VERSION_STRING = @TEE_VERSION_STRING@ + LIBRARY_CLASS = MeTeeLib + +[Sources] +@TEE_HEADERS_MULTILINE@ + +@TEE_SOURCES_MULTILINE@ + +[BuildOptions] + MSFT:*_*_*_CC_FLAGS = /GS- /D EFI /U _WIN32 /W4 /WX + GCC:*_*_*_CC_FLAGS = -D EFI + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + StdLib/StdLib.dec + +[LibraryClasses] + UefiLib + MemoryAllocationLib + UefiBootServicesTableLib + BaseMemoryLib + IoLib + PciSegmentLib + +[Protocols] + gEfiPciRootBridgeIoProtocolGuid diff --git a/src/uefi/metee_efi.c b/src/uefi/metee_efi.c index 93eb7b7..572ce90 100644 --- a/src/uefi/metee_efi.c +++ b/src/uefi/metee_efi.c @@ -41,6 +41,8 @@ static inline struct METEE_EFI_IMPL *to_int(PTEEHANDLE _h) #define GSC_FSTS_OFF 0xC00 +#define PCI_CTL_OFF 0x84 + static struct HECI_HW HwInfoPch( IN UINT32 segment, @@ -64,6 +66,7 @@ HwInfoPch( {FW_STS1, FW_STS2, FW_STS3, FW_STS4, FW_STS5, FW_STS6}, TRUE}, ME_TRC, + 0x0 }; return hw_info; } @@ -92,10 +95,40 @@ HwInfoGfxGsc( GSC_FSTS_OFF | FW_STS4, GSC_FSTS_OFF | FW_STS5, GSC_FSTS_OFF | FW_STS6}, FALSE}, 0x0, + 0x0 }; return hw_info; } +static struct HECI_HW +HwInfoGfxCsc( + IN UINT32 segment, + IN UINT32 bus, + IN UINT32 device, + IN UINT32 function) +{ +#define GFX_CSC_BASE_ADDRESS_OFFSET_HECI2 0x1000 + struct HECI_HW hw_info = + { + {/* Bdf */ + segment, + bus, + device, + function}, + {/* RegisterOffset */ + DEFAULT_OFFSET_H_CB_WW, DEFAULT_OFFSET_H_CSR, + DEFAULT_OFFSET_ME_CB_RW, DEFAULT_OFFSET_ME_CSR_HA, + GFX_CSC_BASE_ADDRESS_OFFSET_HECI2}, + {/* FwStatus */ + {GSC_FSTS_OFF | FW_STS1, GSC_FSTS_OFF | FW_STS2, GSC_FSTS_OFF | FW_STS3, + GSC_FSTS_OFF | FW_STS4, GSC_FSTS_OFF | FW_STS5, GSC_FSTS_OFF | FW_STS6}, + FALSE}, + 0x0, + PCI_CTL_OFF + }; + return hw_info; +} + void CallbackPrintHelper(IN PTEEHANDLE handle, bool is_error, const char* args, ...) { char msg[DEBUG_MSG_LEN + 1]; @@ -129,6 +162,11 @@ SetHwInfo( device->data.bdf.value.device, device->data.bdf.value.function); DBGPRINT(Handle->TeeHandle, "******** HECI_HW_TYPE_GFX_GSC\n"); break; + case HECI_HW_TYPE_GFX_CSC: + Handle->Hw = HwInfoGfxCsc(device->data.bdf.value.segment, device->data.bdf.value.bus, + device->data.bdf.value.device, device->data.bdf.value.function); + DBGPRINT(Handle->TeeHandle, "******** HECI_HW_TYPE_GFX_CSC\n"); + break; default: DBGPRINT(Handle->TeeHandle, "******** Unsupported device kind %d\n", device->data.bdf.hw_type); status = TEE_INVALID_PARAMETER; diff --git a/src/uefi/metee_efi.h b/src/uefi/metee_efi.h index 2f3d90c..24b7179 100644 --- a/src/uefi/metee_efi.h +++ b/src/uefi/metee_efi.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright (C) 2024 Intel Corporation + * Copyright (C) 2024-2026 Intel Corporation */ #ifndef METEE_EFI_H_ #define METEE_EFI_H_ @@ -178,6 +178,7 @@ struct HECI_HW { BOOLEAN ResidesInConfigSpace; /** FW status resides in config space or MMIO */ } FwStatus; UINT32 TrcOffset; /** TRC register offset */ + UINT32 D3WakeOffset; /** D3Wake register offset, if 0 - D3Wake is not supported */ }; struct _TEEHANDLE; diff --git a/src/uefi/pci_utils.c b/src/uefi/pci_utils.c index 6dd4281..e64de3c 100644 --- a/src/uefi/pci_utils.c +++ b/src/uefi/pci_utils.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright (C) 2024 Intel Corporation + * Copyright (C) 2024-2026 Intel Corporation */ #include @@ -52,6 +52,7 @@ CheckAndFixHeciForAccess( UINT64 MemBar; UINT32 LowPart; UINT64 HiPart; + UINT32 reg; #define B_PCI_BAR_MEMORY_TYPE_64 0x4 @@ -89,5 +90,12 @@ CheckAndFixHeciForAccess( /// PciSegmentOr8(HeciBaseAddress + PCI_COMMAND_OFFSET, EFI_PCI_COMMAND_MEMORY_SPACE); + if (Handle->Hw.D3WakeOffset != 0) + { /* Wake from D3 */ + reg = PciSegmentRead32(HeciBaseAddress + Handle->Hw.D3WakeOffset); + reg &= ~0x3u; + PciSegmentWrite32(HeciBaseAddress + Handle->Hw.D3WakeOffset, reg); + } + return MemBar + Handle->Hw.RegisterOffset.BaseAddressOffset; } diff --git a/uefi.cmake b/uefi.cmake new file mode 100644 index 0000000..10d84b5 --- /dev/null +++ b/uefi.cmake @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (C) 2026 Intel Corporation + +set(TEE_SOURCES + ${PROJECT_SOURCE_DIR}/src/uefi/heci_efi.c + ${PROJECT_SOURCE_DIR}/src/uefi/metee_efi.c + ${PROJECT_SOURCE_DIR}/src/uefi/pci_utils.c + ${PROJECT_SOURCE_DIR}/src/uefi/heci_core.c +) + +set(TEE_HEADERS + ${PROJECT_SOURCE_DIR}/include/helpers.h + ${PROJECT_SOURCE_DIR}/include/metee.h + ${PROJECT_SOURCE_DIR}/src/uefi/heci_efi.h + ${PROJECT_SOURCE_DIR}/src/uefi/metee_efi.h + ${PROJECT_SOURCE_DIR}/src/uefi/pci_utils.h + ${PROJECT_SOURCE_DIR}/src/uefi/heci_core.h +) + +add_library(${PROJECT_NAME} ${TEE_SOURCES}) + +string(REPLACE ";" "\n" TEE_SOURCES_MULTILINE "${TEE_SOURCES}") +string(REPLACE ";" "\n" TEE_HEADERS_MULTILINE "${TEE_HEADERS}") + +if(BUILD_SAMPLES) + set(METEE_SAMPLES_LIST + MeTeePkg/Samples/MeTeeBasic/MeTeeBasic.inf + MeTeePkg/Samples/MeTeeGsc/MeTeeGsc.inf + ) + + string(REPLACE ";" "\n" METEE_SAMPLES "${METEE_SAMPLES_LIST}") +endif(BUILD_SAMPLES) + +configure_file ( + "${PROJECT_SOURCE_DIR}/src/uefi/MeTeeLibrary.inf.in" + "${PROJECT_BINARY_DIR}/MeTeePkg/MeTeeLibrary/MeTeeLibrary.inf" +) + +file(RELATIVE_PATH METEE_INCLUDE_RELPATH "${PROJECT_BINARY_DIR}/MeTeePkg" "${PROJECT_SOURCE_DIR}/include" ) + +configure_file ( + "${PROJECT_SOURCE_DIR}/MeTeePkg/MeTeePkg.dec.in" + "${PROJECT_BINARY_DIR}/MeTeePkg/MeTeePkg.dec" +) + +configure_file ( + "${PROJECT_SOURCE_DIR}/MeTeePkg/MeTeePkg.dsc.in" + "${PROJECT_BINARY_DIR}/MeTeePkg/MeTeePkg.dsc" +) \ No newline at end of file