diff options
Diffstat (limited to 'src/detection/tpm')
| -rw-r--r-- | src/detection/tpm/tpm.h | 11 | ||||
| -rw-r--r-- | src/detection/tpm/tpm_apple.c | 30 | ||||
| -rw-r--r-- | src/detection/tpm/tpm_bsd.c | 22 | ||||
| -rw-r--r-- | src/detection/tpm/tpm_linux.c | 24 | ||||
| -rw-r--r-- | src/detection/tpm/tpm_nosupport.c | 5 | ||||
| -rw-r--r-- | src/detection/tpm/tpm_windows.c | 51 |
6 files changed, 143 insertions, 0 deletions
diff --git a/src/detection/tpm/tpm.h b/src/detection/tpm/tpm.h new file mode 100644 index 0000000..da842ec --- /dev/null +++ b/src/detection/tpm/tpm.h @@ -0,0 +1,11 @@ +#pragma once + +#include "fastfetch.h" +#include "modules/tpm/option.h" + +typedef struct FFTPMResult { + FFstrbuf version; + FFstrbuf description; +} FFTPMResult; + +const char* ffDetectTPM(FFTPMResult* result); diff --git a/src/detection/tpm/tpm_apple.c b/src/detection/tpm/tpm_apple.c new file mode 100644 index 0000000..250dd01 --- /dev/null +++ b/src/detection/tpm/tpm_apple.c @@ -0,0 +1,30 @@ +#include "tpm.h" + +#ifndef __aarch64__ + #include "common/apple/cf_helpers.h" + #include <IOKit/IOKitLib.h> +#endif + +const char* ffDetectTPM(FFTPMResult* result) { +#ifdef __aarch64__ + + ffStrbufSetStatic(&result->version, "2.0"); + ffStrbufSetStatic(&result->description, "Apple Silicon Security"); + return NULL; + +#else + + FF_IOOBJECT_AUTO_RELEASE io_service_t t2Service = IOServiceGetMatchingService( + MACH_PORT_NULL, + IOServiceMatching("AppleT2")); + + if (t2Service) { + ffStrbufSetStatic(&result->version, "2.0"); + ffStrbufSetStatic(&result->description, "Apple T2 Security Chip"); + return NULL; + } + +#endif + + return "No Apple Security hardware detected"; +} diff --git a/src/detection/tpm/tpm_bsd.c b/src/detection/tpm/tpm_bsd.c new file mode 100644 index 0000000..4b73413 --- /dev/null +++ b/src/detection/tpm/tpm_bsd.c @@ -0,0 +1,22 @@ +#include "tpm.h" +#include "common/sysctl.h" +#include "common/kmod.h" + +const char* ffDetectTPM(FFTPMResult* result) { + if (ffSysctlGetString("dev.tpmcrb.0.%desc", &result->description) != NULL) { + if (!ffKmodLoaded("tpm")) { + return "`tpm` kernel module is not loaded"; + } + return "TPM device is not found"; + } + + if (ffStrbufContainS(&result->description, "2.0")) { + ffStrbufSetStatic(&result->version, "2.0"); + } else if (ffStrbufContainS(&result->description, "1.2")) { + ffStrbufSetStatic(&result->version, "1.2"); + } else { + ffStrbufSetStatic(&result->version, "unknown"); + } + + return NULL; +} diff --git a/src/detection/tpm/tpm_linux.c b/src/detection/tpm/tpm_linux.c new file mode 100644 index 0000000..040da38 --- /dev/null +++ b/src/detection/tpm/tpm_linux.c @@ -0,0 +1,24 @@ +#include "tpm.h" +#include "common/io.h" + +const char* ffDetectTPM(FFTPMResult* result) { + if (!ffPathExists("/sys/class/tpm/tpm0/", FF_PATHTYPE_DIRECTORY)) { + if (!ffPathExists("/sys/class/tpm/", FF_PATHTYPE_DIRECTORY)) { + return "TPM is not supported by kernel"; + } + return "TPM device is not found"; + } + + if (ffReadFileBuffer("/sys/class/tpm/tpm0/tpm_version_major", &result->version)) { + ffStrbufTrimRightSpace(&result->version); + if (ffStrbufEqualS(&result->version, "2")) { + ffStrbufSetStatic(&result->version, "2.0"); + } + } + + if (ffReadFileBuffer("/sys/class/tpm/tpm0/device/description", &result->description)) { + ffStrbufTrimRightSpace(&result->description); + } + + return NULL; +} diff --git a/src/detection/tpm/tpm_nosupport.c b/src/detection/tpm/tpm_nosupport.c new file mode 100644 index 0000000..492f37f --- /dev/null +++ b/src/detection/tpm/tpm_nosupport.c @@ -0,0 +1,5 @@ +#include "tpm.h" + +const char* ffDetectTPM(FF_A_UNUSED FFTPMResult* result) { + return "Not supported on this platform"; +} diff --git a/src/detection/tpm/tpm_windows.c b/src/detection/tpm/tpm_windows.c new file mode 100644 index 0000000..14f6e8a --- /dev/null +++ b/src/detection/tpm/tpm_windows.c @@ -0,0 +1,51 @@ +#include "tpm.h" +#include "common/library.h" + +#include <windef.h> +#include <tbs.h> +#include <winerror.h> + +const char* ffDetectTPM(FFTPMResult* result) { + FF_LIBRARY_LOAD_MESSAGE(tbs, "TBS" FF_LIBRARY_EXTENSION, -1) + FF_LIBRARY_LOAD_SYMBOL_MESSAGE(tbs, Tbsi_GetDeviceInfo) + + TPM_DEVICE_INFO deviceInfo = {}; + TBS_RESULT code = ffTbsi_GetDeviceInfo(sizeof(deviceInfo), &deviceInfo); + if (code != TBS_SUCCESS) { + return code == (TBS_RESULT) TBS_E_TPM_NOT_FOUND ? "TPM device is not found" : "Tbsi_GetDeviceInfo() failed"; + } + + switch (deviceInfo.tpmVersion) { + case TPM_VERSION_12: + ffStrbufSetStatic(&result->version, "1.2"); + break; + case TPM_VERSION_20: + ffStrbufSetStatic(&result->version, "2.0"); + break; + default: + ffStrbufSetStatic(&result->version, "unknown"); + break; + } + + switch (deviceInfo.tpmInterfaceType) { + case TPM_IFTYPE_1: + ffStrbufSetF(&result->description, "I/O-port or MMIO TPM %s", result->version.chars); + break; + case TPM_IFTYPE_TRUSTZONE: + ffStrbufSetF(&result->description, "Trustzone TPM %s", result->version.chars); + break; + case TPM_IFTYPE_HW: + ffStrbufSetF(&result->description, "HW TPM %s", result->version.chars); + break; + case TPM_IFTYPE_EMULATOR: + ffStrbufSetF(&result->description, "SW-emulator TPM %s", result->version.chars); + break; + case TPM_IFTYPE_SPB: + ffStrbufSetF(&result->description, "SPB attached TPM %s", result->version.chars); + break; + default: + break; + } + + return NULL; +} |