]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
ArmPkg/ArmMmuLib ARM: fix thinko in second level page table handling
[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
a332cfd3 5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/TcgService.h>\r
1abfa4ce 18#include <Protocol/Tcg2Protocol.h>\r
a332cfd3 19\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Library/DebugLib.h>\r
24#include <Library/TpmMeasurementLib.h>\r
25\r
26#include <Guid/Acpi.h>\r
27#include <IndustryStandard/Acpi.h>\r
28\r
29\r
30\r
31/**\r
32 Tpm12 measure and log data, and extend the measurement result into a specific PCR.\r
33\r
34 @param[in] PcrIndex PCR Index.\r
35 @param[in] EventType Event type.\r
36 @param[in] EventLog Measurement event log.\r
37 @param[in] LogLen Event log length in bytes.\r
38 @param[in] HashData The start of the data buffer to be hashed, extended.\r
39 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
40\r
41 @retval EFI_SUCCESS Operation completed successfully.\r
42 @retval EFI_UNSUPPORTED TPM device not available.\r
43 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
44 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
45**/\r
a332cfd3 46EFI_STATUS\r
47Tpm12MeasureAndLogData (\r
48 IN UINT32 PcrIndex,\r
49 IN UINT32 EventType,\r
50 IN VOID *EventLog,\r
51 IN UINT32 LogLen,\r
52 IN VOID *HashData,\r
53 IN UINT64 HashDataLen\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57 EFI_TCG_PROTOCOL *TcgProtocol;\r
58 TCG_PCR_EVENT *TcgEvent;\r
59 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
60 UINT32 EventNumber;\r
61\r
62 TcgEvent = NULL;\r
63\r
64 //\r
65 // Tpm active/deactive state is checked in HashLogExtendEvent\r
66 //\r
67 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
68 if (EFI_ERROR(Status)){\r
69 return Status;\r
70 }\r
71\r
72 TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);\r
73 if(TcgEvent == NULL) {\r
74 return EFI_OUT_OF_RESOURCES;\r
75 }\r
76\r
77 TcgEvent->PCRIndex = PcrIndex;\r
78 TcgEvent->EventType = EventType;\r
79 TcgEvent->EventSize = LogLen;\r
80 CopyMem (&TcgEvent->Event[0], EventLog, LogLen);\r
81 EventNumber = 1;\r
82 Status = TcgProtocol->HashLogExtendEvent (\r
83 TcgProtocol,\r
84 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
85 HashDataLen,\r
86 TPM_ALG_SHA,\r
87 TcgEvent,\r
88 &EventNumber,\r
89 &EventLogLastEntry\r
90 );\r
91\r
92 FreePool (TcgEvent);\r
93\r
94 return Status;\r
95}\r
96\r
c1d93242
JY
97/**\r
98 Tpm20 measure and log data, and extend the measurement result into a specific PCR.\r
99\r
100 @param[in] PcrIndex PCR Index.\r
101 @param[in] EventType Event type.\r
102 @param[in] EventLog Measurement event log.\r
103 @param[in] LogLen Event log length in bytes.\r
104 @param[in] HashData The start of the data buffer to be hashed, extended.\r
105 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
106\r
107 @retval EFI_SUCCESS Operation completed successfully.\r
108 @retval EFI_UNSUPPORTED TPM device not available.\r
109 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
110 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
111**/\r
112EFI_STATUS\r
113Tpm20MeasureAndLogData (\r
114 IN UINT32 PcrIndex,\r
115 IN UINT32 EventType,\r
116 IN VOID *EventLog,\r
117 IN UINT32 LogLen,\r
118 IN VOID *HashData,\r
119 IN UINT64 HashDataLen\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
1abfa4ce
JY
123 EFI_TCG2_PROTOCOL *Tcg2Protocol;\r
124 EFI_TCG2_EVENT *Tcg2Event;\r
c1d93242
JY
125\r
126 //\r
1abfa4ce 127 // TPMPresentFlag is checked in HashLogExtendEvent\r
c1d93242 128 //\r
1abfa4ce 129 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol);\r
c1d93242
JY
130 if (EFI_ERROR (Status)) {\r
131 return Status;\r
132 }\r
133\r
1abfa4ce
JY
134 Tcg2Event = (EFI_TCG2_EVENT *) AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));\r
135 if(Tcg2Event == NULL) {\r
c1d93242
JY
136 return EFI_OUT_OF_RESOURCES;\r
137 }\r
138\r
1abfa4ce
JY
139 Tcg2Event->Size = (UINT32)LogLen + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r
140 Tcg2Event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);\r
141 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
142 Tcg2Event->Header.PCRIndex = PcrIndex;\r
143 Tcg2Event->Header.EventType = EventType;\r
144 CopyMem (&Tcg2Event->Event[0], EventLog, LogLen);\r
c1d93242 145\r
1abfa4ce
JY
146 Status = Tcg2Protocol->HashLogExtendEvent (\r
147 Tcg2Protocol,\r
c1d93242
JY
148 0,\r
149 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
150 HashDataLen,\r
1abfa4ce 151 Tcg2Event\r
c1d93242 152 );\r
1abfa4ce 153 FreePool (Tcg2Event);\r
c1d93242
JY
154\r
155 return Status;\r
156}\r
157\r
a332cfd3 158/**\r
159 Tpm measure and log data, and extend the measurement result into a specific PCR.\r
160\r
161 @param[in] PcrIndex PCR Index.\r
162 @param[in] EventType Event type.\r
163 @param[in] EventLog Measurement event log.\r
164 @param[in] LogLen Event log length in bytes.\r
165 @param[in] HashData The start of the data buffer to be hashed, extended.\r
166 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
167\r
168 @retval EFI_SUCCESS Operation completed successfully.\r
169 @retval EFI_UNSUPPORTED TPM device not available.\r
170 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
171 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
172**/\r
a332cfd3 173EFI_STATUS\r
b3548d32 174EFIAPI\r
a332cfd3 175TpmMeasureAndLogData (\r
176 IN UINT32 PcrIndex,\r
177 IN UINT32 EventType,\r
178 IN VOID *EventLog,\r
179 IN UINT32 LogLen,\r
180 IN VOID *HashData,\r
181 IN UINT64 HashDataLen\r
182 )\r
183{\r
184 EFI_STATUS Status;\r
185\r
186 //\r
d9e206d4 187 // Try to measure using Tpm20 protocol\r
a332cfd3 188 //\r
d9e206d4
ZC
189 Status = Tpm20MeasureAndLogData(\r
190 PcrIndex,\r
191 EventType,\r
192 EventLog,\r
193 LogLen,\r
194 HashData,\r
195 HashDataLen\r
196 );\r
197\r
c1d93242
JY
198 if (EFI_ERROR (Status)) {\r
199 //\r
d9e206d4 200 // Try to measure using Tpm1.2 protocol\r
c1d93242 201 //\r
d9e206d4 202 Status = Tpm12MeasureAndLogData(\r
c1d93242
JY
203 PcrIndex,\r
204 EventType,\r
205 EventLog,\r
206 LogLen,\r
207 HashData,\r
208 HashDataLen\r
209 );\r
210 }\r
a332cfd3 211\r
212 return Status;\r
213}\r