]>
Commit | Line | Data |
---|---|---|
0c18794e | 1 | /** @file \r |
2 | This module implements TCG EFI Protocol.\r | |
be02dcee | 3 | \r |
4 | Caution: This module requires additional review when modified.\r | |
5 | This driver will have external input - TcgDxePassThroughToTpm\r | |
6 | This external input must be validated carefully to avoid security issue like\r | |
7 | buffer overflow, integer overflow.\r | |
8 | \r | |
9 | TcgDxePassThroughToTpm() will receive untrusted input and do basic validation.\r | |
10 | \r | |
e84f07b5 | 11 | Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r |
0c18794e | 12 | This program and the accompanying materials \r |
13 | are licensed and made available under the terms and conditions of the BSD License \r | |
14 | which accompanies this distribution. The full text of the license may be found at \r | |
15 | http://opensource.org/licenses/bsd-license.php\r | |
16 | \r | |
17 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
18 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
19 | \r | |
20 | **/\r | |
21 | \r | |
22 | #include <PiDxe.h>\r | |
23 | #include <IndustryStandard/Tpm12.h>\r | |
24 | #include <IndustryStandard/Acpi.h>\r | |
25 | #include <IndustryStandard/PeImage.h>\r | |
26 | #include <IndustryStandard/SmBios.h>\r | |
c1d93242 | 27 | #include <IndustryStandard/TcpaAcpi.h>\r |
0c18794e | 28 | \r |
29 | #include <Guid/GlobalVariable.h>\r | |
30 | #include <Guid/SmBios.h>\r | |
31 | #include <Guid/HobList.h>\r | |
32 | #include <Guid/TcgEventHob.h>\r | |
33 | #include <Guid/EventGroup.h>\r | |
9e945f78 | 34 | #include <Guid/EventExitBootServiceFailed.h>\r |
c1d93242 JY |
35 | #include <Guid/TpmInstance.h>\r |
36 | \r | |
0c18794e | 37 | #include <Protocol/DevicePath.h>\r |
38 | #include <Protocol/TcgService.h>\r | |
39 | #include <Protocol/AcpiTable.h>\r | |
b25380e3 | 40 | #include <Protocol/MpService.h>\r |
0c18794e | 41 | \r |
42 | #include <Library/DebugLib.h>\r | |
43 | #include <Library/BaseMemoryLib.h>\r | |
44 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
45 | #include <Library/UefiDriverEntryPoint.h>\r | |
46 | #include <Library/HobLib.h>\r | |
47 | #include <Library/UefiBootServicesTableLib.h>\r | |
48 | #include <Library/BaseLib.h>\r | |
49 | #include <Library/MemoryAllocationLib.h>\r | |
50 | #include <Library/PrintLib.h>\r | |
51 | #include <Library/TpmCommLib.h>\r | |
52 | #include <Library/PcdLib.h>\r | |
53 | #include <Library/UefiLib.h>\r | |
54 | \r | |
55 | #include "TpmComm.h"\r | |
56 | \r | |
57 | #define EFI_TCG_LOG_AREA_SIZE 0x10000\r | |
58 | \r | |
0c18794e | 59 | #define TCG_DXE_DATA_FROM_THIS(this) \\r |
60 | BASE_CR (this, TCG_DXE_DATA, TcgProtocol)\r | |
61 | \r | |
62 | typedef struct _TCG_DXE_DATA {\r | |
63 | EFI_TCG_PROTOCOL TcgProtocol;\r | |
64 | TCG_EFI_BOOT_SERVICE_CAPABILITY BsCap;\r | |
65 | EFI_TCG_CLIENT_ACPI_TABLE *TcgClientAcpiTable;\r | |
66 | EFI_TCG_SERVER_ACPI_TABLE *TcgServerAcpiTable;\r | |
67 | UINTN EventLogSize;\r | |
68 | UINT8 *LastEvent;\r | |
69 | TIS_TPM_HANDLE TpmHandle;\r | |
70 | } TCG_DXE_DATA;\r | |
71 | \r | |
72 | \r | |
73 | \r | |
74 | EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate = {\r | |
75 | {\r | |
76 | EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,\r | |
77 | sizeof (mTcgClientAcpiTemplate),\r | |
78 | 0x02 //Revision\r | |
79 | //\r | |
80 | // Compiler initializes the remaining bytes to 0\r | |
81 | // These fields should be filled in in production\r | |
82 | //\r | |
83 | },\r | |
84 | 0, // 0 for PC Client Platform Class\r | |
85 | 0, // Log Area Max Length\r | |
86 | (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1) // Log Area Start Address\r | |
87 | };\r | |
88 | \r | |
89 | //\r | |
90 | // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,\r | |
91 | // the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,\r | |
92 | // this _UID can be changed and should match with the _UID setting of the TPM \r | |
93 | // ACPI device object \r | |
94 | //\r | |
95 | EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate = {\r | |
96 | {\r | |
97 | EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,\r | |
98 | sizeof (mTcgServerAcpiTemplate),\r | |
99 | 0x02 //Revision\r | |
100 | //\r | |
101 | // Compiler initializes the remaining bytes to 0\r | |
102 | // These fields should be filled in in production\r | |
103 | //\r | |
104 | },\r | |
105 | 1, // 1 for Server Platform Class\r | |
106 | 0, // Reserved\r | |
107 | 0, // Log Area Max Length\r | |
108 | (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1), // Log Area Start Address\r | |
109 | 0x0100, // TCG Specification revision 1.0\r | |
110 | 2, // Device Flags\r | |
111 | 0, // Interrupt Flags\r | |
112 | 0, // GPE\r | |
113 | {0}, // Reserved 3 bytes\r | |
114 | 0, // Global System Interrupt\r | |
115 | {\r | |
116 | EFI_ACPI_3_0_SYSTEM_MEMORY,\r | |
117 | 0,\r | |
118 | 0,\r | |
119 | EFI_ACPI_3_0_BYTE,\r | |
120 | TPM_BASE_ADDRESS // Base Address\r | |
121 | },\r | |
122 | 0, // Reserved\r | |
123 | {0}, // Configuration Address\r | |
124 | 0xFF, // ACPI _UID value of the device, can be changed for different platforms\r | |
125 | 0, // ACPI _UID value of the device, can be changed for different platforms\r | |
126 | 0, // ACPI _UID value of the device, can be changed for different platforms\r | |
127 | 0 // ACPI _UID value of the device, can be changed for different platforms\r | |
128 | };\r | |
129 | \r | |
130 | UINTN mBootAttempts = 0;\r | |
131 | CHAR16 mBootVarName[] = L"BootOrder";\r | |
132 | \r | |
b25380e3 | 133 | /**\r |
134 | Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function\r | |
135 | Caller is responsible to free LocationBuf.\r | |
136 | \r | |
137 | @param[out] LocationBuf Returns Processor Location Buffer.\r | |
138 | @param[out] Num Returns processor number.\r | |
139 | \r | |
140 | @retval EFI_SUCCESS Operation completed successfully.\r | |
141 | @retval EFI_UNSUPPORTED MpService protocol not found.\r | |
142 | \r | |
143 | **/\r | |
144 | EFI_STATUS\r | |
145 | GetProcessorsCpuLocation (\r | |
146 | OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,\r | |
147 | OUT UINTN *Num\r | |
148 | )\r | |
149 | {\r | |
150 | EFI_STATUS Status;\r | |
151 | EFI_MP_SERVICES_PROTOCOL *MpProtocol;\r | |
152 | UINTN ProcessorNum;\r | |
153 | UINTN EnabledProcessorNum;\r | |
154 | EFI_PROCESSOR_INFORMATION ProcessorInfo;\r | |
155 | EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;\r | |
156 | UINTN Index;\r | |
157 | \r | |
158 | Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **) &MpProtocol);\r | |
159 | if (EFI_ERROR (Status)) {\r | |
160 | //\r | |
161 | // MP protocol is not installed\r | |
162 | //\r | |
163 | return EFI_UNSUPPORTED;\r | |
164 | }\r | |
165 | \r | |
166 | Status = MpProtocol->GetNumberOfProcessors(\r | |
167 | MpProtocol,\r | |
168 | &ProcessorNum,\r | |
169 | &EnabledProcessorNum\r | |
170 | );\r | |
171 | if (EFI_ERROR(Status)){\r | |
172 | return Status;\r | |
173 | }\r | |
174 | \r | |
175 | Status = gBS->AllocatePool(\r | |
176 | EfiBootServicesData,\r | |
177 | sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,\r | |
bcb76672 | 178 | (VOID **) &ProcessorLocBuf\r |
b25380e3 | 179 | );\r |
180 | if (EFI_ERROR(Status)){\r | |
181 | return Status;\r | |
182 | }\r | |
183 | \r | |
184 | //\r | |
185 | // Get each processor Location info\r | |
186 | //\r | |
187 | for (Index = 0; Index < ProcessorNum; Index++) {\r | |
188 | Status = MpProtocol->GetProcessorInfo(\r | |
189 | MpProtocol,\r | |
190 | Index,\r | |
191 | &ProcessorInfo\r | |
192 | );\r | |
193 | if (EFI_ERROR(Status)){\r | |
194 | FreePool(ProcessorLocBuf);\r | |
195 | return Status;\r | |
196 | }\r | |
197 | \r | |
198 | //\r | |
199 | // Get all Processor Location info & measure\r | |
200 | //\r | |
201 | CopyMem(\r | |
202 | &ProcessorLocBuf[Index],\r | |
203 | &ProcessorInfo.Location,\r | |
204 | sizeof(EFI_CPU_PHYSICAL_LOCATION)\r | |
205 | );\r | |
206 | }\r | |
207 | \r | |
208 | *LocationBuf = ProcessorLocBuf;\r | |
209 | *Num = ProcessorNum;\r | |
210 | \r | |
211 | return Status;\r | |
212 | }\r | |
213 | \r | |
0c18794e | 214 | /**\r |
215 | This service provides EFI protocol capability information, state information \r | |
216 | about the TPM, and Event Log state information.\r | |
217 | \r | |
218 | @param[in] This Indicates the calling context\r | |
219 | @param[out] ProtocolCapability The callee allocates memory for a TCG_BOOT_SERVICE_CAPABILITY \r | |
220 | structure and fills in the fields with the EFI protocol \r | |
221 | capability information and the current TPM state information.\r | |
222 | @param[out] TCGFeatureFlags This is a pointer to the feature flags. No feature \r | |
223 | flags are currently defined so this parameter \r | |
224 | MUST be set to 0. However, in the future, \r | |
225 | feature flags may be defined that, for example, \r | |
226 | enable hash algorithm agility.\r | |
227 | @param[out] EventLogLocation This is a pointer to the address of the event log in memory.\r | |
228 | @param[out] EventLogLastEntry If the Event Log contains more than one entry, \r | |
229 | this is a pointer to the address of the start of \r | |
230 | the last entry in the event log in memory. \r | |
231 | \r | |
232 | @retval EFI_SUCCESS Operation completed successfully.\r | |
233 | @retval EFI_INVALID_PARAMETER ProtocolCapability does not match TCG capability.\r | |
234 | \r | |
235 | **/\r | |
236 | EFI_STATUS\r | |
237 | EFIAPI\r | |
238 | TcgDxeStatusCheck (\r | |
239 | IN EFI_TCG_PROTOCOL *This,\r | |
240 | OUT TCG_EFI_BOOT_SERVICE_CAPABILITY *ProtocolCapability,\r | |
241 | OUT UINT32 *TCGFeatureFlags,\r | |
242 | OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,\r | |
243 | OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry\r | |
244 | )\r | |
245 | {\r | |
246 | TCG_DXE_DATA *TcgData;\r | |
247 | \r | |
248 | TcgData = TCG_DXE_DATA_FROM_THIS (This);\r | |
249 | \r | |
250 | if (ProtocolCapability != NULL) {\r | |
251 | *ProtocolCapability = TcgData->BsCap;\r | |
252 | }\r | |
253 | \r | |
254 | if (TCGFeatureFlags != NULL) {\r | |
255 | *TCGFeatureFlags = 0;\r | |
256 | }\r | |
257 | \r | |
258 | if (EventLogLocation != NULL) {\r | |
259 | if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r | |
260 | *EventLogLocation = TcgData->TcgClientAcpiTable->Lasa;\r | |
261 | } else {\r | |
262 | *EventLogLocation = TcgData->TcgServerAcpiTable->Lasa;\r | |
263 | }\r | |
264 | }\r | |
265 | \r | |
266 | if (EventLogLastEntry != NULL) {\r | |
267 | if (TcgData->BsCap.TPMDeactivatedFlag) {\r | |
268 | *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;\r | |
269 | } else {\r | |
270 | *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)TcgData->LastEvent;\r | |
271 | }\r | |
272 | }\r | |
273 | \r | |
274 | return EFI_SUCCESS;\r | |
275 | }\r | |
276 | \r | |
277 | /**\r | |
278 | This service abstracts the capability to do a hash operation on a data buffer.\r | |
279 | \r | |
280 | @param[in] This Indicates the calling context\r | |
281 | @param[in] HashData Pointer to the data buffer to be hashed\r | |
282 | @param[in] HashDataLen Length of the data buffer to be hashed\r | |
283 | @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation\r | |
284 | @param[in, out] HashedDataLen Resultant length of the hashed data\r | |
285 | @param[in, out] HashedDataResult Resultant buffer of the hashed data \r | |
286 | \r | |
287 | @retval EFI_SUCCESS Operation completed successfully.\r | |
288 | @retval EFI_INVALID_PARAMETER HashDataLen is NULL.\r | |
289 | @retval EFI_INVALID_PARAMETER HashDataLenResult is NULL.\r | |
290 | @retval EFI_OUT_OF_RESOURCES Cannot allocate buffer of size *HashedDataLen.\r | |
291 | @retval EFI_UNSUPPORTED AlgorithmId not supported.\r | |
292 | @retval EFI_BUFFER_TOO_SMALL *HashedDataLen < sizeof (TCG_DIGEST).\r | |
293 | \r | |
294 | **/\r | |
295 | EFI_STATUS\r | |
296 | EFIAPI\r | |
297 | TcgDxeHashAll (\r | |
298 | IN EFI_TCG_PROTOCOL *This,\r | |
299 | IN UINT8 *HashData,\r | |
300 | IN UINT64 HashDataLen,\r | |
301 | IN TCG_ALGORITHM_ID AlgorithmId,\r | |
302 | IN OUT UINT64 *HashedDataLen,\r | |
303 | IN OUT UINT8 **HashedDataResult\r | |
304 | )\r | |
305 | {\r | |
306 | if (HashedDataLen == NULL || HashedDataResult == NULL) {\r | |
307 | return EFI_INVALID_PARAMETER;\r | |
308 | }\r | |
309 | \r | |
310 | switch (AlgorithmId) {\r | |
311 | case TPM_ALG_SHA:\r | |
312 | if (*HashedDataLen == 0) {\r | |
313 | *HashedDataLen = sizeof (TPM_DIGEST);\r | |
314 | *HashedDataResult = AllocatePool ((UINTN) *HashedDataLen);\r | |
315 | if (*HashedDataResult == NULL) {\r | |
316 | return EFI_OUT_OF_RESOURCES;\r | |
317 | }\r | |
318 | }\r | |
319 | \r | |
320 | if (*HashedDataLen < sizeof (TPM_DIGEST)) {\r | |
321 | *HashedDataLen = sizeof (TPM_DIGEST);\r | |
322 | return EFI_BUFFER_TOO_SMALL;\r | |
323 | }\r | |
324 | *HashedDataLen = sizeof (TPM_DIGEST);\r | |
325 | \r | |
f5af77a8 | 326 | if (*HashedDataResult == NULL) {\r |
327 | *HashedDataResult = AllocatePool ((UINTN) *HashedDataLen);\r | |
328 | } \r | |
329 | \r | |
0c18794e | 330 | return TpmCommHashAll (\r |
331 | HashData,\r | |
332 | (UINTN) HashDataLen,\r | |
333 | (TPM_DIGEST*)*HashedDataResult\r | |
334 | );\r | |
335 | default:\r | |
336 | return EFI_UNSUPPORTED;\r | |
337 | }\r | |
338 | }\r | |
339 | \r | |
340 | /**\r | |
341 | Add a new entry to the Event Log.\r | |
342 | \r | |
343 | @param[in] TcgData TCG_DXE_DATA structure.\r | |
344 | @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure. \r | |
345 | @param[in] NewEventData Pointer to the new event data. \r | |
346 | \r | |
347 | @retval EFI_SUCCESS The new event log entry was added.\r | |
348 | @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r | |
349 | \r | |
350 | **/\r | |
351 | EFI_STATUS\r | |
352 | EFIAPI\r | |
353 | TcgDxeLogEventI (\r | |
354 | IN TCG_DXE_DATA *TcgData,\r | |
355 | IN TCG_PCR_EVENT_HDR *NewEventHdr,\r | |
356 | IN UINT8 *NewEventData\r | |
357 | )\r | |
358 | {\r | |
359 | if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r | |
360 | TcgData->LastEvent = (UINT8*)(UINTN)TcgData->TcgClientAcpiTable->Lasa;\r | |
361 | return TpmCommLogEvent (\r | |
362 | &TcgData->LastEvent,\r | |
363 | &TcgData->EventLogSize,\r | |
364 | (UINTN)TcgData->TcgClientAcpiTable->Laml,\r | |
365 | NewEventHdr,\r | |
366 | NewEventData\r | |
367 | );\r | |
368 | } else {\r | |
369 | TcgData->LastEvent = (UINT8*)(UINTN)TcgData->TcgServerAcpiTable->Lasa;\r | |
370 | return TpmCommLogEvent (\r | |
371 | &TcgData->LastEvent,\r | |
372 | &TcgData->EventLogSize,\r | |
373 | (UINTN)TcgData->TcgServerAcpiTable->Laml,\r | |
374 | NewEventHdr,\r | |
375 | NewEventData\r | |
376 | );\r | |
377 | }\r | |
378 | }\r | |
379 | \r | |
380 | /**\r | |
381 | This service abstracts the capability to add an entry to the Event Log.\r | |
382 | \r | |
383 | @param[in] This Indicates the calling context\r | |
384 | @param[in] TCGLogData Pointer to the start of the data buffer containing \r | |
385 | the TCG_PCR_EVENT data structure. All fields in \r | |
386 | this structure are properly filled by the caller.\r | |
387 | @param[in, out] EventNumber The event number of the event just logged\r | |
388 | @param[in] Flags Indicate additional flags. Only one flag has been \r | |
389 | defined at this time, which is 0x01 and means the \r | |
390 | extend operation should not be performed. All \r | |
391 | other bits are reserved. \r | |
392 | \r | |
393 | @retval EFI_SUCCESS Operation completed successfully.\r | |
394 | @retval EFI_OUT_OF_RESOURCES Insufficient memory in the event log to complete this action.\r | |
395 | \r | |
396 | **/\r | |
397 | EFI_STATUS\r | |
398 | EFIAPI\r | |
399 | TcgDxeLogEvent (\r | |
400 | IN EFI_TCG_PROTOCOL *This,\r | |
401 | IN TCG_PCR_EVENT *TCGLogData,\r | |
402 | IN OUT UINT32 *EventNumber,\r | |
403 | IN UINT32 Flags\r | |
404 | )\r | |
405 | {\r | |
406 | TCG_DXE_DATA *TcgData;\r | |
407 | \r | |
677e5c0b | 408 | if (TCGLogData == NULL){\r |
409 | return EFI_INVALID_PARAMETER;\r | |
410 | }\r | |
411 | \r | |
0c18794e | 412 | TcgData = TCG_DXE_DATA_FROM_THIS (This);\r |
413 | \r | |
414 | if (TcgData->BsCap.TPMDeactivatedFlag) {\r | |
415 | return EFI_DEVICE_ERROR;\r | |
416 | }\r | |
417 | return TcgDxeLogEventI (\r | |
418 | TcgData,\r | |
419 | (TCG_PCR_EVENT_HDR*)TCGLogData,\r | |
420 | TCGLogData->Event\r | |
421 | );\r | |
422 | }\r | |
423 | \r | |
424 | /**\r | |
425 | This service is a proxy for commands to the TPM.\r | |
426 | \r | |
427 | @param[in] This Indicates the calling context\r | |
428 | @param[in] TpmInputParameterBlockSize Size of the TPM input parameter block\r | |
429 | @param[in] TpmInputParameterBlock Pointer to the TPM input parameter block\r | |
430 | @param[in] TpmOutputParameterBlockSize Size of the TPM output parameter block\r | |
431 | @param[in] TpmOutputParameterBlock Pointer to the TPM output parameter block\r | |
432 | \r | |
433 | @retval EFI_SUCCESS Operation completed successfully.\r | |
434 | @retval EFI_INVALID_PARAMETER Invalid ordinal.\r | |
435 | @retval EFI_UNSUPPORTED Current Task Priority Level >= EFI_TPL_CALLBACK.\r | |
436 | @retval EFI_TIMEOUT The TIS timed-out.\r | |
437 | \r | |
438 | **/\r | |
439 | EFI_STATUS\r | |
440 | EFIAPI\r | |
441 | TcgDxePassThroughToTpm (\r | |
442 | IN EFI_TCG_PROTOCOL *This,\r | |
443 | IN UINT32 TpmInputParameterBlockSize,\r | |
444 | IN UINT8 *TpmInputParameterBlock,\r | |
445 | IN UINT32 TpmOutputParameterBlockSize,\r | |
446 | IN UINT8 *TpmOutputParameterBlock\r | |
447 | )\r | |
448 | {\r | |
449 | TCG_DXE_DATA *TcgData;\r | |
450 | \r | |
be02dcee | 451 | if (TpmInputParameterBlock == NULL || \r |
452 | TpmOutputParameterBlock == NULL || \r | |
453 | TpmInputParameterBlockSize == 0 ||\r | |
454 | TpmOutputParameterBlockSize == 0) {\r | |
455 | return EFI_INVALID_PARAMETER;\r | |
456 | }\r | |
457 | \r | |
0c18794e | 458 | TcgData = TCG_DXE_DATA_FROM_THIS (This);\r |
459 | \r | |
460 | return TisPcExecute (\r | |
461 | TcgData->TpmHandle,\r | |
462 | "%r%/%r",\r | |
463 | TpmInputParameterBlock,\r | |
464 | (UINTN) TpmInputParameterBlockSize,\r | |
465 | TpmOutputParameterBlock,\r | |
466 | (UINTN) TpmOutputParameterBlockSize\r | |
467 | );\r | |
468 | }\r | |
469 | \r | |
470 | /**\r | |
471 | Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,\r | |
472 | and add an entry to the Event Log.\r | |
473 | \r | |
474 | @param[in] TcgData TCG_DXE_DATA structure.\r | |
475 | @param[in] HashData Physical address of the start of the data buffer \r | |
476 | to be hashed, extended, and logged.\r | |
477 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
478 | @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure. \r | |
479 | @param[in] NewEventData Pointer to the new event data. \r | |
480 | \r | |
481 | @retval EFI_SUCCESS Operation completed successfully.\r | |
482 | @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r | |
483 | @retval EFI_DEVICE_ERROR The command was unsuccessful.\r | |
484 | \r | |
485 | **/\r | |
486 | EFI_STATUS\r | |
487 | EFIAPI\r | |
488 | TcgDxeHashLogExtendEventI (\r | |
489 | IN TCG_DXE_DATA *TcgData,\r | |
490 | IN UINT8 *HashData,\r | |
491 | IN UINT64 HashDataLen,\r | |
492 | IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,\r | |
493 | IN UINT8 *NewEventData\r | |
494 | )\r | |
495 | {\r | |
496 | EFI_STATUS Status;\r | |
497 | \r | |
677e5c0b | 498 | if (HashData == NULL && HashDataLen > 0) {\r |
499 | return EFI_INVALID_PARAMETER;\r | |
500 | }\r | |
501 | \r | |
502 | if (HashDataLen > 0 || HashData != NULL) {\r | |
0c18794e | 503 | Status = TpmCommHashAll (\r |
504 | HashData,\r | |
505 | (UINTN) HashDataLen,\r | |
506 | &NewEventHdr->Digest\r | |
507 | );\r | |
508 | ASSERT_EFI_ERROR (Status);\r | |
509 | }\r | |
510 | \r | |
511 | Status = TpmCommExtend (\r | |
512 | TcgData->TpmHandle,\r | |
513 | &NewEventHdr->Digest,\r | |
514 | NewEventHdr->PCRIndex,\r | |
515 | NULL\r | |
516 | );\r | |
517 | if (!EFI_ERROR (Status)) {\r | |
518 | Status = TcgDxeLogEventI (TcgData, NewEventHdr, NewEventData);\r | |
519 | }\r | |
520 | \r | |
521 | return Status;\r | |
522 | }\r | |
523 | \r | |
524 | /**\r | |
525 | This service abstracts the capability to do a hash operation on a data buffer,\r | |
526 | extend a specific TPM PCR with the hash result, and add an entry to the Event Log\r | |
527 | \r | |
528 | @param[in] This Indicates the calling context\r | |
529 | @param[in] HashData Physical address of the start of the data buffer \r | |
530 | to be hashed, extended, and logged.\r | |
531 | @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r | |
532 | @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation\r | |
533 | @param[in, out] TCGLogData The physical address of the start of the data \r | |
534 | buffer containing the TCG_PCR_EVENT data structure.\r | |
535 | @param[in, out] EventNumber The event number of the event just logged.\r | |
536 | @param[out] EventLogLastEntry Physical address of the first byte of the entry \r | |
537 | just placed in the Event Log. If the Event Log was \r | |
538 | empty when this function was called then this physical \r | |
539 | address will be the same as the physical address of \r | |
540 | the start of the Event Log.\r | |
541 | \r | |
542 | @retval EFI_SUCCESS Operation completed successfully.\r | |
543 | @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.\r | |
544 | @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.\r | |
545 | @retval EFI_DEVICE_ERROR The command was unsuccessful.\r | |
546 | \r | |
547 | **/\r | |
548 | EFI_STATUS\r | |
549 | EFIAPI\r | |
550 | TcgDxeHashLogExtendEvent (\r | |
551 | IN EFI_TCG_PROTOCOL *This,\r | |
552 | IN EFI_PHYSICAL_ADDRESS HashData,\r | |
553 | IN UINT64 HashDataLen,\r | |
554 | IN TPM_ALGORITHM_ID AlgorithmId,\r | |
555 | IN OUT TCG_PCR_EVENT *TCGLogData,\r | |
556 | IN OUT UINT32 *EventNumber,\r | |
557 | OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry\r | |
558 | )\r | |
559 | {\r | |
560 | TCG_DXE_DATA *TcgData;\r | |
15f2d739 | 561 | EFI_STATUS Status;\r |
0c18794e | 562 | \r |
677e5c0b | 563 | if (TCGLogData == NULL || EventLogLastEntry == NULL){\r |
564 | return EFI_INVALID_PARAMETER;\r | |
565 | }\r | |
566 | \r | |
0c18794e | 567 | TcgData = TCG_DXE_DATA_FROM_THIS (This);\r |
568 | \r | |
569 | if (TcgData->BsCap.TPMDeactivatedFlag) {\r | |
570 | return EFI_DEVICE_ERROR;\r | |
571 | }\r | |
572 | \r | |
573 | if (AlgorithmId != TPM_ALG_SHA) {\r | |
574 | return EFI_UNSUPPORTED;\r | |
575 | }\r | |
576 | \r | |
15f2d739 | 577 | Status = TcgDxeHashLogExtendEventI (\r |
578 | TcgData,\r | |
579 | (UINT8 *) (UINTN) HashData,\r | |
580 | HashDataLen,\r | |
581 | (TCG_PCR_EVENT_HDR*)TCGLogData,\r | |
582 | TCGLogData->Event\r | |
583 | );\r | |
584 | \r | |
585 | if (!EFI_ERROR(Status)){\r | |
586 | *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN) TcgData->LastEvent;\r | |
587 | }\r | |
588 | \r | |
589 | return Status;\r | |
0c18794e | 590 | }\r |
591 | \r | |
592 | TCG_DXE_DATA mTcgDxeData = {\r | |
593 | {\r | |
594 | TcgDxeStatusCheck,\r | |
595 | TcgDxeHashAll,\r | |
596 | TcgDxeLogEvent,\r | |
597 | TcgDxePassThroughToTpm,\r | |
598 | TcgDxeHashLogExtendEvent\r | |
599 | },\r | |
600 | {\r | |
601 | sizeof (mTcgDxeData.BsCap),\r | |
602 | { 1, 2, 0, 0 },\r | |
603 | { 1, 2, 0, 0 },\r | |
604 | 1,\r | |
605 | TRUE,\r | |
606 | FALSE\r | |
607 | },\r | |
608 | &mTcgClientAcpiTemplate,\r | |
609 | &mTcgServerAcpiTemplate,\r | |
610 | 0,\r | |
611 | NULL,\r | |
612 | NULL\r | |
613 | };\r | |
614 | \r | |
615 | /**\r | |
616 | Initialize the Event Log and log events passed from the PEI phase.\r | |
617 | \r | |
618 | @retval EFI_SUCCESS Operation completed successfully.\r | |
619 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
620 | \r | |
621 | **/\r | |
622 | EFI_STATUS\r | |
623 | EFIAPI\r | |
624 | SetupEventLog (\r | |
625 | VOID\r | |
626 | )\r | |
627 | {\r | |
628 | EFI_STATUS Status;\r | |
629 | TCG_PCR_EVENT *TcgEvent;\r | |
630 | EFI_PEI_HOB_POINTERS GuidHob;\r | |
631 | EFI_PHYSICAL_ADDRESS Lasa;\r | |
632 | \r | |
633 | if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r | |
634 | Lasa = mTcgClientAcpiTemplate.Lasa;\r | |
635 | \r | |
636 | Status = gBS->AllocatePages (\r | |
637 | AllocateMaxAddress,\r | |
638 | EfiACPIMemoryNVS,\r | |
639 | EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),\r | |
640 | &Lasa\r | |
641 | );\r | |
642 | if (EFI_ERROR (Status)) {\r | |
643 | return Status;\r | |
644 | }\r | |
645 | mTcgClientAcpiTemplate.Lasa = Lasa;\r | |
646 | //\r | |
647 | // To initialize them as 0xFF is recommended \r | |
648 | // because the OS can know the last entry for that.\r | |
649 | //\r | |
650 | SetMem ((VOID *)(UINTN)mTcgClientAcpiTemplate.Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r | |
651 | mTcgClientAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r | |
652 | \r | |
653 | } else {\r | |
654 | Lasa = mTcgServerAcpiTemplate.Lasa;\r | |
655 | \r | |
656 | Status = gBS->AllocatePages (\r | |
657 | AllocateMaxAddress,\r | |
658 | EfiACPIMemoryNVS,\r | |
659 | EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),\r | |
660 | &Lasa\r | |
661 | );\r | |
662 | if (EFI_ERROR (Status)) {\r | |
663 | return Status;\r | |
664 | }\r | |
665 | mTcgServerAcpiTemplate.Lasa = Lasa;\r | |
666 | //\r | |
667 | // To initialize them as 0xFF is recommended \r | |
668 | // because the OS can know the last entry for that.\r | |
669 | //\r | |
670 | SetMem ((VOID *)(UINTN)mTcgServerAcpiTemplate.Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r | |
671 | mTcgServerAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r | |
672 | }\r | |
673 | \r | |
674 | GuidHob.Raw = GetHobList ();\r | |
675 | while (!EFI_ERROR (Status) && \r | |
676 | (GuidHob.Raw = GetNextGuidHob (&gTcgEventEntryHobGuid, GuidHob.Raw)) != NULL) {\r | |
677 | TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);\r | |
678 | GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r | |
679 | Status = TcgDxeLogEventI (\r | |
680 | &mTcgDxeData,\r | |
681 | (TCG_PCR_EVENT_HDR*)TcgEvent,\r | |
682 | TcgEvent->Event\r | |
683 | );\r | |
684 | }\r | |
685 | \r | |
686 | return Status;\r | |
687 | }\r | |
688 | \r | |
689 | /**\r | |
690 | Measure and log an action string, and extend the measurement result into PCR[5].\r | |
691 | \r | |
692 | @param[in] String A specific string that indicates an Action event. \r | |
693 | \r | |
694 | @retval EFI_SUCCESS Operation completed successfully.\r | |
695 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
696 | \r | |
697 | **/\r | |
698 | EFI_STATUS\r | |
699 | EFIAPI\r | |
700 | TcgMeasureAction (\r | |
701 | IN CHAR8 *String\r | |
702 | )\r | |
703 | {\r | |
704 | TCG_PCR_EVENT_HDR TcgEvent;\r | |
705 | \r | |
706 | TcgEvent.PCRIndex = 5;\r | |
707 | TcgEvent.EventType = EV_EFI_ACTION;\r | |
708 | TcgEvent.EventSize = (UINT32)AsciiStrLen (String);\r | |
709 | return TcgDxeHashLogExtendEventI (\r | |
710 | &mTcgDxeData,\r | |
711 | (UINT8*)String,\r | |
712 | TcgEvent.EventSize,\r | |
713 | &TcgEvent,\r | |
714 | (UINT8 *) String\r | |
715 | );\r | |
716 | }\r | |
717 | \r | |
718 | /**\r | |
719 | Measure and log EFI handoff tables, and extend the measurement result into PCR[1].\r | |
720 | \r | |
721 | @retval EFI_SUCCESS Operation completed successfully.\r | |
722 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
723 | \r | |
724 | **/\r | |
725 | EFI_STATUS\r | |
726 | EFIAPI\r | |
727 | MeasureHandoffTables (\r | |
728 | VOID\r | |
729 | )\r | |
730 | {\r | |
731 | EFI_STATUS Status;\r | |
732 | SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r | |
733 | TCG_PCR_EVENT_HDR TcgEvent;\r | |
734 | EFI_HANDOFF_TABLE_POINTERS HandoffTables;\r | |
b25380e3 | 735 | UINTN ProcessorNum;\r |
736 | EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;\r | |
0c18794e | 737 | \r |
b25380e3 | 738 | //\r |
739 | // Measure SMBIOS with EV_EFI_HANDOFF_TABLES to PCR[1]\r | |
740 | //\r | |
0c18794e | 741 | Status = EfiGetSystemConfigurationTable (\r |
742 | &gEfiSmbiosTableGuid,\r | |
743 | (VOID **) &SmbiosTable\r | |
744 | );\r | |
745 | \r | |
746 | if (!EFI_ERROR (Status)) {\r | |
747 | ASSERT (SmbiosTable != NULL);\r | |
748 | \r | |
749 | TcgEvent.PCRIndex = 1;\r | |
750 | TcgEvent.EventType = EV_EFI_HANDOFF_TABLES;\r | |
751 | TcgEvent.EventSize = sizeof (HandoffTables);\r | |
752 | \r | |
753 | HandoffTables.NumberOfTables = 1;\r | |
754 | HandoffTables.TableEntry[0].VendorGuid = gEfiSmbiosTableGuid;\r | |
755 | HandoffTables.TableEntry[0].VendorTable = SmbiosTable;\r | |
756 | \r | |
757 | DEBUG ((DEBUG_INFO, "The Smbios Table starts at: 0x%x\n", SmbiosTable->TableAddress));\r | |
758 | DEBUG ((DEBUG_INFO, "The Smbios Table size: 0x%x\n", SmbiosTable->TableLength));\r | |
759 | \r | |
760 | Status = TcgDxeHashLogExtendEventI (\r | |
761 | &mTcgDxeData,\r | |
762 | (UINT8*)(UINTN)SmbiosTable->TableAddress,\r | |
763 | SmbiosTable->TableLength,\r | |
764 | &TcgEvent,\r | |
765 | (UINT8*)&HandoffTables\r | |
766 | );\r | |
767 | }\r | |
768 | \r | |
b25380e3 | 769 | if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {\r |
770 | //\r | |
771 | // Tcg Server spec. \r | |
772 | // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]\r | |
773 | //\r | |
774 | Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);\r | |
775 | \r | |
776 | if (!EFI_ERROR(Status)){\r | |
777 | TcgEvent.PCRIndex = 1;\r | |
778 | TcgEvent.EventType = EV_TABLE_OF_DEVICES;\r | |
779 | TcgEvent.EventSize = sizeof (HandoffTables);\r | |
780 | \r | |
781 | HandoffTables.NumberOfTables = 1;\r | |
782 | HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;\r | |
783 | HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;\r | |
784 | \r | |
785 | Status = TcgDxeHashLogExtendEventI (\r | |
786 | &mTcgDxeData,\r | |
787 | (UINT8*)(UINTN)ProcessorLocBuf,\r | |
788 | sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,\r | |
789 | &TcgEvent,\r | |
790 | (UINT8*)&HandoffTables\r | |
791 | );\r | |
792 | \r | |
793 | FreePool(ProcessorLocBuf);\r | |
794 | }\r | |
795 | }\r | |
796 | \r | |
0c18794e | 797 | return Status;\r |
798 | }\r | |
799 | \r | |
800 | /**\r | |
801 | Measure and log Separator event, and extend the measurement result into a specific PCR.\r | |
802 | \r | |
803 | @param[in] PCRIndex PCR index. \r | |
804 | \r | |
805 | @retval EFI_SUCCESS Operation completed successfully.\r | |
806 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
807 | \r | |
808 | **/\r | |
809 | EFI_STATUS\r | |
810 | EFIAPI\r | |
811 | MeasureSeparatorEvent (\r | |
812 | IN TPM_PCRINDEX PCRIndex\r | |
813 | )\r | |
814 | {\r | |
815 | TCG_PCR_EVENT_HDR TcgEvent;\r | |
816 | UINT32 EventData;\r | |
817 | \r | |
818 | EventData = 0;\r | |
819 | TcgEvent.PCRIndex = PCRIndex;\r | |
820 | TcgEvent.EventType = EV_SEPARATOR;\r | |
821 | TcgEvent.EventSize = (UINT32)sizeof (EventData);\r | |
822 | return TcgDxeHashLogExtendEventI (\r | |
823 | &mTcgDxeData,\r | |
824 | (UINT8 *)&EventData,\r | |
825 | sizeof (EventData),\r | |
826 | &TcgEvent,\r | |
827 | (UINT8 *)&EventData\r | |
828 | );\r | |
829 | }\r | |
830 | \r | |
831 | /**\r | |
832 | Read an EFI Variable.\r | |
833 | \r | |
834 | This function allocates a buffer to return the contents of the variable. The caller is\r | |
835 | responsible for freeing the buffer.\r | |
836 | \r | |
837 | @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r | |
838 | @param[in] VendorGuid A unique identifier for the vendor.\r | |
839 | @param[out] VarSize The size of the variable data. \r | |
840 | \r | |
841 | @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.\r | |
842 | \r | |
843 | **/\r | |
844 | VOID *\r | |
845 | EFIAPI\r | |
846 | ReadVariable (\r | |
847 | IN CHAR16 *VarName,\r | |
848 | IN EFI_GUID *VendorGuid,\r | |
849 | OUT UINTN *VarSize\r | |
850 | )\r | |
851 | {\r | |
852 | EFI_STATUS Status;\r | |
853 | VOID *VarData;\r | |
854 | \r | |
855 | *VarSize = 0;\r | |
856 | Status = gRT->GetVariable (\r | |
857 | VarName,\r | |
858 | VendorGuid,\r | |
859 | NULL,\r | |
860 | VarSize,\r | |
861 | NULL\r | |
862 | );\r | |
863 | if (Status != EFI_BUFFER_TOO_SMALL) {\r | |
864 | return NULL;\r | |
865 | }\r | |
866 | \r | |
867 | VarData = AllocatePool (*VarSize);\r | |
868 | if (VarData != NULL) {\r | |
869 | Status = gRT->GetVariable (\r | |
870 | VarName,\r | |
871 | VendorGuid,\r | |
872 | NULL,\r | |
873 | VarSize,\r | |
874 | VarData\r | |
875 | );\r | |
876 | if (EFI_ERROR (Status)) {\r | |
877 | FreePool (VarData);\r | |
878 | VarData = NULL;\r | |
879 | *VarSize = 0;\r | |
880 | }\r | |
881 | }\r | |
882 | return VarData;\r | |
883 | }\r | |
884 | \r | |
885 | /**\r | |
886 | Measure and log an EFI variable, and extend the measurement result into a specific PCR.\r | |
887 | \r | |
888 | @param[in] PCRIndex PCR Index. \r | |
889 | @param[in] EventType Event type. \r | |
890 | @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r | |
891 | @param[in] VendorGuid A unique identifier for the vendor.\r | |
892 | @param[in] VarData The content of the variable data. \r | |
893 | @param[in] VarSize The size of the variable data. \r | |
894 | \r | |
895 | @retval EFI_SUCCESS Operation completed successfully.\r | |
896 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
897 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
898 | \r | |
899 | **/\r | |
900 | EFI_STATUS\r | |
901 | EFIAPI\r | |
902 | MeasureVariable (\r | |
903 | IN TPM_PCRINDEX PCRIndex,\r | |
904 | IN TCG_EVENTTYPE EventType,\r | |
905 | IN CHAR16 *VarName,\r | |
906 | IN EFI_GUID *VendorGuid,\r | |
907 | IN VOID *VarData,\r | |
908 | IN UINTN VarSize\r | |
909 | )\r | |
910 | {\r | |
911 | EFI_STATUS Status;\r | |
912 | TCG_PCR_EVENT_HDR TcgEvent;\r | |
913 | UINTN VarNameLength;\r | |
914 | EFI_VARIABLE_DATA *VarLog;\r | |
915 | \r | |
916 | VarNameLength = StrLen (VarName);\r | |
917 | TcgEvent.PCRIndex = PCRIndex;\r | |
918 | TcgEvent.EventType = EventType;\r | |
919 | TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize\r | |
920 | - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));\r | |
921 | \r | |
922 | VarLog = (EFI_VARIABLE_DATA*)AllocatePool (TcgEvent.EventSize);\r | |
923 | if (VarLog == NULL) {\r | |
924 | return EFI_OUT_OF_RESOURCES;\r | |
925 | }\r | |
926 | \r | |
927 | VarLog->VariableName = *VendorGuid;\r | |
928 | VarLog->UnicodeNameLength = VarNameLength;\r | |
929 | VarLog->VariableDataLength = VarSize;\r | |
930 | CopyMem (\r | |
931 | VarLog->UnicodeName,\r | |
932 | VarName,\r | |
933 | VarNameLength * sizeof (*VarName)\r | |
934 | );\r | |
935 | CopyMem (\r | |
936 | (CHAR16 *)VarLog->UnicodeName + VarNameLength,\r | |
937 | VarData,\r | |
938 | VarSize\r | |
939 | );\r | |
940 | \r | |
941 | Status = TcgDxeHashLogExtendEventI (\r | |
942 | &mTcgDxeData,\r | |
943 | (UINT8*)VarData,\r | |
944 | VarSize,\r | |
945 | &TcgEvent,\r | |
946 | (UINT8*)VarLog\r | |
947 | );\r | |
948 | FreePool (VarLog);\r | |
949 | return Status;\r | |
950 | }\r | |
951 | \r | |
952 | /**\r | |
953 | Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].\r | |
954 | \r | |
955 | @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r | |
956 | @param[in] VendorGuid A unique identifier for the vendor.\r | |
957 | @param[out] VarSize The size of the variable data. \r | |
958 | @param[out] VarData Pointer to the content of the variable. \r | |
959 | \r | |
960 | @retval EFI_SUCCESS Operation completed successfully.\r | |
961 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
962 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
963 | \r | |
964 | **/\r | |
965 | EFI_STATUS\r | |
966 | EFIAPI\r | |
967 | ReadAndMeasureBootVariable (\r | |
968 | IN CHAR16 *VarName,\r | |
969 | IN EFI_GUID *VendorGuid,\r | |
970 | OUT UINTN *VarSize,\r | |
971 | OUT VOID **VarData\r | |
972 | )\r | |
973 | {\r | |
974 | EFI_STATUS Status;\r | |
975 | \r | |
976 | *VarData = ReadVariable (VarName, VendorGuid, VarSize);\r | |
977 | if (*VarData == NULL) {\r | |
978 | return EFI_NOT_FOUND;\r | |
979 | }\r | |
980 | \r | |
981 | Status = MeasureVariable (\r | |
982 | 5,\r | |
983 | EV_EFI_VARIABLE_BOOT,\r | |
984 | VarName,\r | |
985 | VendorGuid,\r | |
986 | *VarData,\r | |
987 | *VarSize\r | |
988 | );\r | |
989 | return Status;\r | |
990 | }\r | |
991 | \r | |
992 | /**\r | |
993 | Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.\r | |
994 | \r | |
995 | The EFI boot variables are BootOrder and Boot#### variables.\r | |
996 | \r | |
997 | @retval EFI_SUCCESS Operation completed successfully.\r | |
998 | @retval EFI_OUT_OF_RESOURCES Out of memory.\r | |
999 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
1000 | \r | |
1001 | **/\r | |
1002 | EFI_STATUS\r | |
1003 | EFIAPI\r | |
1004 | MeasureAllBootVariables (\r | |
1005 | VOID\r | |
1006 | )\r | |
1007 | {\r | |
1008 | EFI_STATUS Status;\r | |
1009 | UINT16 *BootOrder;\r | |
1010 | UINTN BootCount;\r | |
1011 | UINTN Index;\r | |
1012 | VOID *BootVarData;\r | |
1013 | UINTN Size;\r | |
1014 | \r | |
1015 | Status = ReadAndMeasureBootVariable (\r | |
1016 | mBootVarName,\r | |
1017 | &gEfiGlobalVariableGuid,\r | |
1018 | &BootCount,\r | |
1019 | (VOID **) &BootOrder\r | |
1020 | );\r | |
1021 | if (Status == EFI_NOT_FOUND) {\r | |
1022 | return EFI_SUCCESS;\r | |
1023 | }\r | |
1024 | ASSERT (BootOrder != NULL);\r | |
1025 | \r | |
1026 | if (EFI_ERROR (Status)) {\r | |
1027 | FreePool (BootOrder);\r | |
1028 | return Status;\r | |
1029 | }\r | |
1030 | \r | |
1031 | BootCount /= sizeof (*BootOrder);\r | |
1032 | for (Index = 0; Index < BootCount; Index++) {\r | |
1033 | UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);\r | |
1034 | Status = ReadAndMeasureBootVariable (\r | |
1035 | mBootVarName,\r | |
1036 | &gEfiGlobalVariableGuid,\r | |
1037 | &Size,\r | |
1038 | &BootVarData\r | |
1039 | );\r | |
1040 | if (!EFI_ERROR (Status)) {\r | |
1041 | FreePool (BootVarData);\r | |
1042 | }\r | |
1043 | }\r | |
1044 | \r | |
1045 | FreePool (BootOrder);\r | |
1046 | return EFI_SUCCESS;\r | |
1047 | }\r | |
1048 | \r | |
1049 | /**\r | |
1050 | Ready to Boot Event notification handler.\r | |
1051 | \r | |
1052 | Sequence of OS boot events is measured in this event notification handler.\r | |
1053 | \r | |
1054 | @param[in] Event Event whose notification function is being invoked\r | |
1055 | @param[in] Context Pointer to the notification function's context\r | |
1056 | \r | |
1057 | **/\r | |
1058 | VOID\r | |
1059 | EFIAPI\r | |
1060 | OnReadyToBoot (\r | |
1061 | IN EFI_EVENT Event,\r | |
1062 | IN VOID *Context\r | |
1063 | )\r | |
1064 | {\r | |
1065 | EFI_STATUS Status;\r | |
1066 | TPM_PCRINDEX PcrIndex;\r | |
1067 | \r | |
1068 | if (mBootAttempts == 0) {\r | |
1069 | \r | |
1070 | //\r | |
1071 | // Measure handoff tables.\r | |
1072 | //\r | |
1073 | Status = MeasureHandoffTables ();\r | |
1074 | if (EFI_ERROR (Status)) {\r | |
1075 | DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));\r | |
1076 | }\r | |
1077 | \r | |
1078 | //\r | |
1079 | // Measure BootOrder & Boot#### variables.\r | |
1080 | //\r | |
1081 | Status = MeasureAllBootVariables ();\r | |
1082 | if (EFI_ERROR (Status)) {\r | |
1083 | DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));\r | |
1084 | }\r | |
1085 | \r | |
1086 | //\r | |
1087 | // 1. This is the first boot attempt.\r | |
1088 | //\r | |
1089 | Status = TcgMeasureAction (\r | |
1090 | EFI_CALLING_EFI_APPLICATION\r | |
1091 | );\r | |
1092 | ASSERT_EFI_ERROR (Status);\r | |
1093 | \r | |
1094 | //\r | |
1095 | // 2. Draw a line between pre-boot env and entering post-boot env.\r | |
1096 | //\r | |
1097 | for (PcrIndex = 0; PcrIndex < 8; PcrIndex++) {\r | |
1098 | Status = MeasureSeparatorEvent (PcrIndex);\r | |
1099 | ASSERT_EFI_ERROR (Status);\r | |
1100 | }\r | |
1101 | \r | |
1102 | //\r | |
1103 | // 3. Measure GPT. It would be done in SAP driver.\r | |
1104 | //\r | |
1105 | \r | |
1106 | //\r | |
1107 | // 4. Measure PE/COFF OS loader. It would be done in SAP driver.\r | |
1108 | //\r | |
1109 | \r | |
1110 | //\r | |
1111 | // 5. Read & Measure variable. BootOrder already measured.\r | |
1112 | //\r | |
1113 | } else {\r | |
1114 | //\r | |
1115 | // 6. Not first attempt, meaning a return from last attempt\r | |
1116 | //\r | |
1117 | Status = TcgMeasureAction (\r | |
1118 | EFI_RETURNING_FROM_EFI_APPLICATOIN\r | |
1119 | );\r | |
1120 | ASSERT_EFI_ERROR (Status);\r | |
1121 | }\r | |
1122 | \r | |
1123 | DEBUG ((EFI_D_INFO, "TPM TcgDxe Measure Data when ReadyToBoot\n"));\r | |
1124 | //\r | |
1125 | // Increase boot attempt counter.\r | |
1126 | //\r | |
1127 | mBootAttempts++;\r | |
1128 | }\r | |
1129 | \r | |
1130 | /**\r | |
1131 | Install TCG ACPI Table when ACPI Table Protocol is available.\r | |
1132 | \r | |
0f7f6d23 | 1133 | A system's firmware uses an ACPI table to identify the system's TCG capabilities \r |
0c18794e | 1134 | to the Post-Boot environment. The information in this ACPI table is not guaranteed \r |
1135 | to be valid until the Host Platform transitions from pre-boot state to post-boot state. \r | |
1136 | \r | |
1137 | @param[in] Event Event whose notification function is being invoked\r | |
1138 | @param[in] Context Pointer to the notification function's context\r | |
1139 | **/\r | |
1140 | VOID\r | |
1141 | EFIAPI\r | |
1142 | InstallAcpiTable (\r | |
1143 | IN EFI_EVENT Event,\r | |
1144 | IN VOID* Context\r | |
1145 | )\r | |
1146 | {\r | |
1147 | UINTN TableKey;\r | |
1148 | EFI_STATUS Status;\r | |
1149 | EFI_ACPI_TABLE_PROTOCOL *AcpiTable;\r | |
1150 | UINT8 Checksum;\r | |
e84f07b5 | 1151 | UINT64 OemTableId;\r |
0c18794e | 1152 | \r |
1153 | Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);\r | |
1154 | if (EFI_ERROR (Status)) {\r | |
1155 | return;\r | |
1156 | }\r | |
1157 | \r | |
1158 | if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r | |
e84f07b5 SZ |
1159 | CopyMem (mTcgClientAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgClientAcpiTemplate.Header.OemId));\r |
1160 | OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);\r | |
1161 | CopyMem (&mTcgClientAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));\r | |
1162 | mTcgClientAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);\r | |
1163 | mTcgClientAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);\r | |
1164 | mTcgClientAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);\r | |
0c18794e | 1165 | //\r |
1166 | // The ACPI table must be checksumed before calling the InstallAcpiTable() \r | |
1167 | // service of the ACPI table protocol to install it.\r | |
1168 | //\r | |
1169 | Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgClientAcpiTemplate, sizeof (mTcgClientAcpiTemplate));\r | |
1170 | mTcgClientAcpiTemplate.Header.Checksum = Checksum;\r | |
1171 | \r | |
1172 | Status = AcpiTable->InstallAcpiTable (\r | |
1173 | AcpiTable,\r | |
1174 | &mTcgClientAcpiTemplate,\r | |
1175 | sizeof (mTcgClientAcpiTemplate),\r | |
1176 | &TableKey\r | |
1177 | );\r | |
1178 | } else {\r | |
e84f07b5 SZ |
1179 | CopyMem (mTcgServerAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgServerAcpiTemplate.Header.OemId));\r |
1180 | OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);\r | |
1181 | CopyMem (&mTcgServerAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));\r | |
1182 | mTcgServerAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);\r | |
1183 | mTcgServerAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);\r | |
1184 | mTcgServerAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);\r | |
0c18794e | 1185 | //\r |
1186 | // The ACPI table must be checksumed before calling the InstallAcpiTable() \r | |
1187 | // service of the ACPI table protocol to install it.\r | |
1188 | //\r | |
1189 | Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgServerAcpiTemplate, sizeof (mTcgServerAcpiTemplate));\r | |
1190 | mTcgServerAcpiTemplate.Header.Checksum = Checksum;\r | |
1191 | \r | |
1192 | Status = AcpiTable->InstallAcpiTable (\r | |
1193 | AcpiTable,\r | |
1194 | &mTcgServerAcpiTemplate,\r | |
1195 | sizeof (mTcgServerAcpiTemplate),\r | |
1196 | &TableKey\r | |
1197 | );\r | |
1198 | }\r | |
1199 | ASSERT_EFI_ERROR (Status);\r | |
1200 | }\r | |
1201 | \r | |
1202 | /**\r | |
1203 | Exit Boot Services Event notification handler.\r | |
1204 | \r | |
1205 | Measure invocation and success of ExitBootServices.\r | |
1206 | \r | |
1207 | @param[in] Event Event whose notification function is being invoked\r | |
1208 | @param[in] Context Pointer to the notification function's context\r | |
1209 | \r | |
1210 | **/\r | |
1211 | VOID\r | |
1212 | EFIAPI\r | |
1213 | OnExitBootServices (\r | |
1214 | IN EFI_EVENT Event,\r | |
1215 | IN VOID *Context\r | |
1216 | )\r | |
1217 | {\r | |
1218 | EFI_STATUS Status;\r | |
1219 | \r | |
1220 | //\r | |
1221 | // Measure invocation of ExitBootServices,\r | |
1222 | //\r | |
1223 | Status = TcgMeasureAction (\r | |
1224 | EFI_EXIT_BOOT_SERVICES_INVOCATION\r | |
1225 | );\r | |
1226 | ASSERT_EFI_ERROR (Status);\r | |
1227 | \r | |
1228 | //\r | |
1229 | // Measure success of ExitBootServices\r | |
1230 | //\r | |
1231 | Status = TcgMeasureAction (\r | |
1232 | EFI_EXIT_BOOT_SERVICES_SUCCEEDED\r | |
1233 | );\r | |
1234 | ASSERT_EFI_ERROR (Status);\r | |
1235 | }\r | |
1236 | \r | |
9e945f78 | 1237 | /**\r |
1238 | Exit Boot Services Failed Event notification handler.\r | |
1239 | \r | |
1240 | Measure Failure of ExitBootServices.\r | |
1241 | \r | |
1242 | @param[in] Event Event whose notification function is being invoked\r | |
1243 | @param[in] Context Pointer to the notification function's context\r | |
1244 | \r | |
1245 | **/\r | |
1246 | VOID\r | |
1247 | EFIAPI\r | |
1248 | OnExitBootServicesFailed (\r | |
1249 | IN EFI_EVENT Event,\r | |
1250 | IN VOID *Context\r | |
1251 | )\r | |
1252 | {\r | |
1253 | EFI_STATUS Status;\r | |
1254 | \r | |
1255 | //\r | |
1256 | // Measure Failure of ExitBootServices,\r | |
1257 | //\r | |
1258 | Status = TcgMeasureAction (\r | |
1259 | EFI_EXIT_BOOT_SERVICES_FAILED\r | |
1260 | );\r | |
1261 | ASSERT_EFI_ERROR (Status);\r | |
1262 | \r | |
1263 | }\r | |
1264 | \r | |
0c18794e | 1265 | /**\r |
1266 | Get TPM Deactivated state.\r | |
1267 | \r | |
1268 | @param[out] TPMDeactivatedFlag Returns TPM Deactivated state. \r | |
1269 | \r | |
1270 | @retval EFI_SUCCESS Operation completed successfully.\r | |
1271 | @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r | |
1272 | \r | |
1273 | **/\r | |
1274 | EFI_STATUS\r | |
1275 | GetTpmStatus (\r | |
1276 | OUT BOOLEAN *TPMDeactivatedFlag\r | |
1277 | )\r | |
1278 | {\r | |
1279 | EFI_STATUS Status;\r | |
1280 | TPM_STCLEAR_FLAGS VFlags;\r | |
1281 | \r | |
1282 | Status = TpmCommGetFlags (\r | |
1283 | mTcgDxeData.TpmHandle,\r | |
1284 | TPM_CAP_FLAG_VOLATILE,\r | |
1285 | &VFlags,\r | |
1286 | sizeof (VFlags)\r | |
1287 | );\r | |
1288 | if (!EFI_ERROR (Status)) {\r | |
1289 | *TPMDeactivatedFlag = VFlags.deactivated;\r | |
1290 | }\r | |
1291 | \r | |
1292 | return Status;\r | |
1293 | }\r | |
1294 | \r | |
1295 | /**\r | |
1296 | The driver's entry point.\r | |
1297 | \r | |
1298 | It publishes EFI TCG Protocol.\r | |
1299 | \r | |
1300 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
1301 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
1302 | \r | |
1303 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
1304 | @retval other Some error occurs when executing this entry point.\r | |
1305 | \r | |
1306 | **/\r | |
1307 | EFI_STATUS\r | |
1308 | EFIAPI\r | |
1309 | DriverEntry (\r | |
1310 | IN EFI_HANDLE ImageHandle,\r | |
1311 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1312 | )\r | |
1313 | {\r | |
1314 | EFI_STATUS Status;\r | |
1315 | EFI_EVENT Event;\r | |
1316 | VOID *Registration;\r | |
1317 | \r | |
c1d93242 JY |
1318 | if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){\r |
1319 | DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));\r | |
1320 | return EFI_UNSUPPORTED;\r | |
1321 | }\r | |
1322 | \r | |
0c18794e | 1323 | mTcgDxeData.TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;\r |
1324 | Status = TisPcRequestUseTpm (mTcgDxeData.TpmHandle);\r | |
1325 | if (EFI_ERROR (Status)) {\r | |
1326 | DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));\r | |
1327 | return Status;\r | |
1328 | }\r | |
1329 | \r | |
1330 | Status = GetTpmStatus (&mTcgDxeData.BsCap.TPMDeactivatedFlag);\r | |
1331 | if (EFI_ERROR (Status)) {\r | |
1332 | DEBUG ((\r | |
1333 | EFI_D_ERROR,\r | |
1334 | "Line %d in file " __FILE__ ":\n "\r | |
1335 | "DriverEntry: TPM not working properly\n",\r | |
1336 | __LINE__\r | |
1337 | ));\r | |
1338 | return Status;\r | |
1339 | }\r | |
1340 | \r | |
1341 | Status = gBS->InstallProtocolInterface (\r | |
1342 | &ImageHandle,\r | |
1343 | &gEfiTcgProtocolGuid,\r | |
1344 | EFI_NATIVE_INTERFACE,\r | |
1345 | &mTcgDxeData.TcgProtocol\r | |
1346 | );\r | |
0c18794e | 1347 | if (!EFI_ERROR (Status) && !mTcgDxeData.BsCap.TPMDeactivatedFlag) {\r |
1348 | //\r | |
1349 | // Setup the log area and copy event log from hob list to it\r | |
1350 | //\r | |
1351 | Status = SetupEventLog ();\r | |
1352 | ASSERT_EFI_ERROR (Status);\r | |
1353 | \r | |
1354 | //\r | |
1355 | // Measure handoff tables, Boot#### variables etc.\r | |
1356 | //\r | |
1357 | Status = EfiCreateEventReadyToBootEx (\r | |
1358 | TPL_CALLBACK,\r | |
1359 | OnReadyToBoot,\r | |
1360 | NULL,\r | |
1361 | &Event\r | |
1362 | );\r | |
1363 | \r | |
1364 | Status = gBS->CreateEventEx (\r | |
1365 | EVT_NOTIFY_SIGNAL,\r | |
1366 | TPL_NOTIFY,\r | |
1367 | OnExitBootServices,\r | |
1368 | NULL,\r | |
1369 | &gEfiEventExitBootServicesGuid,\r | |
1370 | &Event\r | |
1371 | );\r | |
9e945f78 | 1372 | \r |
1373 | //\r | |
1374 | // Measure Exit Boot Service failed \r | |
1375 | //\r | |
1376 | Status = gBS->CreateEventEx (\r | |
1377 | EVT_NOTIFY_SIGNAL,\r | |
1378 | TPL_NOTIFY,\r | |
1379 | OnExitBootServicesFailed,\r | |
1380 | NULL,\r | |
1381 | &gEventExitBootServicesFailedGuid,\r | |
1382 | &Event\r | |
1383 | );\r | |
0c18794e | 1384 | }\r |
1385 | \r | |
627c3961 | 1386 | //\r |
1387 | // Install ACPI Table\r | |
1388 | //\r | |
1389 | EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);\r | |
1390 | \r | |
0c18794e | 1391 | return Status;\r |
1392 | }\r |