]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeTcgPhysicalPresenceLib/DxeTcgPhysicalPresenceLib.c
SecurityPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / Library / DxeTcgPhysicalPresenceLib / DxeTcgPhysicalPresenceLib.c
index 2130b2b43c706fcb9d815f71d6713abad1b0e06f..174172d5d7a55ea93c5d2e54f59405b823f64507 100644 (file)
@@ -8,20 +8,15 @@
 \r
   ExecutePendingTpmRequest() will receive untrusted input and do validation.\r
 \r
-Copyright (c) 2006 - 2012, 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
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include <PiDxe.h>\r
 \r
 #include <Protocol/TcgService.h>\r
+#include <Protocol/VariableLock.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/UefiRuntimeServicesTableLib.h>\r
@@ -33,9 +28,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/HiiLib.h>\r
 #include <Guid/EventGroup.h>\r
 #include <Guid/PhysicalPresenceData.h>\r
+#include <Library/TcgPpVendorLib.h>\r
 \r
-#define TPM_PP_USER_ABORT           ((TPM_RESULT)(-0x10))\r
-#define TPM_PP_BIOS_FAILURE         ((TPM_RESULT)(-0x0f))\r
 #define CONFIRM_BUFFER_SIZE         4096\r
 \r
 EFI_HII_HANDLE mPpStringPackHandle;\r
