]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeTcgPhysicalPresenceLib/DxeTcgPhysicalPresenceLib.c
Add TPM Physical Presence >=128 operation value support.
[mirror_edk2.git] / SecurityPkg / Library / DxeTcgPhysicalPresenceLib / DxeTcgPhysicalPresenceLib.c
index 5316e5b785ce3126eb8eca7b2e85ad0c22d69023..7fbeb0aa2b8dd6659389428a3aab991337b2e8ac 100644 (file)
@@ -8,7 +8,7 @@
 \r
   ExecutePendingTpmRequest() will receive untrusted input and do validation.\r
 \r
-Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2015, 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
@@ -22,6 +22,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #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 +34,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
@@ -183,12 +183,12 @@ TpmPhysicalPresence (
   @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
+  @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 +203,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 +221,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
@@ -233,21 +233,21 @@ TpmCommandNoReturnData (
   @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
+  @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 +330,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 +365,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,27 +375,27 @@ 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
     case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:\r
@@ -403,12 +403,12 @@ ExecutePhysicalPresence (
       // 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 +417,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
+        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
@@ -440,7 +440,7 @@ ExecutePhysicalPresence (
                           If false, F10 is used as confirm key.\r
 \r
   @retval     TRUE        User confirmed the changes by input.\r
-  @retval     FALSE       User discarded the changes.\r
+  @retval     FALSE       User discarded the changes or device error.\r
 \r
 **/\r
 BOOLEAN\r
@@ -451,22 +451,29 @@ ReadUserKey (
   EFI_STATUS                        Status;\r
   EFI_INPUT_KEY                     Key;\r
   UINT16                            InputKey;\r
-      \r
+  UINTN                             Index;\r
+\r
   InputKey = 0; \r
   do {\r
-    Status = gBS->CheckEvent (gST->ConIn->WaitForKey);\r
-    if (!EFI_ERROR (Status)) {\r
-      Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
-      if (Key.ScanCode == SCAN_ESC) {\r
-        InputKey = Key.ScanCode;\r
-      }\r
-      if ((Key.ScanCode == SCAN_F10) && !CautionKey) {\r
-        InputKey = Key.ScanCode;\r
-      }\r
-      if ((Key.ScanCode == SCAN_F12) && CautionKey) {\r
-        InputKey = Key.ScanCode;\r
-      }\r
-    }      \r
+    Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
+    if (Status == EFI_NOT_READY) {\r
+      gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &Index);\r
+      continue;\r
+    }\r
+\r
+    if (Status == EFI_DEVICE_ERROR) {\r
+      return FALSE;\r
+    }\r
+\r
+    if (Key.ScanCode == SCAN_ESC) {\r
+      InputKey = Key.ScanCode;\r
+    }\r
+    if ((Key.ScanCode == SCAN_F10) && !CautionKey) {\r
+      InputKey = Key.ScanCode;\r
+    }\r
+    if ((Key.ScanCode == SCAN_F12) && CautionKey) {\r
+      InputKey = Key.ScanCode;\r
+    }\r
   } while (InputKey == 0);\r
 \r
   if (InputKey != SCAN_ESC) {\r
@@ -511,7 +518,7 @@ TcgPhysicalPresenceLibConstructor (
 **/\r
 BOOLEAN\r
 UserConfirm (\r
-  IN      UINT                    TpmPpCommand\r
+  IN      UINT32                    TpmPpCommand\r
   )\r
 {\r
   CHAR16                            *ConfirmText;\r
@@ -537,7 +544,7 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -549,11 +556,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
       \r
@@ -565,7 +572,7 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -577,11 +584,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1); \r
       break;\r
 \r
@@ -594,12 +601,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
+      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
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -611,11 +618,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -627,15 +634,15 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -647,7 +654,7 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -659,7 +666,7 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -671,11 +678,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -687,15 +694,15 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -708,11 +715,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
       \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -733,19 +740,19 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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, 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
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -757,11 +764,11 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -774,20 +781,20 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_CLEAR));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      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
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -800,15 +807,15 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -821,12 +828,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
+      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
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -839,19 +846,19 @@ UserConfirm (
       FreePool (TmpStr1);\r
 \r
       TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));\r
-      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);\r
+      StrnCat (ConfirmText, 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, 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
+      StrnCat (ConfirmText, 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
+      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);\r
       FreePool (TmpStr1);\r
       break;\r
 \r
@@ -886,32 +893,35 @@ UserConfirm (
 }\r
 \r
 /**\r
-  Check and execute the requested physical presence command.\r
-\r
-  Caution: This function may receive untrusted input.\r
-  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] TcgPpData            Point to the physical presence NV variable.\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]  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
 \r
 **/\r
-VOID\r
-ExecutePendingTpmRequest (\r
-  IN      EFI_TCG_PROTOCOL          *TcgProtocol,\r
-  IN      EFI_PHYSICAL_PRESENCE     *TcgPpData\r
+BOOLEAN\r
+HaveValidTpmRequest  (\r
+  IN      EFI_PHYSICAL_PRESENCE       *TcgPpData,\r
+  IN      EFI_PHYSICAL_PRESENCE_FLAGS Flags,\r
+  OUT     BOOLEAN                     *RequestConfirmed\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINTN                             DataSize;\r
-  UINT8                             Flags;\r
-  BOOLEAN                           RequestConfirmed;\r
+  BOOLEAN  IsRequestValid;\r
+\r
+  *RequestConfirmed = FALSE;\r
 \r
-  Flags            = TcgPpData->Flags;\r
-  RequestConfirmed = FALSE;  \r
   switch (TcgPpData->PPRequest) {\r
     case PHYSICAL_PRESENCE_NO_ACTION:\r
-      return;\r
+      *RequestConfirmed = TRUE;\r
+      return TRUE;\r
     case PHYSICAL_PRESENCE_ENABLE:\r
     case PHYSICAL_PRESENCE_DISABLE:\r
     case PHYSICAL_PRESENCE_ACTIVATE:\r
@@ -923,81 +933,160 @@ ExecutePendingTpmRequest (
     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
-        RequestConfirmed = TRUE;\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
-        RequestConfirmed = TRUE;\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
-        RequestConfirmed = TRUE;\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
-        RequestConfirmed = TRUE;\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
+      break;\r
 \r
     case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE:\r
     case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:\r
     case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE:\r
-      RequestConfirmed = TRUE;\r
+      *RequestConfirmed = TRUE;\r
       break;\r
-      \r
+\r
+    case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_TRUE:\r
+    case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE:\r
+    case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_TRUE:\r
+      break;\r
+\r
     default:\r
-      //\r
-      // Invalid operation request.\r
-      //\r
-      TcgPpData->PPResponse = TPM_PP_BIOS_FAILURE;\r
-      TcgPpData->LastPPRequest = TcgPpData->PPRequest;\r
-      TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;\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
-      return;\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
-    RequestConfirmed = TRUE;\r
+    *RequestConfirmed = TRUE;\r
   }\r
 \r
-  if (!RequestConfirmed) {\r
+  //\r
+  // Physical Presence command is correct\r
+  //\r
+  return TRUE;\r
+}\r
+\r
+\r
+/**\r
+  Check and execute the requested physical presence command.\r
+\r
+  Caution: This function may receive untrusted input.\r
+  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] 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_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 (!HaveValidTpmRequest(TcgPpData, Flags, &RequestConfirmed)) {\r
+    //\r
+    // Invalid operation request.\r
+    //\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
+    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
+    return;\r
+  }\r
+\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
   //\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
   }\r
@@ -1017,7 +1106,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
@@ -1038,6 +1127,13 @@ ExecutePendingTpmRequest (
     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
@@ -1074,11 +1170,57 @@ TcgPhysicalPresenceLibProcessRequest (
   UINTN                             DataSize;\r
   EFI_PHYSICAL_PRESENCE             TcgPpData;\r
   EFI_TCG_PROTOCOL                  *TcgProtocol;\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
+  // 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
@@ -1092,22 +1234,29 @@ 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
@@ -1135,7 +1284,7 @@ TcgPhysicalPresenceLibProcessRequest (
   //\r
   // Execute pending TPM request.\r
   //  \r
-  ExecutePendingTpmRequest (TcgProtocol, &TcgPpData);\r
+  ExecutePendingTpmRequest (TcgProtocol, &TcgPpData, PpiFlags);\r
   DEBUG ((EFI_D_INFO, "[TPM] PPResponse = %x\n", TcgPpData.PPResponse));\r
 \r
   //\r
@@ -1144,3 +1293,101 @@ TcgPhysicalPresenceLibProcessRequest (
   TpmPhysicalPresence (TcgProtocol, TPM_PHYSICAL_PRESENCE_NOTPRESENT | TPM_PHYSICAL_PRESENCE_LOCK);\r
 }\r
 \r
+/**\r
+  Check if the pending TPM request needs user input to confirm.\r
+\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
+  @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
+**/\r
+BOOLEAN\r
+EFIAPI\r
+TcgPhysicalPresenceLibNeedUserConfirm(\r
+  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_PHYSICAL_PRESENCE_FLAGS  PpiFlags;\r
+  \r
+  Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Check Tpm requests\r
+  //\r
+  DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
+  Status = gRT->GetVariable (\r
+                  PHYSICAL_PRESENCE_VARIABLE,\r
+                  &gEfiPhysicalPresenceGuid,\r
+                  NULL,\r
+                  &DataSize,\r
+                  &TcgPpData\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    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
+    //\r
+    return FALSE;\r
+  }\r
+\r
+  if (!HaveValidTpmRequest(&TcgPpData, PpiFlags, &RequestConfirmed)) {\r
+    //\r
+    // Invalid operation request.\r
+    //\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Check Tpm Capability\r
+  //\r
+  Status = GetTpmCapability (TcgProtocol, &LifetimeLock, &CmdEnable);\r
+  if (EFI_ERROR (Status)) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (!CmdEnable) {\r
+    if (LifetimeLock) {\r
+      //\r
+      // physicalPresenceCMDEnable is locked, can't execute physical presence command.\r
+      //\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  if (!RequestConfirmed) {\r
+    //\r
+    // Need UI to confirm\r
+    //\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r