Skip to content

Commit f496e6d

Browse files
committed
TPM: add new module
1 parent 96d885e commit f496e6d

File tree

14 files changed

+284
-1
lines changed

14 files changed

+284
-1
lines changed

CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ set(LIBFASTFETCH_SRC
405405
src/modules/terminalsize/terminalsize.c
406406
src/modules/theme/theme.c
407407
src/modules/title/title.c
408+
src/modules/tpm/tpm.c
408409
src/modules/uptime/uptime.c
409410
src/modules/users/users.c
410411
src/modules/version/version.c
@@ -495,6 +496,7 @@ if(LINUX)
495496
src/detection/terminalshell/terminalshell_linux.c
496497
src/detection/terminalsize/terminalsize_linux.c
497498
src/detection/theme/theme_linux.c
499+
src/detection/tpm/tpm_linux.c
498500
src/detection/uptime/uptime_linux.c
499501
src/detection/users/users_linux.c
500502
src/detection/wallpaper/wallpaper_linux.c
@@ -558,6 +560,7 @@ elseif(ANDROID)
558560
src/detection/terminalshell/terminalshell_linux.c
559561
src/detection/terminalsize/terminalsize_linux.c
560562
src/detection/theme/theme_nosupport.c
563+
src/detection/tpm/tpm_nosupport.c
561564
src/detection/uptime/uptime_linux.c
562565
src/detection/users/users_linux.c
563566
src/detection/wallpaper/wallpaper_nosupport.c
@@ -638,6 +641,7 @@ elseif(FreeBSD)
638641
src/detection/terminalshell/terminalshell_linux.c
639642
src/detection/terminalsize/terminalsize_linux.c
640643
src/detection/theme/theme_linux.c
644+
src/detection/tpm/tpm_nosupport.c
641645
src/detection/uptime/uptime_bsd.c
642646
src/detection/users/users_linux.c
643647
src/detection/wallpaper/wallpaper_linux.c
@@ -703,6 +707,7 @@ elseif(APPLE)
703707
src/detection/terminalshell/terminalshell_linux.c
704708
src/detection/terminalsize/terminalsize_linux.c
705709
src/detection/theme/theme_nosupport.c
710+
src/detection/tpm/tpm_nosupport.c
706711
src/detection/uptime/uptime_bsd.c
707712
src/detection/users/users_linux.c
708713
src/detection/wallpaper/wallpaper_apple.m
@@ -770,6 +775,7 @@ elseif(WIN32)
770775
src/detection/terminalshell/terminalshell_windows.c
771776
src/detection/terminalsize/terminalsize_windows.c
772777
src/detection/theme/theme_nosupport.c
778+
src/detection/tpm/tpm_windows.c
773779
src/detection/uptime/uptime_windows.c
774780
src/detection/users/users_windows.c
775781
src/detection/wallpaper/wallpaper_windows.c
@@ -854,6 +860,7 @@ elseif(SunOS)
854860
src/detection/terminalshell/terminalshell_linux.c
855861
src/detection/terminalsize/terminalsize_linux.c
856862
src/detection/theme/theme_linux.c
863+
src/detection/tpm/tpm_nosupport.c
857864
src/detection/uptime/uptime_sunos.c
858865
src/detection/users/users_linux.c
859866
src/detection/wallpaper/wallpaper_linux.c
@@ -1179,7 +1186,7 @@ elseif(APPLE)
11791186
PRIVATE "-weak_framework Apple80211"
11801187
)
11811188
elseif(WIN32)
1182-
target_compile_definitions(libfastfetch PRIVATE -D_WIN32_WINNT=0x0601)
1189+
target_compile_definitions(libfastfetch PRIVATE -D_WIN32_WINNT=0x0A00)
11831190
target_link_libraries(libfastfetch
11841191
PRIVATE "dwmapi"
11851192
PRIVATE "gdi32"

doc/json_schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@
706706
"terminaltheme",
707707
"title",
708708
"theme",
709+
"tpm",
709710
"uptime",
710711
"users",
711712
"version",
@@ -865,6 +866,10 @@
865866
"const": "theme",
866867
"description": "Print current theme of desktop environment"
867868
},
869+
{
870+
"const": "tpm",
871+
"description": "Print info of Trusted Platform Module (TPM) Security Device"
872+
},
868873
{
869874
"const": "uptime",
870875
"description": "Print how long system has been running"

src/common/modules.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static FFModuleBaseInfo* T[] = {
137137
(void*) &instance.config.modules.terminalTheme,
138138
(void*) &instance.config.modules.title,
139139
(void*) &instance.config.modules.theme,
140+
(void*) &instance.config.modules.tpm,
140141
NULL,
141142
};
142143

src/detection/tpm/tpm.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "fastfetch.h"
4+
5+
typedef struct FFTPMResult
6+
{
7+
FFstrbuf version;
8+
FFstrbuf interfaceType;
9+
} FFTPMResult;
10+
11+
const char* ffDetectTPM(FFTPMResult* result);

src/detection/tpm/tpm_linux.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "tpm.h"
2+
#include "common/io/io.h"
3+
4+
const char* ffDetectTPM(FF_MAYBE_UNUSED FFTPMResult* result)
5+
{
6+
if (ffReadFileBuffer("/sys/class/tpm/tpm0/tpm_version_major", &result->version))
7+
{
8+
ffStrbufTrimRightSpace(&result->version);
9+
if (ffStrbufEqualS(&result->version, "2"))
10+
ffStrbufSetStatic(&result->version, "2.0");
11+
12+
return NULL;
13+
}
14+
15+
if (ffPathExists("/sys/class/tpm/tpm0", FF_PATHTYPE_DIRECTORY))
16+
{
17+
ffStrbufSetStatic(&result->version, "unknown");
18+
return NULL;
19+
}
20+
21+
return "TPM device is not found";
22+
}

src/detection/tpm/tpm_nosupport.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "tpm.h"
2+
3+
const char* ffDetectTPM(FF_MAYBE_UNUSED FFTPMResult* result)
4+
{
5+
return "Not supported on this platform";
6+
}

src/detection/tpm/tpm_windows.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "tpm.h"
2+
#include "common/library.h"
3+
4+
// #include <tbs.h>
5+
6+
#define TBS_SUCCESS 0u
7+
#define TBS_E_INTERNAL_ERROR 0x80284001u
8+
#define TBS_E_INVALID_CONTEXT 0x80284004u
9+
typedef UINT32 TBS_RESULT;
10+
11+
#define TPM_VERSION_UNKNOWN 0
12+
#define TPM_VERSION_12 1
13+
#define TPM_VERSION_20 2
14+
15+
#define TPM_IFTYPE_UNKNOWN 0
16+
#define TPM_IFTYPE_1 1 // for 1.2 - use I/O-port or MMIO
17+
#define TPM_IFTYPE_TRUSTZONE 2 // 2.0: Trustzone
18+
#define TPM_IFTYPE_HW 3 // 2.0: HW TPM
19+
#define TPM_IFTYPE_EMULATOR 4 // 2.0: SW-emulator
20+
#define TPM_IFTYPE_SPB 5 // 2.0: SPB attached
21+
22+
typedef struct _TPM_DEVICE_INFO
23+
{
24+
UINT32 structVersion; // = 1 for now
25+
UINT32 tpmVersion; // 1.2 / 2.0
26+
UINT32 tpmInterfaceType; // HW, simulator, ...
27+
UINT32 tpmImpRevision; // code-drop revision,
28+
// implenmentation-specific
29+
} TPM_DEVICE_INFO, *PTPM_DEVICE_INFO;
30+
typedef const TPM_DEVICE_INFO *PCTPM_DEVICE_INFO;
31+
32+
TBS_RESULT WINAPI
33+
Tbsi_GetDeviceInfo(
34+
_In_ UINT32 Size,
35+
_Out_writes_bytes_(Size) PVOID Info);
36+
37+
const char* ffDetectTPM(FFTPMResult* result)
38+
{
39+
FF_LIBRARY_LOAD(tbs, "dlopen TBS" FF_LIBRARY_EXTENSION " failed", "TBS" FF_LIBRARY_EXTENSION, -1)
40+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(tbs, Tbsi_GetDeviceInfo)
41+
42+
TPM_DEVICE_INFO deviceInfo = {};
43+
TBS_RESULT code = ffTbsi_GetDeviceInfo(sizeof(deviceInfo), &deviceInfo);
44+
if (code != TBS_SUCCESS)
45+
return code == (TBS_RESULT) TBS_E_INVALID_CONTEXT ? "TPM device is not found" : "Tbsi_GetDeviceInfo() failed";
46+
47+
switch (deviceInfo.tpmVersion)
48+
{
49+
case TPM_VERSION_12:
50+
ffStrbufSetStatic(&result->version, "1.2");
51+
break;
52+
case TPM_VERSION_20:
53+
ffStrbufSetStatic(&result->version, "2.0");
54+
break;
55+
default:
56+
ffStrbufSetStatic(&result->version, "unknown");
57+
break;
58+
}
59+
60+
switch (deviceInfo.tpmInterfaceType)
61+
{
62+
case TPM_IFTYPE_1:
63+
ffStrbufSetStatic(&result->interfaceType, "I/O-port or MMIO");
64+
break;
65+
case TPM_IFTYPE_TRUSTZONE:
66+
ffStrbufSetStatic(&result->interfaceType, "Trustzone");
67+
break;
68+
case TPM_IFTYPE_HW:
69+
ffStrbufSetStatic(&result->interfaceType, "HW TPM");
70+
break;
71+
case TPM_IFTYPE_EMULATOR:
72+
ffStrbufSetStatic(&result->interfaceType, "SW-emulator");
73+
break;
74+
case TPM_IFTYPE_SPB:
75+
ffStrbufSetStatic(&result->interfaceType, "SPB attached");
76+
break;
77+
default:
78+
break;
79+
}
80+
81+
return NULL;
82+
}

src/modules/modules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "modules/terminaltheme/terminaltheme.h"
6363
#include "modules/theme/theme.h"
6464
#include "modules/title/title.h"
65+
#include "modules/tpm/tpm.h"
6566
#include "modules/uptime/uptime.h"
6667
#include "modules/users/users.h"
6768
#include "modules/version/version.h"

src/modules/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "modules/terminaltheme/option.h"
6363
#include "modules/theme/option.h"
6464
#include "modules/title/option.h"
65+
#include "modules/tpm/option.h"
6566
#include "modules/uptime/option.h"
6667
#include "modules/users/option.h"
6768
#include "modules/version/option.h"

src/modules/tpm/option.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
4+
5+
#include "common/option.h"
6+
7+
typedef struct FFTPMOptions
8+
{
9+
FFModuleBaseInfo moduleInfo;
10+
FFModuleArgs moduleArgs;
11+
} FFTPMOptions;

0 commit comments

Comments
 (0)