2 It updates TPM2 items in ACPI table and registers SMI2 callback
3 functions for TrEE 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) 2013 - 2014, 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.
25 EFI_TPM2_ACPI_TABLE mTpm2AcpiTemplate
= {
27 EFI_ACPI_5_0_TRUSTED_COMPUTING_PLATFORM_2_TABLE_SIGNATURE
,
28 sizeof (mTpm2AcpiTemplate
),
29 EFI_TPM2_ACPI_TABLE_REVISION
,
31 // Compiler initializes the remaining bytes to 0
32 // These fields should be filled in in production
37 EFI_TPM2_ACPI_TABLE_START_METHOD_TIS
, // StartMethod
40 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
;
44 Software SMI callback for TPM physical presence which is called from ACPI method.
46 Caution: This function may receive untrusted input.
47 Variable and ACPINvs are external input, so this function will validate
48 its data structure to be valid value.
50 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
51 @param[in] Context Points to an optional handler context which was specified when the
52 handler was registered.
53 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
54 be conveyed from a non-SMM environment into an SMM environment.
55 @param[in, out] CommBufferSize The size of the CommBuffer.
57 @retval EFI_SUCCESS The interrupt was handled successfully.
62 PhysicalPresenceCallback (
63 IN EFI_HANDLE DispatchHandle
,
64 IN CONST VOID
*Context
,
65 IN OUT VOID
*CommBuffer
,
66 IN OUT UINTN
*CommBufferSize
71 EFI_TREE_PHYSICAL_PRESENCE PpData
;
73 BOOLEAN RequestConfirmed
;
76 // Get the Physical Presence variable
78 DataSize
= sizeof (EFI_TREE_PHYSICAL_PRESENCE
);
79 Status
= mSmmVariable
->SmmGetVariable (
80 TREE_PHYSICAL_PRESENCE_VARIABLE
,
81 &gEfiTrEEPhysicalPresenceGuid
,
86 if (EFI_ERROR (Status
)) {
87 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_SUBMIT_REQUEST_GENERAL_FAILURE
;
88 DEBUG ((EFI_D_ERROR
, "[TPM] Get PP variable failure! Status = %r\n", Status
));
92 DEBUG ((EFI_D_INFO
, "[TPM2] PP callback, Parameter = %x, Request = %x\n", mTcgNvs
->PhysicalPresence
.Parameter
, mTcgNvs
->PhysicalPresence
.Request
));
94 if (mTcgNvs
->PhysicalPresence
.Parameter
== ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS
) {
95 mTcgNvs
->PhysicalPresence
.LastRequest
= PpData
.LastPPRequest
;
96 mTcgNvs
->PhysicalPresence
.Response
= PpData
.PPResponse
;
97 } else if ((mTcgNvs
->PhysicalPresence
.Parameter
== ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS
)
98 || (mTcgNvs
->PhysicalPresence
.Parameter
== ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2
)) {
99 if (mTcgNvs
->PhysicalPresence
.Request
> TREE_PHYSICAL_PRESENCE_NO_ACTION_MAX
) {
101 // This command requires UI to prompt user for Auth data.
103 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_SUBMIT_REQUEST_NOT_IMPLEMENTED
;
107 if (PpData
.PPRequest
!= mTcgNvs
->PhysicalPresence
.Request
) {
108 PpData
.PPRequest
= (UINT8
) mTcgNvs
->PhysicalPresence
.Request
;
109 DataSize
= sizeof (EFI_TREE_PHYSICAL_PRESENCE
);
110 Status
= mSmmVariable
->SmmSetVariable (
111 TREE_PHYSICAL_PRESENCE_VARIABLE
,
112 &gEfiTrEEPhysicalPresenceGuid
,
113 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
119 if (EFI_ERROR (Status
)) {
120 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_SUBMIT_REQUEST_GENERAL_FAILURE
;
121 DEBUG ((EFI_D_ERROR
, "[TPM] Set PP variable failure! Status = %r\n", Status
));
124 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_SUBMIT_REQUEST_SUCCESS
;
125 } else if (mTcgNvs
->PhysicalPresence
.Parameter
== ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST
) {
127 // Get the Physical Presence flags
129 DataSize
= sizeof (UINT8
);
130 Status
= mSmmVariable
->SmmGetVariable (
131 TREE_PHYSICAL_PRESENCE_FLAGS_VARIABLE
,
132 &gEfiTrEEPhysicalPresenceGuid
,
137 if (EFI_ERROR (Status
)) {
138 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_SUBMIT_REQUEST_GENERAL_FAILURE
;
139 DEBUG ((EFI_D_ERROR
, "[TPM] Get PP flags failure! Status = %r\n", Status
));
143 RequestConfirmed
= FALSE
;
145 switch (mTcgNvs
->PhysicalPresence
.Request
) {
147 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR
:
148 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR_2
:
149 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR_3
:
150 case TREE_PHYSICAL_PRESENCE_CLEAR_CONTROL_CLEAR_4
:
151 if ((Flags
& TREE_FLAG_NO_PPI_CLEAR
) != 0) {
152 RequestConfirmed
= TRUE
;
156 case TREE_PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE
:
157 RequestConfirmed
= TRUE
;
160 case TREE_PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE
:
164 if (mTcgNvs
->PhysicalPresence
.Request
<= TREE_PHYSICAL_PRESENCE_NO_ACTION_MAX
) {
165 RequestConfirmed
= TRUE
;
167 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_REQUEST_NOT_IMPLEMENTED
;
173 if (RequestConfirmed
) {
174 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_REQUEST_ALLOWED_AND_PPUSER_NOT_REQUIRED
;
176 mTcgNvs
->PhysicalPresence
.ReturnCode
= PP_REQUEST_ALLOWED_AND_PPUSER_REQUIRED
;
185 Software SMI callback for MemoryClear which is called from ACPI method.
187 Caution: This function may receive untrusted input.
188 Variable and ACPINvs are external input, so this function will validate
189 its data structure to be valid value.
191 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
192 @param[in] Context Points to an optional handler context which was specified when the
193 handler was registered.
194 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
195 be conveyed from a non-SMM environment into an SMM environment.
196 @param[in, out] CommBufferSize The size of the CommBuffer.
198 @retval EFI_SUCCESS The interrupt was handled successfully.
203 MemoryClearCallback (
204 IN EFI_HANDLE DispatchHandle
,
205 IN CONST VOID
*Context
,
206 IN OUT VOID
*CommBuffer
,
207 IN OUT UINTN
*CommBufferSize
214 mTcgNvs
->MemoryClear
.ReturnCode
= MOR_REQUEST_SUCCESS
;
215 if (mTcgNvs
->MemoryClear
.Parameter
== ACPI_FUNCTION_DSM_MEMORY_CLEAR_INTERFACE
) {
216 MorControl
= (UINT8
) mTcgNvs
->MemoryClear
.Request
;
217 } else if (mTcgNvs
->MemoryClear
.Parameter
== ACPI_FUNCTION_PTS_CLEAR_MOR_BIT
) {
218 DataSize
= sizeof (UINT8
);
219 Status
= mSmmVariable
->SmmGetVariable (
220 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME
,
221 &gEfiMemoryOverwriteControlDataGuid
,
226 if (EFI_ERROR (Status
)) {
227 mTcgNvs
->MemoryClear
.ReturnCode
= MOR_REQUEST_GENERAL_FAILURE
;
228 DEBUG ((EFI_D_ERROR
, "[TPM] Get MOR variable failure! Status = %r\n", Status
));
232 if (MOR_CLEAR_MEMORY_VALUE (MorControl
) == 0x0) {
235 MorControl
&= ~MOR_CLEAR_MEMORY_BIT_MASK
;
238 DataSize
= sizeof (UINT8
);
239 Status
= mSmmVariable
->SmmSetVariable (
240 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME
,
241 &gEfiMemoryOverwriteControlDataGuid
,
242 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
246 if (EFI_ERROR (Status
)) {
247 mTcgNvs
->MemoryClear
.ReturnCode
= MOR_REQUEST_GENERAL_FAILURE
;
248 DEBUG ((EFI_D_ERROR
, "[TPM] Set MOR variable failure! Status = %r\n", Status
));
255 Find the operation region in TCG ACPI table by given Name and Size,
256 and initialize it if the region is found.
258 @param[in, out] Table The TPM item in ACPI table.
259 @param[in] Name The name string to find in TPM table.
260 @param[in] Size The size of the region to find.
262 @return The allocated address for the found region.
267 EFI_ACPI_DESCRIPTION_HEADER
*Table
,
273 AML_OP_REGION_32_8
*OpRegion
;
274 EFI_PHYSICAL_ADDRESS MemoryAddress
;
276 MemoryAddress
= SIZE_4GB
- 1;
279 // Patch some pointers for the ASL code before loading the SSDT.
281 for (OpRegion
= (AML_OP_REGION_32_8
*) (Table
+ 1);
282 OpRegion
<= (AML_OP_REGION_32_8
*) ((UINT8
*) Table
+ Table
->Length
);
283 OpRegion
= (AML_OP_REGION_32_8
*) ((UINT8
*) OpRegion
+ 1)) {
284 if ((OpRegion
->OpRegionOp
== AML_EXT_REGION_OP
) &&
285 (OpRegion
->NameString
== Name
) &&
286 (OpRegion
->DWordPrefix
== AML_DWORD_PREFIX
) &&
287 (OpRegion
->BytePrefix
== AML_BYTE_PREFIX
)) {
289 Status
= gBS
->AllocatePages(AllocateMaxAddress
, EfiACPIMemoryNVS
, EFI_SIZE_TO_PAGES (Size
), &MemoryAddress
);
290 ASSERT_EFI_ERROR (Status
);
291 ZeroMem ((VOID
*)(UINTN
)MemoryAddress
, Size
);
292 OpRegion
->RegionOffset
= (UINT32
) (UINTN
) MemoryAddress
;
293 OpRegion
->RegionLen
= (UINT8
) Size
;
298 return (VOID
*) (UINTN
) MemoryAddress
;
302 Initialize and publish TPM items in ACPI table.
304 @retval EFI_SUCCESS The TCG ACPI table is published successfully.
305 @retval Others The TCG ACPI table is not published.
314 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
316 EFI_ACPI_DESCRIPTION_HEADER
*Table
;
319 Status
= GetSectionFromFv (
326 ASSERT_EFI_ERROR (Status
);
330 // Measure to PCR[0] with event EV_POST_CODE ACPI DATA
332 TpmMeasureAndLogData(
335 EV_POSTCODE_INFO_ACPI_DATA
,
342 ASSERT (Table
->OemTableId
== SIGNATURE_64 ('T', 'p', 'm', '2', 'T', 'a', 'b', 'l'));
343 CopyMem (Table
->OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (Table
->OemId
) );
344 mTcgNvs
= AssignOpRegion (Table
, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16
) sizeof (TCG_NVS
));
345 ASSERT (mTcgNvs
!= NULL
);
348 // Publish the TPM ACPI table
350 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**) &AcpiTable
);
351 ASSERT_EFI_ERROR (Status
);
354 Status
= AcpiTable
->InstallAcpiTable (
360 ASSERT_EFI_ERROR (Status
);
366 Publish TPM2 ACPI table
368 @retval EFI_SUCCESS The TPM2 ACPI table is published successfully.
369 @retval Others The TPM2 ACPI table is not published.
378 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
383 // Measure to PCR[0] with event EV_POST_CODE ACPI DATA
385 TpmMeasureAndLogData(
388 EV_POSTCODE_INFO_ACPI_DATA
,
391 sizeof(mTpm2AcpiTemplate
)
394 CopyMem (mTpm2AcpiTemplate
.Header
.OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (mTpm2AcpiTemplate
.Header
.OemId
));
395 OemTableId
= PcdGet64 (PcdAcpiDefaultOemTableId
);
396 CopyMem (&mTpm2AcpiTemplate
.Header
.OemTableId
, &OemTableId
, sizeof (UINT64
));
397 mTpm2AcpiTemplate
.Header
.OemRevision
= PcdGet32 (PcdAcpiDefaultOemRevision
);
398 mTpm2AcpiTemplate
.Header
.CreatorId
= PcdGet32 (PcdAcpiDefaultCreatorId
);
399 mTpm2AcpiTemplate
.Header
.CreatorRevision
= PcdGet32 (PcdAcpiDefaultCreatorRevision
);
402 // Construct ACPI table
404 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**) &AcpiTable
);
405 ASSERT_EFI_ERROR (Status
);
407 Status
= AcpiTable
->InstallAcpiTable (
410 sizeof(mTpm2AcpiTemplate
),
413 ASSERT_EFI_ERROR (Status
);
419 The driver's entry point.
421 It install callbacks for TPM physical presence and MemoryClear, and locate
422 SMM variable to be used in the callback function.
424 @param[in] ImageHandle The firmware allocated handle for the EFI image.
425 @param[in] SystemTable A pointer to the EFI System Table.
427 @retval EFI_SUCCESS The entry point is executed successfully.
428 @retval Others Some error occurs when executing this entry point.
434 IN EFI_HANDLE ImageHandle
,
435 IN EFI_SYSTEM_TABLE
*SystemTable
439 EFI_SMM_SW_DISPATCH2_PROTOCOL
*SwDispatch
;
440 EFI_SMM_SW_REGISTER_CONTEXT SwContext
;
443 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceTpm20DtpmGuid
)){
444 DEBUG ((EFI_D_ERROR
, "No TPM2 DTPM instance required!\n"));
445 return EFI_UNSUPPORTED
;
448 Status
= PublishAcpiTable ();
449 ASSERT_EFI_ERROR (Status
);
452 // Get the Sw dispatch protocol and register SMI callback functions.
454 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid
, NULL
, (VOID
**)&SwDispatch
);
455 ASSERT_EFI_ERROR (Status
);
456 SwContext
.SwSmiInputValue
= (UINTN
) -1;
457 Status
= SwDispatch
->Register (SwDispatch
, PhysicalPresenceCallback
, &SwContext
, &SwHandle
);
458 ASSERT_EFI_ERROR (Status
);
459 if (EFI_ERROR (Status
)) {
462 mTcgNvs
->PhysicalPresence
.SoftwareSmi
= (UINT8
) SwContext
.SwSmiInputValue
;
464 SwContext
.SwSmiInputValue
= (UINTN
) -1;
465 Status
= SwDispatch
->Register (SwDispatch
, MemoryClearCallback
, &SwContext
, &SwHandle
);
466 ASSERT_EFI_ERROR (Status
);
467 if (EFI_ERROR (Status
)) {
470 mTcgNvs
->MemoryClear
.SoftwareSmi
= (UINT8
) SwContext
.SwSmiInputValue
;
473 // Locate SmmVariableProtocol.
475 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**)&mSmmVariable
);
476 ASSERT_EFI_ERROR (Status
);
479 // Set TPM2 ACPI table
481 Status
= PublishTpm2 ();
482 ASSERT_EFI_ERROR (Status
);