@@ -60,10 +54,10 @@ PhysicalPresenceGetStringById (
 /**\r
   Get TPM physical presence permanent flags.\r
 \r
-  @param[in]  TcgProtocol   EFI TCG Protocol instance.  \r
-  @param[out] LifetimeLock  physicalPresenceLifetimeLock permanent flag.  \r
+  @param[in]  TcgProtocol   EFI TCG Protocol instance.\r
+  @param[out] LifetimeLock  physicalPresenceLifetimeLock permanent flag.\r
   @param[out] CmdEnable     physicalPresenceCMDEnable permanent flag.\r
-  \r
+\r
   @retval EFI_SUCCESS       Flags were returns successfully.\r
   @retval other             Failed to locate EFI TCG Protocol.\r
 \r
@@ -82,13 +76,13 @@ GetTpmCapability (
   UINT8                             SendBuffer[sizeof (*TpmRqu) + sizeof (UINT32) * 3];\r
   TPM_PERMANENT_FLAGS               *TpmPermanentFlags;\r
   UINT8                             RecvBuffer[40];\r
-  \r
+\r
   //\r
   // Fill request header\r
   //\r
   TpmRsp = (TPM_RSP_COMMAND_HDR*)RecvBuffer;\r
   TpmRqu = (TPM_RQU_COMMAND_HDR*)SendBuffer;\r
-  \r
+\r
   TpmRqu->tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
   TpmRqu->paramSize = SwapBytes32 (sizeof (SendBuffer));\r
   TpmRqu->ordinal   = SwapBytes32 (TPM_ORD_GetCapability);\r
@@ -99,8 +93,8 @@ GetTpmCapability (
   SendBufPtr      = (UINT32*)(TpmRqu + 1);\r
   WriteUnaligned32 (SendBufPtr++, SwapBytes32 (TPM_CAP_FLAG));\r
   WriteUnaligned32 (SendBufPtr++, SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT)));\r
-  WriteUnaligned32 (SendBufPtr, SwapBytes32 (TPM_CAP_FLAG_PERMANENT));  \r
-  \r
+  WriteUnaligned32 (SendBufPtr, SwapBytes32 (TPM_CAP_FLAG_PERMANENT));\r
+\r
   Status = TcgProtocol->PassThroughToTpm (\r
                           TcgProtocol,\r
                           sizeof (SendBuffer),\r
@@ -111,9 +105,9 @@ GetTpmCapability (
   ASSERT_EFI_ERROR (Status);\r
   ASSERT (TpmRsp->tag == SwapBytes16 (TPM_TAG_RSP_COMMAND));\r
   ASSERT (TpmRsp->returnCode == 0);\r
-  \r
+\r
   TpmPermanentFlags = (TPM_PERMANENT_FLAGS *)&RecvBuffer[sizeof (TPM_RSP_COMMAND_HDR) + sizeof (UINT32)];\r
-  \r
+\r
   if (LifetimeLock != NULL) {\r
     *LifetimeLock = TpmPermanentFlags->physicalPresenceLifetimeLock;\r
   }\r
@@ -128,9 +122,9 @@ GetTpmCapability (
 /**\r
   Issue TSC_PhysicalPresence command to TPM.\r
 \r
-  @param[in] TcgProtocol          EFI TCG Protocol instance.  \r
-  @param[in] PhysicalPresence     The state to set the TPM's Physical Presence flags.  \r
-  \r
+  @param[in] TcgProtocol          EFI TCG Protocol instance.\r
+  @param[in] PhysicalPresence     The state to set the TPM's Physical Presence flags.\r
+\r
   @retval EFI_SUCCESS             TPM executed the command successfully.\r
   @retval EFI_SECURITY_VIOLATION  TPM returned error when executing the command.\r
   @retval other                   Failed to locate EFI TCG Protocol.\r
@@ -154,7 +148,7 @@ TpmPhysicalPresence (
   TpmRqu->tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
   TpmRqu->paramSize = SwapBytes32 (sizeof (Buffer));\r
   TpmRqu->ordinal   = SwapBytes32 (TSC_ORD_PhysicalPresence);\r
-  WriteUnaligned16 (TpmPp, (TPM_PHYSICAL_PRESENCE) SwapBytes16 (PhysicalPresence));  \r
+  WriteUnaligned16 (TpmPp, (TPM_PHYSICAL_PRESENCE) SwapBytes16 (PhysicalPresence));\r
 \r
   Status = TcgProtocol->PassThroughToTpm (\r
                           TcgProtocol,\r
@@ -171,24 +165,24 @@ TpmPhysicalPresence (
     //\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
-  \r
+\r
   return Status;\r
 }\r
 \r
 /**\r
   Issue a TPM command for which no additional output data will be returned.\r
 \r
-  @param[in] TcgProtocol              EFI TCG Protocol instance.  \r
-  @param[in] Ordinal                  TPM command code.  \r
-  @param[in] AdditionalParameterSize  Additional parameter size.  \r
-  @param[in] AdditionalParameters     Pointer to the Additional paramaters.  \r
-  \r
-  @retval TPM_PP_BIOS_FAILURE         Error occurred during sending command to TPM or \r
-                                      receiving response from TPM.\r
-  @retval Others                      Return code from the TPM device after command execution.\r
+  @param[in] TcgProtocol              EFI TCG Protocol instance.\r
+  @param[in] Ordinal                  TPM command code.\r
+  @param[in] AdditionalParameterSize  Additional parameter size.\r
+  @param[in] AdditionalParameters     Pointer to the Additional paramaters.\r
+\r
+  @retval TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE  Error occurred during sending command to TPM or\r
+                                                  receiving response from TPM.\r
+  @retval Others                                  Return code from the TPM device after command execution.\r
 \r
 **/\r
-TPM_RESULT\r
+UINT32\r
 TpmCommandNoReturnData (\r
   IN      EFI_TCG_PROTOCOL          *TcgProtocol,\r
   IN      TPM_COMMAND_CODE          Ordinal,\r
@@ -203,7 +197,7 @@ TpmCommandNoReturnData (
 \r
   TpmRqu = (TPM_RQU_COMMAND_HDR*) AllocatePool (sizeof (*TpmRqu) + AdditionalParameterSize);\r
   if (TpmRqu == NULL) {\r
-    return TPM_PP_BIOS_FAILURE;\r
+    return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;\r
   }\r
 \r
   TpmRqu->tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
@@ -221,7 +215,7 @@ TpmCommandNoReturnData (
                           );\r
   FreePool (TpmRqu);\r
   if (EFI_ERROR (Status) || (TpmRsp.tag != SwapBytes16 (TPM_TAG_RSP_COMMAND))) {\r
-    return TPM_PP_BIOS_FAILURE;\r
+    return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;\r
   }\r
   return SwapBytes32 (TpmRsp.returnCode);\r
 }\r
@@ -232,22 +226,22 @@ TpmCommandNoReturnData (
   @param[in]      TcgProtocol         EFI TCG Protocol instance.\r
   @param[in]      CommandCode         Physical presence operation value.\r
   @param[in, out] PpiFlags            The physical presence interface flags.\r
-  \r
-  @retval TPM_PP_BIOS_FAILURE         Unknown physical presence operation.\r
-  @retval TPM_PP_BIOS_FAILURE         Error occurred during sending command to TPM or \r
-                                      receiving response from TPM.\r
-  @retval Others                      Return code from the TPM device after command execution.\r
+\r
+  @retval TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE  Unknown physical presence operation.\r
+  @retval TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE  Error occurred during sending command to TPM or\r
+                                                  receiving response from TPM.\r
+  @retval Others                                  Return code from the TPM device after command execution.\r
 \r
 **/\r
-TPM_RESULT\r
+UINT32\r
 ExecutePhysicalPresence (\r
-  IN      EFI_TCG_PROTOCOL          *TcgProtocol,\r
-  IN      UINT8                     CommandCode,\r
-  IN OUT  UINT8                     *PpiFlags\r
+  IN      EFI_TCG_PROTOCOL            *TcgProtocol,\r
+  IN      UINT32                      CommandCode,\r
+  IN OUT  EFI_PHYSICAL_PRESENCE_FLAGS *PpiFlags\r
   )\r
 {\r
   BOOLEAN                           BoolVal;\r
-  TPM_RESULT                        TpmResponse;\r
+  UINT32                            TpmResponse;\r
   UINT32                            InData[5];\r
 \r
   switch (CommandCode) {\r
@@ -330,12 +324,12 @@ ExecutePhysicalPresence (
       // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE\r
       // PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE will be executed after reboot\r
       //\r
-      if ((*PpiFlags & FLAG_RESET_TRACK) == 0) {\r
+      if ((PpiFlags->PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {\r
         TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);\r
-        *PpiFlags |= FLAG_RESET_TRACK;\r
+        PpiFlags->PPFlags |= TCG_VENDOR_LIB_FLAG_RESET_TRACK;\r
       } else {\r
         TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE, PpiFlags);\r
-        *PpiFlags &= ~FLAG_RESET_TRACK;\r
+        PpiFlags->PPFlags &= ~TCG_VENDOR_LIB_FLAG_RESET_TRACK;\r
       }\r
       return TpmResponse;\r
 \r
@@ -365,7 +359,7 @@ ExecutePhysicalPresence (
       // This command requires UI to prompt user for Auth data\r
       // Here it is NOT implemented\r
       //\r
-      return TPM_PP_BIOS_FAILURE;\r
+      return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;\r
 \r
     case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:\r
       TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_CLEAR, PpiFlags);\r
@@ -375,40 +369,40 @@ ExecutePhysicalPresence (
       return TpmResponse;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE:\r
-      *PpiFlags &= ~FLAG_NO_PPI_PROVISION;\r
+      PpiFlags->PPFlags &= ~TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;\r
       return 0;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_TRUE:\r
-      *PpiFlags |= FLAG_NO_PPI_PROVISION;\r
+      PpiFlags->PPFlags |= TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;\r
       return 0;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:\r
-      *PpiFlags &= ~FLAG_NO_PPI_CLEAR;\r
+      PpiFlags->PPFlags &= ~TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR;\r
       return 0;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE:\r
-      *PpiFlags |= FLAG_NO_PPI_CLEAR;\r
+      PpiFlags->PPFlags |= TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR;\r
       return 0;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE:\r
-      *PpiFlags &= ~FLAG_NO_PPI_MAINTENANCE;\r
+      PpiFlags->PPFlags &= ~TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE;\r
       return 0;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_TRUE:\r
-      *PpiFlags |= FLAG_NO_PPI_MAINTENANCE;\r
+      PpiFlags->PPFlags |= TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE;\r
       return 0;\r
-  \r
+\r
     case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:\r
       //\r
       // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_CLEAR\r
       // PHYSICAL_PRESENCE_CLEAR will be executed after reboot.\r
       //\r
-      if ((*PpiFlags & FLAG_RESET_TRACK) == 0) {\r
+      if ((PpiFlags->PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {\r
         TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);\r
-        *PpiFlags |= FLAG_RESET_TRACK;\r
+        PpiFlags->PPFlags |= TCG_VENDOR_LIB_FLAG_RESET_TRACK;\r
       } else {\r
         TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_CLEAR, PpiFlags);\r
-        *PpiFlags &= ~FLAG_RESET_TRACK;\r
+        PpiFlags->PPFlags &= ~TCG_VENDOR_LIB_FLAG_RESET_TRACK;\r
       }\r
       return TpmResponse;\r
 \r
@@ -417,19 +411,19 @@ ExecutePhysicalPresence (
       // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE\r
       // PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE will be executed after reboot.\r
       //\r
-      if ((*PpiFlags & FLAG_RESET_TRACK) == 0) {\r
+      if ((PpiFlags->PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {\r
         TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);\r
-        *PpiFlags |= FLAG_RESET_TRACK;\r
+        PpiFlags->PPFlags |= TCG_VENDOR_LIB_FLAG_RESET_TRACK;\r
       } else {\r
         TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE, PpiFlags);\r
-        *PpiFlags &= ~FLAG_RESET_TRACK;\r
-      } \r
+        PpiFlags->PPFlags &= ~TCG_VENDOR_LIB_FLAG_RESET_TRACK;\r
+      }\r
       return TpmResponse;\r
 \r
     default:\r
       ;\r
   }\r
-  return TPM_PP_BIOS_FAILURE;\r
+  return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;\r
 }\r
 \r
 \r
@@ -453,7 +447,7 @@ ReadUserKey (
   UINT16                            InputKey;\r
   UINTN                             Index;\r
 \r
-  InputKey = 0; \r
+  InputKey = 0;\r
   do {\r
     Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
     if (Status == EFI_NOT_READY) {\r
@@ -479,18 +473,18 @@ ReadUserKey (
   if (InputKey != SCAN_ESC) {\r
     return TRUE;\r
   }\r
-  \r
+\r
   return FALSE;\r
 }\r
 \r
 /**\r
   The constructor function register UNI strings into imageHandle.\r
-  \r
-  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
+\r
+  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
 \r
   @param  ImageHandle   The firmware allocated handle for the EFI image.\r
   @param  SystemTable   A pointer to the EFI System Table.\r
-  \r
+\r
   @retval EFI_SUCCESS   The constructor successfully added string package.\r
   @retval Other value   The constructor can't add string package.\r
 \r
@@ -518,17 +512,17 @@ TcgPhysicalPresenceLibConstructor (
 **/\r
 BOOLEAN\r
 UserConfirm (\r
-  IN      UINT                    TpmPpCommand\r
+  IN      UINT32                    TpmPpCommand\r
   )\r
 {\r
   CHAR16                            *ConfirmText;\r
   CHAR16                            *TmpStr1;\r
-  CHAR16                            *TmpStr2; \r
+  CHAR16                            *TmpStr2;\r
   UINTN                             BufSize;\r
   BOOLEAN                           CautionKey;\r
   UINT16                            Index;\r
   CHAR16                            DstStr[81];\r
-    \r
+\r
   TmpStr2     = NULL;\r
   CautionKey  = FALSE;\r
   BufSize     = CONFIRM_BUFFER_SIZE;\r
@@ -538,41 +532,41 @@ UserConfirm (
   switch (TpmPpCommand) {\r
     case PHYSICAL_PRESENCE_ENABLE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ENABLE));\r
-      \r
+\r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_DISABLE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DISABLE));\r
-      \r
+\r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
-      \r
+\r
     case PHYSICAL_PRESENCE_ACTIVATE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACTIVATE));\r
-      \r
+\r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -584,12 +578,12 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
-      FreePool (TmpStr1); \r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
+      FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_CLEAR:\r
@@ -601,12 +595,12 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
-      StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
-      FreePool (TmpStr1);      \r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), L" \n\n", (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
+      FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -618,55 +612,55 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DEACTIVATE_DISABLE));\r
-      \r
-      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));      \r
+\r
+      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
-      \r
+\r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ALLOW_TAKE_OWNERSHIP));\r
-      \r
-      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));      \r
+\r
+      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DISALLOW_TAKE_OWNERSHIP));\r
-      \r
-      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));      \r
+\r
+      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -678,48 +672,48 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE:\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_TURN_OFF));\r
-      \r
-      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));      \r
+\r
+      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
-      \r
+\r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
       CautionKey = TRUE;\r
       TmpStr2 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_UNOWNED_FIELD_UPGRADE));\r
