]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
SecurityPkg: Apply uncrustify changes
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasurementLib / DxeTpmMeasurementLib.c
CommitLineData
a332cfd3 1/** @file\r
2 This library is used by other modules to measure data to TPM.\r
3\r
b3548d32 4Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved. <BR>\r
289b714b 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
a332cfd3 6\r
7**/\r
8\r
9#include <PiDxe.h>\r
10\r
11#include <Protocol/TcgService.h>\r
1abfa4ce 12#include <Protocol/Tcg2Protocol.h>\r
a332cfd3 13\r
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/MemoryAllocationLib.h>\r
16#include <Library/UefiBootServicesTableLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/TpmMeasurementLib.h>\r
19\r
20#include <Guid/Acpi.h>\r
21#include <IndustryStandard/Acpi.h>\r
22\r
a332cfd3 23/**\r
24 Tpm12 measure and log data, and extend the measurement result into a specific PCR.\r
25\r
26 @param[in] PcrIndex PCR Index.\r
27 @param[in] EventType Event type.\r
28 @param[in] EventLog Measurement event log.\r
29 @param[in] LogLen Event log length in bytes.\r
30 @param[in] HashData The start of the data buffer to be hashed, extended.\r
31 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
32\r
33 @retval EFI_SUCCESS Operation completed successfully.\r
34 @retval EFI_UNSUPPORTED TPM device not available.\r
35 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
36 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
37**/\r
a332cfd3 38EFI_STATUS\r
39Tpm12MeasureAndLogData (\r
c411b485
MK
40 IN UINT32 PcrIndex,\r
41 IN UINT32 EventType,\r
42 IN VOID *EventLog,\r
43 IN UINT32 LogLen,\r
44 IN VOID *HashData,\r
45 IN UINT64 HashDataLen\r
a332cfd3 46 )\r
47{\r
c411b485
MK
48 EFI_STATUS Status;\r
49 EFI_TCG_PROTOCOL *TcgProtocol;\r
50 TCG_PCR_EVENT *TcgEvent;\r
51 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
52 UINT32 EventNumber;\r
a332cfd3 53\r
54 TcgEvent = NULL;\r
55\r
56 //\r
d6b926e7 57 // Tpm activation state is checked in HashLogExtendEvent\r
a332cfd3 58 //\r
c411b485
MK
59 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);\r
60 if (EFI_ERROR (Status)) {\r
a332cfd3 61 return Status;\r
62 }\r
63\r
64 TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);\r
c411b485 65 if (TcgEvent == NULL) {\r
a332cfd3 66 return EFI_OUT_OF_RESOURCES;\r
67 }\r
68\r
69 TcgEvent->PCRIndex = PcrIndex;\r
70 TcgEvent->EventType = EventType;\r
71 TcgEvent->EventSize = LogLen;\r
72 CopyMem (&TcgEvent->Event[0], EventLog, LogLen);\r
73 EventNumber = 1;\r
c411b485
MK
74 Status = TcgProtocol->HashLogExtendEvent (\r
75 TcgProtocol,\r
76 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
77 HashDataLen,\r
78 TPM_ALG_SHA,\r
79 TcgEvent,\r
80 &EventNumber,\r
81 &EventLogLastEntry\r
82 );\r
a332cfd3 83\r
84 FreePool (TcgEvent);\r
85\r
86 return Status;\r
87}\r
88\r
c1d93242
JY
89/**\r
90 Tpm20 measure and log data, and extend the measurement result into a specific PCR.\r
91\r
92 @param[in] PcrIndex PCR Index.\r
93 @param[in] EventType Event type.\r
94 @param[in] EventLog Measurement event log.\r
95 @param[in] LogLen Event log length in bytes.\r
96 @param[in] HashData The start of the data buffer to be hashed, extended.\r
97 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
98\r
99 @retval EFI_SUCCESS Operation completed successfully.\r
100 @retval EFI_UNSUPPORTED TPM device not available.\r
101 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
102 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
103**/\r
104EFI_STATUS\r
105Tpm20MeasureAndLogData (\r
c411b485
MK
106 IN UINT32 PcrIndex,\r
107 IN UINT32 EventType,\r
108 IN VOID *EventLog,\r
109 IN UINT32 LogLen,\r
110 IN VOID *HashData,\r
111 IN UINT64 HashDataLen\r
c1d93242
JY
112 )\r
113{\r
c411b485
MK
114 EFI_STATUS Status;\r
115 EFI_TCG2_PROTOCOL *Tcg2Protocol;\r
116 EFI_TCG2_EVENT *Tcg2Event;\r
c1d93242
JY
117\r
118 //\r
1abfa4ce 119 // TPMPresentFlag is checked in HashLogExtendEvent\r
c1d93242 120 //\r
c411b485 121 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **)&Tcg2Protocol);\r
c1d93242
JY
122 if (EFI_ERROR (Status)) {\r
123 return Status;\r
124 }\r
125\r
c411b485
MK
126 Tcg2Event = (EFI_TCG2_EVENT *)AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));\r
127 if (Tcg2Event == NULL) {\r
c1d93242
JY
128 return EFI_OUT_OF_RESOURCES;\r
129 }\r
130\r
c411b485
MK
131 Tcg2Event->Size = (UINT32)LogLen + sizeof (EFI_TCG2_EVENT) - sizeof (Tcg2Event->Event);\r
132 Tcg2Event->Header.HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER);\r
1abfa4ce
JY
133 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
134 Tcg2Event->Header.PCRIndex = PcrIndex;\r
135 Tcg2Event->Header.EventType = EventType;\r
136 CopyMem (&Tcg2Event->Event[0], EventLog, LogLen);\r
c1d93242 137\r
1abfa4ce
JY
138 Status = Tcg2Protocol->HashLogExtendEvent (\r
139 Tcg2Protocol,\r
c1d93242
JY
140 0,\r
141 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
142 HashDataLen,\r
1abfa4ce 143 Tcg2Event\r
c1d93242 144 );\r
1abfa4ce 145 FreePool (Tcg2Event);\r
c1d93242
JY
146\r
147 return Status;\r
148}\r
149\r
a332cfd3 150/**\r
151 Tpm measure and log data, and extend the measurement result into a specific PCR.\r
152\r
153 @param[in] PcrIndex PCR Index.\r
154 @param[in] EventType Event type.\r
155 @param[in] EventLog Measurement event log.\r
156 @param[in] LogLen Event log length in bytes.\r
157 @param[in] HashData The start of the data buffer to be hashed, extended.\r
158 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
159\r
160 @retval EFI_SUCCESS Operation completed successfully.\r
161 @retval EFI_UNSUPPORTED TPM device not available.\r
162 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
163 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
164**/\r
a332cfd3 165EFI_STATUS\r
b3548d32 166EFIAPI\r
a332cfd3 167TpmMeasureAndLogData (\r
c411b485
MK
168 IN UINT32 PcrIndex,\r
169 IN UINT32 EventType,\r
170 IN VOID *EventLog,\r
171 IN UINT32 LogLen,\r
172 IN VOID *HashData,\r
173 IN UINT64 HashDataLen\r
a332cfd3 174 )\r
175{\r
176 EFI_STATUS Status;\r
177\r
178 //\r
d9e206d4 179 // Try to measure using Tpm20 protocol\r
a332cfd3 180 //\r
c411b485 181 Status = Tpm20MeasureAndLogData (\r
d9e206d4
ZC
182 PcrIndex,\r
183 EventType,\r
184 EventLog,\r
185 LogLen,\r
186 HashData,\r
187 HashDataLen\r
188 );\r
189\r
c1d93242
JY
190 if (EFI_ERROR (Status)) {\r
191 //\r
d9e206d4 192 // Try to measure using Tpm1.2 protocol\r
c1d93242 193 //\r
c411b485 194 Status = Tpm12MeasureAndLogData (\r
c1d93242
JY
195 PcrIndex,\r
196 EventType,\r
197 EventLog,\r
198 LogLen,\r
199 HashData,\r
200 HashDataLen\r
201 );\r
202 }\r
a332cfd3 203\r
204 return Status;\r
205}\r