This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventLog.c
189 lines (163 loc) · 6.84 KB
/
EventLog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
#include <Library/ShellLib.h>
#include <Library/BaseLib.h>
#include <Library/PrintLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/EfiShell.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/TcgService.h>
#include <IndustryStandard/UefiTcgPlatform.h>
VOID
PrintEventDetail(UINT8 *Detail, UINT32 Size, CHAR16 *TextBuffer)
{
UINT8 *d = Detail;
int Offset = 0;
int Row = 1;
CHAR16 Convert[128];
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L" Event Detail: ");
StrCatS(TextBuffer, 40960, Convert);
for (int i = 0; i < Size; i++) {
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L"%02x", *d++);
StrCatS(TextBuffer, 40960, Convert);
Offset++; Row++;
if (Row == 17 || Row == 33) {
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L" ");
StrCatS(TextBuffer, 40960, Convert);
}
if (Row > 48) {
Row = 1;
}
}
StrCatS(TextBuffer, 40960, L"\n");
}
VOID
PrintEventType(UINT32 EventType, BOOLEAN Verbose, CHAR16 *TextBuffer)
{
CHAR16 Convert[1024];
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L" Event Type: ");
switch (EventType) {
case EV_POST_CODE: StrCatS(Convert, 1024, L"Post Code");
break;
case EV_NO_ACTION: StrCatS(Convert, 1024, L"No Action");
break;
case EV_SEPARATOR: StrCatS(Convert, 1024, L"Separator");
break;
case EV_S_CRTM_CONTENTS: StrCatS(Convert, 1024, L"CTRM Contents");
break;
case EV_S_CRTM_VERSION: StrCatS(Convert, 1024, L"CRTM Version");
break;
case EV_CPU_MICROCODE: StrCatS(Convert, 1024, L"CPU Microcode");
break;
case EV_TABLE_OF_DEVICES: StrCatS(Convert, 1024, L"Table of Devices");
break;
case EV_EFI_VARIABLE_DRIVER_CONFIG: StrCatS(Convert, 1024, L"Variable Driver Config");
break;
case EV_EFI_VARIABLE_BOOT: StrCatS(Convert, 1024, L"Variable Boot");
break;
case EV_EFI_BOOT_SERVICES_APPLICATION: StrCatS(Convert, 1024, L"Boot Services Application");
break;
case EV_EFI_BOOT_SERVICES_DRIVER: StrCatS(Convert, 1024, L"Boot Services Driver");
break;
case EV_EFI_RUNTIME_SERVICES_DRIVER: StrCatS(Convert, 1024, L"Runtime Services Driver");
break;
case EV_EFI_GPT_EVENT: StrCatS(Convert, 1024, L"GPT Event");
break;
case EV_EFI_ACTION: StrCatS(Convert, 1024, L"Action");
break;
case EV_EFI_PLATFORM_FIRMWARE_BLOB: StrCatS(Convert, 1024, L"Platform Fireware Blob");
break;
case EV_EFI_HANDOFF_TABLES: StrCatS(Convert, 1024, L"Handoff Tables");
break;
case EV_EFI_VARIABLE_AUTHORITY: StrCatS(Convert, 1024, L"Variable Authority");
break;
default: StrCatS(Convert, 1024, L"Unknown Type");
break;
}
StrCatS(Convert, 1024, L"\n");
StrCatS(TextBuffer, 40960, Convert);
}
VOID
PrintSHA1(TCG_DIGEST Digest, CHAR16 *TextBuffer)
{
CHAR16 Convert[256];
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L" SHA1 Digest: " );
StrCatS(TextBuffer, 40960, Convert);
for (int j = 0; j < SHA1_DIGEST_SIZE; j++ ) {
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L"%02x", Digest.digest[j]);
StrCatS(TextBuffer, 40960, Convert);
}
StrCatS(Convert, 1024, L"\n");
StrCatS(TextBuffer, 40960, Convert);
}
VOID
PrintLog(
TCG_PCR_EVENT *Event,
BOOLEAN Verbose,
CHAR16 *TextBuffer
)
{
CHAR16 Convert[128];
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L"Event PCR Index: %u\n", Event->PCRIndex);
StrCatS(TextBuffer, 40960, Convert);
PrintEventType(Event->EventType, Verbose, TextBuffer);
PrintSHA1(Event->Digest, TextBuffer);
ZeroMem(Convert, sizeof(Convert));
UnicodeSPrint(Convert, sizeof(Convert), L" Event Size: %d\n", Event->EventSize);
StrCatS(TextBuffer, 40960, Convert);
if (Verbose) {
PrintEventDetail(Event->Event, Event->EventSize, TextBuffer);
}
StrCatS(TextBuffer, 40960, (CHAR16*)L"\n");
}
EFI_STATUS
GetEventLog(
IN CHAR16 *TextBuffer
)
{
EFI_STATUS Status = EFI_SUCCESS;
EFI_TCG_PROTOCOL *TcgProtocol;
EFI_GUID gEfiTcgProtocolGuid = EFI_TCG_PROTOCOL_GUID;
EFI_PHYSICAL_ADDRESS LogLocation;
EFI_PHYSICAL_ADDRESS LogLastEntry;
EFI_PHYSICAL_ADDRESS LogAddress;
TCG_EFI_BOOT_SERVICE_CAPABILITY BootCapacity;
TCG_PCR_EVENT *Event = NULL;
UINT32 FeatureFlag;
BOOLEAN Verbose = TRUE;
Status = gBS->LocateProtocol( &gEfiTcgProtocolGuid,
NULL,
(VOID **) &TcgProtocol);
if (EFI_ERROR (Status)) {
Print(L"Failed to locate EFI_TCG_PROTOCOL [%d]\n", Status);
return Status;
}
Status = TcgProtocol->StatusCheck( TcgProtocol,
&BootCapacity,
&FeatureFlag,
&LogLocation,
&LogLastEntry);
if (EFI_ERROR (Status)) {
Print(L"ERROR: TcgProtocol StatusCheck Failed [%d]\n", Status);
return Status;
}
LogAddress = LogLocation;
if (LogLocation != LogLastEntry) {
do {
Event = (TCG_PCR_EVENT *) LogAddress;
PrintLog(Event, Verbose, TextBuffer);
LogAddress += sizeof(TCG_PCR_EVENT_HDR) + Event->EventSize;
} while (LogAddress != LogLastEntry);
}
PrintLog((TCG_PCR_EVENT *)LogAddress, Verbose, TextBuffer);
return Status;
}