-      \r
-      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_UPGRADE_HEAD_STR));      \r
+\r
+      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_UPGRADE_HEAD_STR));\r
       UnicodeSPrint (ConfirmText, BufSize, TmpStr1, TmpStr2);\r
       FreePool (TmpStr1);\r
-      \r
+\r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -740,19 +734,19 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR_CONT));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -764,11 +758,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -781,20 +775,20 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
-      StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
-      FreePool (TmpStr1); \r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), L" \n\n", (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
+      FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -807,15 +801,15 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -828,12 +822,12 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
-      StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), L" \n\n", (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -846,19 +840,19 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR_CONT));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCatS (ConfirmText, BufSize / sizeof (CHAR16), TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -877,10 +871,10 @@ UserConfirm (
 \r
   DstStr[80] = L'\0';\r
   for (Index = 0; Index < StrLen (ConfirmText); Index += 80) {\r
-    StrnCpy(DstStr, ConfirmText + Index, 80);    \r
-    Print (DstStr);    \r
+    StrnCpyS(DstStr, sizeof (DstStr) / sizeof (CHAR16), ConfirmText + Index, sizeof (DstStr) / sizeof (CHAR16) - 1);\r
+    Print (DstStr);\r
   }\r
-  \r
+\r
   FreePool (TmpStr1);\r
   FreePool (TmpStr2);\r
   FreePool (ConfirmText);\r
@@ -889,18 +883,19 @@ UserConfirm (
     return TRUE;\r
   }\r
 \r
-  return FALSE;  \r
+  return FALSE;\r
 }\r
 \r
 /**\r
-  Check if there is a valid physical presence command request. Also updates parameter value \r
+  Check if there is a valid physical presence command request. Also updates parameter value\r
   to whether the requested physical presence command already confirmed by user\r
\r
-   @param[in]  TcgProtocol                 EFI TCG Protocol instance. \r
-   @param[out] RequestConfirmed            If the physical presence operation command required user confirm from UI.\r
-                                             True, it indicates the command doesn't require user confirm, or already confirmed \r
-                                                   in last boot cycle by user.\r
-                                             False, it indicates the command need user confirm from UI.\r
+\r
+   @param[in]  TcgPpData           EFI TCG Physical Presence request data.\r
+   @param[in]  Flags               The physical presence interface flags.\r
+   @param[out] RequestConfirmed    If the physical presence operation command required user confirm from UI.\r
+                                   True, it indicates the command doesn't require user confirm, or already confirmed\r
+                                   in last boot cycle by user.\r
+                                   False, it indicates the command need user confirm from UI.\r
 \r
    @retval  TRUE        Physical Presence operation command is valid.\r
    @retval  FALSE       Physical Presence operation command is invalid.\r
@@ -908,13 +903,13 @@ UserConfirm (
 **/\r
 BOOLEAN\r
 HaveValidTpmRequest  (\r
-  IN      EFI_PHYSICAL_PRESENCE     *TcgPpData,\r
-  OUT     BOOLEAN                   *RequestConfirmed\r
+  IN      EFI_PHYSICAL_PRESENCE       *TcgPpData,\r
+  IN      EFI_PHYSICAL_PRESENCE_FLAGS Flags,\r
+  OUT     BOOLEAN                     *RequestConfirmed\r
   )\r
 {\r
-  UINT8                             Flags;\r
-  \r
-  Flags = TcgPpData->Flags;\r
+  BOOLEAN  IsRequestValid;\r
+\r
   *RequestConfirmed = FALSE;\r
 \r
   switch (TcgPpData->PPRequest) {\r
@@ -932,27 +927,27 @@ HaveValidTpmRequest  (
     case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE:\r
     case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE:\r
     case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH:\r
-      if ((Flags & FLAG_NO_PPI_PROVISION) != 0) {\r
+      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION) != 0) {\r
         *RequestConfirmed = TRUE;\r
       }\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_CLEAR:\r
     case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:\r
-      if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {\r
+      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR) != 0) {\r
         *RequestConfirmed = TRUE;\r
       }\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
-      if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {\r
+      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE) != 0) {\r
         *RequestConfirmed = TRUE;\r
       }\r
       break;\r
 \r
     case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:\r
     case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:\r
-      if ((Flags & FLAG_NO_PPI_CLEAR) != 0 && (Flags & FLAG_NO_PPI_PROVISION) != 0) {\r
+      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR) != 0 && (Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION) != 0) {\r
         *RequestConfirmed = TRUE;\r
       }\r
       break;\r
