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(),
|
2026-03-30 10:17:45 +08:00
|
|
|
.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
|
|
|
ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2024-09-25 22:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2024-09-25 22:55:57 +08:00
|
|
|
ffPrintLogoAndKey(FF_TPM_MODULE_NAME, 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 {
|
|
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
|
|
|
|
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);
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
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) {
|
2025-08-02 18:51:03 +08:00
|
|
|
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
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2024-09-25 22:55:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2024-09-25 22:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 16:20:59 +08:00
|
|
|
bool ffGenerateTPMJsonResult(FF_A_UNUSED FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2024-09-25 22:55:57 +08:00
|
|
|
FFTPMResult result = {
|
|
|
|
|
.version = ffStrbufCreate(),
|
2026-03-30 10:17:45 +08:00
|
|
|
.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);
|
2025-08-18 13:38:30 +08:00
|
|
|
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);
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2024-09-25 22:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitTPMOptions(FFTPMOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyTPMOptions(FFTPMOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffTPMModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_TPM_MODULE_NAME,
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "Print information about the Trusted Platform Module (TPM) security device",
|
2025-08-03 18:39:41 +08:00
|
|
|
.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-03-30 10:17:45 +08:00
|
|
|
}))
|
|
|
|
|
};
|