2 It updates TPM items in ACPI table and registers SMI callback
3 functions for physical presence and ClearMemory.
5 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <IndustryStandard/Acpi.h>
19 #include <Guid/PhysicalPresenceData.h>
20 #include <Guid/MemoryOverwriteControl.h>
21 #include <Protocol/SmmSwDispatch2.h>
22 #include <Protocol/AcpiTable.h>
23 #include <Protocol/SmmVariable.h>
25 #include <Library/BaseLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/SmmServicesTableLib.h>
29 #include <Library/UefiDriverEntryPoint.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/DxeServicesLib.h>
34 // AML parsing definitions
36 #define AML_OPREGION_OP 0x80
37 #define AML_BYTE_PREFIX 0x0A
38 #define AML_DWORD_PREFIX 0x0C
48 } PHYSICAL_PRESENCE_NVS
;
57 PHYSICAL_PRESENCE_NVS PhysicalPresence
;
58 MEMORY_CLEAR_NVS MemoryClear
;
72 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
;
76 Software SMI callback for TPM physical presence which is called from ACPI method.
78 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
79 @param[in] Context Points to an optional handler context which was specified when the
80 handler was registered.
81 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
82 be conveyed from a non-SMM environment into an SMM environment.
83 @param[in, out] CommBufferSize The size of the CommBuffer.
85 @retval EFI_SUCCESS The interrupt was handled successfully.
90 PhysicalPresenceCallback (
91 IN EFI_HANDLE DispatchHandle
,
92 IN CONST VOID
*Context
,
93 IN OUT VOID
*CommBuffer
,
94 IN OUT UINTN
*CommBufferSize
99 EFI_PHYSICAL_PRESENCE PpData
;
101 BOOLEAN RequestConfirmed
;
104 // Get the Physical Presence variable
106 DataSize
= sizeof (EFI_PHYSICAL_PRESENCE
);
107 Status
= mSmmVariable
->SmmGetVariable (
108 PHYSICAL_PRESENCE_VARIABLE
,
109 &gEfiPhysicalPresenceGuid
,
114 if (EFI_ERROR (Status
)) {
118 DEBUG ((EFI_D_INFO
, "[TPM] PP callback, Parameter = %x\n", mTcgNvs
->PhysicalPresence
.Parameter
));
119 if (mTcgNvs
->PhysicalPresence
.Parameter
== 5) {
121 // Return TPM Operation Response to OS Environment
123 mTcgNvs
->PhysicalPresence
.LastRequest
= PpData
.LastPPRequest
;
124 mTcgNvs
->PhysicalPresence
.Response
= PpData
.PPResponse
;
126 } else if ((mTcgNvs
->PhysicalPresence
.Parameter
== 2) || (mTcgNvs
->PhysicalPresence
.Parameter
== 7)) {
128 // Submit TPM Operation Request to Pre-OS Environment
131 if (mTcgNvs
->PhysicalPresence
.Request
== SET_OPERATOR_AUTH
) {
133 // This command requires UI to prompt user for Auth data, NOT implemented.
135 mTcgNvs
->PhysicalPresence
.ReturnCode
= 1;
139 if (PpData
.PPRequest
!= mTcgNvs
->PhysicalPresence
.Request
) {
140 PpData
.PPRequest
= (UINT8
) mTcgNvs
->PhysicalPresence
.Request
;
141 DataSize
= sizeof (EFI_PHYSICAL_PRESENCE
);
142 Status
= mSmmVariable
->SmmSetVariable (
143 PHYSICAL_PRESENCE_VARIABLE
,
144 &gEfiPhysicalPresenceGuid
,
145 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
151 if (EFI_ERROR (Status
)) {
155 mTcgNvs
->PhysicalPresence
.ReturnCode
= 2;
158 mTcgNvs
->PhysicalPresence
.ReturnCode
= 0;
159 } else if (mTcgNvs
->PhysicalPresence
.Parameter
== 8) {
161 // Get User Confirmation Status for Operation
163 Flags
= PpData
.Flags
;
164 RequestConfirmed
= FALSE
;
166 switch (mTcgNvs
->PhysicalPresence
.Request
) {
171 case ENABLE_ACTIVATE
:
172 case DEACTIVATE_DISABLE
:
173 case SET_OWNER_INSTALL_TRUE
:
174 case SET_OWNER_INSTALL_FALSE
:
175 case ENABLE_ACTIVATE_OWNER_TRUE
:
176 case DEACTIVATE_DISABLE_OWNER_FALSE
:
177 if ((Flags
& FLAG_NO_PPI_PROVISION
) != 0) {
178 RequestConfirmed
= TRUE
;
183 case ENABLE_ACTIVATE_CLEAR
:
184 if ((Flags
& FLAG_NO_PPI_CLEAR
) != 0) {
185 RequestConfirmed
= TRUE
;
189 case DEFERRED_PP_UNOWNERED_FIELD_UPGRADE
:
190 if ((Flags
& FLAG_NO_PPI_MAINTENANCE
) != 0) {
191 RequestConfirmed
= TRUE
;
195 case ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE
:
196 case CLEAR_ENABLE_ACTIVATE
:
197 if ((Flags
& FLAG_NO_PPI_CLEAR
) != 0 && (Flags
& FLAG_NO_PPI_PROVISION
) != 0) {
198 RequestConfirmed
= TRUE
;
202 case SET_NO_PPI_PROVISION_FALSE
:
203 case SET_NO_PPI_CLEAR_FALSE
:
204 case SET_NO_PPI_MAINTENANCE_FALSE
:
206 RequestConfirmed
= TRUE
;
209 case SET_OPERATOR_AUTH
:
211 // This command requires UI to prompt user for Auth data
212 // Here it is NOT implemented
214 mTcgNvs
->PhysicalPresence
.ReturnCode
= 0;
218 if (RequestConfirmed
) {
220 // Allowed and physically present user not required
222 mTcgNvs
->PhysicalPresence
.ReturnCode
= 4;
225 // Allowed and physically present user required
227 mTcgNvs
->PhysicalPresence
.ReturnCode
= 3;
236 Software SMI callback for MemoryClear which is called from ACPI method.
238 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
239 @param[in] Context Points to an optional handler context which was specified when the
240 handler was registered.
241 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
242 be conveyed from a non-SMM environment into an SMM environment.
243 @param[in, out] CommBufferSize The size of the CommBuffer.
245 @retval EFI_SUCCESS The interrupt was handled successfully.
250 MemoryClearCallback (
251 IN EFI_HANDLE DispatchHandle
,
252 IN CONST VOID
*Context
,
253 IN OUT VOID
*CommBuffer
,
254 IN OUT UINTN
*CommBufferSize
261 if (mTcgNvs
->MemoryClear
.Parameter
== 1) {
263 // Called from ACPI _DSM method, save the MOR data to variable.
265 MorControl
= (UINT8
) mTcgNvs
->MemoryClear
.Request
;
266 } else if (mTcgNvs
->MemoryClear
.Parameter
== 2) {
268 // Called from ACPI _PTS method, setup ClearMemory flags if needed.
270 DataSize
= sizeof (UINT8
);
271 Status
= mSmmVariable
->SmmGetVariable (
272 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME
,
273 &gEfiMemoryOverwriteControlDataGuid
,
278 if (EFI_ERROR (Status
)) {
279 ASSERT (Status
== EFI_NOT_FOUND
);
283 if (MOR_CLEAR_MEMORY_VALUE (MorControl
) == 0x0) {
286 MorControl
&= ~MOR_CLEAR_MEMORY_BIT_MASK
;
289 DataSize
= sizeof (UINT8
);
290 Status
= mSmmVariable
->SmmSetVariable (
291 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME
,
292 &gEfiMemoryOverwriteControlDataGuid
,
293 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
297 ASSERT_EFI_ERROR (Status
);
303 Find the operation region in TCG ACPI table by given Name and Size,
304 and initialize it if the region is found.
306 @param[in, out] Table The TPM item in ACPI table.
307 @param[in] Name The name string to find in TPM table.
308 @param[in] Size The size of the region to find.
310 @return The allocated address for the found region.
315 EFI_ACPI_DESCRIPTION_HEADER
*Table
,
321 AML_OP_REGION_32_8
*OpRegion
;
322 EFI_PHYSICAL_ADDRESS MemoryAddress
;
324 MemoryAddress
= SIZE_4GB
- 1;
327 // Patch some pointers for the ASL code before loading the SSDT.
329 for (OpRegion
= (AML_OP_REGION_32_8
*) (Table
+ 1);
330 OpRegion
<= (AML_OP_REGION_32_8
*) ((UINT8
*) Table
+ Table
->Length
);
331 OpRegion
= (AML_OP_REGION_32_8
*) ((UINT8
*) OpRegion
+ 1)) {
332 if ((OpRegion
->OpRegionOp
== AML_OPREGION_OP
) &&
333 (OpRegion
->NameString
== Name
) &&
334 (OpRegion
->RegionLen
== Size
) &&
335 (OpRegion
->DWordPrefix
== AML_DWORD_PREFIX
) &&
336 (OpRegion
->BytePrefix
== AML_BYTE_PREFIX
)) {
338 Status
= gBS
->AllocatePages(AllocateMaxAddress
, EfiACPIMemoryNVS
, EFI_SIZE_TO_PAGES (Size
), &MemoryAddress
);
339 ASSERT_EFI_ERROR (Status
);
340 ZeroMem ((VOID
*)(UINTN
)MemoryAddress
, Size
);
341 OpRegion
->RegionOffset
= (UINT32
) (UINTN
) MemoryAddress
;
346 return (VOID
*) (UINTN
) MemoryAddress
;
350 Initialize and publish TPM items in ACPI table.
352 @retval EFI_SUCCESS The TCG ACPI table is published successfully.
353 @retval Others The TCG ACPI table is not published.
362 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
364 EFI_ACPI_DESCRIPTION_HEADER
*Table
;
367 Status
= GetSectionFromFv (
374 ASSERT_EFI_ERROR (Status
);
376 ASSERT (Table
->OemTableId
== SIGNATURE_64 ('T', 'c', 'g', 'T', 'a', 'b', 'l', 'e'));
377 mTcgNvs
= AssignOpRegion (Table
, SIGNATURE_32 ('T', 'N', 'V', 'S'), (UINT16
) sizeof (TCG_NVS
));
378 ASSERT (mTcgNvs
!= NULL
);
381 // Publish the TPM ACPI table
383 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**) &AcpiTable
);
384 ASSERT_EFI_ERROR (Status
);
387 Status
= AcpiTable
->InstallAcpiTable (
393 ASSERT_EFI_ERROR (Status
);
399 The driver's entry point.
401 It install callbacks for TPM physical presence and MemoryClear, and locate
402 SMM variable to be used in the callback function.
404 @param[in] ImageHandle The firmware allocated handle for the EFI image.
405 @param[in] SystemTable A pointer to the EFI System Table.
407 @retval EFI_SUCCESS The entry point is executed successfully.
408 @retval Others Some error occurs when executing this entry point.
414 IN EFI_HANDLE ImageHandle
,
415 IN EFI_SYSTEM_TABLE
*SystemTable
419 EFI_SMM_SW_DISPATCH2_PROTOCOL
*SwDispatch
;
420 EFI_SMM_SW_REGISTER_CONTEXT SwContext
;
423 Status
= PublishAcpiTable ();
424 ASSERT_EFI_ERROR (Status
);
427 // Get the Sw dispatch protocol and register SMI callback functions.
429 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid
, NULL
, (VOID
**)&SwDispatch
);
430 ASSERT_EFI_ERROR (Status
);
431 SwContext
.SwSmiInputValue
= (UINTN
) -1;
432 Status
= SwDispatch
->Register (SwDispatch
, PhysicalPresenceCallback
, &SwContext
, &SwHandle
);
433 ASSERT_EFI_ERROR (Status
);
434 if (EFI_ERROR (Status
)) {
437 mTcgNvs
->PhysicalPresence
.SoftwareSmi
= (UINT8
) SwContext
.SwSmiInputValue
;
439 SwContext
.SwSmiInputValue
= (UINTN
) -1;
440 Status
= SwDispatch
->Register (SwDispatch
, MemoryClearCallback
, &SwContext
, &SwHandle
);
441 ASSERT_EFI_ERROR (Status
);
442 if (EFI_ERROR (Status
)) {
445 mTcgNvs
->MemoryClear
.SoftwareSmi
= (UINT8
) SwContext
.SwSmiInputValue
;
448 // Locate SmmVariableProtocol.
450 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**)&mSmmVariable
);
451 ASSERT_EFI_ERROR (Status
);