@@ -969,13 +964,22 @@ HaveValidTpmRequest  (
       break;\r
 \r
     default:\r
-      //\r
-      // Wrong Physical Presence command\r
-      //\r
-      return FALSE;\r
+      if (TcgPpData->PPRequest >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {\r
+        IsRequestValid = TcgPpVendorLibHasValidRequest (TcgPpData->PPRequest, Flags.PPFlags, RequestConfirmed);\r
+        if (!IsRequestValid) {\r
+          return FALSE;\r
+        } else {\r
+          break;\r
+        }\r
+      } else {\r
+        //\r
+        // Wrong Physical Presence command\r
+        //\r
+        return FALSE;\r
+      }\r
   }\r
 \r
-  if ((Flags & FLAG_RESET_TRACK) != 0) {\r
+  if ((Flags.PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) != 0) {\r
     //\r
     // It had been confirmed in last boot, it doesn't need confirm again.\r
     //\r
@@ -996,32 +1000,30 @@ HaveValidTpmRequest  (
   TcgPpData variable is external input, so this function will validate\r
   its data structure to be valid value.\r
 \r
-  @param[in] TcgProtocol          EFI TCG Protocol instance. \r
+  @param[in] TcgProtocol          EFI TCG Protocol instance.\r
   @param[in] TcgPpData            Point to the physical presence NV variable.\r
+  @param[in] Flags                The physical presence interface flags.\r
 \r
 **/\r
 VOID\r
 ExecutePendingTpmRequest (\r
-  IN      EFI_TCG_PROTOCOL          *TcgProtocol,\r
-  IN      EFI_PHYSICAL_PRESENCE     *TcgPpData\r
+  IN      EFI_TCG_PROTOCOL            *TcgProtocol,\r
+  IN      EFI_PHYSICAL_PRESENCE       *TcgPpData,\r
+  IN      EFI_PHYSICAL_PRESENCE_FLAGS Flags\r
   )\r
 {\r
   EFI_STATUS                        Status;\r
   UINTN                             DataSize;\r
   BOOLEAN                           RequestConfirmed;\r
+  EFI_PHYSICAL_PRESENCE_FLAGS       NewFlags;\r
+  BOOLEAN                           ResetRequired;\r
+  UINT32                            NewPPFlags;\r
 \r
-  if (TcgPpData->PPRequest == PHYSICAL_PRESENCE_NO_ACTION) {\r
-    //\r
-    // No operation request\r
-    //\r
-    return;\r
-  }\r
-\r
-  if (!HaveValidTpmRequest(TcgPpData, &RequestConfirmed)) {\r
+  if (!HaveValidTpmRequest(TcgPpData, Flags, &RequestConfirmed)) {\r
     //\r
     // Invalid operation request.\r
     //\r
-    TcgPpData->PPResponse = TPM_PP_BIOS_FAILURE;\r
+    TcgPpData->PPResponse = TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;\r
     TcgPpData->LastPPRequest = TcgPpData->PPRequest;\r
     TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;\r
     DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
@@ -1035,27 +1037,52 @@ ExecutePendingTpmRequest (
     return;\r
   }\r
 \r
-  if (!RequestConfirmed) {\r
+  ResetRequired = FALSE;\r
+  if (TcgPpData->PPRequest >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {\r
+    NewFlags = Flags;\r
+    NewPPFlags = NewFlags.PPFlags;\r
+    TcgPpData->PPResponse = TcgPpVendorLibExecutePendingRequest (TcgPpData->PPRequest, &NewPPFlags, &ResetRequired);\r
+    NewFlags.PPFlags = (UINT8)NewPPFlags;\r
+  } else {\r
+    if (!RequestConfirmed) {\r
+      //\r
+      // Print confirm text and wait for approval.\r
+      //\r
+      RequestConfirmed = UserConfirm (TcgPpData->PPRequest);\r
+    }\r
+\r
     //\r
-    // Print confirm text and wait for approval. \r
+    // Execute requested physical presence command\r
     //\r
-    RequestConfirmed = UserConfirm (TcgPpData->PPRequest);\r
+    TcgPpData->PPResponse = TCG_PP_OPERATION_RESPONSE_USER_ABORT;\r
+    NewFlags = Flags;\r
+    if (RequestConfirmed) {\r
+      TcgPpData->PPResponse = ExecutePhysicalPresence (TcgProtocol, TcgPpData->PPRequest, &NewFlags);\r
+    }\r
   }\r
 \r
   //\r
-  // Execute requested physical presence command\r
+  // Save the flags if it is updated.\r
   //\r
-  TcgPpData->PPResponse = TPM_PP_USER_ABORT;\r
-  if (RequestConfirmed) {\r
-    TcgPpData->PPResponse = ExecutePhysicalPresence (TcgProtocol, TcgPpData->PPRequest, &TcgPpData->Flags);\r
+  if (CompareMem (&Flags, &NewFlags, sizeof(EFI_PHYSICAL_PRESENCE_FLAGS)) != 0) {\r
+    Status   = gRT->SetVariable (\r
+                      PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                      &gEfiPhysicalPresenceGuid,\r
+                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                      sizeof (EFI_PHYSICAL_PRESENCE_FLAGS),\r
+                      &NewFlags\r
+                      );\r
+    if (EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
   }\r
 \r
   //\r
   // Clear request\r
   //\r
-  if ((TcgPpData->Flags & FLAG_RESET_TRACK) == 0) {\r
+  if ((NewFlags.PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {\r
     TcgPpData->LastPPRequest = TcgPpData->PPRequest;\r
-    TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;    \r
+    TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;\r
   }\r
 \r
   //\r
@@ -1073,7 +1100,7 @@ ExecutePendingTpmRequest (
     return;\r
   }\r
 \r
-  if (TcgPpData->PPResponse == TPM_PP_USER_ABORT) {\r
+  if (TcgPpData->PPResponse == TCG_PP_OPERATION_RESPONSE_USER_ABORT) {\r
     return;\r
   }\r
 \r
@@ -1091,9 +1118,16 @@ ExecutePendingTpmRequest (
     case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:\r
     case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:\r
     case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:\r
-    case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:      \r
+    case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:\r
       break;\r
     default:\r
+      if (TcgPpData->LastPPRequest >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {\r
+        if (ResetRequired) {\r
+          break;\r
+        } else {\r
+          return ;\r
+        }\r
+      }\r
       if (TcgPpData->PPRequest != PHYSICAL_PRESENCE_NO_ACTION) {\r
         break;\r
       }\r
@@ -1102,21 +1136,21 @@ ExecutePendingTpmRequest (
 \r
   Print (L"Rebooting system to make TPM settings in effect\n");\r
   gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
-  ASSERT (FALSE);  \r
+  ASSERT (FALSE);\r
 }\r
 \r
 /**\r
   Check and execute the pending TPM request and Lock TPM.\r
 \r
-  The TPM request may come from OS or BIOS. This API will display request information and wait \r
+  The TPM request may come from OS or BIOS. This API will display request information and wait\r
   for user confirmation if TPM request exists. The TPM request will be sent to TPM device after\r
-  the TPM request is confirmed, and one or more reset may be required to make TPM request to \r
+  the TPM request is confirmed, and one or more reset may be required to make TPM request to\r
   take effect. At last, it will lock TPM to prevent TPM state change by malware.\r
-  \r
+\r
   This API should be invoked after console in and console out are all ready as they are required\r
-  to display request information and get user input to confirm the request. This API should also \r
+  to display request information and get user input to confirm the request. This API should also\r
   be invoked as early as possible as TPM is locked in this function.\r
-  \r
+\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1130,12 +1164,58 @@ TcgPhysicalPresenceLibProcessRequest (
   UINTN                             DataSize;\r
   EFI_PHYSICAL_PRESENCE             TcgPpData;\r
   EFI_TCG_PROTOCOL                  *TcgProtocol;\r
-  \r
+  EDKII_VARIABLE_LOCK_PROTOCOL      *VariableLockProtocol;\r
+  EFI_PHYSICAL_PRESENCE_FLAGS       PpiFlags;\r
+\r
   Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);\r
   if (EFI_ERROR (Status)) {\r
     return ;\r
   }\r
-  \r
+\r
+  //\r
+  // Initialize physical presence flags.\r
+  //\r
+  DataSize = sizeof (EFI_PHYSICAL_PRESENCE_FLAGS);\r
+  Status = gRT->GetVariable (\r
+                  PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                  &gEfiPhysicalPresenceGuid,\r
+                  NULL,\r
+                  &DataSize,\r
+                  &PpiFlags\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    PpiFlags.PPFlags = TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;\r
+    Status   = gRT->SetVariable (\r
+                      PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                      &gEfiPhysicalPresenceGuid,\r
+                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                      sizeof (EFI_PHYSICAL_PRESENCE_FLAGS),\r
+                      &PpiFlags\r
+                      );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Set physical presence flag failed, Status = %r\n", Status));\r
+      return ;\r
+    }\r
+  }\r
+  DEBUG ((EFI_D_INFO, "[TPM] PpiFlags = %x\n", PpiFlags.PPFlags));\r
+\r
+  //\r
+  // This flags variable controls whether physical presence is required for TPM command.\r
+  // It should be protected from malicious software. We set it as read-only variable here.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **)&VariableLockProtocol);\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = VariableLockProtocol->RequestToLock (\r
+                                     VariableLockProtocol,\r
+                                     PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                                     &gEfiPhysicalPresenceGuid\r
+                                     );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Error when lock variable %s, Status = %r\n", PHYSICAL_PRESENCE_FLAGS_VARIABLE, Status));\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  }\r
+\r
   //\r
   // Initialize physical presence variable.\r
   //\r
@@ -1148,28 +1228,35 @@ TcgPhysicalPresenceLibProcessRequest (
                   &TcgPpData\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    if (Status == EFI_NOT_FOUND) {\r
-      ZeroMem ((VOID*)&TcgPpData, sizeof (TcgPpData));\r
-      TcgPpData.Flags |= FLAG_NO_PPI_PROVISION;\r
-      DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
-      Status   = gRT->SetVariable (\r
-                        PHYSICAL_PRESENCE_VARIABLE,\r
-                        &gEfiPhysicalPresenceGuid,\r
-                        EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
-                        DataSize,\r
-                        &TcgPpData\r
-                        );\r
+    ZeroMem ((VOID*)&TcgPpData, sizeof (TcgPpData));\r
+    DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
+    Status   = gRT->SetVariable (\r
+                      PHYSICAL_PRESENCE_VARIABLE,\r
+                      &gEfiPhysicalPresenceGuid,\r
+                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                      DataSize,\r
+                      &TcgPpData\r
+                      );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "[TPM] Set physical presence variable failed, Status = %r\n", Status));\r
+      return;\r
     }\r
-    ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
-  DEBUG ((EFI_D_INFO, "[TPM] Flags=%x, PPRequest=%x\n", TcgPpData.Flags, TcgPpData.PPRequest));\r
+  DEBUG ((EFI_D_INFO, "[TPM] Flags=%x, PPRequest=%x\n", PpiFlags.PPFlags, TcgPpData.PPRequest));\r
+\r
+  if (TcgPpData.PPRequest == PHYSICAL_PRESENCE_NO_ACTION) {\r
+    //\r
+    // No operation request\r
+    //\r
+    return;\r
+  }\r
 \r
   Status = GetTpmCapability (TcgProtocol, &LifetimeLock, &CmdEnable);\r
   if (EFI_ERROR (Status)) {\r
     return ;\r
   }\r
-  \r
+\r
   if (!CmdEnable) {\r
     if (LifetimeLock) {\r
       //\r
@@ -1182,7 +1269,7 @@ TcgPhysicalPresenceLibProcessRequest (
       return ;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Set operator physical presence flags\r
   //\r
@@ -1190,8 +1277,8 @@ TcgPhysicalPresenceLibProcessRequest (
 \r
   //\r
   // Execute pending TPM request.\r
-  //  \r
-  ExecutePendingTpmRequest (TcgProtocol, &TcgPpData);\r
+  //\r
+  ExecutePendingTpmRequest (TcgProtocol, &TcgPpData, PpiFlags);\r
   DEBUG ((EFI_D_INFO, "[TPM] PPResponse = %x\n", TcgPpData.PPResponse));\r
 \r
   //\r
@@ -1205,7 +1292,7 @@ TcgPhysicalPresenceLibProcessRequest (
 \r
   The TPM request may come from OS. This API will check if TPM request exists and need user\r
   input to confirmation.\r
-  \r
+\r
   @retval    TRUE        TPM needs input to confirm user physical presence.\r
   @retval    FALSE       TPM doesn't need input to confirm user physical presence.\r
 \r
@@ -1216,13 +1303,14 @@ TcgPhysicalPresenceLibNeedUserConfirm(
   VOID\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
-  EFI_PHYSICAL_PRESENCE   TcgPpData;\r
-  UINTN                   DataSize;\r
-  BOOLEAN                 RequestConfirmed;\r
-  BOOLEAN                 LifetimeLock;\r
-  BOOLEAN                 CmdEnable;\r
-  EFI_TCG_PROTOCOL        *TcgProtocol;\r
+  EFI_STATUS                   Status;\r
+  EFI_PHYSICAL_PRESENCE        TcgPpData;\r
+  UINTN                        DataSize;\r
+  BOOLEAN                      RequestConfirmed;\r
+  BOOLEAN                      LifetimeLock;\r
+  BOOLEAN                      CmdEnable;\r
+  EFI_TCG_PROTOCOL             *TcgProtocol;\r
+  EFI_PHYSICAL_PRESENCE_FLAGS  PpiFlags;\r
 \r
   Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);\r
   if (EFI_ERROR (Status)) {\r
@@ -1244,6 +1332,18 @@ TcgPhysicalPresenceLibNeedUserConfirm(
     return FALSE;\r
   }\r
 \r
+  DataSize = sizeof (EFI_PHYSICAL_PRESENCE_FLAGS);\r
+  Status = gRT->GetVariable (\r
+                  PHYSICAL_PRESENCE_FLAGS_VARIABLE,\r
+                  &gEfiPhysicalPresenceGuid,\r
+                  NULL,\r
+                  &DataSize,\r
+                  &PpiFlags\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+\r
   if (TcgPpData.PPRequest == PHYSICAL_PRESENCE_NO_ACTION) {\r
     //\r
     // No operation request\r
@@ -1251,7 +1351,7 @@ TcgPhysicalPresenceLibNeedUserConfirm(
     return FALSE;\r
   }\r
 \r
-  if (!HaveValidTpmRequest(&TcgPpData, &RequestConfirmed)) {\r
+  if (!HaveValidTpmRequest(&TcgPpData, PpiFlags, &RequestConfirmed)) {\r
     //\r
     // Invalid operation request.\r
     //\r