Handle TPM device error and avoid deadloop in BDS.
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEDxe / TrEEDxe.c
1 /** @file
2 This module implements TrEE Protocol.
3
4 Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16 #include <IndustryStandard/Acpi.h>
17 #include <IndustryStandard/PeImage.h>
18 #include <IndustryStandard/SmBios.h>
19 #include <IndustryStandard/TcpaAcpi.h>
20
21 #include <Guid/GlobalVariable.h>
22 #include <Guid/SmBios.h>
23 #include <Guid/HobList.h>
24 #include <Guid/TcgEventHob.h>
25 #include <Guid/EventGroup.h>
26 #include <Guid/EventExitBootServiceFailed.h>
27 #include <Guid/ImageAuthentication.h>
28 #include <Guid/TpmInstance.h>
29
30 #include <Protocol/DevicePath.h>
31 #include <Protocol/AcpiTable.h>
32 #include <Protocol/MpService.h>
33 #include <Protocol/VariableWrite.h>
34 #include <Protocol/TrEEProtocol.h>
35
36 #include <Library/DebugLib.h>
37 #include <Library/BaseMemoryLib.h>
38 #include <Library/UefiRuntimeServicesTableLib.h>
39 #include <Library/UefiDriverEntryPoint.h>
40 #include <Library/HobLib.h>
41 #include <Library/UefiBootServicesTableLib.h>
42 #include <Library/BaseLib.h>
43 #include <Library/MemoryAllocationLib.h>
44 #include <Library/PrintLib.h>
45 #include <Library/Tpm2CommandLib.h>
46 #include <Library/PcdLib.h>
47 #include <Library/UefiLib.h>
48 #include <Library/Tpm2DeviceLib.h>
49 #include <Library/HashLib.h>
50 #include <Library/PerformanceLib.h>
51 #include <Library/ReportStatusCodeLib.h>
52
53 #define PERF_ID_TREE_DXE 0x3120
54
55 typedef struct {
56 CHAR16 *VariableName;
57 EFI_GUID *VendorGuid;
58 } VARIABLE_TYPE;
59
60 #define EFI_TCG_LOG_AREA_SIZE 0x10000
61
62 #define TREE_DEFAULT_MAX_COMMAND_SIZE 0x1000
63 #define TREE_DEFAULT_MAX_RESPONSE_SIZE 0x1000
64
65 typedef struct {
66 EFI_GUID *EventGuid;
67 TREE_EVENT_LOG_FORMAT LogFormat;
68 } TREE_EVENT_INFO_STRUCT;
69
70 TREE_EVENT_INFO_STRUCT mTreeEventInfo[] = {
71 {&gTcgEventEntryHobGuid, TREE_EVENT_LOG_FORMAT_TCG_1_2},
72 };
73
74 #define TCG_EVENT_LOG_AREA_COUNT_MAX 2
75
76 typedef struct {
77 TREE_EVENT_LOG_FORMAT EventLogFormat;
78 EFI_PHYSICAL_ADDRESS Lasa;
79 UINT64 Laml;
80 UINTN EventLogSize;
81 UINT8 *LastEvent;
82 BOOLEAN EventLogStarted;
83 BOOLEAN EventLogTruncated;
84 } TCG_EVENT_LOG_AREA_STRUCT;
85
86 typedef struct _TCG_DXE_DATA {
87 TREE_BOOT_SERVICE_CAPABILITY BsCap;
88 EFI_TCG_CLIENT_ACPI_TABLE *TcgClientAcpiTable;
89 EFI_TCG_SERVER_ACPI_TABLE *TcgServerAcpiTable;
90 TCG_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
91 } TCG_DXE_DATA;
92
93 EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate = {
94 {
95 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,
96 sizeof (mTcgClientAcpiTemplate),
97 0x02 //Revision
98 //
99 // Compiler initializes the remaining bytes to 0
100 // These fields should be filled in in production
101 //
102 },
103 0, // 0 for PC Client Platform Class
104 0, // Log Area Max Length
105 (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1) // Log Area Start Address
106 };
107
108 //
109 // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,
110 // the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,
111 // this _UID can be changed and should match with the _UID setting of the TPM
112 // ACPI device object
113 //
114 EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate = {
115 {
116 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,
117 sizeof (mTcgServerAcpiTemplate),
118 0x02 //Revision
119 //
120 // Compiler initializes the remaining bytes to 0
121 // These fields should be filled in in production
122 //
123 },
124 1, // 1 for Server Platform Class
125 0, // Reserved
126 0, // Log Area Max Length
127 (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1), // Log Area Start Address
128 0x0100, // TCG Specification revision 1.0
129 2, // Device Flags
130 0, // Interrupt Flags
131 0, // GPE
132 {0}, // Reserved 3 bytes
133 0, // Global System Interrupt
134 {
135 EFI_ACPI_3_0_SYSTEM_MEMORY,
136 0,
137 0,
138 EFI_ACPI_3_0_BYTE,
139 0x0 // Base Address
140 },
141 0, // Reserved
142 {0}, // Configuration Address
143 0xFF, // ACPI _UID value of the device, can be changed for different platforms
144 0, // ACPI _UID value of the device, can be changed for different platforms
145 0, // ACPI _UID value of the device, can be changed for different platforms
146 0 // ACPI _UID value of the device, can be changed for different platforms
147 };
148
149 TCG_DXE_DATA mTcgDxeData = {
150 {
151 sizeof (TREE_BOOT_SERVICE_CAPABILITY_1_0), // Size
152 { 1, 0 }, // StructureVersion
153 { 1, 0 }, // ProtocolVersion
154 TREE_BOOT_HASH_ALG_SHA1, // HashAlgorithmBitmap
155 TREE_EVENT_LOG_FORMAT_TCG_1_2, // SupportedEventLogs
156 TRUE, // TrEEPresentFlag
157 TREE_DEFAULT_MAX_COMMAND_SIZE, // MaxCommandSize
158 TREE_DEFAULT_MAX_RESPONSE_SIZE, // MaxResponseSize
159 0 // ManufacturerID
160 },
161 &mTcgClientAcpiTemplate,
162 &mTcgServerAcpiTemplate,
163 };
164
165 UINTN mBootAttempts = 0;
166 CHAR16 mBootVarName[] = L"BootOrder";
167
168 VARIABLE_TYPE mVariableType[] = {
169 {EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid},
170 {EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid},
171 {EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid},
172 {EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid},
173 {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid},
174 };
175
176 EFI_HANDLE mImageHandle;
177
178 /**
179 Measure PE image into TPM log based on the authenticode image hashing in
180 PE/COFF Specification 8.0 Appendix A.
181
182 Caution: This function may receive untrusted input.
183 PE/COFF image is external input, so this function will validate its data structure
184 within this image buffer before use.
185
186 @param[in] PCRIndex TPM PCR index
187 @param[in] ImageAddress Start address of image buffer.
188 @param[in] ImageSize Image size
189 @param[out] DigestList Digeest list of this image.
190
191 @retval EFI_SUCCESS Successfully measure image.
192 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
193 @retval other error value
194 **/
195 EFI_STATUS
196 MeasurePeImageAndExtend (
197 IN UINT32 PCRIndex,
198 IN EFI_PHYSICAL_ADDRESS ImageAddress,
199 IN UINTN ImageSize,
200 OUT TPML_DIGEST_VALUES *DigestList
201 );
202
203 /**
204
205 This function dump raw data.
206
207 @param Data raw data
208 @param Size raw data size
209
210 **/
211 VOID
212 InternalDumpData (
213 IN UINT8 *Data,
214 IN UINTN Size
215 )
216 {
217 UINTN Index;
218 for (Index = 0; Index < Size; Index++) {
219 DEBUG ((EFI_D_INFO, "%02x", (UINTN)Data[Index]));
220 }
221 }
222
223 /**
224
225 This function dump raw data with colume format.
226
227 @param Data raw data
228 @param Size raw data size
229
230 **/
231 VOID
232 InternalDumpHex (
233 IN UINT8 *Data,
234 IN UINTN Size
235 )
236 {
237 UINTN Index;
238 UINTN Count;
239 UINTN Left;
240
241 #define COLUME_SIZE (16 * 2)
242
243 Count = Size / COLUME_SIZE;
244 Left = Size % COLUME_SIZE;
245 for (Index = 0; Index < Count; Index++) {
246 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));
247 InternalDumpData (Data + Index * COLUME_SIZE, COLUME_SIZE);
248 DEBUG ((EFI_D_INFO, "\n"));
249 }
250
251 if (Left != 0) {
252 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));
253 InternalDumpData (Data + Index * COLUME_SIZE, Left);
254 DEBUG ((EFI_D_INFO, "\n"));
255 }
256 }
257
258 /**
259 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
260 Caller is responsible to free LocationBuf.
261
262 @param[out] LocationBuf Returns Processor Location Buffer.
263 @param[out] Num Returns processor number.
264
265 @retval EFI_SUCCESS Operation completed successfully.
266 @retval EFI_UNSUPPORTED MpService protocol not found.
267
268 **/
269 EFI_STATUS
270 GetProcessorsCpuLocation (
271 OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,
272 OUT UINTN *Num
273 )
274 {
275 EFI_STATUS Status;
276 EFI_MP_SERVICES_PROTOCOL *MpProtocol;
277 UINTN ProcessorNum;
278 UINTN EnabledProcessorNum;
279 EFI_PROCESSOR_INFORMATION ProcessorInfo;
280 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
281 UINTN Index;
282
283 Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **) &MpProtocol);
284 if (EFI_ERROR (Status)) {
285 //
286 // MP protocol is not installed
287 //
288 return EFI_UNSUPPORTED;
289 }
290
291 Status = MpProtocol->GetNumberOfProcessors(
292 MpProtocol,
293 &ProcessorNum,
294 &EnabledProcessorNum
295 );
296 if (EFI_ERROR(Status)){
297 return Status;
298 }
299
300 Status = gBS->AllocatePool(
301 EfiBootServicesData,
302 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
303 (VOID **) &ProcessorLocBuf
304 );
305 if (EFI_ERROR(Status)){
306 return Status;
307 }
308
309 //
310 // Get each processor Location info
311 //
312 for (Index = 0; Index < ProcessorNum; Index++) {
313 Status = MpProtocol->GetProcessorInfo(
314 MpProtocol,
315 Index,
316 &ProcessorInfo
317 );
318 if (EFI_ERROR(Status)){
319 FreePool(ProcessorLocBuf);
320 return Status;
321 }
322
323 //
324 // Get all Processor Location info & measure
325 //
326 CopyMem(
327 &ProcessorLocBuf[Index],
328 &ProcessorInfo.Location,
329 sizeof(EFI_CPU_PHYSICAL_LOCATION)
330 );
331 }
332
333 *LocationBuf = ProcessorLocBuf;
334 *Num = ProcessorNum;
335
336 return Status;
337 }
338
339 /**
340 The EFI_TREE_PROTOCOL GetCapability function call provides protocol
341 capability information and state information about the TrEE.
342
343 @param[in] This Indicates the calling context
344 @param[in, out] ProtocolCapability The caller allocates memory for a TREE_BOOT_SERVICE_CAPABILITY
345 structure and sets the size field to the size of the structure allocated.
346 The callee fills in the fields with the EFI protocol capability information
347 and the current TrEE state information up to the number of fields which
348 fit within the size of the structure passed in.
349
350 @retval EFI_SUCCESS Operation completed successfully.
351 @retval EFI_DEVICE_ERROR The command was unsuccessful.
352 The ProtocolCapability variable will not be populated.
353 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
354 The ProtocolCapability variable will not be populated.
355 @retval EFI_BUFFER_TOO_SMALL The ProtocolCapability variable is too small to hold the full response.
356 It will be partially populated (required Size field will be set).
357 **/
358 EFI_STATUS
359 EFIAPI
360 TreeGetCapability (
361 IN EFI_TREE_PROTOCOL *This,
362 IN OUT TREE_BOOT_SERVICE_CAPABILITY *ProtocolCapability
363 )
364 {
365 DEBUG ((EFI_D_INFO, "TreeGetCapability ...\n"));
366
367 if ((This == NULL) || (ProtocolCapability == NULL)) {
368 return EFI_INVALID_PARAMETER;
369 }
370
371 if (ProtocolCapability->Size < mTcgDxeData.BsCap.Size) {
372 ProtocolCapability->Size = mTcgDxeData.BsCap.Size;
373 return EFI_BUFFER_TOO_SMALL;
374 }
375
376 CopyMem (ProtocolCapability, &mTcgDxeData.BsCap, mTcgDxeData.BsCap.Size);
377 DEBUG ((EFI_D_INFO, "TreeGetCapability - %r\n", EFI_SUCCESS));
378 return EFI_SUCCESS;
379 }
380
381 /**
382 This function dump event log.
383
384 @param[in] EventLogFormat The type of the event log for which the information is requested.
385 @param[in] EventLogLocation A pointer to the memory address of the event log.
386 @param[in] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
387 address of the start of the last entry in the event log in memory.
388 **/
389 VOID
390 DumpEventLog (
391 IN TREE_EVENT_LOG_FORMAT EventLogFormat,
392 IN EFI_PHYSICAL_ADDRESS EventLogLocation,
393 IN EFI_PHYSICAL_ADDRESS EventLogLastEntry
394 )
395 {
396 TCG_PCR_EVENT_HDR *EventHdr;
397 UINTN Index;
398
399 DEBUG ((EFI_D_INFO, "EventLogFormat: (0x%x)\n", EventLogFormat));
400
401 switch (EventLogFormat) {
402 case TREE_EVENT_LOG_FORMAT_TCG_1_2:
403 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
404 while ((UINTN)EventHdr <= EventLogLastEntry) {
405 DEBUG ((EFI_D_INFO, " Event:\n"));
406 DEBUG ((EFI_D_INFO, " PCRIndex - %d\n", EventHdr->PCRIndex));
407 DEBUG ((EFI_D_INFO, " EventType - 0x%08x\n", EventHdr->EventType));
408 DEBUG ((EFI_D_INFO, " Digest - "));
409 for (Index = 0; Index < sizeof(TCG_DIGEST); Index++) {
410 DEBUG ((EFI_D_INFO, "%02x ", EventHdr->Digest.digest[Index]));
411 }
412 DEBUG ((EFI_D_INFO, "\n"));
413 DEBUG ((EFI_D_INFO, " EventSize - 0x%08x\n", EventHdr->EventSize));
414 InternalDumpHex ((UINT8 *)(EventHdr + 1), EventHdr->EventSize);
415 EventHdr = (TCG_PCR_EVENT_HDR *)((UINTN)EventHdr + sizeof(TCG_PCR_EVENT_HDR) + EventHdr->EventSize);
416 }
417 break;
418 }
419
420 return ;
421 }
422
423 /**
424 The EFI_TREE_PROTOCOL Get Event Log function call allows a caller to
425 retrieve the address of a given event log and its last entry.
426
427 @param[in] This Indicates the calling context
428 @param[in] EventLogFormat The type of the event log for which the information is requested.
429 @param[out] EventLogLocation A pointer to the memory address of the event log.
430 @param[out] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
431 address of the start of the last entry in the event log in memory.
432 @param[out] EventLogTruncated If the Event Log is missing at least one entry because an event would
433 have exceeded the area allocated for events, this value is set to TRUE.
434 Otherwise, the value will be FALSE and the Event Log will be complete.
435
436 @retval EFI_SUCCESS Operation completed successfully.
437 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect
438 (e.g. asking for an event log whose format is not supported).
439 **/
440 EFI_STATUS
441 EFIAPI
442 TreeGetEventLog (
443 IN EFI_TREE_PROTOCOL *This,
444 IN TREE_EVENT_LOG_FORMAT EventLogFormat,
445 OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,
446 OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry,
447 OUT BOOLEAN *EventLogTruncated
448 )
449 {
450 UINTN Index;
451
452 DEBUG ((EFI_D_INFO, "TreeGetEventLog ...\n"));
453
454 if (This == NULL) {
455 return EFI_INVALID_PARAMETER;
456 }
457
458 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {
459 if (EventLogFormat == mTreeEventInfo[Index].LogFormat) {
460 break;
461 }
462 }
463
464 if (Index == sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0])) {
465 return EFI_INVALID_PARAMETER;
466 }
467
468 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {
469 if (EventLogLocation != NULL) {
470 *EventLogLocation = 0;
471 }
472 if (EventLogLastEntry != NULL) {
473 *EventLogLastEntry = 0;
474 }
475 if (EventLogTruncated != NULL) {
476 *EventLogTruncated = FALSE;
477 }
478 return EFI_SUCCESS;
479 }
480
481 if (EventLogLocation != NULL) {
482 *EventLogLocation = mTcgDxeData.EventLogAreaStruct[Index].Lasa;
483 DEBUG ((EFI_D_INFO, "TreeGetEventLog (EventLogLocation - %x)\n", *EventLogLocation));
484 }
485
486 if (EventLogLastEntry != NULL) {
487 if (!mTcgDxeData.EventLogAreaStruct[Index].EventLogStarted) {
488 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;
489 } else {
490 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)mTcgDxeData.EventLogAreaStruct[Index].LastEvent;
491 }
492 DEBUG ((EFI_D_INFO, "TreeGetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry));
493 }
494
495 if (EventLogTruncated != NULL) {
496 *EventLogTruncated = mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated;
497 DEBUG ((EFI_D_INFO, "TreeGetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated));
498 }
499
500 DEBUG ((EFI_D_INFO, "TreeGetEventLog - %r\n", EFI_SUCCESS));
501
502 // Dump Event Log for debug purpose
503 if ((EventLogLocation != NULL) && (EventLogLastEntry != NULL)) {
504 DumpEventLog (EventLogFormat, *EventLogLocation, *EventLogLastEntry);
505 }
506
507 return EFI_SUCCESS;
508 }
509
510 /**
511 Add a new entry to the Event Log.
512
513 @param[in, out] EventLogPtr Pointer to the Event Log data.
514 @param[in, out] LogSize Size of the Event Log.
515 @param[in] MaxSize Maximum size of the Event Log.
516 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
517 @param[in] NewEventHdrSize New event header size.
518 @param[in] NewEventData Pointer to the new event data.
519 @param[in] NewEventSize New event data size.
520
521 @retval EFI_SUCCESS The new event log entry was added.
522 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
523
524 **/
525 EFI_STATUS
526 TcgCommLogEvent (
527 IN OUT UINT8 **EventLogPtr,
528 IN OUT UINTN *LogSize,
529 IN UINTN MaxSize,
530 IN VOID *NewEventHdr,
531 IN UINT32 NewEventHdrSize,
532 IN UINT8 *NewEventData,
533 IN UINT32 NewEventSize
534 )
535 {
536 UINTN NewLogSize;
537
538 if (NewEventSize > MAX_ADDRESS - NewEventHdrSize) {
539 return EFI_OUT_OF_RESOURCES;
540 }
541
542 NewLogSize = NewEventHdrSize + NewEventSize;
543
544 if (NewLogSize > MAX_ADDRESS - *LogSize) {
545 return EFI_OUT_OF_RESOURCES;
546 }
547
548 if (NewLogSize + *LogSize > MaxSize) {
549 DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", MaxSize));
550 DEBUG ((EFI_D_INFO, " NewLogSize - 0x%x\n", NewLogSize));
551 DEBUG ((EFI_D_INFO, " LogSize - 0x%x\n", *LogSize));
552 DEBUG ((EFI_D_INFO, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES));
553 return EFI_OUT_OF_RESOURCES;
554 }
555
556 *EventLogPtr += *LogSize;
557 *LogSize += NewLogSize;
558 CopyMem (*EventLogPtr, NewEventHdr, NewEventHdrSize);
559 CopyMem (
560 *EventLogPtr + NewEventHdrSize,
561 NewEventData,
562 NewEventSize
563 );
564 return EFI_SUCCESS;
565 }
566
567 /**
568 Add a new entry to the Event Log.
569
570 @param[in] EventLogFormat The type of the event log for which the information is requested.
571 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
572 @param[in] NewEventHdrSize New event header size.
573 @param[in] NewEventData Pointer to the new event data.
574 @param[in] NewEventSize New event data size.
575
576 @retval EFI_SUCCESS The new event log entry was added.
577 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
578
579 **/
580 EFI_STATUS
581 TcgDxeLogEvent (
582 IN TREE_EVENT_LOG_FORMAT EventLogFormat,
583 IN VOID *NewEventHdr,
584 IN UINT32 NewEventHdrSize,
585 IN UINT8 *NewEventData,
586 IN UINT32 NewEventSize
587 )
588 {
589 EFI_STATUS Status;
590 UINTN Index;
591
592 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {
593 if (EventLogFormat == mTreeEventInfo[Index].LogFormat) {
594 break;
595 }
596 }
597
598 if (Index == sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0])) {
599 return EFI_INVALID_PARAMETER;
600 }
601
602 if (mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated) {
603 return EFI_VOLUME_FULL;
604 }
605
606 mTcgDxeData.EventLogAreaStruct[Index].LastEvent = (UINT8*)(UINTN)mTcgDxeData.EventLogAreaStruct[Index].Lasa;
607 Status = TcgCommLogEvent (
608 &mTcgDxeData.EventLogAreaStruct[Index].LastEvent,
609 &mTcgDxeData.EventLogAreaStruct[Index].EventLogSize,
610 (UINTN)mTcgDxeData.EventLogAreaStruct[Index].Laml,
611 NewEventHdr,
612 NewEventHdrSize,
613 NewEventData,
614 NewEventSize
615 );
616
617 if (Status == EFI_DEVICE_ERROR) {
618 return EFI_DEVICE_ERROR;
619 } else if (Status == EFI_OUT_OF_RESOURCES) {
620 mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated = TRUE;
621 return EFI_VOLUME_FULL;
622 } else if (Status == EFI_SUCCESS) {
623 mTcgDxeData.EventLogAreaStruct[Index].EventLogStarted = TRUE;
624 }
625
626 return Status;
627 }
628
629 /**
630 This function get digest from digest list.
631
632 @param HashAlg digest algorithm
633 @param DigestList digest list
634 @param Digest digest
635
636 @retval EFI_SUCCESS Sha1Digest is found and returned.
637 @retval EFI_NOT_FOUND Sha1Digest is not found.
638 **/
639 EFI_STATUS
640 Tpm2GetDigestFromDigestList (
641 IN TPMI_ALG_HASH HashAlg,
642 IN TPML_DIGEST_VALUES *DigestList,
643 IN VOID *Digest
644 )
645 {
646 UINTN Index;
647 UINT16 DigestSize;
648
649 DigestSize = GetHashSizeFromAlgo (HashAlg);
650 for (Index = 0; Index < DigestList->count; Index++) {
651 if (DigestList->digests[Index].hashAlg == HashAlg) {
652 CopyMem (
653 Digest,
654 &DigestList->digests[Index].digest,
655 DigestSize
656 );
657 return EFI_SUCCESS;
658 }
659 }
660
661 return EFI_NOT_FOUND;
662 }
663
664 /**
665 Add a new entry to the Event Log.
666
667 @param[in] DigestList A list of digest.
668 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
669 @param[in] NewEventData Pointer to the new event data.
670
671 @retval EFI_SUCCESS The new event log entry was added.
672 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
673 **/
674 EFI_STATUS
675 TcgDxeLogHashEvent (
676 IN TPML_DIGEST_VALUES *DigestList,
677 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
678 IN UINT8 *NewEventData
679 )
680 {
681 EFI_STATUS Status;
682 EFI_TPL OldTpl;
683 UINTN Index;
684 EFI_STATUS RetStatus;
685
686 RetStatus = EFI_SUCCESS;
687 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {
688 DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTreeEventInfo[Index].LogFormat));
689 switch (mTreeEventInfo[Index].LogFormat) {
690 case TREE_EVENT_LOG_FORMAT_TCG_1_2:
691 Status = Tpm2GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &NewEventHdr->Digest);
692 if (!EFI_ERROR (Status)) {
693 //
694 // Enter critical region
695 //
696 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
697 Status = TcgDxeLogEvent (
698 mTreeEventInfo[Index].LogFormat,
699 NewEventHdr,
700 sizeof(TCG_PCR_EVENT_HDR),
701 NewEventData,
702 NewEventHdr->EventSize
703 );
704 if (Status != EFI_SUCCESS) {
705 RetStatus = Status;
706 }
707 gBS->RestoreTPL (OldTpl);
708 //
709 // Exit critical region
710 //
711 }
712 break;
713 }
714 }
715
716 return RetStatus;
717 }
718
719 /**
720 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
721 and add an entry to the Event Log.
722
723 @param[in] Flags Bitmap providing additional information.
724 @param[in] HashData Physical address of the start of the data buffer
725 to be hashed, extended, and logged.
726 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
727 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
728 @param[in] NewEventData Pointer to the new event data.
729
730 @retval EFI_SUCCESS Operation completed successfully.
731 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
732 @retval EFI_DEVICE_ERROR The command was unsuccessful.
733
734 **/
735 EFI_STATUS
736 TcgDxeHashLogExtendEvent (
737 IN UINT64 Flags,
738 IN UINT8 *HashData,
739 IN UINT64 HashDataLen,
740 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
741 IN UINT8 *NewEventData
742 )
743 {
744 EFI_STATUS Status;
745 TPML_DIGEST_VALUES DigestList;
746
747 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {
748 return EFI_DEVICE_ERROR;
749 }
750
751 Status = HashAndExtend (
752 NewEventHdr->PCRIndex,
753 HashData,
754 (UINTN)HashDataLen,
755 &DigestList
756 );
757 if (!EFI_ERROR (Status)) {
758 if ((Flags & TREE_EXTEND_ONLY) == 0) {
759 Status = TcgDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);
760 }
761 }
762
763 if (Status == EFI_DEVICE_ERROR) {
764 DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status));
765 mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;
766 REPORT_STATUS_CODE (
767 EFI_ERROR_CODE | EFI_ERROR_MINOR,
768 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
769 );
770 }
771
772 return Status;
773 }
774
775 /**
776 The EFI_TREE_PROTOCOL HashLogExtendEvent function call provides callers with
777 an opportunity to extend and optionally log events without requiring
778 knowledge of actual TPM commands.
779 The extend operation will occur even if this function cannot create an event
780 log entry (e.g. due to the event log being full).
781
782 @param[in] This Indicates the calling context
783 @param[in] Flags Bitmap providing additional information.
784 @param[in] DataToHash Physical address of the start of the data buffer to be hashed.
785 @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.
786 @param[in] Event Pointer to data buffer containing information about the event.
787
788 @retval EFI_SUCCESS Operation completed successfully.
789 @retval EFI_DEVICE_ERROR The command was unsuccessful.
790 @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.
791 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
792 @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.
793 **/
794 EFI_STATUS
795 EFIAPI
796 TreeHashLogExtendEvent (
797 IN EFI_TREE_PROTOCOL *This,
798 IN UINT64 Flags,
799 IN EFI_PHYSICAL_ADDRESS DataToHash,
800 IN UINT64 DataToHashLen,
801 IN TrEE_EVENT *Event
802 )
803 {
804 EFI_STATUS Status;
805 TCG_PCR_EVENT_HDR NewEventHdr;
806 TPML_DIGEST_VALUES DigestList;
807
808 DEBUG ((EFI_D_INFO, "TreeHashLogExtendEvent ...\n"));
809
810 if ((This == NULL) || (DataToHash == 0) || (Event == NULL)) {
811 return EFI_INVALID_PARAMETER;
812 }
813
814 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {
815 return EFI_UNSUPPORTED;
816 }
817
818 if (Event->Size < Event->Header.HeaderSize + sizeof(UINT32)) {
819 return EFI_INVALID_PARAMETER;
820 }
821
822 if (Event->Header.PCRIndex > MAX_PCR_INDEX) {
823 return EFI_INVALID_PARAMETER;
824 }
825
826 NewEventHdr.PCRIndex = Event->Header.PCRIndex;
827 NewEventHdr.EventType = Event->Header.EventType;
828 NewEventHdr.EventSize = Event->Size - sizeof(UINT32) - Event->Header.HeaderSize;
829 if ((Flags & PE_COFF_IMAGE) != 0) {
830 Status = MeasurePeImageAndExtend (
831 NewEventHdr.PCRIndex,
832 DataToHash,
833 (UINTN)DataToHashLen,
834 &DigestList
835 );
836 if (!EFI_ERROR (Status)) {
837 if ((Flags & TREE_EXTEND_ONLY) == 0) {
838 Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);
839 }
840 }
841 if (Status == EFI_DEVICE_ERROR) {
842 DEBUG ((EFI_D_ERROR, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status));
843 mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;
844 REPORT_STATUS_CODE (
845 EFI_ERROR_CODE | EFI_ERROR_MINOR,
846 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
847 );
848 }
849 } else {
850 Status = TcgDxeHashLogExtendEvent (
851 Flags,
852 (UINT8 *) (UINTN) DataToHash,
853 DataToHashLen,
854 &NewEventHdr,
855 Event->Event
856 );
857 }
858 DEBUG ((EFI_D_INFO, "TreeHashLogExtendEvent - %r\n", Status));
859 return Status;
860 }
861
862 /**
863 This service enables the sending of commands to the TrEE.
864
865 @param[in] This Indicates the calling context
866 @param[in] InputParameterBlockSize Size of the TrEE input parameter block.
867 @param[in] InputParameterBlock Pointer to the TrEE input parameter block.
868 @param[in] OutputParameterBlockSize Size of the TrEE output parameter block.
869 @param[in] OutputParameterBlock Pointer to the TrEE output parameter block.
870
871 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
872 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
873 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
874 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
875 **/
876 EFI_STATUS
877 EFIAPI
878 TreeSubmitCommand (
879 IN EFI_TREE_PROTOCOL *This,
880 IN UINT32 InputParameterBlockSize,
881 IN UINT8 *InputParameterBlock,
882 IN UINT32 OutputParameterBlockSize,
883 IN UINT8 *OutputParameterBlock
884 )
885 {
886 EFI_STATUS Status;
887
888 DEBUG ((EFI_D_INFO, "TreeSubmitCommand ...\n"));
889
890 if ((This == NULL) ||
891 (InputParameterBlockSize == 0) || (InputParameterBlock == NULL) ||
892 (OutputParameterBlockSize == 0) || (OutputParameterBlock == NULL)) {
893 return EFI_INVALID_PARAMETER;
894 }
895
896 if (!mTcgDxeData.BsCap.TrEEPresentFlag) {
897 return EFI_UNSUPPORTED;
898 }
899
900 if (InputParameterBlockSize >= mTcgDxeData.BsCap.MaxCommandSize) {
901 return EFI_INVALID_PARAMETER;
902 }
903 if (OutputParameterBlockSize >= mTcgDxeData.BsCap.MaxResponseSize) {
904 return EFI_INVALID_PARAMETER;
905 }
906
907 Status = Tpm2SubmitCommand (
908 InputParameterBlockSize,
909 InputParameterBlock,
910 &OutputParameterBlockSize,
911 OutputParameterBlock
912 );
913 DEBUG ((EFI_D_INFO, "TreeSubmitCommand - %r\n", Status));
914 return Status;
915 }
916
917
918 EFI_TREE_PROTOCOL mTreeProtocol = {
919 TreeGetCapability,
920 TreeGetEventLog,
921 TreeHashLogExtendEvent,
922 TreeSubmitCommand
923 };
924
925 /**
926 Initialize the Event Log and log events passed from the PEI phase.
927
928 @retval EFI_SUCCESS Operation completed successfully.
929 @retval EFI_OUT_OF_RESOURCES Out of memory.
930
931 **/
932 EFI_STATUS
933 SetupEventLog (
934 VOID
935 )
936 {
937 EFI_STATUS Status;
938 VOID *TcgEvent;
939 EFI_PEI_HOB_POINTERS GuidHob;
940 EFI_PHYSICAL_ADDRESS Lasa;
941 UINTN Index;
942
943 DEBUG ((EFI_D_INFO, "SetupEventLog\n"));
944
945 //
946 // 1. Create Log Area
947 //
948 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {
949 mTcgDxeData.EventLogAreaStruct[Index].EventLogFormat = mTreeEventInfo[Index].LogFormat;
950 Lasa = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);
951 Status = gBS->AllocatePages (
952 AllocateMaxAddress,
953 EfiACPIMemoryNVS,
954 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),
955 &Lasa
956 );
957 if (EFI_ERROR (Status)) {
958 return Status;
959 }
960 mTcgDxeData.EventLogAreaStruct[Index].Lasa = Lasa;
961 mTcgDxeData.EventLogAreaStruct[Index].Laml = EFI_TCG_LOG_AREA_SIZE;
962 //
963 // To initialize them as 0xFF is recommended
964 // because the OS can know the last entry for that.
965 //
966 SetMem ((VOID *)(UINTN)Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);
967 }
968
969 //
970 // 2. Create ACPI table for TCG1.2 only
971 //
972 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {
973 mTcgClientAcpiTemplate.Lasa = mTcgDxeData.EventLogAreaStruct[0].Lasa;
974 mTcgClientAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;
975 } else {
976 mTcgServerAcpiTemplate.Lasa = mTcgDxeData.EventLogAreaStruct[0].Lasa;
977 mTcgServerAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;
978 }
979
980 //
981 // 3. Sync data from PEI to DXE
982 //
983 Status = EFI_SUCCESS;
984 for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {
985 GuidHob.Raw = GetHobList ();
986 Status = EFI_SUCCESS;
987 while (!EFI_ERROR (Status) &&
988 (GuidHob.Raw = GetNextGuidHob (mTreeEventInfo[Index].EventGuid, GuidHob.Raw)) != NULL) {
989 TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);
990 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
991 switch (mTreeEventInfo[Index].LogFormat) {
992 case TREE_EVENT_LOG_FORMAT_TCG_1_2:
993 Status = TcgDxeLogEvent (
994 mTreeEventInfo[Index].LogFormat,
995 TcgEvent,
996 sizeof(TCG_PCR_EVENT_HDR),
997 ((TCG_PCR_EVENT*)TcgEvent)->Event,
998 ((TCG_PCR_EVENT_HDR*)TcgEvent)->EventSize
999 );
1000 break;
1001 }
1002 }
1003 }
1004
1005 return Status;
1006 }
1007
1008 /**
1009 Measure and log an action string, and extend the measurement result into PCR[5].
1010
1011 @param[in] String A specific string that indicates an Action event.
1012
1013 @retval EFI_SUCCESS Operation completed successfully.
1014 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1015
1016 **/
1017 EFI_STATUS
1018 TcgMeasureAction (
1019 IN CHAR8 *String
1020 )
1021 {
1022 TCG_PCR_EVENT_HDR TcgEvent;
1023
1024 TcgEvent.PCRIndex = 5;
1025 TcgEvent.EventType = EV_EFI_ACTION;
1026 TcgEvent.EventSize = (UINT32)AsciiStrLen (String);
1027 return TcgDxeHashLogExtendEvent (
1028 0,
1029 (UINT8*)String,
1030 TcgEvent.EventSize,
1031 &TcgEvent,
1032 (UINT8 *) String
1033 );
1034 }
1035
1036 /**
1037 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
1038
1039 @retval EFI_SUCCESS Operation completed successfully.
1040 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1041
1042 **/
1043 EFI_STATUS
1044 MeasureHandoffTables (
1045 VOID
1046 )
1047 {
1048 EFI_STATUS Status;
1049 SMBIOS_TABLE_ENTRY_POINT *SmbiosTable;
1050 TCG_PCR_EVENT_HDR TcgEvent;
1051 EFI_HANDOFF_TABLE_POINTERS HandoffTables;
1052 UINTN ProcessorNum;
1053 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
1054
1055 ProcessorLocBuf = NULL;
1056
1057 //
1058 // Measure SMBIOS with EV_EFI_HANDOFF_TABLES to PCR[1]
1059 //
1060 Status = EfiGetSystemConfigurationTable (
1061 &gEfiSmbiosTableGuid,
1062 (VOID **) &SmbiosTable
1063 );
1064
1065 if (!EFI_ERROR (Status) && SmbiosTable != NULL) {
1066 TcgEvent.PCRIndex = 1;
1067 TcgEvent.EventType = EV_EFI_HANDOFF_TABLES;
1068 TcgEvent.EventSize = sizeof (HandoffTables);
1069
1070 HandoffTables.NumberOfTables = 1;
1071 HandoffTables.TableEntry[0].VendorGuid = gEfiSmbiosTableGuid;
1072 HandoffTables.TableEntry[0].VendorTable = SmbiosTable;
1073
1074 DEBUG ((DEBUG_INFO, "The Smbios Table starts at: 0x%x\n", SmbiosTable->TableAddress));
1075 DEBUG ((DEBUG_INFO, "The Smbios Table size: 0x%x\n", SmbiosTable->TableLength));
1076
1077 Status = TcgDxeHashLogExtendEvent (
1078 0,
1079 (UINT8*)(UINTN)SmbiosTable->TableAddress,
1080 SmbiosTable->TableLength,
1081 &TcgEvent,
1082 (UINT8*)&HandoffTables
1083 );
1084 }
1085
1086 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
1087 //
1088 // Tcg Server spec.
1089 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
1090 //
1091 Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);
1092
1093 if (!EFI_ERROR(Status)){
1094 TcgEvent.PCRIndex = 1;
1095 TcgEvent.EventType = EV_TABLE_OF_DEVICES;
1096 TcgEvent.EventSize = sizeof (HandoffTables);
1097
1098 HandoffTables.NumberOfTables = 1;
1099 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
1100 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
1101
1102 Status = TcgDxeHashLogExtendEvent (
1103 0,
1104 (UINT8*)(UINTN)ProcessorLocBuf,
1105 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
1106 &TcgEvent,
1107 (UINT8*)&HandoffTables
1108 );
1109
1110 FreePool(ProcessorLocBuf);
1111 }
1112 }
1113
1114 return Status;
1115 }
1116
1117 /**
1118 Measure and log Separator event, and extend the measurement result into a specific PCR.
1119
1120 @param[in] PCRIndex PCR index.
1121
1122 @retval EFI_SUCCESS Operation completed successfully.
1123 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1124
1125 **/
1126 EFI_STATUS
1127 MeasureSeparatorEvent (
1128 IN TPM_PCRINDEX PCRIndex
1129 )
1130 {
1131 TCG_PCR_EVENT_HDR TcgEvent;
1132 UINT32 EventData;
1133
1134 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent Pcr - %x\n", PCRIndex));
1135
1136 EventData = 0;
1137 TcgEvent.PCRIndex = PCRIndex;
1138 TcgEvent.EventType = EV_SEPARATOR;
1139 TcgEvent.EventSize = (UINT32)sizeof (EventData);
1140 return TcgDxeHashLogExtendEvent (
1141 0,
1142 (UINT8 *)&EventData,
1143 sizeof (EventData),
1144 &TcgEvent,
1145 (UINT8 *)&EventData
1146 );
1147 }
1148
1149 /**
1150 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1151
1152 @param[in] PCRIndex PCR Index.
1153 @param[in] EventType Event type.
1154 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1155 @param[in] VendorGuid A unique identifier for the vendor.
1156 @param[in] VarData The content of the variable data.
1157 @param[in] VarSize The size of the variable data.
1158
1159 @retval EFI_SUCCESS Operation completed successfully.
1160 @retval EFI_OUT_OF_RESOURCES Out of memory.
1161 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1162
1163 **/
1164 EFI_STATUS
1165 MeasureVariable (
1166 IN TPM_PCRINDEX PCRIndex,
1167 IN TCG_EVENTTYPE EventType,
1168 IN CHAR16 *VarName,
1169 IN EFI_GUID *VendorGuid,
1170 IN VOID *VarData,
1171 IN UINTN VarSize
1172 )
1173 {
1174 EFI_STATUS Status;
1175 TCG_PCR_EVENT_HDR TcgEvent;
1176 UINTN VarNameLength;
1177 EFI_VARIABLE_DATA_TREE *VarLog;
1178
1179 DEBUG ((EFI_D_INFO, "TrEEDxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)PCRIndex, (UINTN)EventType));
1180 DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
1181
1182 VarNameLength = StrLen (VarName);
1183 TcgEvent.PCRIndex = PCRIndex;
1184 TcgEvent.EventType = EventType;
1185 TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
1186 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
1187
1188 VarLog = (EFI_VARIABLE_DATA_TREE*)AllocatePool (TcgEvent.EventSize);
1189 if (VarLog == NULL) {
1190 return EFI_OUT_OF_RESOURCES;
1191 }
1192
1193 VarLog->VariableName = *VendorGuid;
1194 VarLog->UnicodeNameLength = VarNameLength;
1195 VarLog->VariableDataLength = VarSize;
1196 CopyMem (
1197 VarLog->UnicodeName,
1198 VarName,
1199 VarNameLength * sizeof (*VarName)
1200 );
1201 if (VarSize != 0 && VarData != NULL) {
1202 CopyMem (
1203 (CHAR16 *)VarLog->UnicodeName + VarNameLength,
1204 VarData,
1205 VarSize
1206 );
1207 }
1208
1209 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1210 //
1211 // Digest is the event data (EFI_VARIABLE_DATA_TREE)
1212 //
1213 Status = TcgDxeHashLogExtendEvent (
1214 0,
1215 (UINT8*)VarLog,
1216 TcgEvent.EventSize,
1217 &TcgEvent,
1218 (UINT8*)VarLog
1219 );
1220 } else {
1221 Status = TcgDxeHashLogExtendEvent (
1222 0,
1223 (UINT8*)VarData,
1224 VarSize,
1225 &TcgEvent,
1226 (UINT8*)VarLog
1227 );
1228 }
1229 FreePool (VarLog);
1230 return Status;
1231 }
1232
1233 /**
1234 Read then Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1235
1236 @param[in] PCRIndex PCR Index.
1237 @param[in] EventType Event type.
1238 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1239 @param[in] VendorGuid A unique identifier for the vendor.
1240 @param[out] VarSize The size of the variable data.
1241 @param[out] VarData Pointer to the content of the variable.
1242
1243 @retval EFI_SUCCESS Operation completed successfully.
1244 @retval EFI_OUT_OF_RESOURCES Out of memory.
1245 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1246
1247 **/
1248 EFI_STATUS
1249 ReadAndMeasureVariable (
1250 IN TPM_PCRINDEX PCRIndex,
1251 IN TCG_EVENTTYPE EventType,
1252 IN CHAR16 *VarName,
1253 IN EFI_GUID *VendorGuid,
1254 OUT UINTN *VarSize,
1255 OUT VOID **VarData
1256 )
1257 {
1258 EFI_STATUS Status;
1259
1260 Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);
1261 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1262 if (EFI_ERROR (Status)) {
1263 //
1264 // It is valid case, so we need handle it.
1265 //
1266 *VarData = NULL;
1267 *VarSize = 0;
1268 }
1269 } else {
1270 //
1271 // if status error, VarData is freed and set NULL by GetVariable2
1272 //
1273 if (EFI_ERROR (Status)) {
1274 return EFI_NOT_FOUND;
1275 }
1276 }
1277
1278 Status = MeasureVariable (
1279 PCRIndex,
1280 EventType,
1281 VarName,
1282 VendorGuid,
1283 *VarData,
1284 *VarSize
1285 );
1286 return Status;
1287 }
1288
1289 /**
1290 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
1291
1292 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1293 @param[in] VendorGuid A unique identifier for the vendor.
1294 @param[out] VarSize The size of the variable data.
1295 @param[out] VarData Pointer to the content of the variable.
1296
1297 @retval EFI_SUCCESS Operation completed successfully.
1298 @retval EFI_OUT_OF_RESOURCES Out of memory.
1299 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1300
1301 **/
1302 EFI_STATUS
1303 ReadAndMeasureBootVariable (
1304 IN CHAR16 *VarName,
1305 IN EFI_GUID *VendorGuid,
1306 OUT UINTN *VarSize,
1307 OUT VOID **VarData
1308 )
1309 {
1310 return ReadAndMeasureVariable (
1311 5,
1312 EV_EFI_VARIABLE_BOOT,
1313 VarName,
1314 VendorGuid,
1315 VarSize,
1316 VarData
1317 );
1318 }
1319
1320 /**
1321 Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].
1322
1323 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1324 @param[in] VendorGuid A unique identifier for the vendor.
1325 @param[out] VarSize The size of the variable data.
1326 @param[out] VarData Pointer to the content of the variable.
1327
1328 @retval EFI_SUCCESS Operation completed successfully.
1329 @retval EFI_OUT_OF_RESOURCES Out of memory.
1330 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1331
1332 **/
1333 EFI_STATUS
1334 ReadAndMeasureSecureVariable (
1335 IN CHAR16 *VarName,
1336 IN EFI_GUID *VendorGuid,
1337 OUT UINTN *VarSize,
1338 OUT VOID **VarData
1339 )
1340 {
1341 return ReadAndMeasureVariable (
1342 7,
1343 EV_EFI_VARIABLE_DRIVER_CONFIG,
1344 VarName,
1345 VendorGuid,
1346 VarSize,
1347 VarData
1348 );
1349 }
1350
1351 /**
1352 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
1353
1354 The EFI boot variables are BootOrder and Boot#### variables.
1355
1356 @retval EFI_SUCCESS Operation completed successfully.
1357 @retval EFI_OUT_OF_RESOURCES Out of memory.
1358 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1359
1360 **/
1361 EFI_STATUS
1362 MeasureAllBootVariables (
1363 VOID
1364 )
1365 {
1366 EFI_STATUS Status;
1367 UINT16 *BootOrder;
1368 UINTN BootCount;
1369 UINTN Index;
1370 VOID *BootVarData;
1371 UINTN Size;
1372
1373 Status = ReadAndMeasureBootVariable (
1374 mBootVarName,
1375 &gEfiGlobalVariableGuid,
1376 &BootCount,
1377 (VOID **) &BootOrder
1378 );
1379 if (Status == EFI_NOT_FOUND || BootOrder == NULL) {
1380 return EFI_SUCCESS;
1381 }
1382
1383 if (EFI_ERROR (Status)) {
1384 //
1385 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
1386 //
1387 FreePool (BootOrder);
1388 return Status;
1389 }
1390
1391 BootCount /= sizeof (*BootOrder);
1392 for (Index = 0; Index < BootCount; Index++) {
1393 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
1394 Status = ReadAndMeasureBootVariable (
1395 mBootVarName,
1396 &gEfiGlobalVariableGuid,
1397 &Size,
1398 &BootVarData
1399 );
1400 if (!EFI_ERROR (Status)) {
1401 FreePool (BootVarData);
1402 }
1403 }
1404
1405 FreePool (BootOrder);
1406 return EFI_SUCCESS;
1407 }
1408
1409 /**
1410 Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.
1411
1412 The EFI boot variables are BootOrder and Boot#### variables.
1413
1414 @retval EFI_SUCCESS Operation completed successfully.
1415 @retval EFI_OUT_OF_RESOURCES Out of memory.
1416 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1417
1418 **/
1419 EFI_STATUS
1420 MeasureAllSecureVariables (
1421 VOID
1422 )
1423 {
1424 EFI_STATUS Status;
1425 VOID *Data;
1426 UINTN DataSize;
1427 UINTN Index;
1428
1429 Status = EFI_NOT_FOUND;
1430 for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
1431 Status = ReadAndMeasureSecureVariable (
1432 mVariableType[Index].VariableName,
1433 mVariableType[Index].VendorGuid,
1434 &DataSize,
1435 &Data
1436 );
1437 if (!EFI_ERROR (Status)) {
1438 if (Data != NULL) {
1439 FreePool (Data);
1440 }
1441 }
1442 }
1443
1444 return EFI_SUCCESS;
1445 }
1446
1447 /**
1448 Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.
1449
1450 @retval EFI_SUCCESS Operation completed successfully.
1451 @retval EFI_OUT_OF_RESOURCES Out of memory.
1452 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1453
1454 **/
1455 EFI_STATUS
1456 MeasureLaunchOfFirmwareDebugger (
1457 VOID
1458 )
1459 {
1460 TCG_PCR_EVENT_HDR TcgEvent;
1461
1462 TcgEvent.PCRIndex = 7;
1463 TcgEvent.EventType = EV_EFI_ACTION;
1464 TcgEvent.EventSize = sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1;
1465 return TcgDxeHashLogExtendEvent (
1466 0,
1467 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,
1468 sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1,
1469 &TcgEvent,
1470 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING
1471 );
1472 }
1473
1474 /**
1475 Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.
1476
1477 Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)
1478 - The contents of the SecureBoot variable
1479 - The contents of the PK variable
1480 - The contents of the KEK variable
1481 - The contents of the EFI_IMAGE_SECURITY_DATABASE variable
1482 - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable
1483 - Separator
1484 - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path
1485
1486 NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,
1487 EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].
1488
1489 @param[in] Event Event whose notification function is being invoked
1490 @param[in] Context Pointer to the notification function's context
1491 **/
1492 VOID
1493 EFIAPI
1494 MeasureSecureBootPolicy (
1495 IN EFI_EVENT Event,
1496 IN VOID *Context
1497 )
1498 {
1499 EFI_STATUS Status;
1500 VOID *Protocol;
1501
1502 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);
1503 if (EFI_ERROR (Status)) {
1504 return;
1505 }
1506
1507 if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {
1508 Status = MeasureLaunchOfFirmwareDebugger ();
1509 DEBUG ((EFI_D_INFO, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));
1510 }
1511
1512 Status = MeasureAllSecureVariables ();
1513 DEBUG ((EFI_D_INFO, "MeasureAllSecureVariables - %r\n", Status));
1514
1515 //
1516 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
1517 // and ImageVerification (Authority)
1518 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
1519 // the Authority measurement happen before ReadToBoot event.
1520 //
1521 Status = MeasureSeparatorEvent (7);
1522 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent - %r\n", Status));
1523 return ;
1524 }
1525
1526 /**
1527 Ready to Boot Event notification handler.
1528
1529 Sequence of OS boot events is measured in this event notification handler.
1530
1531 @param[in] Event Event whose notification function is being invoked
1532 @param[in] Context Pointer to the notification function's context
1533
1534 **/
1535 VOID
1536 EFIAPI
1537 OnReadyToBoot (
1538 IN EFI_EVENT Event,
1539 IN VOID *Context
1540 )
1541 {
1542 EFI_STATUS Status;
1543 TPM_PCRINDEX PcrIndex;
1544
1545 PERF_START_EX (mImageHandle, "EventRec", "TrEEDxe", 0, PERF_ID_TREE_DXE);
1546 if (mBootAttempts == 0) {
1547
1548 //
1549 // Measure handoff tables.
1550 //
1551 Status = MeasureHandoffTables ();
1552 if (EFI_ERROR (Status)) {
1553 DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));
1554 }
1555
1556 //
1557 // Measure BootOrder & Boot#### variables.
1558 //
1559 Status = MeasureAllBootVariables ();
1560 if (EFI_ERROR (Status)) {
1561 DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));
1562 }
1563
1564 //
1565 // 1. This is the first boot attempt.
1566 //
1567 Status = TcgMeasureAction (
1568 EFI_CALLING_EFI_APPLICATION
1569 );
1570 if (EFI_ERROR (Status)) {
1571 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
1572 }
1573
1574 //
1575 // 2. Draw a line between pre-boot env and entering post-boot env.
1576 // PCR[7] is already done.
1577 //
1578 for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {
1579 Status = MeasureSeparatorEvent (PcrIndex);
1580 if (EFI_ERROR (Status)) {
1581 DEBUG ((EFI_D_ERROR, "Seperator Event not Measured. Error!\n"));
1582 }
1583 }
1584
1585 //
1586 // 3. Measure GPT. It would be done in SAP driver.
1587 //
1588
1589 //
1590 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
1591 //
1592
1593 //
1594 // 5. Read & Measure variable. BootOrder already measured.
1595 //
1596 } else {
1597 //
1598 // 6. Not first attempt, meaning a return from last attempt
1599 //
1600 Status = TcgMeasureAction (
1601 EFI_RETURNING_FROM_EFI_APPLICATOIN
1602 );
1603 if (EFI_ERROR (Status)) {
1604 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));
1605 }
1606 }
1607
1608 DEBUG ((EFI_D_INFO, "TPM2 TrEEDxe Measure Data when ReadyToBoot\n"));
1609 //
1610 // Increase boot attempt counter.
1611 //
1612 mBootAttempts++;
1613 PERF_END_EX (mImageHandle, "EventRec", "TrEEDxe", 0, PERF_ID_TREE_DXE + 1);
1614 }
1615
1616 /**
1617 Install TCG ACPI Table when ACPI Table Protocol is available.
1618
1619 A system's firmware uses an ACPI table to identify the system's TCG capabilities
1620 to the Post-Boot environment. The information in this ACPI table is not guaranteed
1621 to be valid until the Host Platform transitions from pre-boot state to post-boot state.
1622
1623 @param[in] Event Event whose notification function is being invoked
1624 @param[in] Context Pointer to the notification function's context
1625 **/
1626 VOID
1627 EFIAPI
1628 InstallAcpiTable (
1629 IN EFI_EVENT Event,
1630 IN VOID *Context
1631 )
1632 {
1633 UINTN TableKey;
1634 EFI_STATUS Status;
1635 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
1636 UINT8 Checksum;
1637 UINT64 OemTableId;
1638
1639 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);
1640 if (EFI_ERROR (Status)) {
1641 return;
1642 }
1643
1644 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {
1645 CopyMem (mTcgClientAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgClientAcpiTemplate.Header.OemId));
1646 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
1647 CopyMem (&mTcgClientAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
1648 mTcgClientAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
1649 mTcgClientAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
1650 mTcgClientAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1651 //
1652 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1653 // service of the ACPI table protocol to install it.
1654 //
1655 Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgClientAcpiTemplate, sizeof (mTcgClientAcpiTemplate));
1656 mTcgClientAcpiTemplate.Header.Checksum = Checksum;
1657
1658 Status = AcpiTable->InstallAcpiTable (
1659 AcpiTable,
1660 &mTcgClientAcpiTemplate,
1661 sizeof (mTcgClientAcpiTemplate),
1662 &TableKey
1663 );
1664 } else {
1665 CopyMem (mTcgServerAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgServerAcpiTemplate.Header.OemId));
1666 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
1667 CopyMem (&mTcgServerAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
1668 mTcgServerAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
1669 mTcgServerAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
1670 mTcgServerAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1671 //
1672 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1673 // service of the ACPI table protocol to install it.
1674 //
1675 Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgServerAcpiTemplate, sizeof (mTcgServerAcpiTemplate));
1676 mTcgServerAcpiTemplate.Header.Checksum = Checksum;
1677
1678 mTcgServerAcpiTemplate.BaseAddress.Address = PcdGet64 (PcdTpmBaseAddress);
1679 Status = AcpiTable->InstallAcpiTable (
1680 AcpiTable,
1681 &mTcgServerAcpiTemplate,
1682 sizeof (mTcgServerAcpiTemplate),
1683 &TableKey
1684 );
1685 }
1686
1687 if (EFI_ERROR (Status)) {
1688 DEBUG((EFI_D_ERROR, "Tcg Acpi Table installation failure"));
1689 }
1690 }
1691
1692 /**
1693 Exit Boot Services Event notification handler.
1694
1695 Measure invocation and success of ExitBootServices.
1696
1697 @param[in] Event Event whose notification function is being invoked
1698 @param[in] Context Pointer to the notification function's context
1699
1700 **/
1701 VOID
1702 EFIAPI
1703 OnExitBootServices (
1704 IN EFI_EVENT Event,
1705 IN VOID *Context
1706 )
1707 {
1708 EFI_STATUS Status;
1709
1710 //
1711 // Measure invocation of ExitBootServices,
1712 //
1713 Status = TcgMeasureAction (
1714 EFI_EXIT_BOOT_SERVICES_INVOCATION
1715 );
1716 if (EFI_ERROR (Status)) {
1717 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
1718 }
1719
1720 //
1721 // Measure success of ExitBootServices
1722 //
1723 Status = TcgMeasureAction (
1724 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
1725 );
1726 if (EFI_ERROR (Status)) {
1727 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
1728 }
1729 }
1730
1731 /**
1732 Exit Boot Services Failed Event notification handler.
1733
1734 Measure Failure of ExitBootServices.
1735
1736 @param[in] Event Event whose notification function is being invoked
1737 @param[in] Context Pointer to the notification function's context
1738
1739 **/
1740 VOID
1741 EFIAPI
1742 OnExitBootServicesFailed (
1743 IN EFI_EVENT Event,
1744 IN VOID *Context
1745 )
1746 {
1747 EFI_STATUS Status;
1748
1749 //
1750 // Measure Failure of ExitBootServices,
1751 //
1752 Status = TcgMeasureAction (
1753 EFI_EXIT_BOOT_SERVICES_FAILED
1754 );
1755 if (EFI_ERROR (Status)) {
1756 DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
1757 }
1758
1759 }
1760
1761 /**
1762 The function install TrEE protocol.
1763
1764 @retval EFI_SUCCESS TrEE protocol is installed.
1765 @retval other Some error occurs.
1766 **/
1767 EFI_STATUS
1768 InstallTrEE (
1769 VOID
1770 )
1771 {
1772 EFI_STATUS Status;
1773 EFI_HANDLE Handle;
1774
1775 Handle = NULL;
1776 Status = gBS->InstallMultipleProtocolInterfaces (
1777 &Handle,
1778 &gEfiTrEEProtocolGuid,
1779 &mTreeProtocol,
1780 NULL
1781 );
1782 return Status;
1783 }
1784
1785 /**
1786 The driver's entry point. It publishes EFI TrEE Protocol.
1787
1788 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1789 @param[in] SystemTable A pointer to the EFI System Table.
1790
1791 @retval EFI_SUCCESS The entry point is executed successfully.
1792 @retval other Some error occurs when executing this entry point.
1793 **/
1794 EFI_STATUS
1795 EFIAPI
1796 DriverEntry (
1797 IN EFI_HANDLE ImageHandle,
1798 IN EFI_SYSTEM_TABLE *SystemTable
1799 )
1800 {
1801 EFI_STATUS Status;
1802 EFI_EVENT Event;
1803 VOID *Registration;
1804 UINT32 MaxCommandSize;
1805 UINT32 MaxResponseSize;
1806 TPML_PCR_SELECTION Pcrs;
1807 UINTN Index;
1808 UINT32 TpmHashAlgorithmBitmap;
1809
1810 mImageHandle = ImageHandle;
1811
1812 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||
1813 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
1814 DEBUG ((EFI_D_ERROR, "No TPM2 instance required!\n"));
1815 return EFI_UNSUPPORTED;
1816 }
1817
1818 Status = Tpm2RequestUseTpm ();
1819 if (EFI_ERROR (Status)) {
1820 DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));
1821 return Status;
1822 }
1823
1824 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
1825 mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;
1826 }
1827
1828 //
1829 // Fill information
1830 //
1831 DEBUG ((EFI_D_INFO, "TrEE.ProtocolVersion - %02x.%02x\n", mTcgDxeData.BsCap.ProtocolVersion.Major, mTcgDxeData.BsCap.ProtocolVersion.Minor));
1832 DEBUG ((EFI_D_INFO, "TrEE.StructureVersion - %02x.%02x\n", mTcgDxeData.BsCap.StructureVersion.Major, mTcgDxeData.BsCap.StructureVersion.Minor));
1833
1834 Status = Tpm2GetCapabilityManufactureID (&mTcgDxeData.BsCap.ManufacturerID);
1835 if (EFI_ERROR (Status)) {
1836 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityManufactureID fail!\n"));
1837 } else {
1838 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityManufactureID - %08x\n", mTcgDxeData.BsCap.ManufacturerID));
1839 }
1840
1841 DEBUG_CODE (
1842 UINT32 FirmwareVersion1;
1843 UINT32 FirmwareVersion2;
1844
1845 Status = Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1, &FirmwareVersion2);
1846 if (EFI_ERROR (Status)) {
1847 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityFirmwareVersion fail!\n"));
1848 } else {
1849 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityFirmwareVersion - %08x %08x\n", FirmwareVersion1, FirmwareVersion2));
1850 }
1851 );
1852
1853 Status = Tpm2GetCapabilityMaxCommandResponseSize (&MaxCommandSize, &MaxResponseSize);
1854 if (EFI_ERROR (Status)) {
1855 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityMaxCommandResponseSize fail!\n"));
1856 } else {
1857 mTcgDxeData.BsCap.MaxCommandSize = (UINT16)MaxCommandSize;
1858 mTcgDxeData.BsCap.MaxResponseSize = (UINT16)MaxResponseSize;
1859 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityMaxCommandResponseSize - %08x, %08x\n", MaxCommandSize, MaxResponseSize));
1860 }
1861
1862 Status = Tpm2GetCapabilityPcrs (&Pcrs);
1863 if (EFI_ERROR (Status)) {
1864 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityPcrs fail!\n"));
1865 TpmHashAlgorithmBitmap = TREE_BOOT_HASH_ALG_SHA1;
1866 } else {
1867 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityPcrs Count - %08x\n", Pcrs.count));
1868 TpmHashAlgorithmBitmap = 0;
1869 for (Index = 0; Index < Pcrs.count; Index++) {
1870 DEBUG ((EFI_D_INFO, "hash - %x\n", Pcrs.pcrSelections[Index].hash));
1871 switch (Pcrs.pcrSelections[Index].hash) {
1872 case TPM_ALG_SHA1:
1873 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA1;
1874 break;
1875 case TPM_ALG_SHA256:
1876 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA256;
1877 break;
1878 case TPM_ALG_SHA384:
1879 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA384;
1880 break;
1881 case TPM_ALG_SHA512:
1882 TpmHashAlgorithmBitmap |= TREE_BOOT_HASH_ALG_SHA512;
1883 break;
1884 case TPM_ALG_SM3_256:
1885 // TBD: Spec not define TREE_BOOT_HASH_ALG_SM3_256 yet
1886 break;
1887 }
1888 }
1889 }
1890 DEBUG ((EFI_D_INFO, "TPM.HashAlgorithmBitmap - 0x%08x\n", TpmHashAlgorithmBitmap));
1891
1892 DEBUG ((EFI_D_INFO, "TrEE.SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
1893 mTcgDxeData.BsCap.HashAlgorithmBitmap = TpmHashAlgorithmBitmap;
1894 DEBUG ((EFI_D_INFO, "TrEE.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData.BsCap.HashAlgorithmBitmap));
1895
1896 if (mTcgDxeData.BsCap.TrEEPresentFlag) {
1897 //
1898 // Setup the log area and copy event log from hob list to it
1899 //
1900 Status = SetupEventLog ();
1901 ASSERT_EFI_ERROR (Status);
1902
1903 //
1904 // Measure handoff tables, Boot#### variables etc.
1905 //
1906 Status = EfiCreateEventReadyToBootEx (
1907 TPL_CALLBACK,
1908 OnReadyToBoot,
1909 NULL,
1910 &Event
1911 );
1912
1913 Status = gBS->CreateEventEx (
1914 EVT_NOTIFY_SIGNAL,
1915 TPL_NOTIFY,
1916 OnExitBootServices,
1917 NULL,
1918 &gEfiEventExitBootServicesGuid,
1919 &Event
1920 );
1921
1922 //
1923 // Measure Exit Boot Service failed
1924 //
1925 Status = gBS->CreateEventEx (
1926 EVT_NOTIFY_SIGNAL,
1927 TPL_NOTIFY,
1928 OnExitBootServicesFailed,
1929 NULL,
1930 &gEventExitBootServicesFailedGuid,
1931 &Event
1932 );
1933
1934 //
1935 // Create event callback, because we need access variable on SecureBootPolicyVariable
1936 // We should use VariableWriteArch instead of VariableArch, because Variable driver
1937 // may update SecureBoot value based on last setting.
1938 //
1939 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);
1940 }
1941
1942 //
1943 // Install ACPI Table
1944 //
1945 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);
1946
1947 //
1948 // Install TrEEProtocol
1949 //
1950 Status = InstallTrEE ();
1951 DEBUG ((EFI_D_INFO, "InstallTrEE - %r\n", Status));
1952
1953 return Status;
1954 }