Files
fastfetch/src/modules/tpm/tpm.c
T

124 lines
4.1 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2024-09-25 22:55:57 +08:00
#include "detection/tpm/tpm.h"
#include "modules/tpm/tpm.h"
2026-03-29 09:28:49 +08:00
bool ffPrintTPM(FFTPMOptions* options) {
2024-09-25 22:55:57 +08:00
FFTPMResult result = {
.version = ffStrbufCreate(),
.description = ffStrbufCreate()
};
2024-09-25 22:55:57 +08:00
const char* error = ffDetectTPM(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2024-09-25 22:55:57 +08:00
}
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
2026-08-14 23:33:54 +08:00
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2026-03-29 09:28:49 +08:00
if (result.description.length > 0) {
2024-09-26 09:53:00 +08:00
ffStrbufPutTo(&result.description, stdout);
2026-03-29 09:28:49 +08:00
} else {
2024-09-26 09:53:00 +08:00
ffStrbufPutTo(&result.version, stdout);
2026-03-29 09:28:49 +08:00
}
} else {
2026-08-14 23:33:54 +08:00
FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result.version, "version"),
FF_ARG(result.description, "description"),
}));
2024-09-25 22:55:57 +08:00
}
ffStrbufDestroy(&result.version);
2024-09-26 09:53:00 +08:00
ffStrbufDestroy(&result.description);
return true;
2024-09-25 22:55:57 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseTPMJsonObject(FFTPMOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2024-09-25 22:55:57 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2024-09-25 22:55:57 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-09-25 22:55:57 +08:00
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-09-25 22:55:57 +08:00
}
}
[[gnu::cold]]
2026-03-29 09:28:49 +08:00
void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-09-25 22:55:57 +08:00
}
bool ffGenerateTPMJsonResult([[maybe_unused]] FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2024-09-25 22:55:57 +08:00
FFTPMResult result = {
.version = ffStrbufCreate(),
.description = ffStrbufCreate()
};
2024-09-25 22:55:57 +08:00
const char* error = ffDetectTPM(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2024-09-25 22:55:57 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2024-09-25 22:55:57 +08:00
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
2024-09-26 09:53:00 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "description", &result.description);
2024-09-25 22:55:57 +08:00
ffStrbufDestroy(&result.version);
2024-09-26 09:53:00 +08:00
ffStrbufDestroy(&result.description);
return true;
2024-09-25 22:55:57 +08:00
}
2026-03-29 09:28:49 +08:00
void ffInitTPMOptions(FFTPMOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
}
2026-03-29 09:28:49 +08:00
void ffDestroyTPMOptions(FFTPMOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffTPMModuleInfo = {
2026-08-14 23:33:54 +08:00
.name = "TPM",
2026-04-16 18:27:11 +08:00
.description = "Print information about the Trusted Platform Module (TPM) security device",
2026-08-14 20:38:58 +08:00
.displayName = {
.en = "TPM",
2026-08-16 10:51:42 +08:00
.ar = "TPM",
2026-08-21 10:19:06 +08:00
.cs = "TPM",
2026-08-14 20:38:58 +08:00
.de = "TPM",
.es = "TPM",
2026-10-03 13:35:56 +03:00
.fi = "TPM",
2026-08-14 20:38:58 +08:00
.fr = "TPM",
2026-08-20 16:25:32 +00:00
.gl = "TPM",
2026-08-16 10:51:42 +08:00
.he = "TPM",
2026-08-21 10:19:06 +08:00
.id = "TPM",
2026-08-14 20:38:58 +08:00
.it = "TPM",
.ja = "TPM",
.ko = "TPM",
.pl = "TPM",
2026-08-16 10:37:06 +08:00
.pt = "TPM",
2026-08-14 20:38:58 +08:00
.ru = "TPM",
2026-08-21 10:19:06 +08:00
.tr = "TPM",
.uk = "TPM",
.vi = "TPM",
2026-08-14 20:38:58 +08:00
.zh_CN = "TPM",
.zh_TW = "TPM",
},
.initOptions = (void*) ffInitTPMOptions,
.destroyOptions = (void*) ffDestroyTPMOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseTPMJsonObject,
.printModule = (void*) ffPrintTPM,
.generateJsonResult = (void*) ffGenerateTPMJsonResult,
.generateJsonConfig = (void*) ffGenerateTPMJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "TPM device version", "version" },
{ "TPM general description", "description" },
})),
2026-09-22 14:49:43 +08:00
.defaultOrder = 70,
};