-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
284 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include "fastfetch.h" | ||
|
||
typedef struct FFTPMResult | ||
{ | ||
FFstrbuf version; | ||
FFstrbuf interfaceType; | ||
} FFTPMResult; | ||
|
||
const char* ffDetectTPM(FFTPMResult* result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "tpm.h" | ||
#include "common/io/io.h" | ||
|
||
const char* ffDetectTPM(FF_MAYBE_UNUSED FFTPMResult* result) | ||
{ | ||
if (ffReadFileBuffer("/sys/class/tpm/tpm0/tpm_version_major", &result->version)) | ||
{ | ||
ffStrbufTrimRightSpace(&result->version); | ||
if (ffStrbufEqualS(&result->version, "2")) | ||
ffStrbufSetStatic(&result->version, "2.0"); | ||
|
||
return NULL; | ||
} | ||
|
||
if (ffPathExists("/sys/class/tpm/tpm0", FF_PATHTYPE_DIRECTORY)) | ||
{ | ||
ffStrbufSetStatic(&result->version, "unknown"); | ||
return NULL; | ||
} | ||
|
||
return "TPM device is not found"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "tpm.h" | ||
|
||
const char* ffDetectTPM(FF_MAYBE_UNUSED FFTPMResult* result) | ||
{ | ||
return "Not supported on this platform"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "tpm.h" | ||
#include "common/library.h" | ||
|
||
// #include <tbs.h> | ||
|
||
#define TBS_SUCCESS 0u | ||
#define TBS_E_INTERNAL_ERROR 0x80284001u | ||
#define TBS_E_INVALID_CONTEXT 0x80284004u | ||
typedef UINT32 TBS_RESULT; | ||
|
||
#define TPM_VERSION_UNKNOWN 0 | ||
#define TPM_VERSION_12 1 | ||
#define TPM_VERSION_20 2 | ||
|
||
#define TPM_IFTYPE_UNKNOWN 0 | ||
#define TPM_IFTYPE_1 1 // for 1.2 - use I/O-port or MMIO | ||
#define TPM_IFTYPE_TRUSTZONE 2 // 2.0: Trustzone | ||
#define TPM_IFTYPE_HW 3 // 2.0: HW TPM | ||
#define TPM_IFTYPE_EMULATOR 4 // 2.0: SW-emulator | ||
#define TPM_IFTYPE_SPB 5 // 2.0: SPB attached | ||
|
||
typedef struct _TPM_DEVICE_INFO | ||
{ | ||
UINT32 structVersion; // = 1 for now | ||
UINT32 tpmVersion; // 1.2 / 2.0 | ||
UINT32 tpmInterfaceType; // HW, simulator, ... | ||
UINT32 tpmImpRevision; // code-drop revision, | ||
// implenmentation-specific | ||
} TPM_DEVICE_INFO, *PTPM_DEVICE_INFO; | ||
typedef const TPM_DEVICE_INFO *PCTPM_DEVICE_INFO; | ||
|
||
TBS_RESULT WINAPI | ||
Tbsi_GetDeviceInfo( | ||
_In_ UINT32 Size, | ||
_Out_writes_bytes_(Size) PVOID Info); | ||
|
||
const char* ffDetectTPM(FFTPMResult* result) | ||
{ | ||
FF_LIBRARY_LOAD(tbs, "dlopen TBS" FF_LIBRARY_EXTENSION " failed", "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_INVALID_CONTEXT ? "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: | ||
ffStrbufSetStatic(&result->interfaceType, "I/O-port or MMIO"); | ||
break; | ||
case TPM_IFTYPE_TRUSTZONE: | ||
ffStrbufSetStatic(&result->interfaceType, "Trustzone"); | ||
break; | ||
case TPM_IFTYPE_HW: | ||
ffStrbufSetStatic(&result->interfaceType, "HW TPM"); | ||
break; | ||
case TPM_IFTYPE_EMULATOR: | ||
ffStrbufSetStatic(&result->interfaceType, "SW-emulator"); | ||
break; | ||
case TPM_IFTYPE_SPB: | ||
ffStrbufSetStatic(&result->interfaceType, "SPB attached"); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here | ||
|
||
#include "common/option.h" | ||
|
||
typedef struct FFTPMOptions | ||
{ | ||
FFModuleBaseInfo moduleInfo; | ||
FFModuleArgs moduleArgs; | ||
} FFTPMOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#include "common/printing.h" | ||
#include "common/jsonconfig.h" | ||
#include "detection/tpm/tpm.h" | ||
#include "modules/tpm/tpm.h" | ||
#include "util/stringUtils.h" | ||
|
||
#define FF_TPM_NUM_FORMAT_ARGS 2 | ||
|
||
void ffPrintTPM(FFTPMOptions* options) | ||
{ | ||
FFTPMResult result = { | ||
.version = ffStrbufCreate(), | ||
.interfaceType = ffStrbufCreate() | ||
}; | ||
const char* error = ffDetectTPM(&result); | ||
|
||
if(error) | ||
{ | ||
ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); | ||
return; | ||
} | ||
|
||
if(options->moduleArgs.outputFormat.length == 0) | ||
{ | ||
ffPrintLogoAndKey(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); | ||
ffStrbufPutTo(&result.version, stdout); | ||
} | ||
else | ||
{ | ||
FF_PRINT_FORMAT_CHECKED(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_TPM_NUM_FORMAT_ARGS, ((FFformatarg[]){ | ||
FF_FORMAT_ARG(result.version, "version"), | ||
FF_FORMAT_ARG(result.interfaceType, "interface-type"), | ||
})); | ||
} | ||
|
||
ffStrbufDestroy(&result.version); | ||
ffStrbufDestroy(&result.interfaceType); | ||
} | ||
|
||
bool ffParseTPMCommandOptions(FFTPMOptions* options, const char* key, const char* value) | ||
{ | ||
const char* subKey = ffOptionTestPrefix(key, FF_TPM_MODULE_NAME); | ||
if (!subKey) return false; | ||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs)) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
void ffParseTPMJsonObject(FFTPMOptions* options, yyjson_val* module) | ||
{ | ||
yyjson_val *key_, *val; | ||
size_t idx, max; | ||
yyjson_obj_foreach(module, idx, max, key_, val) | ||
{ | ||
const char* key = yyjson_get_str(key_); | ||
if(ffStrEqualsIgnCase(key, "type")) | ||
continue; | ||
|
||
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) | ||
continue; | ||
|
||
ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key); | ||
} | ||
} | ||
|
||
void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) | ||
{ | ||
__attribute__((__cleanup__(ffDestroyTPMOptions))) FFTPMOptions defaultOptions; | ||
ffInitTPMOptions(&defaultOptions); | ||
|
||
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs); | ||
} | ||
|
||
void ffGenerateTPMJsonResult(FF_MAYBE_UNUSED FFTerminalOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) | ||
{ | ||
FFTPMResult result = { | ||
.version = ffStrbufCreate(), | ||
.interfaceType = ffStrbufCreate() | ||
}; | ||
const char* error = ffDetectTPM(&result); | ||
|
||
if(error) | ||
{ | ||
yyjson_mut_obj_add_str(doc, module, "error", error); | ||
return; | ||
} | ||
|
||
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); | ||
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); | ||
yyjson_mut_obj_add_strbuf(doc, obj, "interfaceType", &result.interfaceType); | ||
|
||
ffStrbufDestroy(&result.version); | ||
ffStrbufDestroy(&result.interfaceType); | ||
} | ||
|
||
void ffPrintTPMHelpFormat(void) | ||
{ | ||
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_TPM_MODULE_NAME, "{1} [{2}]", FF_TPM_NUM_FORMAT_ARGS, ((const char* []) { | ||
"TPM device version - version", | ||
"TPM interface type - interface-type", | ||
})); | ||
} | ||
|
||
void ffInitTPMOptions(FFTPMOptions* options) | ||
{ | ||
ffOptionInitModuleBaseInfo( | ||
&options->moduleInfo, | ||
FF_TPM_MODULE_NAME, | ||
"Print info of Trusted Platform Module (TPM) Security Device", | ||
ffParseTPMCommandOptions, | ||
ffParseTPMJsonObject, | ||
ffPrintTPM, | ||
ffGenerateTPMJsonResult, | ||
ffPrintTPMHelpFormat, | ||
ffGenerateTPMJsonConfig | ||
); | ||
ffOptionInitModuleArg(&options->moduleArgs, ""); | ||
} | ||
|
||
void ffDestroyTPMOptions(FFTPMOptions* options) | ||
{ | ||
ffOptionDestroyModuleArg(&options->moduleArgs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "fastfetch.h" | ||
|
||
#define FF_TPM_MODULE_NAME "TPM" | ||
|
||
void ffPrintTPM(FFTPMOptions* options); | ||
void ffInitTPMOptions(FFTPMOptions* options); | ||
void ffDestroyTPMOptions(FFTPMOptions* options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.