2 It updates TPM2 items in ACPI table and registers SMI2 callback
3 functions for Tcg2 physical presence, ClearMemory, and sample
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable and ACPINvs data in SMM mode.
8 This external input must be validated carefully to avoid security issue.
10 PhysicalPresenceCallback() and MemoryClearCallback() will receive untrusted input and do some check.
12 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
33 Return PTP interface type.
35 @param[in] Register Pointer to PTP register.
37 @return PTP interface type.
44 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId
;
45 PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability
;
50 InterfaceId
.Uint32
= MmioRead32 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->InterfaceId
);
51 InterfaceCapability
.Uint32
= MmioRead32 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->InterfaceCapability
);
53 if (InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_TIS
) {
54 return PtpInterfaceTis
;
57 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB
) &&
58 (InterfaceId
.Bits
.InterfaceVersion
== PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_CRB
) &&
59 (InterfaceId
.Bits
.CapCRB
!= 0)) {
60 return PtpInterfaceCrb
;
63 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO
) &&
64 (InterfaceId
.Bits
.InterfaceVersion
== PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_FIFO
) &&
65 (InterfaceId
.Bits
.CapFIFO
!= 0) &&
66 (InterfaceCapability
.Bits
.InterfaceVersion
== INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP
)) {
67 return PtpInterfaceFifo
;
71 // No Ptp interface available
73 return PtpInterfaceMax
;
76 EFI_TPM2_ACPI_TABLE mTpm2AcpiTemplate
= {
78 EFI_ACPI_5_0_TRUSTED_COMPUTING_PLATFORM_2_TABLE_SIGNATURE
,
79 sizeof (mTpm2AcpiTemplate
),
80 EFI_TPM2_ACPI_TABLE_REVISION
,
82 // Compiler initializes the remaining bytes to 0
83 // These fields should be filled in in production
88 EFI_TPM2_ACPI_TABLE_START_METHOD_TIS
, // StartMethod
91 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
;
95 Software SMI callback for TPM physical presence which is called from ACPI method.
97 Caution: This function may receive untrusted input.
98 Variable and ACPINvs are external input, so this function will validate
99 its data structure to be valid value.
101 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
102 @param[in] Context Points to an optional handler context which was specified when the
103 handler was registered.
104 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
105 be conveyed from a non-SMM environment into an SMM environment.
106 @param[in, out] CommBufferSize The size of the CommBuffer.
108 @retval EFI_SUCCESS The interrupt was handled successfully.
113 PhysicalPresenceCallback (
114 IN EFI_HANDLE DispatchHandle
,
115 IN CONST VOID
*Context
,
116 IN OUT VOID
*CommBuffer
,
117 IN OUT UINTN
*CommBufferSize
120 UINT32 MostRecentRequest
;
122 UINT32 OperationRequest
;
123 UINT32 RequestParameter
;
126 if (mTcgNvs
->PhysicalPresence
.Parameter
== TCG_ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS
) {
127 mTcgNvs
->PhysicalPresence
.ReturnCode
= Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (
131 mTcgNvs
->PhysicalPresence
.LastRequest
= MostRecentRequest
;
132 mTcgNvs
->PhysicalPresence
.Response
= Response
;
134 } else if ((mTcgNvs
->PhysicalPresence
.Parameter
== TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS
)
135 || (mTcgNvs
->PhysicalPresence
.Parameter
== TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2
)) {
137 OperationRequest
= mTcgNvs
->PhysicalPresence
.Request
;
138 RequestParameter
= mTcgNvs
->PhysicalPresence
.RequestParameter
;
139 mTcgNvs
->PhysicalPresence
.ReturnCode
= Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx (
143 mTcgNvs
->PhysicalPresence
.Request
= OperationRequest
;
144 mTcgNvs
->PhysicalPresence
.RequestParameter
= RequestParameter
;
145 } else if (mTcgNvs
->PhysicalPresence
.Parameter
== TCG_ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST
) {
146 mTcgNvs
->PhysicalPresence
.ReturnCode
= Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (mTcgNvs
->PPRequestUserConfirm
);
154 Software SMI callback for MemoryClear which is called from ACPI method.
156 Caution: This function may receive untrusted input.
157 Variable and ACPINvs are external input, so this function will validate
158 its data structure to be valid value.
160 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
161 @param[in] Context Points to an optional handler context which was specified when the
162 handler was registered.
163 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
164 be conveyed from a non-SMM environment into an SMM environment.
165 @param[in, out] CommBufferSize The size of the CommBuffer.
167 @retval EFI_SUCCESS The interrupt was handled successfully.
172 MemoryClearCallback (
173 IN EFI_HANDLE DispatchHandle
,
174 IN CONST VOID
*Context
,
175 IN OUT VOID
*CommBuffer
,
176 IN OUT UINTN
*CommBufferSize
183 mTcgNvs
->MemoryClear
.ReturnCode
= MOR_REQUEST_SUCCESS
;
184 if (mTcgNvs
->MemoryClear
.Parameter
== ACPI_FUNCTION_DSM_MEMORY_CLEAR_INTERFACE
) {
185 MorControl
= (UINT8
) mTcgNvs
->MemoryClear
.Request
;
186 } else if (mTcgNvs
->MemoryClear
.Parameter
== ACPI_FUNCTION_PTS_CLEAR_MOR_BIT
) {
187 DataSize
= sizeof (UINT8
);
188 Status
= mSmmVariable
->SmmGetVariable (
189 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME
,
190 &gEfiMemoryOverwriteControlDataGuid
,
195 if (EFI_ERROR (Status
)) {
196 mTcgNvs
->MemoryClear
.ReturnCode
= MOR_REQUEST_GENERAL_FAILURE
;
197 DEBUG ((EFI_D_ERROR
, "[TPM] Get MOR variable failure! Status = %r\n", Status
));
201 if (MOR_CLEAR_MEMORY_VALUE (MorControl
) == 0x0) {
204 MorControl
&= ~MOR_CLEAR_MEMORY_BIT_MASK
;
207 DataSize
= sizeof (UINT8
);
208 Status
= mSmmVariable
->SmmSetVariable (
209 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME
,
210 &gEfiMemoryOverwriteControlDataGuid
,
211 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
215 if (EFI_ERROR (Status
)) {
216 mTcgNvs
->MemoryClear
.ReturnCode
= MOR_REQUEST_GENERAL_FAILURE
;
217 DEBUG ((EFI_D_ERROR
, "[TPM] Set MOR variable failure! Status = %r\n", Status
));
224 Find the operation region in TCG ACPI table by given Name and Size,
225 and initialize it if the region is found.
227 @param[in, out] Table The TPM item in ACPI table.
228 @param[in] Name The name string to find in TPM table.
229 @param[in] Size The size of the region to find.
231 @return The allocated address for the found region.
236 EFI_ACPI_DESCRIPTION_HEADER
*Table
,
242 AML_OP_REGION_32_8
*OpRegion
;
243 EFI_PHYSICAL_ADDRESS MemoryAddress
;
245 MemoryAddress
= SIZE_4GB
- 1;
248 // Patch some pointers for the ASL code before loading the SSDT.
250 for (OpRegion
= (AML_OP_REGION_32_8
*) (Table
+ 1);
251 OpRegion
<= (AML_OP_REGION_32_8
*) ((UINT8
*) Table
+ Table
->Length
);
252 OpRegion
= (AML_OP_REGION_32_8
*) ((UINT8
*) OpRegion
+ 1)) {
253 if ((OpRegion
->OpRegionOp
== AML_EXT_REGION_OP
) &&
254 (OpRegion
->NameString
== Name
) &&
255 (OpRegion
->DWordPrefix
== AML_DWORD_PREFIX
) &&
256 (OpRegion
->BytePrefix
== AML_BYTE_PREFIX
)) {
258 Status
= gBS
->AllocatePages(AllocateMaxAddress
, EfiACPIMemoryNVS
, EFI_SIZE_TO_PAGES (Size
), &MemoryAddress
);
259 ASSERT_EFI_ERROR (Status
);
260 ZeroMem ((VOID
*)(UINTN
)MemoryAddress
, Size
);
261 OpRegion
->RegionOffset
= (UINT32
) (UINTN
) MemoryAddress
;
262 OpRegion
->RegionLen
= (UINT8
) Size
;
267 return (VOID
*) (UINTN
) MemoryAddress
;
271 Patch version string of Physical Presence interface supported by platform. The initial string tag in TPM
274 @param[in, out] Table The TPM item in ACPI table.
275 @param[in] PPVer Version string of Physical Presence interface supported by platform.
277 @return The allocated address for the found region.
282 EFI_ACPI_DESCRIPTION_HEADER
*Table
,
290 // Patch some pointers for the ASL code before loading the SSDT.
292 for (DataPtr
= (UINT8
*)(Table
+ 1);
293 DataPtr
<= (UINT8
*) ((UINT8
*) Table
+ Table
->Length
- PHYSICAL_PRESENCE_VERSION_SIZE
);
295 if (AsciiStrCmp((CHAR8
*)DataPtr
, PHYSICAL_PRESENCE_VERSION_TAG
) == 0) {
296 Status
= AsciiStrCpyS((CHAR8
*)DataPtr
, PHYSICAL_PRESENCE_VERSION_SIZE
, PPVer
);
297 DEBUG((EFI_D_INFO
, "TPM2 Physical Presence Interface Version update status 0x%x\n", Status
));
302 return EFI_NOT_FOUND
;
306 Initialize and publish TPM items in ACPI table.
308 @retval EFI_SUCCESS The TCG ACPI table is published successfully.
309 @retval Others The TCG ACPI table is not published.
318 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
320 EFI_ACPI_DESCRIPTION_HEADER
*Table
;
323 Status
= GetSectionFromFv (
330 ASSERT_EFI_ERROR (Status
);
333 // Update Table version before measuring it to PCR
335 Status
= UpdatePPVersion(Table
, (CHAR8
*)PcdGetPtr(PcdTcgPhysicalPresenceInterfaceVer
));
336 ASSERT_EFI_ERROR (Status
);
339 // Measure to PCR[0] with event EV_POST_CODE ACPI DATA
341 TpmMeasureAndLogData(
344 EV_POSTCODE_INFO_ACPI_DATA
,
351 ASSERT (Table
->OemTableId
== SIGNATURE_64 ('T', 'p', 'm', '2', 'T', 'a', 'b', 'l'));
352 CopyMem (Table
->OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (Table
->OemId
) );
353 mTcgNvs
= AssignOpRegion (Table
, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16
) sizeof (TCG_NVS
));
354 ASSERT (mTcgNvs
!= NULL
);
357 // Publish the TPM ACPI table. Table is re-checksumed.
359 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**) &AcpiTable
);
360 ASSERT_EFI_ERROR (Status
);
363 Status
= AcpiTable
->InstallAcpiTable (
369 ASSERT_EFI_ERROR (Status
);
375 Publish TPM2 ACPI table
377 @retval EFI_SUCCESS The TPM2 ACPI table is published successfully.
378 @retval Others The TPM2 ACPI table is not published.
387 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
390 EFI_TPM2_ACPI_CONTROL_AREA
*ControlArea
;
391 PTP_INTERFACE_TYPE InterfaceType
;
394 // Measure to PCR[0] with event EV_POST_CODE ACPI DATA
396 TpmMeasureAndLogData(
399 EV_POSTCODE_INFO_ACPI_DATA
,
402 sizeof(mTpm2AcpiTemplate
)
405 InterfaceType
= GetPtpInterface ((VOID
*) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
406 switch (InterfaceType
) {
407 case PtpInterfaceCrb
:
408 mTpm2AcpiTemplate
.StartMethod
= EFI_TPM2_ACPI_TABLE_START_METHOD_COMMAND_RESPONSE_BUFFER_INTERFACE
;
409 mTpm2AcpiTemplate
.AddressOfControlArea
= PcdGet64 (PcdTpmBaseAddress
) + 0x40;
410 ControlArea
= (EFI_TPM2_ACPI_CONTROL_AREA
*)(UINTN
)mTpm2AcpiTemplate
.AddressOfControlArea
;
411 ControlArea
->CommandSize
= 0xF80;
412 ControlArea
->ResponseSize
= 0xF80;
413 ControlArea
->Command
= PcdGet64 (PcdTpmBaseAddress
) + 0x80;
414 ControlArea
->Response
= PcdGet64 (PcdTpmBaseAddress
) + 0x80;
416 case PtpInterfaceFifo
:
417 case PtpInterfaceTis
:
420 DEBUG((EFI_D_ERROR
, "TPM2 InterfaceType get error! %d\n", InterfaceType
));
424 CopyMem (mTpm2AcpiTemplate
.Header
.OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (mTpm2AcpiTemplate
.Header
.OemId
));
425 OemTableId
= PcdGet64 (PcdAcpiDefaultOemTableId
);
426 CopyMem (&mTpm2AcpiTemplate
.Header
.OemTableId
, &OemTableId
, sizeof (UINT64
));
427 mTpm2AcpiTemplate
.Header
.OemRevision
= PcdGet32 (PcdAcpiDefaultOemRevision
);
428 mTpm2AcpiTemplate
.Header
.CreatorId
= PcdGet32 (PcdAcpiDefaultCreatorId
);
429 mTpm2AcpiTemplate
.Header
.CreatorRevision
= PcdGet32 (PcdAcpiDefaultCreatorRevision
);
432 // Construct ACPI table
434 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**) &AcpiTable
);
435 ASSERT_EFI_ERROR (Status
);
437 Status
= AcpiTable
->InstallAcpiTable (
440 sizeof(mTpm2AcpiTemplate
),
443 ASSERT_EFI_ERROR (Status
);
449 The driver's entry point.
451 It install callbacks for TPM physical presence and MemoryClear, and locate
452 SMM variable to be used in the callback function.
454 @param[in] ImageHandle The firmware allocated handle for the EFI image.
455 @param[in] SystemTable A pointer to the EFI System Table.
457 @retval EFI_SUCCESS The entry point is executed successfully.
458 @retval Others Some error occurs when executing this entry point.
464 IN EFI_HANDLE ImageHandle
,
465 IN EFI_SYSTEM_TABLE
*SystemTable
469 EFI_SMM_SW_DISPATCH2_PROTOCOL
*SwDispatch
;
470 EFI_SMM_SW_REGISTER_CONTEXT SwContext
;
473 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceTpm20DtpmGuid
)){
474 DEBUG ((EFI_D_ERROR
, "No TPM2 DTPM instance required!\n"));
475 return EFI_UNSUPPORTED
;
478 Status
= PublishAcpiTable ();
479 ASSERT_EFI_ERROR (Status
);
482 // Get the Sw dispatch protocol and register SMI callback functions.
484 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid
, NULL
, (VOID
**)&SwDispatch
);
485 ASSERT_EFI_ERROR (Status
);
486 SwContext
.SwSmiInputValue
= (UINTN
) -1;
487 Status
= SwDispatch
->Register (SwDispatch
, PhysicalPresenceCallback
, &SwContext
, &SwHandle
);
488 ASSERT_EFI_ERROR (Status
);
489 if (EFI_ERROR (Status
)) {
492 mTcgNvs
->PhysicalPresence
.SoftwareSmi
= (UINT8
) SwContext
.SwSmiInputValue
;
494 SwContext
.SwSmiInputValue
= (UINTN
) -1;
495 Status
= SwDispatch
->Register (SwDispatch
, MemoryClearCallback
, &SwContext
, &SwHandle
);
496 ASSERT_EFI_ERROR (Status
);
497 if (EFI_ERROR (Status
)) {
500 mTcgNvs
->MemoryClear
.SoftwareSmi
= (UINT8
) SwContext
.SwSmiInputValue
;
503 // Locate SmmVariableProtocol.
505 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**)&mSmmVariable
);
506 ASSERT_EFI_ERROR (Status
);
509 // Set TPM2 ACPI table
511 Status
= PublishTpm2 ();
512 ASSERT_EFI_ERROR (Status
);