]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/TcgDxe/TcgDxe.c
Add failed image Name in the Image Execution Information Table.
[mirror_edk2.git] / SecurityPkg / Tcg / TcgDxe / TcgDxe.c
CommitLineData
0c18794e 1/** @file \r
2 This module implements TCG EFI Protocol.\r
be02dcee 3 \r
4Caution: This module requires additional review when modified.\r
5This driver will have external input - TcgDxePassThroughToTpm\r
6This external input must be validated carefully to avoid security issue like\r
7buffer overflow, integer overflow.\r
8\r
9TcgDxePassThroughToTpm() will receive untrusted input and do basic validation.\r
10\r
d4193108 11Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>\r
0c18794e 12This program and the accompanying materials \r
13are licensed and made available under the terms and conditions of the BSD License \r
14which accompanies this distribution. The full text of the license may be found at \r
15http://opensource.org/licenses/bsd-license.php\r
16\r
17THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
18WITHOUT 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
62typedef 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
74EFI_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
95EFI_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
130UINTN mBootAttempts = 0;\r
131CHAR16 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
144EFI_STATUS\r
145GetProcessorsCpuLocation (\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
236EFI_STATUS\r
237EFIAPI\r
238TcgDxeStatusCheck (\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
295EFI_STATUS\r
296EFIAPI\r
297TcgDxeHashAll (\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
351EFI_STATUS\r
352EFIAPI\r
353TcgDxeLogEventI (\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
397EFI_STATUS\r
398EFIAPI\r
399TcgDxeLogEvent (\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
439EFI_STATUS\r
440EFIAPI\r
441TcgDxePassThroughToTpm (\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
486EFI_STATUS\r
487EFIAPI\r
488TcgDxeHashLogExtendEventI (\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
f7fe68db
CZ
508 if (EFI_ERROR(Status)) {\r
509 DEBUG ((DEBUG_ERROR, "TpmCommHashAll Failed. %x\n", Status));\r
510 return Status;\r
511 }\r
0c18794e 512 }\r
513\r
514 Status = TpmCommExtend (\r
515 TcgData->TpmHandle,\r
516 &NewEventHdr->Digest,\r
517 NewEventHdr->PCRIndex,\r
518 NULL\r
519 );\r
520 if (!EFI_ERROR (Status)) {\r
521 Status = TcgDxeLogEventI (TcgData, NewEventHdr, NewEventData);\r
522 }\r
523\r
524 return Status;\r
525}\r
526\r
527/**\r
528 This service abstracts the capability to do a hash operation on a data buffer,\r
529 extend a specific TPM PCR with the hash result, and add an entry to the Event Log\r
530\r
531 @param[in] This Indicates the calling context\r
532 @param[in] HashData Physical address of the start of the data buffer \r
533 to be hashed, extended, and logged.\r
534 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData\r
535 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation\r
536 @param[in, out] TCGLogData The physical address of the start of the data \r
537 buffer containing the TCG_PCR_EVENT data structure.\r
538 @param[in, out] EventNumber The event number of the event just logged.\r
539 @param[out] EventLogLastEntry Physical address of the first byte of the entry \r
540 just placed in the Event Log. If the Event Log was \r
541 empty when this function was called then this physical \r
542 address will be the same as the physical address of \r
543 the start of the Event Log.\r
544\r
545 @retval EFI_SUCCESS Operation completed successfully.\r
546 @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.\r
547 @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.\r
548 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
549 \r
550**/\r
551EFI_STATUS\r
552EFIAPI\r
553TcgDxeHashLogExtendEvent (\r
554 IN EFI_TCG_PROTOCOL *This,\r
555 IN EFI_PHYSICAL_ADDRESS HashData,\r
556 IN UINT64 HashDataLen,\r
557 IN TPM_ALGORITHM_ID AlgorithmId,\r
558 IN OUT TCG_PCR_EVENT *TCGLogData,\r
559 IN OUT UINT32 *EventNumber,\r
560 OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry\r
561 )\r
562{\r
563 TCG_DXE_DATA *TcgData;\r
15f2d739 564 EFI_STATUS Status;\r
0c18794e 565\r
677e5c0b 566 if (TCGLogData == NULL || EventLogLastEntry == NULL){\r
567 return EFI_INVALID_PARAMETER;\r
568 }\r
569\r
0c18794e 570 TcgData = TCG_DXE_DATA_FROM_THIS (This);\r
571 \r
572 if (TcgData->BsCap.TPMDeactivatedFlag) {\r
573 return EFI_DEVICE_ERROR;\r
574 }\r
575 \r
576 if (AlgorithmId != TPM_ALG_SHA) {\r
577 return EFI_UNSUPPORTED;\r
578 }\r
579\r
15f2d739 580 Status = TcgDxeHashLogExtendEventI (\r
581 TcgData,\r
582 (UINT8 *) (UINTN) HashData,\r
583 HashDataLen,\r
584 (TCG_PCR_EVENT_HDR*)TCGLogData,\r
585 TCGLogData->Event\r
586 );\r
587\r
588 if (!EFI_ERROR(Status)){\r
589 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN) TcgData->LastEvent;\r
590 }\r
591\r
592 return Status;\r
0c18794e 593}\r
594\r
595TCG_DXE_DATA mTcgDxeData = {\r
596 {\r
597 TcgDxeStatusCheck,\r
598 TcgDxeHashAll,\r
599 TcgDxeLogEvent,\r
600 TcgDxePassThroughToTpm,\r
601 TcgDxeHashLogExtendEvent\r
602 },\r
603 {\r
604 sizeof (mTcgDxeData.BsCap),\r
605 { 1, 2, 0, 0 },\r
606 { 1, 2, 0, 0 },\r
607 1,\r
608 TRUE,\r
609 FALSE\r
610 },\r
611 &mTcgClientAcpiTemplate,\r
612 &mTcgServerAcpiTemplate,\r
613 0,\r
614 NULL,\r
615 NULL\r
616};\r
617\r
618/**\r
619 Initialize the Event Log and log events passed from the PEI phase.\r
620\r
621 @retval EFI_SUCCESS Operation completed successfully.\r
622 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
623\r
624**/\r
625EFI_STATUS\r
626EFIAPI\r
627SetupEventLog (\r
628 VOID\r
629 )\r
630{\r
631 EFI_STATUS Status;\r
632 TCG_PCR_EVENT *TcgEvent;\r
633 EFI_PEI_HOB_POINTERS GuidHob;\r
634 EFI_PHYSICAL_ADDRESS Lasa;\r
635 \r
636 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r
637 Lasa = mTcgClientAcpiTemplate.Lasa;\r
638 \r
639 Status = gBS->AllocatePages (\r
640 AllocateMaxAddress,\r
641 EfiACPIMemoryNVS,\r
642 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),\r
643 &Lasa\r
644 );\r
645 if (EFI_ERROR (Status)) {\r
646 return Status;\r
647 }\r
648 mTcgClientAcpiTemplate.Lasa = Lasa;\r
649 //\r
650 // To initialize them as 0xFF is recommended \r
651 // because the OS can know the last entry for that.\r
652 //\r
653 SetMem ((VOID *)(UINTN)mTcgClientAcpiTemplate.Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r
654 mTcgClientAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
655 \r
656 } else {\r
657 Lasa = mTcgServerAcpiTemplate.Lasa;\r
658 \r
659 Status = gBS->AllocatePages (\r
660 AllocateMaxAddress,\r
661 EfiACPIMemoryNVS,\r
662 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),\r
663 &Lasa\r
664 );\r
665 if (EFI_ERROR (Status)) {\r
666 return Status;\r
667 }\r
668 mTcgServerAcpiTemplate.Lasa = Lasa;\r
669 //\r
670 // To initialize them as 0xFF is recommended \r
671 // because the OS can know the last entry for that.\r
672 //\r
673 SetMem ((VOID *)(UINTN)mTcgServerAcpiTemplate.Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r
674 mTcgServerAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
675 }\r
676\r
677 GuidHob.Raw = GetHobList ();\r
678 while (!EFI_ERROR (Status) && \r
679 (GuidHob.Raw = GetNextGuidHob (&gTcgEventEntryHobGuid, GuidHob.Raw)) != NULL) {\r
680 TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);\r
681 GuidHob.Raw = GET_NEXT_HOB (GuidHob);\r
682 Status = TcgDxeLogEventI (\r
683 &mTcgDxeData,\r
684 (TCG_PCR_EVENT_HDR*)TcgEvent,\r
685 TcgEvent->Event\r
686 );\r
687 }\r
688\r
689 return Status;\r
690}\r
691\r
692/**\r
693 Measure and log an action string, and extend the measurement result into PCR[5].\r
694\r
695 @param[in] String A specific string that indicates an Action event. \r
696 \r
697 @retval EFI_SUCCESS Operation completed successfully.\r
698 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
699\r
700**/\r
701EFI_STATUS\r
702EFIAPI\r
703TcgMeasureAction (\r
704 IN CHAR8 *String\r
705 )\r
706{\r
707 TCG_PCR_EVENT_HDR TcgEvent;\r
708\r
709 TcgEvent.PCRIndex = 5;\r
710 TcgEvent.EventType = EV_EFI_ACTION;\r
711 TcgEvent.EventSize = (UINT32)AsciiStrLen (String);\r
712 return TcgDxeHashLogExtendEventI (\r
713 &mTcgDxeData,\r
714 (UINT8*)String,\r
715 TcgEvent.EventSize,\r
716 &TcgEvent,\r
717 (UINT8 *) String\r
718 );\r
719}\r
720\r
721/**\r
722 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].\r
723\r
724 @retval EFI_SUCCESS Operation completed successfully.\r
725 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
726\r
727**/\r
728EFI_STATUS\r
729EFIAPI\r
730MeasureHandoffTables (\r
731 VOID\r
732 )\r
733{\r
734 EFI_STATUS Status;\r
735 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;\r
736 TCG_PCR_EVENT_HDR TcgEvent;\r
737 EFI_HANDOFF_TABLE_POINTERS HandoffTables;\r
b25380e3 738 UINTN ProcessorNum;\r
739 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;\r
0c18794e 740\r
d4193108
ED
741 ProcessorLocBuf = NULL;\r
742\r
b25380e3 743 //\r
744 // Measure SMBIOS with EV_EFI_HANDOFF_TABLES to PCR[1]\r
745 //\r
0c18794e 746 Status = EfiGetSystemConfigurationTable (\r
747 &gEfiSmbiosTableGuid,\r
748 (VOID **) &SmbiosTable\r
749 );\r
750\r
f7fe68db 751 if (!EFI_ERROR (Status) && SmbiosTable != NULL) {\r
0c18794e 752 TcgEvent.PCRIndex = 1;\r
753 TcgEvent.EventType = EV_EFI_HANDOFF_TABLES;\r
754 TcgEvent.EventSize = sizeof (HandoffTables);\r
755\r
756 HandoffTables.NumberOfTables = 1;\r
757 HandoffTables.TableEntry[0].VendorGuid = gEfiSmbiosTableGuid;\r
758 HandoffTables.TableEntry[0].VendorTable = SmbiosTable;\r
759\r
760 DEBUG ((DEBUG_INFO, "The Smbios Table starts at: 0x%x\n", SmbiosTable->TableAddress));\r
761 DEBUG ((DEBUG_INFO, "The Smbios Table size: 0x%x\n", SmbiosTable->TableLength));\r
762\r
763 Status = TcgDxeHashLogExtendEventI (\r
764 &mTcgDxeData,\r
765 (UINT8*)(UINTN)SmbiosTable->TableAddress,\r
766 SmbiosTable->TableLength,\r
767 &TcgEvent,\r
768 (UINT8*)&HandoffTables\r
769 );\r
770 }\r
771\r
b25380e3 772 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {\r
773 //\r
774 // Tcg Server spec. \r
775 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]\r
776 //\r
777 Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);\r
778\r
779 if (!EFI_ERROR(Status)){\r
780 TcgEvent.PCRIndex = 1;\r
781 TcgEvent.EventType = EV_TABLE_OF_DEVICES;\r
782 TcgEvent.EventSize = sizeof (HandoffTables);\r
783\r
784 HandoffTables.NumberOfTables = 1;\r
785 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;\r
786 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;\r
787\r
788 Status = TcgDxeHashLogExtendEventI (\r
789 &mTcgDxeData,\r
790 (UINT8*)(UINTN)ProcessorLocBuf,\r
791 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,\r
792 &TcgEvent,\r
793 (UINT8*)&HandoffTables\r
794 );\r
795\r
796 FreePool(ProcessorLocBuf);\r
797 }\r
798 }\r
799\r
0c18794e 800 return Status;\r
801}\r
802\r
803/**\r
804 Measure and log Separator event, and extend the measurement result into a specific PCR.\r
805\r
806 @param[in] PCRIndex PCR index. \r
807\r
808 @retval EFI_SUCCESS Operation completed successfully.\r
809 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
810\r
811**/\r
812EFI_STATUS\r
813EFIAPI\r
814MeasureSeparatorEvent (\r
815 IN TPM_PCRINDEX PCRIndex\r
816 )\r
817{\r
818 TCG_PCR_EVENT_HDR TcgEvent;\r
819 UINT32 EventData;\r
820\r
821 EventData = 0;\r
822 TcgEvent.PCRIndex = PCRIndex;\r
823 TcgEvent.EventType = EV_SEPARATOR;\r
824 TcgEvent.EventSize = (UINT32)sizeof (EventData);\r
825 return TcgDxeHashLogExtendEventI (\r
826 &mTcgDxeData,\r
827 (UINT8 *)&EventData,\r
828 sizeof (EventData),\r
829 &TcgEvent,\r
830 (UINT8 *)&EventData\r
831 );\r
832}\r
833\r
834/**\r
835 Read an EFI Variable.\r
836\r
837 This function allocates a buffer to return the contents of the variable. The caller is\r
838 responsible for freeing the buffer.\r
839\r
840 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
841 @param[in] VendorGuid A unique identifier for the vendor.\r
842 @param[out] VarSize The size of the variable data. \r
843\r
844 @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.\r
845\r
846**/\r
847VOID *\r
848EFIAPI\r
849ReadVariable (\r
850 IN CHAR16 *VarName,\r
851 IN EFI_GUID *VendorGuid,\r
852 OUT UINTN *VarSize\r
853 )\r
854{\r
855 EFI_STATUS Status;\r
856 VOID *VarData;\r
857\r
858 *VarSize = 0;\r
859 Status = gRT->GetVariable (\r
860 VarName,\r
861 VendorGuid,\r
862 NULL,\r
863 VarSize,\r
864 NULL\r
865 );\r
866 if (Status != EFI_BUFFER_TOO_SMALL) {\r
867 return NULL;\r
868 }\r
869\r
870 VarData = AllocatePool (*VarSize);\r
871 if (VarData != NULL) {\r
872 Status = gRT->GetVariable (\r
873 VarName,\r
874 VendorGuid,\r
875 NULL,\r
876 VarSize,\r
877 VarData\r
878 );\r
879 if (EFI_ERROR (Status)) {\r
880 FreePool (VarData);\r
881 VarData = NULL;\r
882 *VarSize = 0;\r
883 }\r
884 }\r
885 return VarData;\r
886}\r
887\r
888/**\r
889 Measure and log an EFI variable, and extend the measurement result into a specific PCR.\r
890\r
891 @param[in] PCRIndex PCR Index. \r
892 @param[in] EventType Event type. \r
893 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
894 @param[in] VendorGuid A unique identifier for the vendor.\r
895 @param[in] VarData The content of the variable data. \r
896 @param[in] VarSize The size of the variable data. \r
897 \r
898 @retval EFI_SUCCESS Operation completed successfully.\r
899 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
900 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
901\r
902**/\r
903EFI_STATUS\r
904EFIAPI\r
905MeasureVariable (\r
906 IN TPM_PCRINDEX PCRIndex,\r
907 IN TCG_EVENTTYPE EventType,\r
908 IN CHAR16 *VarName,\r
909 IN EFI_GUID *VendorGuid,\r
910 IN VOID *VarData,\r
911 IN UINTN VarSize\r
912 )\r
913{\r
914 EFI_STATUS Status;\r
915 TCG_PCR_EVENT_HDR TcgEvent;\r
916 UINTN VarNameLength;\r
917 EFI_VARIABLE_DATA *VarLog;\r
918\r
919 VarNameLength = StrLen (VarName);\r
920 TcgEvent.PCRIndex = PCRIndex;\r
921 TcgEvent.EventType = EventType;\r
922 TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize\r
923 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));\r
924\r
925 VarLog = (EFI_VARIABLE_DATA*)AllocatePool (TcgEvent.EventSize);\r
926 if (VarLog == NULL) {\r
927 return EFI_OUT_OF_RESOURCES;\r
928 }\r
929\r
930 VarLog->VariableName = *VendorGuid;\r
931 VarLog->UnicodeNameLength = VarNameLength;\r
932 VarLog->VariableDataLength = VarSize;\r
933 CopyMem (\r
934 VarLog->UnicodeName,\r
935 VarName,\r
936 VarNameLength * sizeof (*VarName)\r
937 );\r
938 CopyMem (\r
939 (CHAR16 *)VarLog->UnicodeName + VarNameLength,\r
940 VarData,\r
941 VarSize\r
942 );\r
943\r
944 Status = TcgDxeHashLogExtendEventI (\r
945 &mTcgDxeData,\r
946 (UINT8*)VarData,\r
947 VarSize,\r
948 &TcgEvent,\r
949 (UINT8*)VarLog\r
950 );\r
951 FreePool (VarLog);\r
952 return Status;\r
953}\r
954\r
955/**\r
956 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].\r
957\r
958 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
959 @param[in] VendorGuid A unique identifier for the vendor.\r
960 @param[out] VarSize The size of the variable data. \r
961 @param[out] VarData Pointer to the content of the variable. \r
962 \r
963 @retval EFI_SUCCESS Operation completed successfully.\r
964 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
965 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
966\r
967**/\r
968EFI_STATUS\r
969EFIAPI\r
970ReadAndMeasureBootVariable (\r
971 IN CHAR16 *VarName,\r
972 IN EFI_GUID *VendorGuid,\r
973 OUT UINTN *VarSize,\r
974 OUT VOID **VarData\r
975 )\r
976{\r
977 EFI_STATUS Status;\r
978\r
979 *VarData = ReadVariable (VarName, VendorGuid, VarSize);\r
980 if (*VarData == NULL) {\r
981 return EFI_NOT_FOUND;\r
982 }\r
983\r
984 Status = MeasureVariable (\r
985 5,\r
986 EV_EFI_VARIABLE_BOOT,\r
987 VarName,\r
988 VendorGuid,\r
989 *VarData,\r
990 *VarSize\r
991 );\r
992 return Status;\r
993}\r
994\r
995/**\r
996 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.\r
997\r
998 The EFI boot variables are BootOrder and Boot#### variables.\r
999\r
1000 @retval EFI_SUCCESS Operation completed successfully.\r
1001 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
1002 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1003\r
1004**/\r
1005EFI_STATUS\r
1006EFIAPI\r
1007MeasureAllBootVariables (\r
1008 VOID\r
1009 )\r
1010{\r
1011 EFI_STATUS Status;\r
1012 UINT16 *BootOrder;\r
1013 UINTN BootCount;\r
1014 UINTN Index;\r
1015 VOID *BootVarData;\r
1016 UINTN Size;\r
1017\r
1018 Status = ReadAndMeasureBootVariable (\r
1019 mBootVarName,\r
1020 &gEfiGlobalVariableGuid,\r
1021 &BootCount,\r
1022 (VOID **) &BootOrder\r
1023 );\r
1024 if (Status == EFI_NOT_FOUND) {\r
1025 return EFI_SUCCESS;\r
1026 }\r
0c18794e 1027\r
1028 if (EFI_ERROR (Status)) {\r
f7fe68db
CZ
1029 //\r
1030 // BootOrder can't be NULL if status is not EFI_NOT_FOUND\r
1031 //\r
0c18794e 1032 FreePool (BootOrder);\r
1033 return Status;\r
1034 }\r
1035\r
1036 BootCount /= sizeof (*BootOrder);\r
1037 for (Index = 0; Index < BootCount; Index++) {\r
1038 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);\r
1039 Status = ReadAndMeasureBootVariable (\r
1040 mBootVarName,\r
1041 &gEfiGlobalVariableGuid,\r
1042 &Size,\r
1043 &BootVarData\r
1044 );\r
1045 if (!EFI_ERROR (Status)) {\r
1046 FreePool (BootVarData);\r
1047 }\r
1048 }\r
1049\r
1050 FreePool (BootOrder);\r
1051 return EFI_SUCCESS;\r
1052}\r
1053\r
1054/**\r
1055 Ready to Boot Event notification handler.\r
1056\r
1057 Sequence of OS boot events is measured in this event notification handler.\r
1058\r
1059 @param[in] Event Event whose notification function is being invoked\r
1060 @param[in] Context Pointer to the notification function's context\r
1061\r
1062**/\r
1063VOID\r
1064EFIAPI\r
1065OnReadyToBoot (\r
1066 IN EFI_EVENT Event,\r
1067 IN VOID *Context\r
1068 )\r
1069{\r
1070 EFI_STATUS Status;\r
1071 TPM_PCRINDEX PcrIndex;\r
1072\r
1073 if (mBootAttempts == 0) {\r
1074\r
1075 //\r
1076 // Measure handoff tables.\r
1077 //\r
1078 Status = MeasureHandoffTables ();\r
1079 if (EFI_ERROR (Status)) {\r
1080 DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));\r
1081 }\r
1082\r
1083 //\r
1084 // Measure BootOrder & Boot#### variables.\r
1085 //\r
1086 Status = MeasureAllBootVariables ();\r
1087 if (EFI_ERROR (Status)) {\r
1088 DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));\r
1089 }\r
1090\r
1091 //\r
1092 // 1. This is the first boot attempt.\r
1093 //\r
1094 Status = TcgMeasureAction (\r
1095 EFI_CALLING_EFI_APPLICATION\r
1096 );\r
f7fe68db
CZ
1097 if (EFI_ERROR (Status)) {\r
1098 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));\r
1099 }\r
0c18794e 1100\r
1101 //\r
1102 // 2. Draw a line between pre-boot env and entering post-boot env.\r
1103 //\r
1104 for (PcrIndex = 0; PcrIndex < 8; PcrIndex++) {\r
1105 Status = MeasureSeparatorEvent (PcrIndex);\r
f7fe68db
CZ
1106 if (EFI_ERROR (Status)) {\r
1107 DEBUG ((EFI_D_ERROR, "Seperator Event not Measured. Error!\n"));\r
1108 }\r
0c18794e 1109 }\r
1110\r
1111 //\r
1112 // 3. Measure GPT. It would be done in SAP driver.\r
1113 //\r
1114\r
1115 //\r
1116 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.\r
1117 //\r
1118\r
1119 //\r
1120 // 5. Read & Measure variable. BootOrder already measured.\r
1121 //\r
1122 } else {\r
1123 //\r
1124 // 6. Not first attempt, meaning a return from last attempt\r
1125 //\r
1126 Status = TcgMeasureAction (\r
1127 EFI_RETURNING_FROM_EFI_APPLICATOIN\r
1128 );\r
f7fe68db
CZ
1129 if (EFI_ERROR (Status)) {\r
1130 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));\r
1131 }\r
0c18794e 1132 }\r
1133\r
1134 DEBUG ((EFI_D_INFO, "TPM TcgDxe Measure Data when ReadyToBoot\n"));\r
1135 //\r
1136 // Increase boot attempt counter.\r
1137 //\r
1138 mBootAttempts++;\r
1139}\r
1140\r
1141/**\r
1142 Install TCG ACPI Table when ACPI Table Protocol is available.\r
1143\r
0f7f6d23 1144 A system's firmware uses an ACPI table to identify the system's TCG capabilities \r
0c18794e 1145 to the Post-Boot environment. The information in this ACPI table is not guaranteed \r
1146 to be valid until the Host Platform transitions from pre-boot state to post-boot state. \r
1147\r
1148 @param[in] Event Event whose notification function is being invoked\r
1149 @param[in] Context Pointer to the notification function's context\r
1150**/\r
1151VOID\r
1152EFIAPI\r
1153InstallAcpiTable (\r
1154 IN EFI_EVENT Event,\r
1155 IN VOID* Context\r
1156 )\r
1157{\r
1158 UINTN TableKey;\r
1159 EFI_STATUS Status;\r
1160 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;\r
1161 UINT8 Checksum;\r
e84f07b5 1162 UINT64 OemTableId;\r
0c18794e 1163\r
1164 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);\r
1165 if (EFI_ERROR (Status)) {\r
1166 return;\r
1167 }\r
1168\r
1169 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r
e84f07b5
SZ
1170 CopyMem (mTcgClientAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgClientAcpiTemplate.Header.OemId));\r
1171 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);\r
1172 CopyMem (&mTcgClientAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));\r
1173 mTcgClientAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);\r
1174 mTcgClientAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);\r
1175 mTcgClientAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);\r
0c18794e 1176 //\r
1177 // The ACPI table must be checksumed before calling the InstallAcpiTable() \r
1178 // service of the ACPI table protocol to install it.\r
1179 //\r
1180 Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgClientAcpiTemplate, sizeof (mTcgClientAcpiTemplate));\r
1181 mTcgClientAcpiTemplate.Header.Checksum = Checksum;\r
1182\r
1183 Status = AcpiTable->InstallAcpiTable (\r
1184 AcpiTable,\r
1185 &mTcgClientAcpiTemplate,\r
1186 sizeof (mTcgClientAcpiTemplate),\r
1187 &TableKey\r
1188 );\r
1189 } else {\r
e84f07b5
SZ
1190 CopyMem (mTcgServerAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgServerAcpiTemplate.Header.OemId));\r
1191 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);\r
1192 CopyMem (&mTcgServerAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));\r
1193 mTcgServerAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);\r
1194 mTcgServerAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);\r
1195 mTcgServerAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);\r
0c18794e 1196 //\r
1197 // The ACPI table must be checksumed before calling the InstallAcpiTable() \r
1198 // service of the ACPI table protocol to install it.\r
1199 //\r
1200 Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgServerAcpiTemplate, sizeof (mTcgServerAcpiTemplate));\r
1201 mTcgServerAcpiTemplate.Header.Checksum = Checksum;\r
1202\r
1203 Status = AcpiTable->InstallAcpiTable (\r
1204 AcpiTable,\r
1205 &mTcgServerAcpiTemplate,\r
1206 sizeof (mTcgServerAcpiTemplate),\r
1207 &TableKey\r
1208 );\r
1209 }\r
f7fe68db
CZ
1210\r
1211 if (EFI_ERROR (Status)) {\r
1212 DEBUG((EFI_D_ERROR, "Tcg Acpi Table installation failure"));\r
1213 }\r
0c18794e 1214}\r
1215\r
1216/**\r
1217 Exit Boot Services Event notification handler.\r
1218\r
1219 Measure invocation and success of ExitBootServices.\r
1220\r
1221 @param[in] Event Event whose notification function is being invoked\r
1222 @param[in] Context Pointer to the notification function's context\r
1223\r
1224**/\r
1225VOID\r
1226EFIAPI\r
1227OnExitBootServices (\r
1228 IN EFI_EVENT Event,\r
1229 IN VOID *Context\r
1230 )\r
1231{\r
1232 EFI_STATUS Status;\r
1233\r
1234 //\r
1235 // Measure invocation of ExitBootServices,\r
1236 //\r
1237 Status = TcgMeasureAction (\r
1238 EFI_EXIT_BOOT_SERVICES_INVOCATION\r
1239 );\r
f7fe68db
CZ
1240 if (EFI_ERROR (Status)) {\r
1241 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));\r
1242 }\r
0c18794e 1243\r
1244 //\r
1245 // Measure success of ExitBootServices\r
1246 //\r
1247 Status = TcgMeasureAction (\r
1248 EFI_EXIT_BOOT_SERVICES_SUCCEEDED\r
1249 );\r
f7fe68db
CZ
1250 if (EFI_ERROR (Status)){\r
1251 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));\r
1252 }\r
0c18794e 1253}\r
1254\r
9e945f78 1255/**\r
1256 Exit Boot Services Failed Event notification handler.\r
1257\r
1258 Measure Failure of ExitBootServices.\r
1259\r
1260 @param[in] Event Event whose notification function is being invoked\r
1261 @param[in] Context Pointer to the notification function's context\r
1262\r
1263**/\r
1264VOID\r
1265EFIAPI\r
1266OnExitBootServicesFailed (\r
1267 IN EFI_EVENT Event,\r
1268 IN VOID *Context\r
1269 )\r
1270{\r
1271 EFI_STATUS Status;\r
1272\r
1273 //\r
1274 // Measure Failure of ExitBootServices,\r
1275 //\r
1276 Status = TcgMeasureAction (\r
1277 EFI_EXIT_BOOT_SERVICES_FAILED\r
1278 );\r
f7fe68db
CZ
1279 if (EFI_ERROR (Status)){\r
1280 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));\r
1281 }\r
9e945f78 1282}\r
1283\r
0c18794e 1284/**\r
1285 Get TPM Deactivated state.\r
1286\r
1287 @param[out] TPMDeactivatedFlag Returns TPM Deactivated state. \r
1288\r
1289 @retval EFI_SUCCESS Operation completed successfully.\r
1290 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
1291\r
1292**/\r
1293EFI_STATUS\r
1294GetTpmStatus (\r
1295 OUT BOOLEAN *TPMDeactivatedFlag\r
1296 )\r
1297{\r
1298 EFI_STATUS Status;\r
1299 TPM_STCLEAR_FLAGS VFlags;\r
1300\r
1301 Status = TpmCommGetFlags (\r
1302 mTcgDxeData.TpmHandle,\r
1303 TPM_CAP_FLAG_VOLATILE,\r
1304 &VFlags,\r
1305 sizeof (VFlags)\r
1306 );\r
1307 if (!EFI_ERROR (Status)) {\r
1308 *TPMDeactivatedFlag = VFlags.deactivated;\r
1309 }\r
1310\r
1311 return Status;\r
1312}\r
1313\r
1314/**\r
1315 The driver's entry point.\r
1316\r
1317 It publishes EFI TCG Protocol.\r
1318\r
1319 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
1320 @param[in] SystemTable A pointer to the EFI System Table.\r
1321 \r
1322 @retval EFI_SUCCESS The entry point is executed successfully.\r
1323 @retval other Some error occurs when executing this entry point.\r
1324\r
1325**/\r
1326EFI_STATUS\r
1327EFIAPI\r
1328DriverEntry (\r
1329 IN EFI_HANDLE ImageHandle,\r
1330 IN EFI_SYSTEM_TABLE *SystemTable\r
1331 )\r
1332{\r
1333 EFI_STATUS Status;\r
1334 EFI_EVENT Event;\r
1335 VOID *Registration;\r
1336\r
c1d93242
JY
1337 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){\r
1338 DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));\r
1339 return EFI_UNSUPPORTED;\r
1340 }\r
1341\r
0c18794e 1342 mTcgDxeData.TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;\r
1343 Status = TisPcRequestUseTpm (mTcgDxeData.TpmHandle);\r
1344 if (EFI_ERROR (Status)) {\r
1345 DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));\r
1346 return Status;\r
1347 }\r
1348\r
1349 Status = GetTpmStatus (&mTcgDxeData.BsCap.TPMDeactivatedFlag);\r
1350 if (EFI_ERROR (Status)) {\r
1351 DEBUG ((\r
1352 EFI_D_ERROR,\r
1353 "Line %d in file " __FILE__ ":\n "\r
1354 "DriverEntry: TPM not working properly\n",\r
1355 __LINE__\r
1356 ));\r
1357 return Status;\r
1358 }\r
1359\r
1360 Status = gBS->InstallProtocolInterface (\r
1361 &ImageHandle,\r
1362 &gEfiTcgProtocolGuid,\r
1363 EFI_NATIVE_INTERFACE,\r
1364 &mTcgDxeData.TcgProtocol\r
1365 );\r
0c18794e 1366 if (!EFI_ERROR (Status) && !mTcgDxeData.BsCap.TPMDeactivatedFlag) {\r
1367 //\r
1368 // Setup the log area and copy event log from hob list to it\r
1369 //\r
1370 Status = SetupEventLog ();\r
1371 ASSERT_EFI_ERROR (Status);\r
1372\r
1373 //\r
1374 // Measure handoff tables, Boot#### variables etc.\r
1375 //\r
1376 Status = EfiCreateEventReadyToBootEx (\r
1377 TPL_CALLBACK,\r
1378 OnReadyToBoot,\r
1379 NULL,\r
1380 &Event\r
1381 );\r
1382\r
1383 Status = gBS->CreateEventEx (\r
1384 EVT_NOTIFY_SIGNAL,\r
1385 TPL_NOTIFY,\r
1386 OnExitBootServices,\r
1387 NULL,\r
1388 &gEfiEventExitBootServicesGuid,\r
1389 &Event\r
1390 );\r
9e945f78 1391\r
1392 //\r
1393 // Measure Exit Boot Service failed \r
1394 //\r
1395 Status = gBS->CreateEventEx (\r
1396 EVT_NOTIFY_SIGNAL,\r
1397 TPL_NOTIFY,\r
1398 OnExitBootServicesFailed,\r
1399 NULL,\r
1400 &gEventExitBootServicesFailedGuid,\r
1401 &Event\r
1402 );\r
0c18794e 1403 }\r
1404\r
627c3961 1405 //\r
1406 // Install ACPI Table\r
1407 //\r
1408 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);\r
1409 \r
0c18794e 1410 return Status;\r
1411}\r