]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TcgSmm/TcgSmm.c
Add security package to repository.
[mirror_edk2.git] / SecurityPkg / Tcg / TcgSmm / TcgSmm.c
diff --git a/SecurityPkg/Tcg/TcgSmm/TcgSmm.c b/SecurityPkg/Tcg/TcgSmm/TcgSmm.c
new file mode 100644 (file)
index 0000000..9116944
--- /dev/null
@@ -0,0 +1,455 @@
+/** @file\r
+  It updates TPM items in ACPI table and registers SMI callback\r
+  functions for physical presence and ClearMemory.\r
+\r
+Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials \r
+are licensed and made available under the terms and conditions of the BSD License \r
+which accompanies this distribution.  The full text of the license may be found at \r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+\r
+#include <PiDxe.h>\r
+#include <IndustryStandard/Acpi.h>\r
+#include <Guid/PhysicalPresenceData.h>\r
+#include <Guid/MemoryOverwriteControl.h>\r
+#include <Protocol/SmmSwDispatch2.h>\r
+#include <Protocol/AcpiTable.h>\r
+#include <Protocol/SmmVariable.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/SmmServicesTableLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DxeServicesLib.h>\r
+\r
+//\r
+// AML parsing definitions\r
+//\r
+#define AML_OPREGION_OP     0x80\r
+#define AML_BYTE_PREFIX     0x0A\r
+#define AML_DWORD_PREFIX    0x0C\r
+\r
+#pragma pack(1)\r
+typedef struct {\r
+  UINT8                  SoftwareSmi;\r
+  UINT32                 Parameter;\r
+  UINT32                 Response;\r
+  UINT32                 Request;\r
+  UINT32                 LastRequest;\r
+  UINT32                 ReturnCode;\r
+} PHYSICAL_PRESENCE_NVS;\r
+\r
+typedef struct {\r
+  UINT8                  SoftwareSmi;\r
+  UINT32                 Parameter;\r
+  UINT32                 Request;\r
+} MEMORY_CLEAR_NVS;\r
+\r
+typedef struct {\r
+  PHYSICAL_PRESENCE_NVS  PhysicalPresence;\r
+  MEMORY_CLEAR_NVS       MemoryClear;\r
+} TCG_NVS;\r
+\r
+typedef struct {\r
+  UINT8                  OpRegionOp;\r
+  UINT32                 NameString;\r
+  UINT8                  RegionSpace;\r
+  UINT8                  DWordPrefix;\r
+  UINT32                 RegionOffset;\r
+  UINT8                  BytePrefix;\r
+  UINT8                  RegionLen;\r
+} AML_OP_REGION_32_8;\r
+#pragma pack()\r
+\r
+EFI_SMM_VARIABLE_PROTOCOL  *mSmmVariable;\r
+TCG_NVS                    *mTcgNvs;\r
+\r
+/**\r
+  Software SMI callback for TPM physical presence which is called from ACPI method.\r
+\r
+  @param[in]      DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param[in]      Context         Points to an optional handler context which was specified when the\r
+                                  handler was registered.\r
+  @param[in, out] CommBuffer      A pointer to a collection of data in memory that will\r
+                                  be conveyed from a non-SMM environment into an SMM environment.\r
+  @param[in, out] CommBufferSize  The size of the CommBuffer.\r
+\r
+  @retval EFI_SUCCESS             The interrupt was handled successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PhysicalPresenceCallback (\r
+  IN EFI_HANDLE                  DispatchHandle,\r
+  IN CONST VOID                  *Context,\r
+  IN OUT VOID                    *CommBuffer,\r
+  IN OUT UINTN                   *CommBufferSize\r
+  )\r
+{\r
+  EFI_STATUS                     Status;\r
+  UINTN                          DataSize;\r
+  EFI_PHYSICAL_PRESENCE          PpData;\r
+  UINT8                          Flags;\r
+  BOOLEAN                        RequestConfirmed;\r
+\r
+  //\r
+  // Get the Physical Presence variable\r
+  //\r
+  DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
+  Status = mSmmVariable->SmmGetVariable (\r
+                           PHYSICAL_PRESENCE_VARIABLE,\r
+                           &gEfiPhysicalPresenceGuid,\r
+                           NULL,\r
+                           &DataSize,\r
+                           &PpData\r
+                           );\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  DEBUG ((EFI_D_INFO, "[TPM] PP callback, Parameter = %x\n", mTcgNvs->PhysicalPresence.Parameter));\r
+  if (mTcgNvs->PhysicalPresence.Parameter == 5) {\r
+    //\r
+    // Return TPM Operation Response to OS Environment\r
+    //\r
+    mTcgNvs->PhysicalPresence.LastRequest = PpData.LastPPRequest;\r
+    mTcgNvs->PhysicalPresence.Response    = PpData.PPResponse;\r
+\r
+  } else if ((mTcgNvs->PhysicalPresence.Parameter == 2) || (mTcgNvs->PhysicalPresence.Parameter == 7)) {\r
+    //\r
+    // Submit TPM Operation Request to Pre-OS Environment\r
+    //\r
+\r
+    if (mTcgNvs->PhysicalPresence.Request == SET_OPERATOR_AUTH) {\r
+      //\r
+      // This command requires UI to prompt user for Auth data, NOT implemented.\r
+      //\r
+      mTcgNvs->PhysicalPresence.ReturnCode = 1;\r
+      return EFI_SUCCESS;\r
+    }\r
+\r
+    if (PpData.PPRequest != mTcgNvs->PhysicalPresence.Request) {\r
+      PpData.PPRequest = (UINT8) mTcgNvs->PhysicalPresence.Request;\r
+      DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
+      Status = mSmmVariable->SmmSetVariable (\r
+                               PHYSICAL_PRESENCE_VARIABLE,\r
+                               &gEfiPhysicalPresenceGuid,\r
+                               EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                               DataSize,\r
+                               &PpData\r
+                               );\r
+    }\r
+\r
+    if (EFI_ERROR (Status)) { \r
+      //\r
+      // General failure.\r
+      //\r
+      mTcgNvs->PhysicalPresence.ReturnCode = 2;\r
+      return EFI_SUCCESS;\r
+    }\r
+    mTcgNvs->PhysicalPresence.ReturnCode = 0;\r
+  } else if (mTcgNvs->PhysicalPresence.Parameter == 8) {\r
+    // \r
+    // Get User Confirmation Status for Operation\r
+    //\r
+    Flags = PpData.Flags;  \r
+    RequestConfirmed = FALSE;\r
+\r
+    switch (mTcgNvs->PhysicalPresence.Request) {\r
+      case ENABLE:\r
+      case DISABLE:\r
+      case ACTIVATE:\r
+      case DEACTIVATE:\r
+      case ENABLE_ACTIVATE:\r
+      case DEACTIVATE_DISABLE:\r
+      case SET_OWNER_INSTALL_TRUE:\r
+      case SET_OWNER_INSTALL_FALSE:\r
+      case ENABLE_ACTIVATE_OWNER_TRUE:\r
+      case DEACTIVATE_DISABLE_OWNER_FALSE:\r
+        if ((Flags & FLAG_NO_PPI_PROVISION) != 0) {\r
+          RequestConfirmed = TRUE;\r
+        }\r
+        break;\r
+\r
+      case CLEAR:\r
+      case ENABLE_ACTIVATE_CLEAR:\r
+        if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {\r
+          RequestConfirmed = TRUE;\r
+        }\r
+        break;\r
+\r
+      case DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
+        if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {\r
+          RequestConfirmed = TRUE;\r
+        }\r
+        break;\r
+\r
+      case ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:\r
+      case CLEAR_ENABLE_ACTIVATE:\r
+        if ((Flags & FLAG_NO_PPI_CLEAR) != 0 && (Flags & FLAG_NO_PPI_PROVISION) != 0) {\r
+          RequestConfirmed = TRUE;\r
+        }\r
+        break;  \r
+\r
+      case SET_NO_PPI_PROVISION_FALSE:\r
+      case SET_NO_PPI_CLEAR_FALSE:\r
+      case SET_NO_PPI_MAINTENANCE_FALSE:\r
+      case NO_ACTION:\r
+        RequestConfirmed = TRUE;\r
+        break;\r
+\r
+      case SET_OPERATOR_AUTH:\r
+        //\r
+        // This command requires UI to prompt user for Auth data\r
+        // Here it is NOT implemented\r
+        //\r
+        mTcgNvs->PhysicalPresence.ReturnCode = 0; \r
+        return EFI_SUCCESS;\r
+    }\r
+\r
+    if (RequestConfirmed) {\r
+      //\r
+      // Allowed and physically present user not required \r
+      //\r
+      mTcgNvs->PhysicalPresence.ReturnCode = 4;\r
+    } else {\r
+      //\r
+      // Allowed and physically present user required \r
+      //\r
+      mTcgNvs->PhysicalPresence.ReturnCode = 3;\r
+    }    \r
+  } \r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Software SMI callback for MemoryClear which is called from ACPI method.\r
+\r
+  @param[in]      DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param[in]      Context         Points to an optional handler context which was specified when the\r
+                                  handler was registered.\r
+  @param[in, out] CommBuffer      A pointer to a collection of data in memory that will\r
+                                  be conveyed from a non-SMM environment into an SMM environment.\r
+  @param[in, out] CommBufferSize  The size of the CommBuffer.\r
+\r
+  @retval EFI_SUCCESS             The interrupt was handled successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MemoryClearCallback (\r
+  IN EFI_HANDLE                  DispatchHandle,\r
+  IN CONST VOID                  *Context,\r
+  IN OUT VOID                    *CommBuffer,\r
+  IN OUT UINTN                   *CommBufferSize\r
+  )\r
+{\r
+  EFI_STATUS                     Status;\r
+  UINTN                          DataSize;\r
+  UINT8                          MorControl;\r
+\r
+  if (mTcgNvs->MemoryClear.Parameter == 1) {\r
+    //\r
+    // Called from ACPI _DSM method, save the MOR data to variable.\r
+    //\r
+    MorControl = (UINT8) mTcgNvs->MemoryClear.Request;\r
+  } else if (mTcgNvs->MemoryClear.Parameter == 2) {\r
+    //\r
+    // Called from ACPI _PTS method, setup ClearMemory flags if needed.\r
+    //\r
+    DataSize = sizeof (UINT8);\r
+    Status = mSmmVariable->SmmGetVariable (\r
+                             MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+                             &gEfiMemoryOverwriteControlDataGuid,\r
+                             NULL,\r
+                             &DataSize,\r
+                             &MorControl\r
+                             );\r
+    if (EFI_ERROR (Status)) {\r
+      ASSERT (Status == EFI_NOT_FOUND);\r
+      return EFI_SUCCESS;\r
+    }\r
+\r
+    if (MOR_CLEAR_MEMORY_VALUE (MorControl) == 0x0) {\r
+      return EFI_SUCCESS;\r
+    }\r
+    MorControl &= ~MOR_CLEAR_MEMORY_BIT_MASK;\r
+  }\r
+\r
+  DataSize = sizeof (UINT8);\r
+  Status = mSmmVariable->SmmSetVariable (\r
+                           MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+                           &gEfiMemoryOverwriteControlDataGuid,\r
+                           EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                           DataSize,\r
+                           &MorControl\r
+                           );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Find the operation region in TCG ACPI table by given Name and Size,\r
+  and initialize it if the region is found.\r
+\r
+  @param[in, out] Table          The TPM item in ACPI table.\r
+  @param[in]      Name           The name string to find in TPM table.\r
+  @param[in]      Size           The size of the region to find.\r
+\r
+  @return                        The allocated address for the found region.\r
+\r
+**/\r
+VOID *\r
+AssignOpRegion (\r
+  EFI_ACPI_DESCRIPTION_HEADER    *Table,\r
+  UINT32                         Name,\r
+  UINT16                         Size\r
+  )\r
+{\r
+  EFI_STATUS                     Status;\r
+  AML_OP_REGION_32_8             *OpRegion;\r
+  EFI_PHYSICAL_ADDRESS           MemoryAddress;\r
+\r
+  MemoryAddress = SIZE_4GB - 1;\r
+\r
+  //\r
+  // Patch some pointers for the ASL code before loading the SSDT.\r
+  //\r
+  for (OpRegion  = (AML_OP_REGION_32_8 *) (Table + 1);\r
+       OpRegion <= (AML_OP_REGION_32_8 *) ((UINT8 *) Table + Table->Length);\r
+       OpRegion  = (AML_OP_REGION_32_8 *) ((UINT8 *) OpRegion + 1)) {\r
+    if ((OpRegion->OpRegionOp  == AML_OPREGION_OP) && \r
+        (OpRegion->NameString  == Name) &&\r
+        (OpRegion->RegionLen   == Size) &&\r
+        (OpRegion->DWordPrefix == AML_DWORD_PREFIX) &&\r
+        (OpRegion->BytePrefix  == AML_BYTE_PREFIX)) {\r
+\r
+      Status = gBS->AllocatePages(AllocateMaxAddress, EfiACPIMemoryNVS, EFI_SIZE_TO_PAGES (Size), &MemoryAddress);\r
+      ASSERT_EFI_ERROR (Status);\r
+      ZeroMem ((VOID *)(UINTN)MemoryAddress, Size);\r
+      OpRegion->RegionOffset = (UINT32) (UINTN) MemoryAddress;\r
+      break;\r
+    }\r
+  }\r
+\r
+  return (VOID *) (UINTN) MemoryAddress;\r
+}\r
+\r
+/**\r
+  Initialize and publish TPM items in ACPI table.\r
+\r
+  @retval   EFI_SUCCESS     The TCG ACPI table is published successfully.\r
+  @retval   Others          The TCG ACPI table is not published.\r
+\r
+**/\r
+EFI_STATUS\r
+PublishAcpiTable (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                     Status;\r
+  EFI_ACPI_TABLE_PROTOCOL        *AcpiTable;\r
+  UINTN                          TableKey;\r
+  EFI_ACPI_DESCRIPTION_HEADER    *Table;\r
+  UINTN                          TableSize;\r
+\r
+  Status = GetSectionFromFv (\r
+             &gEfiCallerIdGuid,\r
+             EFI_SECTION_RAW,\r
+             0,\r
+             (VOID **) &Table,\r
+             &TableSize\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ASSERT (Table->OemTableId == SIGNATURE_64 ('T', 'c', 'g', 'T', 'a', 'b', 'l', 'e'));\r
+  mTcgNvs = AssignOpRegion (Table, SIGNATURE_32 ('T', 'N', 'V', 'S'), sizeof (TCG_NVS));\r
+  ASSERT (mTcgNvs != NULL);\r
+\r
+  //\r
+  // Publish the TPM ACPI table\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTable);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  TableKey = 0;\r
+  Status = AcpiTable->InstallAcpiTable (\r
+                        AcpiTable,\r
+                        Table,\r
+                        TableSize,\r
+                        &TableKey\r
+                        );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  The driver's entry point.\r
+\r
+  It install callbacks for TPM physical presence and MemoryClear, and locate \r
+  SMM variable to be used in the callback function.\r
+\r
+  @param[in] ImageHandle  The firmware allocated handle for the EFI image.  \r
+  @param[in] SystemTable  A pointer to the EFI System Table.\r
+  \r
+  @retval EFI_SUCCESS     The entry point is executed successfully.\r
+  @retval Others          Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeTcgSmm (\r
+  IN EFI_HANDLE                  ImageHandle,\r
+  IN EFI_SYSTEM_TABLE            *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS                     Status;\r
+  EFI_SMM_SW_DISPATCH2_PROTOCOL  *SwDispatch;\r
+  EFI_SMM_SW_REGISTER_CONTEXT    SwContext;\r
+  EFI_HANDLE                     SwHandle;\r
+\r
+  Status = PublishAcpiTable ();\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Get the Sw dispatch protocol and register SMI callback functions.\r
+  //\r
+  Status = gSmst->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID**)&SwDispatch);\r
+  ASSERT_EFI_ERROR (Status);\r
+  SwContext.SwSmiInputValue = (UINTN) -1;\r
+  Status = SwDispatch->Register (SwDispatch, PhysicalPresenceCallback, &SwContext, &SwHandle);\r
+  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  mTcgNvs->PhysicalPresence.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;\r
+\r
+  SwContext.SwSmiInputValue = (UINTN) -1;\r
+  Status = SwDispatch->Register (SwDispatch, MemoryClearCallback, &SwContext, &SwHandle);\r
+  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  mTcgNvs->MemoryClear.SoftwareSmi = (UINT8) SwContext.SwSmiInputValue;\r
+  \r
+  //\r
+  // Locate SmmVariableProtocol.\r
+  //\r
+  Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mSmmVariable);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r