]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / PiDxeS3BootScriptLib / BootScriptSave.c
index bc3daad9160ef81298816cee4a5fca3f7bd3d134..8c7b58b2f960e5609045eff250199ca4fb4f146c 100644 (file)
@@ -1,16 +1,9 @@
 /** @file\r
-  Save the S3 data to S3 boot script. \r
\r
-  Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Save the S3 data to S3 boot script.\r
 \r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions\r
-  of the BSD License which accompanies this distribution.  The\r
-  full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 #include "InternalBootScriptLib.h"\r
 \r
   Data structure usage:\r
 \r
-  +------------------------------+<-- PcdS3BootScriptTablePrivateDataPtr\r
+  +------------------------------+<------- PcdS3BootScriptTablePrivateDataPtr\r
+  | SCRIPT_TABLE_PRIVATE_DATA    |          (mS3BootScriptTablePtr, Before SmmReadyToLock)\r
+  |    TableBase                 |---      PcdS3BootScriptTablePrivateSmmDataPtr\r
+  |    TableLength               |--|--     (mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr, After SmmReadyToLock InSmm)\r
+  |    TableMemoryPageNumber     |--|-|----\r
+  |    AtRuntime                 |  | |   |\r
+  |    InSmm                     |  | |   |\r
+  |    BootTimeScriptLength      |--|-|---|---\r
+  |    SmmLocked                 |  | |   |  |\r
+  |    BackFromS3                |  | |   |  |\r
+  +------------------------------+  | |   |  |\r
+                                    | |   |  |\r
+  +------------------------------+<-- |   |  |\r
+  | EFI_BOOT_SCRIPT_TABLE_HEADER |    |   |  |\r
+  |    TableLength               |----|-- |  |\r
+  +------------------------------+    | | |  |\r
+  |     ......                   |    | | |  |\r
+  +------------------------------+<---- | |  |\r
+  | EFI_BOOT_SCRIPT_TERMINATE    |      | |  |\r
+  +------------------------------+<------ |  |\r
+                                          |  |\r
+                                          |  |\r
+  mBootScriptDataBootTimeGuid LockBox:    |  |\r
+   Used to restore data after back from S3|  |\r
+   to handle potential INSERT boot script |  |\r
+   at runtime.                            |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | Before SmmReadyToLock        |        |  |\r
+  |                              |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | After SmmReadyToLock InSmm   |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+<-------|--|\r
+                                          |  |\r
+                                          |  |\r
+  mBootScriptDataGuid LockBox: (IN_PLACE) |  |\r
+   Used to restore data at S3 resume.     |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | Before SmmReadyToLock        |        |  |\r
+  |                              |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | After SmmReadyToLock InSmm   |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+<-------|---\r
+  | Runtime Boot Script          |        |\r
+  | After SmmReadyToLock InSmm   |        |\r
+  +------------------------------+        |\r
+  |     ......                   |        |\r
+  +------------------------------+<--------\r
+\r
+\r
+  mBootScriptTableBaseGuid LockBox: (IN_PLACE)\r
+  +------------------------------+\r
+  | mS3BootScriptTablePtr->      |\r
+  |  TableBase                   |\r
+  +------------------------------+\r
+\r
+\r
+  mBootScriptSmmPrivateDataGuid LockBox: (IN_PLACE)\r
+   SMM private data with BackFromS3 = TRUE\r
+   at runtime. S3 will help restore it to\r
+   tell the Library the system is back from S3.\r
+  +------------------------------+\r
   | SCRIPT_TABLE_PRIVATE_DATA    |\r
-  |    TableBase                 |---\r
-  |    TableLength               |--|--\r
-  |    AtRuntime                 |  | |\r
-  |    InSmm                     |  | |\r
-  +------------------------------+  | |\r
-                                    | |\r
-  +------------------------------+<-- |\r
-  | EFI_BOOT_SCRIPT_TABLE_HEADER |    |\r
-  |    TableLength               |----|--\r
-  +------------------------------+    | |\r
-  |     ......                   |    | |\r
-  +------------------------------+<---- |\r
-  | EFI_BOOT_SCRIPT_TERMINATE    |      |\r
-  +------------------------------+<------\r
+  |    TableBase                 |\r
+  |    TableLength               |\r
+  |    TableMemoryPageNumber     |\r
+  |    AtRuntime                 |\r
+  |    InSmm                     |\r
+  |    BootTimeScriptLength      |\r
+  |    SmmLocked                 |\r
+  |    BackFromS3 = TRUE         |\r
+  +------------------------------+\r
 \r
 **/\r
 \r
-SCRIPT_TABLE_PRIVATE_DATA        *mS3BootScriptTablePtr;\r
-EFI_EVENT                        mEnterRuntimeEvent;\r
+SCRIPT_TABLE_PRIVATE_DATA  *mS3BootScriptTablePtr;\r
+\r
 //\r
-// Allocate SMM copy because we can not use mS3BootScriptTablePtr when we AtRuntime in InSmm.\r
+// Allocate SMM copy because we can not use mS3BootScriptTablePtr after SmmReadyToLock in InSmm.\r
 //\r
-SCRIPT_TABLE_PRIVATE_DATA        *mS3BootScriptTableSmmPtr;\r
-UINTN                            mLockBoxLength;\r
+SCRIPT_TABLE_PRIVATE_DATA  *mS3BootScriptTableSmmPtr;\r
 \r
-EFI_GUID                         mBootScriptDataGuid = {\r
+EFI_GUID  mBootScriptDataGuid = {\r
   0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }\r
 };\r
 \r
-EFI_GUID                         mBootScriptDataOrgGuid = {\r
+EFI_GUID  mBootScriptDataBootTimeGuid = {\r
   0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }\r
 };\r
 \r
-EFI_GUID                         mBootScriptHeaderDataGuid = {\r
+EFI_GUID  mBootScriptTableBaseGuid = {\r
   0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }\r
 };\r
 \r
+EFI_GUID  mBootScriptSmmPrivateDataGuid = {\r
+  0x627ee2da, 0x3bf9, 0x439b, { 0x92, 0x9f, 0x2e, 0xe, 0x6e, 0x9d, 0xba, 0x62 }\r
+};\r
+\r
+EFI_EVENT              mEventDxeSmmReadyToLock           = NULL;\r
+VOID                   *mRegistrationSmmExitBootServices = NULL;\r
+VOID                   *mRegistrationSmmLegacyBoot       = NULL;\r
+VOID                   *mRegistrationSmmReadyToLock      = NULL;\r
+BOOLEAN                mS3BootScriptTableAllocated       = FALSE;\r
+BOOLEAN                mS3BootScriptTableSmmAllocated    = FALSE;\r
+EFI_SMM_SYSTEM_TABLE2  *mBootScriptSmst                  = NULL;\r
+BOOLEAN                mS3BootScriptAcpiS3Enable         = TRUE;\r
+\r
 /**\r
-  This is an internal function to add a terminate node the entry, recalculate the table \r
-  length and fill into the table. \r
-  \r
-  @return the base address of the boot script tble.   \r
+  This is an internal function to add a terminate node the entry, recalculate the table\r
+  length and fill into the table.\r
+\r
+  @return the base address of the boot script table.\r
  **/\r
-UINT8*\r
+UINT8 *\r
 S3BootScriptInternalCloseTable (\r
   VOID\r
   )\r
 {\r
-  UINT8                          *S3TableBase;\r
-  EFI_BOOT_SCRIPT_TERMINATE      ScriptTerminate;\r
-  EFI_BOOT_SCRIPT_TABLE_HEADER   *ScriptTableInfo;\r
+  UINT8                         *S3TableBase;\r
+  EFI_BOOT_SCRIPT_TERMINATE     ScriptTerminate;\r
+  EFI_BOOT_SCRIPT_TABLE_HEADER  *ScriptTableInfo;\r
+\r
   S3TableBase = mS3BootScriptTablePtr->TableBase;\r
-  \r
+\r
   if (S3TableBase == NULL) {\r
     //\r
     // the table is not exist\r
     //\r
     return S3TableBase;\r
   }\r
+\r
   //\r
   // Append the termination entry.\r
   //\r
-  ScriptTerminate.OpCode  = S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE;\r
-  ScriptTerminate.Length  = (UINT8) sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
+  ScriptTerminate.OpCode = S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE;\r
+  ScriptTerminate.Length = (UINT8)sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
   CopyMem (mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength, &ScriptTerminate, sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
   //\r
   // fill the table length\r
   //\r
-  ScriptTableInfo                = (EFI_BOOT_SCRIPT_TABLE_HEADER*)(mS3BootScriptTablePtr->TableBase);\r
+  ScriptTableInfo              = (EFI_BOOT_SCRIPT_TABLE_HEADER *)(mS3BootScriptTablePtr->TableBase);\r
   ScriptTableInfo->TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
-  \r
\r
-  \r
+\r
   return S3TableBase;\r
   //\r
-  // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to \r
-  // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE). Because \r
-  // maybe in runtime, we still need add entries into the table, and the runtime entry should be\r
-  // added start before this TERMINATE node.\r
+  // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to\r
+  // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE).\r
+  // Because maybe after SmmReadyToLock, we still need add entries into the table,\r
+  // and the entry should be added start before this TERMINATE node.\r
   //\r
-}  \r
+}\r
 \r
 /**\r
   This function save boot script data to LockBox.\r
-  1. BootSriptPrivate data, BootScript data - Image and DispatchContext are handled by platform.\r
-  2. BootScriptExecutor, BootScriptExecutor context\r
-  - ACPI variable - (PI version) sould be handled by SMM driver. S3 Page table is handled here.\r
-  - ACPI variable - framework version is already handled by Framework CPU driver.\r
+\r
 **/\r
 VOID\r
 SaveBootScriptDataToLockBox (\r
   VOID\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
-  // mS3BootScriptTablePtr->TableLength does not include EFI_BOOT_SCRIPT_TERMINATE, because we need add entry at runtime.\r
-  // Save all info here, just in case that no one will add boot script entry in SMM.\r
+  // Save whole memory copy into LockBox.\r
+  // It will be used to restore data at S3 resume.\r
   //\r
   Status = SaveLockBox (\r
              &mBootScriptDataGuid,\r
              (VOID *)mS3BootScriptTablePtr->TableBase,\r
-             mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)\r
+             EFI_PAGES_TO_SIZE (mS3BootScriptTablePtr->TableMemoryPageNumber)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   Status = SetLockBoxAttributes (&mBootScriptDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  //\r
-  // We need duplicate the original copy, because it may have INSERT boot script at runtime in SMM.\r
-  // If so, we should use original copy to restore data after OS rewrites the ACPINvs region.\r
-  // Or the data inserted may cause some original boot script data lost.\r
-  //\r
-  Status = SaveLockBox (\r
-             &mBootScriptDataOrgGuid,\r
-             (VOID *)mS3BootScriptTablePtr->TableBase,\r
-             mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)\r
-             );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
   //\r
   // Just need save TableBase.\r
   // Do not update other field because they will NOT be used in S3.\r
   //\r
   Status = SaveLockBox (\r
-             &mBootScriptHeaderDataGuid,\r
+             &mBootScriptTableBaseGuid,\r
              (VOID *)&mS3BootScriptTablePtr->TableBase,\r
-             sizeof(mS3BootScriptTablePtr->TableBase)\r
+             sizeof (mS3BootScriptTablePtr->TableBase)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  Status = SetLockBoxAttributes (&mBootScriptHeaderDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
+  Status = SetLockBoxAttributes (&mBootScriptTableBaseGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r
   This is the Event call back function to notify the Library the system is entering\r
-  run time phase.\r
-  \r
+  SmmLocked phase.\r
+\r
   @param  Event   Pointer to this event\r
-  @param  Context Event hanlder private data \r
+  @param  Context Event handler private data\r
  **/\r
 VOID\r
 EFIAPI\r
@@ -172,8 +225,8 @@ S3BootScriptEventCallBack (
   IN VOID       *Context\r
   )\r
 {\r
-  EFI_STATUS   Status;\r
-  VOID         *Interface;\r
+  EFI_STATUS  Status;\r
+  VOID        *Interface;\r
 \r
   //\r
   // Try to locate it because EfiCreateProtocolNotifyEvent will trigger it once when registration.\r
@@ -185,32 +238,33 @@ S3BootScriptEventCallBack (
                   &Interface\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return ;\r
+    return;\r
   }\r
 \r
   //\r
-  // Here we should tell the library that we are enter into runtime phase. and \r
-  // the memory page number occupied by the table should not grow anymore.\r
+  // Here we should tell the library that we are entering SmmLocked phase.\r
+  // and the memory page number occupied by the table should not grow anymore.\r
   //\r
-  if (!mS3BootScriptTablePtr->AtRuntime) {\r
+  if (!mS3BootScriptTablePtr->SmmLocked) {\r
     //\r
-    // In boot time, we need not write the terminate node when adding a node to boot scipt table\r
-    // or else, that will impact the performance. However, in runtime, we should append terminate\r
-    // node on every add to boot script table\r
+    // Before SmmReadyToLock, we need not write the terminate node when adding a node to boot scipt table\r
+    // or else, that will impact the performance. However, after SmmReadyToLock, we should append terminate\r
+    // node on every add to boot script table.\r
     //\r
     S3BootScriptInternalCloseTable ();\r
-    mS3BootScriptTablePtr->AtRuntime = TRUE;\r
+    mS3BootScriptTablePtr->SmmLocked = TRUE;\r
 \r
     //\r
     // Save BootScript data to lockbox\r
     //\r
     SaveBootScriptDataToLockBox ();\r
   }\r
-} \r
+}\r
+\r
 /**\r
-  This is the Event call back function is triggered in SMM to notify the Library the system is entering\r
-  run time phase and set InSmm flag.\r
-  \r
+  This is the Event call back function is triggered in SMM to notify the Library\r
+  the system is entering SmmLocked phase and set InSmm flag.\r
+\r
   @param  Protocol   Points to the protocol's unique identifier\r
   @param  Interface  Points to the interface instance\r
   @param  Handle     The handle on which the interface was installed\r
@@ -233,7 +287,7 @@ S3BootScriptSmmEventCallBack (
   }\r
 \r
   //\r
-  // Last chance to call-out, just make sure AtRuntime is set\r
+  // Last chance to call-out, just make sure SmmLocked is set.\r
   //\r
   S3BootScriptEventCallBack (NULL, NULL);\r
 \r
@@ -241,55 +295,156 @@ S3BootScriptSmmEventCallBack (
   // Save a SMM copy. If TableBase is NOT null, it means SMM copy has been ready, skip copy mem.\r
   //\r
   if (mS3BootScriptTableSmmPtr->TableBase == NULL) {\r
-    CopyMem (mS3BootScriptTableSmmPtr, mS3BootScriptTablePtr, sizeof(*mS3BootScriptTablePtr));\r
+    CopyMem (mS3BootScriptTableSmmPtr, mS3BootScriptTablePtr, sizeof (*mS3BootScriptTablePtr));\r
+\r
+    //\r
+    // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.\r
+    // InSmm will only be checked if SmmLocked is TRUE.\r
+    //\r
+    mS3BootScriptTableSmmPtr->InSmm = TRUE;\r
   }\r
+\r
   //\r
-  // We should not use ACPINvs copy, because it is not safe.\r
+  // We should not use ACPI Reserved copy, because it is not safe.\r
   //\r
   mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr;\r
 \r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This function is to save boot time boot script data to LockBox.\r
+\r
+  Because there may be INSERT boot script at runtime in SMM.\r
+  The boot time copy will be used to restore data after back from S3.\r
+  Otherwise the data inserted may cause some boot time boot script data lost\r
+  if only BootScriptData used.\r
+\r
+**/\r
+VOID\r
+SaveBootTimeDataToLockBox (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
   //\r
-  // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.\r
-  // InSmm will only be checked if AtRuntime is TRUE.\r
+  // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,\r
+  // and then save the data to BootScriptDataBootTime LockBox.\r
   //\r
-  mS3BootScriptTablePtr->InSmm = TRUE;\r
+  Status = RestoreLockBox (\r
+             &mBootScriptDataGuid,\r
+             NULL,\r
+             NULL\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
-  // Record LockBoxLength\r
+  // Save BootScriptDataBootTime\r
+  // It will be used to restore data after back from S3.\r
   //\r
-  mLockBoxLength = mS3BootScriptTableSmmPtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE);\r
+  Status = SaveLockBox (\r
+             &mBootScriptDataBootTimeGuid,\r
+             (VOID *)mS3BootScriptTablePtr->TableBase,\r
+             mS3BootScriptTablePtr->BootTimeScriptLength\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+  This function save boot script SMM private data to LockBox with BackFromS3 = TRUE at runtime.\r
+  S3 resume will help restore it to tell the Library the system is back from S3.\r
+\r
+**/\r
+VOID\r
+SaveSmmPriviateDataToLockBoxAtRuntime (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Save boot script SMM private data with BackFromS3 = TRUE.\r
+  //\r
+  mS3BootScriptTablePtr->BackFromS3 = TRUE;\r
+  Status                            = SaveLockBox (\r
+                                        &mBootScriptSmmPrivateDataGuid,\r
+                                        (VOID *)mS3BootScriptTablePtr,\r
+                                        sizeof (SCRIPT_TABLE_PRIVATE_DATA)\r
+                                        );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = SetLockBoxAttributes (&mBootScriptSmmPrivateDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Set BackFromS3 flag back to FALSE to indicate that now is not back from S3.\r
+  //\r
+  mS3BootScriptTablePtr->BackFromS3 = FALSE;\r
+}\r
+\r
+/**\r
+  This is the Event call back function is triggered in SMM to notify the Library\r
+  the system is entering runtime phase.\r
+\r
+  @param[in] Protocol   Points to the protocol's unique identifier\r
+  @param[in] Interface  Points to the interface instance\r
+  @param[in] Handle     The handle on which the interface was installed\r
+\r
+  @retval EFI_SUCCESS SmmAtRuntimeCallBack runs successfully\r
+ **/\r
+EFI_STATUS\r
+EFIAPI\r
+S3BootScriptSmmAtRuntimeCallBack (\r
+  IN CONST EFI_GUID  *Protocol,\r
+  IN VOID            *Interface,\r
+  IN EFI_HANDLE      Handle\r
+  )\r
+{\r
+  if (!mS3BootScriptTablePtr->AtRuntime) {\r
+    mS3BootScriptTablePtr->BootTimeScriptLength = (UINT32)(mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
+    SaveBootTimeDataToLockBox ();\r
+\r
+    mS3BootScriptTablePtr->AtRuntime = TRUE;\r
+    SaveSmmPriviateDataToLockBoxAtRuntime ();\r
+  }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   Library Constructor.\r
-  this function just identify it is a smm driver or non-smm driver linked against \r
-  with the library   \r
+  this function just identify it is a smm driver or non-smm driver linked against\r
+  with the library\r
 \r
   @param  ImageHandle   The firmware allocated handle for the EFI image.\r
   @param  SystemTable   A pointer to the EFI System Table.\r
 \r
-  @retval  RETURN_SUCCESS            Allocate the global memory space to store S3 boot script table private data\r
-  @retval  RETURN_OUT_OF_RESOURCES   No enough memory to allocated.\r
+  @retval RETURN_SUCCESS    The constructor always returns RETURN_SUCCESS.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptLibInitialize (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  EFI_STATUS                      Status;\r
-  SCRIPT_TABLE_PRIVATE_DATA      *S3TablePtr;\r
-  SCRIPT_TABLE_PRIVATE_DATA      *S3TableSmmPtr;\r
-  VOID                           *Registration;\r
-  EFI_SMM_BASE2_PROTOCOL         *SmmBase2;\r
-  BOOLEAN                        InSmm;\r
-  EFI_SMM_SYSTEM_TABLE2          *Smst;\r
-  EFI_PHYSICAL_ADDRESS           Buffer;\r
+  EFI_STATUS                 Status;\r
+  SCRIPT_TABLE_PRIVATE_DATA  *S3TablePtr;\r
+  SCRIPT_TABLE_PRIVATE_DATA  *S3TableSmmPtr;\r
+  VOID                       *Registration;\r
+  EFI_SMM_BASE2_PROTOCOL     *SmmBase2;\r
+  BOOLEAN                    InSmm;\r
+  EFI_PHYSICAL_ADDRESS       Buffer;\r
+\r
+  if (!PcdGetBool (PcdAcpiS3Enable)) {\r
+    mS3BootScriptAcpiS3Enable = FALSE;\r
+    DEBUG ((DEBUG_INFO, "%a: Skip S3BootScript because ACPI S3 disabled.\n", gEfiCallerBaseName));\r
+    return RETURN_SUCCESS;\r
+  }\r
 \r
-  S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateDataPtr);\r
+  S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA *)(UINTN)PcdGet64 (PcdS3BootScriptTablePrivateDataPtr);\r
   //\r
   // The Boot script private data is not be initialized. create it\r
   //\r
@@ -297,302 +452,421 @@ S3BootScriptLibInitialize (
     Buffer = SIZE_4GB - 1;\r
     Status = gBS->AllocatePages (\r
                     AllocateMaxAddress,\r
-                    EfiACPIMemoryNVS,\r
-                    EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)),\r
+                    EfiReservedMemoryType,\r
+                    EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA)),\r
                     &Buffer\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      return RETURN_OUT_OF_RESOURCES;\r
-    }\r
-    S3TablePtr = (VOID *) (UINTN) Buffer;\r
+    ASSERT_EFI_ERROR (Status);\r
+    mS3BootScriptTableAllocated = TRUE;\r
+    S3TablePtr                  = (VOID *)(UINTN)Buffer;\r
 \r
-    PcdSet64 (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr); \r
-    ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));  \r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, (UINT64)(UINTN)S3TablePtr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    ZeroMem (S3TablePtr, sizeof (SCRIPT_TABLE_PRIVATE_DATA));\r
     //\r
-    // create event to notify the library system enter the runtime phase\r
+    // Create event to notify the library system enter the SmmLocked phase.\r
     //\r
-    mEnterRuntimeEvent = EfiCreateProtocolNotifyEvent  (\r
-                          &gEfiDxeSmmReadyToLockProtocolGuid,\r
-                          TPL_CALLBACK,\r
-                          S3BootScriptEventCallBack,\r
-                          NULL,\r
-                          &Registration\r
-                          );\r
-    ASSERT (mEnterRuntimeEvent != NULL);\r
-  } \r
+    mEventDxeSmmReadyToLock = EfiCreateProtocolNotifyEvent (\r
+                                &gEfiDxeSmmReadyToLockProtocolGuid,\r
+                                TPL_CALLBACK,\r
+                                S3BootScriptEventCallBack,\r
+                                NULL,\r
+                                &Registration\r
+                                );\r
+    ASSERT (mEventDxeSmmReadyToLock != NULL);\r
+  }\r
+\r
   mS3BootScriptTablePtr = S3TablePtr;\r
 \r
   //\r
   // Get InSmm, we need to register SmmReadyToLock if this library is linked to SMM driver.\r
   //\r
-  Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID**) &SmmBase2);\r
+  Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **)&SmmBase2);\r
   if (EFI_ERROR (Status)) {\r
     return RETURN_SUCCESS;\r
   }\r
+\r
   Status = SmmBase2->InSmm (SmmBase2, &InSmm);\r
   if (EFI_ERROR (Status)) {\r
     return RETURN_SUCCESS;\r
   }\r
+\r
   if (!InSmm) {\r
     return RETURN_SUCCESS;\r
   }\r
+\r
   //\r
   // Good, we are in SMM\r
   //\r
-  Status = SmmBase2->GetSmstLocation (SmmBase2, &Smst);\r
+  Status = SmmBase2->GetSmstLocation (SmmBase2, &mBootScriptSmst);\r
   if (EFI_ERROR (Status)) {\r
     return RETURN_SUCCESS;\r
   }\r
 \r
-  S3TableSmmPtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateSmmDataPtr);\r
+  S3TableSmmPtr = (SCRIPT_TABLE_PRIVATE_DATA *)(UINTN)PcdGet64 (PcdS3BootScriptTablePrivateSmmDataPtr);\r
   //\r
   // The Boot script private data in SMM is not be initialized. create it\r
   //\r
   if (S3TableSmmPtr == 0) {\r
-    Status = Smst->SmmAllocatePool (\r
-                     EfiRuntimeServicesData,\r
-                     sizeof(SCRIPT_TABLE_PRIVATE_DATA),\r
-                     (VOID **) &S3TableSmmPtr\r
-                     );\r
-    if (EFI_ERROR (Status)) {\r
-      return RETURN_OUT_OF_RESOURCES;\r
-    }\r
+    Status = mBootScriptSmst->SmmAllocatePool (\r
+                                EfiRuntimeServicesData,\r
+                                sizeof (SCRIPT_TABLE_PRIVATE_DATA),\r
+                                (VOID **)&S3TableSmmPtr\r
+                                );\r
+    ASSERT_EFI_ERROR (Status);\r
+    mS3BootScriptTableSmmAllocated = TRUE;\r
+\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, (UINT64)(UINTN)S3TableSmmPtr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    ZeroMem (S3TableSmmPtr, sizeof (SCRIPT_TABLE_PRIVATE_DATA));\r
 \r
-    PcdSet64 (PcdS3BootScriptTablePrivateSmmDataPtr, (UINT64) (UINTN)S3TableSmmPtr);\r
-    ZeroMem (S3TableSmmPtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));\r
+    //\r
+    // Register SmmExitBootServices and SmmLegacyBoot notification.\r
+    //\r
+    Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                &gEdkiiSmmExitBootServicesProtocolGuid,\r
+                                S3BootScriptSmmAtRuntimeCallBack,\r
+                                &mRegistrationSmmExitBootServices\r
+                                );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                &gEdkiiSmmLegacyBootProtocolGuid,\r
+                                S3BootScriptSmmAtRuntimeCallBack,\r
+                                &mRegistrationSmmLegacyBoot\r
+                                );\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
+\r
   mS3BootScriptTableSmmPtr = S3TableSmmPtr;\r
 \r
   //\r
-  // Then register event after lock\r
+  // Register SmmReadyToLock notification.\r
   //\r
-  Registration = NULL;\r
-  Status = Smst->SmmRegisterProtocolNotify (\r
-                   &gEfiSmmReadyToLockProtocolGuid,\r
-                   S3BootScriptSmmEventCallBack,\r
-                   &Registration\r
-                   );\r
+  Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                              &gEfiSmmReadyToLockProtocolGuid,\r
+                              S3BootScriptSmmEventCallBack,\r
+                              &mRegistrationSmmReadyToLock\r
+                              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Library Destructor to free the resources allocated by\r
+  S3BootScriptLibInitialize() and unregister callbacks.\r
+\r
+  NOTICE: The destructor doesn't support unloading as a separate action, and it\r
+  only supports unloading if the containing driver's entry point function fails.\r
+\r
+  @param ImageHandle        The firmware allocated handle for the EFI image.\r
+  @param SystemTable        A pointer to the EFI System Table.\r
+\r
+  @retval RETURN_SUCCESS    The destructor always returns RETURN_SUCCESS.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+S3BootScriptLibDeinitialize (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "%a() in %a module\n", __FUNCTION__, gEfiCallerBaseName));\r
+\r
+  if (mEventDxeSmmReadyToLock != NULL) {\r
+    //\r
+    // Close the DxeSmmReadyToLock event.\r
+    //\r
+    Status = gBS->CloseEvent (mEventDxeSmmReadyToLock);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  if (mBootScriptSmst != NULL) {\r
+    if (mRegistrationSmmExitBootServices != NULL) {\r
+      //\r
+      // Unregister SmmExitBootServices notification.\r
+      //\r
+      Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                  &gEdkiiSmmExitBootServicesProtocolGuid,\r
+                                  NULL,\r
+                                  &mRegistrationSmmExitBootServices\r
+                                  );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+\r
+    if (mRegistrationSmmLegacyBoot != NULL) {\r
+      //\r
+      // Unregister SmmLegacyBoot notification.\r
+      //\r
+      Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                  &gEdkiiSmmLegacyBootProtocolGuid,\r
+                                  NULL,\r
+                                  &mRegistrationSmmLegacyBoot\r
+                                  );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+\r
+    if (mRegistrationSmmReadyToLock != NULL) {\r
+      //\r
+      // Unregister SmmReadyToLock notification.\r
+      //\r
+      Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                  &gEfiSmmReadyToLockProtocolGuid,\r
+                                  NULL,\r
+                                  &mRegistrationSmmReadyToLock\r
+                                  );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  }\r
+\r
+  //\r
+  // Free the resources allocated and set PCDs to 0.\r
+  //\r
+  if (mS3BootScriptTableAllocated) {\r
+    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)mS3BootScriptTablePtr, EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA)));\r
+    ASSERT_EFI_ERROR (Status);\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, 0);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  if ((mBootScriptSmst != NULL) && mS3BootScriptTableSmmAllocated) {\r
+    Status = mBootScriptSmst->SmmFreePool (mS3BootScriptTableSmmPtr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, 0);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
 /**\r
   To get the start address from which a new boot time s3 boot script entry will write into.\r
   If the table is not exist, the functio will first allocate a buffer for the table\r
-  If the table buffer is not enough for the new entry, in non-smm mode, the funtion will \r
+  If the table buffer is not enough for the new entry, in non-smm mode, the funtion will\r
   invoke reallocate to enlarge buffer.\r
-  \r
+\r
   @param EntryLength      the new entry length.\r
-  \r
-  @retval the address from which the a new s3 boot script entry will write into \r
+\r
+  @retval the address from which the a new s3 boot script entry will write into\r
  **/\r
-UINT8*\r
+UINT8 *\r
 S3BootScriptGetBootTimeEntryAddAddress (\r
   UINT8  EntryLength\r
   )\r
 {\r
-   EFI_PHYSICAL_ADDRESS              S3TableBase;\r
-   EFI_PHYSICAL_ADDRESS              NewS3TableBase;\r
-   UINT8                            *NewEntryPtr;\r
-   UINT32                            TableLength;\r
-   UINT16                            PageNumber;\r
-   EFI_STATUS                        Status;\r
-   EFI_BOOT_SCRIPT_TABLE_HEADER      *ScriptTableInfo;\r
-   \r
-   S3TableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(mS3BootScriptTablePtr->TableBase);\r
-   if (S3TableBase == 0) {\r
-     // The table is not exist. This is the first to add entry. \r
-     // Allocate ACPI script table space under 4G memory. We need it to save\r
-     // some settings done by CSM, which runs after normal script table closed\r
-     //\r
-     S3TableBase = 0xffffffff;\r
-     Status = gBS->AllocatePages (\r
-                  AllocateMaxAddress,\r
-                  EfiACPIMemoryNVS,\r
-                  2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),\r
-                  (EFI_PHYSICAL_ADDRESS*)&S3TableBase\r
-                  );\r
-     \r
-     if (EFI_ERROR(Status)) {\r
-       ASSERT_EFI_ERROR (Status);\r
-       return 0;\r
-     }\r
-     //\r
-     // Fill Table Header\r
-     //\r
-     ScriptTableInfo              = (EFI_BOOT_SCRIPT_TABLE_HEADER*)(UINTN)S3TableBase;\r
-     ScriptTableInfo->OpCode      = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;\r
-     ScriptTableInfo->Length      = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
-     ScriptTableInfo->TableLength = 0;   // will be calculate at CloseTable\r
-     mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
-     mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)S3TableBase;\r
-     mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16)(2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));\r
-   }\r
-     \r
-   // Here we do not count the reserved memory for runtime script table.\r
-   PageNumber   = (UINT16)(mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));   \r
-   TableLength =  mS3BootScriptTablePtr->TableLength;\r
-   if ((UINT32)(PageNumber * EFI_PAGE_SIZE) < (TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE))) {\r
-     // \r
-     // The buffer is too small to hold the table, Reallocate the buffer\r
-     //\r
-     NewS3TableBase = 0xffffffff;\r
-     Status = gBS->AllocatePages (\r
-                  AllocateMaxAddress,\r
-                  EfiACPIMemoryNVS,\r
-                  2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),\r
-                  (EFI_PHYSICAL_ADDRESS*)&NewS3TableBase\r
-                  );\r
-   \r
-     if (EFI_ERROR(Status)) {\r
-       ASSERT_EFI_ERROR (Status);\r
-       return 0;\r
-     }\r
-     \r
-     CopyMem ((VOID*)(UINTN)NewS3TableBase, (VOID*)(UINTN)S3TableBase, TableLength);\r
-     gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);\r
-         \r
-     mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)NewS3TableBase;\r
-     mS3BootScriptTablePtr->TableMemoryPageNumber =  (UINT16) (2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber)); \r
-   }\r
-   //\r
-   // calculate the the start address for the new entry. \r
-   //\r
-   NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;\r
-   \r
-   //\r
-   // update the table lenghth\r
-   //\r
-   mS3BootScriptTablePtr->TableLength =  TableLength + EntryLength;\r
-   \r
-   //\r
-   // In the boot time, we will not append the termination entry to the boot script\r
-   // table until the callers think there is no boot time data that should be added and \r
-   // it is caller's responsibility to explicit call the CloseTable. \r
-   //\r
-   //\r
-  \r
-   return NewEntryPtr;    \r
+  EFI_PHYSICAL_ADDRESS          S3TableBase;\r
+  EFI_PHYSICAL_ADDRESS          NewS3TableBase;\r
+  UINT8                         *NewEntryPtr;\r
+  UINT32                        TableLength;\r
+  UINT16                        PageNumber;\r
+  EFI_STATUS                    Status;\r
+  EFI_BOOT_SCRIPT_TABLE_HEADER  *ScriptTableInfo;\r
+\r
+  S3TableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(mS3BootScriptTablePtr->TableBase);\r
+  if (S3TableBase == 0) {\r
+    //\r
+    // The table is not exist. This is the first to add entry.\r
+    // Allocate ACPI script table space under 4G memory.\r
+    //\r
+    S3TableBase = 0xffffffff;\r
+    Status      = gBS->AllocatePages (\r
+                         AllocateMaxAddress,\r
+                         EfiReservedMemoryType,\r
+                         2 + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber),\r
+                         (EFI_PHYSICAL_ADDRESS *)&S3TableBase\r
+                         );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      ASSERT_EFI_ERROR (Status);\r
+      return 0;\r
+    }\r
+\r
+    //\r
+    // Fill Table Header\r
+    //\r
+    ScriptTableInfo                              = (EFI_BOOT_SCRIPT_TABLE_HEADER *)(UINTN)S3TableBase;\r
+    ScriptTableInfo->OpCode                      = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;\r
+    ScriptTableInfo->Length                      = (UINT8)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
+    ScriptTableInfo->Version                     = BOOT_SCRIPT_TABLE_VERSION;\r
+    ScriptTableInfo->TableLength                 = 0; // will be calculate at CloseTable\r
+    mS3BootScriptTablePtr->TableLength           = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
+    mS3BootScriptTablePtr->TableBase             = (UINT8 *)(UINTN)S3TableBase;\r
+    mS3BootScriptTablePtr->TableMemoryPageNumber = (UINT16)(2 + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber));\r
+  }\r
+\r
+  // Here we do not count the reserved memory for runtime script table.\r
+  PageNumber  = (UINT16)(mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber));\r
+  TableLength =  mS3BootScriptTablePtr->TableLength;\r
+  if (EFI_PAGES_TO_SIZE ((UINTN)PageNumber) < (TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE))) {\r
+    //\r
+    // The buffer is too small to hold the table, Reallocate the buffer\r
+    //\r
+    NewS3TableBase = 0xffffffff;\r
+    Status         = gBS->AllocatePages (\r
+                            AllocateMaxAddress,\r
+                            EfiReservedMemoryType,\r
+                            2 + PageNumber + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber),\r
+                            (EFI_PHYSICAL_ADDRESS *)&NewS3TableBase\r
+                            );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      ASSERT_EFI_ERROR (Status);\r
+      return 0;\r
+    }\r
+\r
+    CopyMem ((VOID *)(UINTN)NewS3TableBase, (VOID *)(UINTN)S3TableBase, TableLength);\r
+    gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);\r
+\r
+    mS3BootScriptTablePtr->TableBase             = (UINT8 *)(UINTN)NewS3TableBase;\r
+    mS3BootScriptTablePtr->TableMemoryPageNumber =  (UINT16)(2 + PageNumber + PcdGet16 (PcdS3BootScriptRuntimeTableReservePageNumber));\r
+  }\r
+\r
+  //\r
+  // calculate the the start address for the new entry.\r
+  //\r
+  NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;\r
+\r
+  //\r
+  // update the table lenghth\r
+  //\r
+  mS3BootScriptTablePtr->TableLength =  TableLength + EntryLength;\r
+\r
+  //\r
+  // In the boot time, we will not append the termination entry to the boot script\r
+  // table until the callers think there is no boot time data that should be added and\r
+  // it is caller's responsibility to explicit call the CloseTable.\r
+  //\r
+  //\r
+\r
+  return NewEntryPtr;\r
 }\r
+\r
 /**\r
-  To get the start address from which a new runtime s3 boot script entry will write into.\r
+  To get the start address from which a new runtime(after SmmReadyToLock) s3 boot script entry will write into.\r
   In this case, it should be ensured that there is enough buffer to hold the entry.\r
-  \r
+\r
   @param EntryLength      the new entry length.\r
-  \r
-  @retval the address from which the a new s3 runtime script entry will write into\r
+\r
+  @retval the address from which the a new s3 runtime(after SmmReadyToLock) script entry will write into\r
  **/\r
-UINT8*\r
+UINT8 *\r
 S3BootScriptGetRuntimeEntryAddAddress (\r
   UINT8  EntryLength\r
   )\r
 {\r
-   UINT8     *NewEntryPtr;\r
-   \r
-   NewEntryPtr = NULL;   \r
-   //\r
-   // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node. \r
-   //\r
-   if (mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE) <= EFI_PAGES_TO_SIZE((UINT32)(mS3BootScriptTablePtr->TableMemoryPageNumber))) {\r
-     NewEntryPtr = mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength;   \r
-     mS3BootScriptTablePtr->TableLength = mS3BootScriptTablePtr->TableLength + EntryLength;\r
-     //\r
-     // Append a terminate node on every insert\r
-     //\r
-     S3BootScriptInternalCloseTable ();\r
-   }\r
-   return (UINT8*)NewEntryPtr;    \r
+  UINT8  *NewEntryPtr;\r
+\r
+  NewEntryPtr = NULL;\r
+  //\r
+  // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.\r
+  //\r
+  if ((mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE)) <= EFI_PAGES_TO_SIZE ((UINTN)(mS3BootScriptTablePtr->TableMemoryPageNumber))) {\r
+    NewEntryPtr                        = mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength;\r
+    mS3BootScriptTablePtr->TableLength = mS3BootScriptTablePtr->TableLength + EntryLength;\r
+    //\r
+    // Append a terminate node on every insert\r
+    //\r
+    S3BootScriptInternalCloseTable ();\r
+  }\r
+\r
+  return (UINT8 *)NewEntryPtr;\r
+}\r
+\r
+/**\r
+  This function is to restore boot time boot script data from LockBox.\r
+\r
+**/\r
+VOID\r
+RestoreBootTimeDataFromLockBox (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       LockBoxLength;\r
+\r
+  //\r
+  // Restore boot time boot script data from LockBox.\r
+  //\r
+  LockBoxLength = mS3BootScriptTablePtr->BootTimeScriptLength;\r
+  Status        = RestoreLockBox (\r
+                    &mBootScriptDataBootTimeGuid,\r
+                    (VOID *)mS3BootScriptTablePtr->TableBase,\r
+                    &LockBoxLength\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Update the data to BootScriptData LockBox.\r
+  //\r
+  Status = UpdateLockBox (\r
+             &mBootScriptDataGuid,\r
+             0,\r
+             (VOID *)mS3BootScriptTablePtr->TableBase,\r
+             LockBoxLength\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Update TableLength.\r
+  //\r
+  mS3BootScriptTablePtr->TableLength = (UINT32)(mS3BootScriptTablePtr->BootTimeScriptLength - sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
 }\r
+\r
 /**\r
   To get the start address from which a new s3 boot script entry will write into.\r
-  \r
+\r
   @param EntryLength      the new entry length.\r
-  \r
-  @retval the address from which the a new s3 runtime script entry will write into \r
- **/ \r
-UINT8\r
+\r
+  @retval the address from which the a new s3 boot script entry will write into\r
+ **/\r
+UINT8 *\r
 S3BootScriptGetEntryAddAddress (\r
   UINT8  EntryLength\r
   )\r
 {\r
-  UINT8*                         NewEntryPtr;\r
-  EFI_BOOT_SCRIPT_TABLE_HEADER   TableHeader;\r
-  EFI_STATUS                     Status;\r
-  UINTN                          OrgLockBoxLength;\r
+  UINT8  *NewEntryPtr;\r
 \r
-  if (mS3BootScriptTablePtr->AtRuntime) {\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return NULL;\r
+  }\r
+\r
+  if (mS3BootScriptTablePtr->SmmLocked) {\r
     //\r
-    // We need check InSmm when AtRuntime, because after SmmReadyToLock, only SMM driver is allowed to write boot script.\r
+    // We need check InSmm, because after SmmReadyToLock, only SMM driver is allowed to write boot script.\r
     //\r
     if (!mS3BootScriptTablePtr->InSmm) {\r
       //\r
-      // Add DEBUG ERROR, so that we can find it at boot time.\r
-      // Do not use ASSERT, because we may have test invoke this interface.\r
+      // Add DEBUG ERROR, so that we can find it after SmmReadyToLock.\r
+      // Do not use ASSERT, because we may have test to invoke this interface.\r
       //\r
-      DEBUG ((EFI_D_ERROR, "FATAL ERROR: Set boot script after ReadyToLock!!!\n"));\r
+      DEBUG ((DEBUG_ERROR, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));\r
       return NULL;\r
     }\r
 \r
-    //\r
-    // NOTE: OS will restore ACPINvs data. After S3, the table length in mS3BootScriptTableSmmPtr (SMM) is different with\r
-    // table length in BootScriptTable header (ACPINvs).\r
-    // So here we need sync them. We choose ACPINvs table length, because we want to override the boot script saved\r
-    // in SMM every time.\r
-    //\r
-    ASSERT (mS3BootScriptTablePtr == mS3BootScriptTableSmmPtr);\r
-    CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
-    if (mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE) != TableHeader.TableLength) {\r
-      //\r
-      // Restore it to use original value\r
-      //\r
-      OrgLockBoxLength = mLockBoxLength;\r
-      Status = RestoreLockBox (\r
-                 &mBootScriptDataOrgGuid,\r
-                 (VOID *)mS3BootScriptTablePtr->TableBase,\r
-                 &OrgLockBoxLength\r
-                 );\r
-     ASSERT_EFI_ERROR (Status);\r
-     ASSERT (OrgLockBoxLength == mLockBoxLength);\r
-\r
-      //\r
-      // Update the current BootScriptData into LockBox as well\r
-      //\r
-      Status = UpdateLockBox (\r
-                 &mBootScriptDataGuid,\r
-                 0,\r
-                 (VOID *)mS3BootScriptTablePtr->TableBase,\r
-                 OrgLockBoxLength\r
-                 );\r
-      ASSERT_EFI_ERROR (Status);\r
-\r
+    if (mS3BootScriptTablePtr->BackFromS3) {\r
       //\r
-      // NOTE: We should NOT use TableHeader.TableLength, because it is already updated to be whole length.\r
+      // Back from S3, restore boot time boot script data from LockBox\r
+      // and set BackFromS3 flag back to FALSE.\r
       //\r
-      mS3BootScriptTablePtr->TableLength = (UINT32)(mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE));\r
+      RestoreBootTimeDataFromLockBox ();\r
+      mS3BootScriptTablePtr->BackFromS3 = FALSE;\r
     }\r
 \r
-    NewEntryPtr  = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);\r
+    NewEntryPtr = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);\r
+  } else {\r
+    NewEntryPtr = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);\r
+  }\r
 \r
-    if (EntryLength != 0) {\r
-      //\r
-      // Now the length field is updated, need sync to lockbox.\r
-      // So in S3 resume, the data can be restored correctly.\r
-      //\r
-      CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
-      Status = UpdateLockBox (\r
-                 &mBootScriptDataGuid,\r
-                 OFFSET_OF(EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),\r
-                 &TableHeader.TableLength,\r
-                 sizeof(TableHeader.TableLength)\r
-                 );\r
-      ASSERT_EFI_ERROR (Status);\r
-    }\r
-  } else {   \r
-    NewEntryPtr  = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);\r
-  }  \r
   return NewEntryPtr;\r
-  \r
-}  \r
+}\r
 \r
 /**\r
   Sync BootScript LockBox data.\r
@@ -602,21 +876,25 @@ S3BootScriptGetEntryAddAddress (
 **/\r
 VOID\r
 SyncBootScript (\r
-  IN UINT8      *Script\r
+  IN UINT8  *Script\r
   )\r
 {\r
   EFI_STATUS  Status;\r
-  UINTN       ScriptOffset;\r
+  UINT32      ScriptOffset;\r
+  UINT32      TotalScriptLength;\r
 \r
-  ScriptOffset = (UINTN) (Script - mS3BootScriptTablePtr->TableBase);\r
-\r
-  if (!mS3BootScriptTablePtr->AtRuntime || !mS3BootScriptTablePtr->InSmm || ScriptOffset >= mLockBoxLength) {\r
+  if (!mS3BootScriptTablePtr->SmmLocked || !mS3BootScriptTablePtr->InSmm) {\r
     //\r
-    // If it is not at runtime in SMM or in the range that needs to be synced in LockBox, just return.\r
+    // If it is not after SmmReadyToLock in SMM,\r
+    // just return.\r
     //\r
-    return ;\r
+    return;\r
   }\r
 \r
+  ScriptOffset = (UINT32)(Script - mS3BootScriptTablePtr->TableBase);\r
+\r
+  TotalScriptLength = (UINT32)(mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
+\r
   //\r
   // Update BootScriptData\r
   // So in S3 resume, the data can be restored correctly.\r
@@ -625,85 +903,100 @@ SyncBootScript (
              &mBootScriptDataGuid,\r
              ScriptOffset,\r
              (VOID *)((UINTN)mS3BootScriptTablePtr->TableBase + ScriptOffset),\r
-             mLockBoxLength - ScriptOffset\r
+             TotalScriptLength - ScriptOffset\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Now the length field is updated, need sync to lockbox.\r
+  // So at S3 resume, the data can be restored correctly.\r
+  //\r
+  Status = UpdateLockBox (\r
+             &mBootScriptDataGuid,\r
+             OFFSET_OF (EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),\r
+             &TotalScriptLength,\r
+             sizeof (TotalScriptLength)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
-/** \r
-  This is an function to close the S3 boot script table. The function could only be called in \r
-  BOOT time phase. To comply with the Framework spec definition on \r
+/**\r
+  This is an function to close the S3 boot script table. The function could only be called in\r
+  BOOT time phase. To comply with the Framework spec definition on\r
   EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:\r
   1. Closes the specified boot script table\r
-  2. It allocates a new memory pool to duplicate all the boot scripts in the specified table. \r
-     Once this function is called, the table maintained by the library will be destroyed \r
+  2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.\r
+     Once this function is called, the table maintained by the library will be destroyed\r
      after it is copied into the allocated pool.\r
-  3. Any attempts to add a script record after calling this function will cause a new table \r
+  3. Any attempts to add a script record after calling this function will cause a new table\r
      to be created by the library.\r
-  4. The base address of the allocated pool will be returned in Address. Note that after \r
+  4. The base address of the allocated pool will be returned in Address. Note that after\r
      using the boot script table, the CALLER is responsible for freeing the pool that is allocated\r
-     by this function. \r
+     by this function.\r
 \r
-  In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is \r
+  In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is\r
   for Framework Spec compatibility.\r
-  \r
-  If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out \r
-  how to get the script to run on an S3 resume because the boot script maintained by the lib will be \r
+\r
+  If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out\r
+  how to get the script to run at S3 resume because the boot script maintained by the lib will be\r
   destroyed.\r
\r
-  @return the base address of the new copy of the boot script tble.   \r
+\r
+  @return the base address of the new copy of the boot script table.\r
   @note this function could only called in boot time phase\r
 \r
 **/\r
-UINT8*\r
+UINT8 *\r
 EFIAPI\r
 S3BootScriptCloseTable (\r
   VOID\r
   )\r
 {\r
-  UINT8                          *S3TableBase;\r
-  UINT32                          TableLength;\r
-  UINT8                          *Buffer;\r
-  EFI_STATUS                      Status;\r
-  EFI_BOOT_SCRIPT_TABLE_HEADER      *ScriptTableInfo;\r
-  \r
-  S3TableBase =    mS3BootScriptTablePtr->TableBase;    \r
+  UINT8                         *S3TableBase;\r
+  UINT32                        TableLength;\r
+  UINT8                         *Buffer;\r
+  EFI_STATUS                    Status;\r
+  EFI_BOOT_SCRIPT_TABLE_HEADER  *ScriptTableInfo;\r
+\r
+  S3TableBase =    mS3BootScriptTablePtr->TableBase;\r
   if (S3TableBase == 0) {\r
-    return 0; \r
+    return 0;\r
   }\r
+\r
   //\r
   // Append the termination record the S3 boot script table\r
   //\r
-  S3BootScriptInternalCloseTable();\r
+  S3BootScriptInternalCloseTable ();\r
   TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
   //\r
-  // Allocate the buffer and copy the boot script to the buffer. \r
+  // Allocate the buffer and copy the boot script to the buffer.\r
   //\r
   Status = gBS->AllocatePool (\r
                   EfiBootServicesData,\r
                   (UINTN)TableLength,\r
-                  (VOID **) &Buffer\r
+                  (VOID **)&Buffer\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-        return 0; \r
+    return 0;\r
   }\r
+\r
   CopyMem (Buffer, S3TableBase, TableLength);\r
-  \r
+\r
   //\r
-  // Destroy the table maintained by the library so that the next write operation \r
+  // Destroy the table maintained by the library so that the next write operation\r
   // will write the record to the first entry of the table.\r
   //\r
   // Fill the table header.\r
-  ScriptTableInfo                    = (EFI_BOOT_SCRIPT_TABLE_HEADER*)S3TableBase;\r
+  ScriptTableInfo              = (EFI_BOOT_SCRIPT_TABLE_HEADER *)S3TableBase;\r
   ScriptTableInfo->OpCode      = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;\r
-  ScriptTableInfo->Length      = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
+  ScriptTableInfo->Length      = (UINT8)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
   ScriptTableInfo->TableLength = 0;   // will be calculate at close the table\r
-  \r
+\r
   mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
   return Buffer;\r
 }\r
+\r
 /**\r
-  Save I/O write to boot script \r
+  Save I/O write to boot script\r
 \r
   @param Width   The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
   @param Address The base address of the I/O operations.\r
@@ -711,30 +1004,45 @@ S3BootScriptCloseTable (
   @param Buffer  The source buffer from which to write data.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveIoWrite (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH          Width,\r
-  IN  UINT64                            Address,\r
-  IN  UINTN                             Count,\r
-  IN  VOID                              *Buffer\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  UINTN                     Count,\r
+  IN  VOID                      *Buffer\r
   )\r
 \r
 {\r
   UINT8                     Length;\r
-  UINT8                    *Script;\r
+  UINT8                     *Script;\r
   UINT8                     WidthInByte;\r
   EFI_BOOT_SCRIPT_IO_WRITE  ScriptIoWrite;\r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if ((Count > MAX_UINT8) ||\r
+      (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_IO_WRITE)))\r
+  {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // save script data\r
   //\r
@@ -742,9 +1050,9 @@ S3BootScriptSaveIoWrite (
   ScriptIoWrite.Length  = Length;\r
   ScriptIoWrite.Width   = Width;\r
   ScriptIoWrite.Address = Address;\r
-  ScriptIoWrite.Count   = (UINT32) Count;\r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptIoWrite, sizeof(EFI_BOOT_SCRIPT_IO_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE)), Buffer, WidthInByte * Count);\r
+  ScriptIoWrite.Count   = (UINT32)Count;\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptIoWrite, sizeof (EFI_BOOT_SCRIPT_IO_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_WRITE)), Buffer, WidthInByte * Count);\r
 \r
   SyncBootScript (Script);\r
 \r
@@ -760,29 +1068,34 @@ S3BootScriptSaveIoWrite (
   @param DataMask  A pointer to the data mask to be AND-ed with the data read from the register\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveIoReadWrite (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH         Width,\r
-  IN  UINT64                           Address,\r
-  IN  VOID                            *Data,\r
-  IN  VOID                            *DataMask\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  VOID                      *Data,\r
+  IN  VOID                      *DataMask\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
+  UINT8                          Length;\r
+  UINT8                          *Script;\r
+  UINT8                          WidthInByte;\r
   EFI_BOOT_SCRIPT_IO_READ_WRITE  ScriptIoReadWrite;\r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
@@ -790,15 +1103,16 @@ S3BootScriptSaveIoReadWrite (
   ScriptIoReadWrite.Length  = Length;\r
   ScriptIoReadWrite.Width   = Width;\r
   ScriptIoReadWrite.Address = Address;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptIoReadWrite, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE)), Data, WidthInByte);\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptIoReadWrite, sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE)), Data, WidthInByte);\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
 \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for a memory write operation into a specified boot script table.\r
 \r
@@ -808,45 +1122,61 @@ S3BootScriptSaveIoReadWrite (
   @param Buffer  The source buffer from which to write the data.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveMemWrite (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH          Width,\r
-  IN  UINT64                            Address,\r
-  IN  UINTN                             Count,\r
-  IN  VOID                              *Buffer\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  UINTN                     Count,\r
+  IN  VOID                      *Buffer\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
+  UINT8                      Length;\r
+  UINT8                      *Script;\r
+  UINT8                      WidthInByte;\r
   EFI_BOOT_SCRIPT_MEM_WRITE  ScriptMemWrite;\r
-  \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if ((Count > MAX_UINT8) ||\r
+      (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)))\r
+  {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptMemWrite.OpCode   = EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE;\r
-  ScriptMemWrite.Length   = Length;\r
-  ScriptMemWrite.Width    = Width;\r
-  ScriptMemWrite.Address  = Address;\r
-  ScriptMemWrite.Count    = (UINT32) Count;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptMemWrite, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)), Buffer, WidthInByte * Count);\r
-  \r
+  ScriptMemWrite.OpCode  = EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE;\r
+  ScriptMemWrite.Length  = Length;\r
+  ScriptMemWrite.Width   = Width;\r
+  ScriptMemWrite.Address = Address;\r
+  ScriptMemWrite.Count   = (UINT32)Count;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptMemWrite, sizeof (EFI_BOOT_SCRIPT_MEM_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)), Buffer, WidthInByte * Count);\r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for a memory modify operation into a specified boot script table.\r
 \r
@@ -856,45 +1186,51 @@ S3BootScriptSaveMemWrite (
   @param DataMask  A pointer to the data mask to be AND-ed with the data read from the register.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveMemReadWrite (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH          Width,\r
-  IN  UINT64                            Address,\r
-  IN  VOID                              *Data,\r
-  IN  VOID                              *DataMask\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  VOID                      *Data,\r
+  IN  VOID                      *DataMask\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
+  UINT8                           Length;\r
+  UINT8                           *Script;\r
+  UINT8                           WidthInByte;\r
   EFI_BOOT_SCRIPT_MEM_READ_WRITE  ScriptMemReadWrite;\r
\r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
+\r
   //\r
   // Build script data\r
-  //    \r
-  ScriptMemReadWrite.OpCode   = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;\r
-  ScriptMemReadWrite.Length   = Length;\r
-  ScriptMemReadWrite.Width    = Width;\r
-  ScriptMemReadWrite.Address  = Address;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptMemReadWrite , sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE)), Data, WidthInByte);\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
+  //\r
+  ScriptMemReadWrite.OpCode  = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;\r
+  ScriptMemReadWrite.Length  = Length;\r
+  ScriptMemReadWrite.Width   = Width;\r
+  ScriptMemReadWrite.Address = Address;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptMemReadWrite, sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE)), Data, WidthInByte);\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
 \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for a PCI configuration space write operation into a specified boot script table.\r
 \r
@@ -904,45 +1240,70 @@ S3BootScriptSaveMemReadWrite (
   @param Buffer    The source buffer from which to write the data.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSavePciCfgWrite (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH         Width,\r
-  IN  UINT64                           Address,\r
-  IN  UINTN                            Count,\r
-  IN  VOID                            *Buffer\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  UINTN                     Count,\r
+  IN  VOID                      *Buffer\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
+  UINT8                             Length;\r
+  UINT8                             *Script;\r
+  UINT8                             WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE  ScriptPciWrite;\r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if ((Width == S3BootScriptWidthUint64) ||\r
+      (Width == S3BootScriptWidthFifoUint64) ||\r
+      (Width == S3BootScriptWidthFillUint64))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if ((Count > MAX_UINT8) ||\r
+      (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)))\r
+  {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptPciWrite.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE;\r
-  ScriptPciWrite.Length   = Length;\r
-  ScriptPciWrite.Width    = Width;\r
-  ScriptPciWrite.Address  = Address;\r
-  ScriptPciWrite.Count    = (UINT32) Count;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite,  sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)), Buffer, WidthInByte * Count);\r
-  \r
+  ScriptPciWrite.OpCode  = EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE_OPCODE;\r
+  ScriptPciWrite.Length  = Length;\r
+  ScriptPciWrite.Width   = Width;\r
+  ScriptPciWrite.Address = Address;\r
+  ScriptPciWrite.Count   = (UINT32)Count;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptPciWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)), Buffer, WidthInByte * Count);\r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for a PCI configuration space modify operation into a specified boot script table.\r
 \r
@@ -952,41 +1313,55 @@ S3BootScriptSavePciCfgWrite (
   @param DataMask    A pointer to the data mask to be AND-ed.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN__SUCCESS           Opcode is added.\r
+  @retval RETURN__SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSavePciCfgReadWrite (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH          Width,\r
-  IN  UINT64                            Address,\r
-  IN  VOID                              *Data,\r
-  IN  VOID                              *DataMask\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  VOID                      *Data,\r
+  IN  VOID                      *DataMask\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
+  UINT8                                  Length;\r
+  UINT8                                  *Script;\r
+  UINT8                                  WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE  ScriptPciReadWrite;\r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if ((Width == S3BootScriptWidthUint64) ||\r
+      (Width == S3BootScriptWidthFifoUint64) ||\r
+      (Width == S3BootScriptWidthFillUint64))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
-  // \r
-  ScriptPciReadWrite.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;\r
-  ScriptPciReadWrite.Length   = Length;\r
-  ScriptPciReadWrite.Width    = Width;\r
-  ScriptPciReadWrite.Address  = Address;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE)), Data, WidthInByte);\r
+  //\r
+  ScriptPciReadWrite.OpCode  = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;\r
+  ScriptPciReadWrite.Length  = Length;\r
+  ScriptPciReadWrite.Width   = Width;\r
+  ScriptPciReadWrite.Address = Address;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptPciReadWrite, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE)), Data, WidthInByte);\r
   CopyMem (\r
-    (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + WidthInByte),\r
+    (VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + WidthInByte),\r
     DataMask,\r
     WidthInByte\r
     );\r
@@ -995,6 +1370,7 @@ S3BootScriptSavePciCfgReadWrite (
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for a PCI configuration 2 space write operation into a specified boot script table.\r
 \r
@@ -1004,48 +1380,73 @@ S3BootScriptSavePciCfgReadWrite (
   @param Count     The number of PCI operations to perform.\r
   @param Buffer    The source buffer from which to write the data.\r
 \r
-  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
-**/\r
-RETURN_STATUS\r
-EFIAPI\r
-S3BootScriptSavePciCfg2Write (\r
-  IN S3_BOOT_SCRIPT_LIB_WIDTH        Width,\r
-  IN UINT16                          Segment,\r
-  IN UINT64                          Address,\r
-  IN UINTN                           Count,\r
-  IN VOID                           *Buffer\r
-  )\r
-{\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
-  EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE  ScriptPciWrite2;\r
-  \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
+  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+S3BootScriptSavePciCfg2Write (\r
+  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN UINT16                    Segment,\r
+  IN UINT64                    Address,\r
+  IN UINTN                     Count,\r
+  IN VOID                      *Buffer\r
+  )\r
+{\r
+  UINT8                              Length;\r
+  UINT8                              *Script;\r
+  UINT8                              WidthInByte;\r
+  EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE  ScriptPciWrite2;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if ((Width == S3BootScriptWidthUint64) ||\r
+      (Width == S3BootScriptWidthFifoUint64) ||\r
+      (Width == S3BootScriptWidthFillUint64))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if ((Count > MAX_UINT8) ||\r
+      (WidthInByte * Count > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)))\r
+  {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptPciWrite2.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE;\r
-  ScriptPciWrite2.Length   = Length;\r
-  ScriptPciWrite2.Width    = Width;\r
-  ScriptPciWrite2.Address  = Address;\r
-  ScriptPciWrite2.Segment  = Segment;\r
-  ScriptPciWrite2.Count    = (UINT32)Count;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)), Buffer, WidthInByte * Count);\r
+  ScriptPciWrite2.OpCode  = EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE;\r
+  ScriptPciWrite2.Length  = Length;\r
+  ScriptPciWrite2.Width   = Width;\r
+  ScriptPciWrite2.Address = Address;\r
+  ScriptPciWrite2.Segment = Segment;\r
+  ScriptPciWrite2.Count   = (UINT32)Count;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptPciWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)), Buffer, WidthInByte * Count);\r
 \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for a PCI configuration 2 space modify operation into a specified boot script table.\r
 \r
@@ -1056,51 +1457,172 @@ S3BootScriptSavePciCfg2Write (
   @param DataMask    A pointer to the data mask to be AND-ed.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSavePciCfg2ReadWrite (\r
-  IN S3_BOOT_SCRIPT_LIB_WIDTH        Width,\r
-  IN UINT16                          Segment,\r
-  IN UINT64                          Address,\r
-  IN VOID                           *Data,\r
-  IN VOID                           *DataMask\r
+  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN UINT16                    Segment,\r
+  IN UINT64                    Address,\r
+  IN VOID                      *Data,\r
+  IN VOID                      *DataMask\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte;\r
+  UINT8                                   Length;\r
+  UINT8                                   *Script;\r
+  UINT8                                   WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE  ScriptPciReadWrite2;\r
-  \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if ((Width == S3BootScriptWidthUint64) ||\r
+      (Width == S3BootScriptWidthFifoUint64) ||\r
+      (Width == S3BootScriptWidthFillUint64))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptPciReadWrite2.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE;\r
-  ScriptPciReadWrite2.Length   = Length;\r
-  ScriptPciReadWrite2.Width    = Width;\r
-  ScriptPciReadWrite2.Segment  = Segment;\r
-  ScriptPciReadWrite2.Address  = Address;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE)), Data, WidthInByte);\r
+  ScriptPciReadWrite2.OpCode  = EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE;\r
+  ScriptPciReadWrite2.Length  = Length;\r
+  ScriptPciReadWrite2.Width   = Width;\r
+  ScriptPciReadWrite2.Segment = Segment;\r
+  ScriptPciReadWrite2.Address = Address;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptPciReadWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE)), Data, WidthInByte);\r
   CopyMem (\r
-    (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + WidthInByte),\r
+    (VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + WidthInByte),\r
     DataMask,\r
     WidthInByte\r
     );\r
-  \r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Checks the parameter of S3BootScriptSaveSmbusExecute().\r
+\r
+  This function checks the input parameters of SmbusExecute().  If the input parameters are valid\r
+  for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain\r
+  error code based on the input SMBus bus protocol.\r
+\r
+  @param  SmBusAddress            Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length,\r
+                                  and PEC.\r
+  @param  Operation               Signifies which particular SMBus hardware protocol instance that\r
+                                  it will use to execute the SMBus transactions. This SMBus\r
+                                  hardware protocol is defined by the SMBus Specification and is\r
+                                  not related to EFI.\r
+  @param  Length                  Signifies the number of bytes that this operation will do. The\r
+                                  maximum number of bytes can be revision specific and operation\r
+                                  specific. This field will contain the actual number of bytes that\r
+                                  are executed for this operation. Not all operations require this\r
+                                  argument.\r
+  @param  Buffer                  Contains the value of data to execute to the SMBus slave device.\r
+                                  Not all operations require this argument. The length of this\r
+                                  buffer is identified by Length.\r
+\r
+  @retval EFI_SUCCESS             All the parameters are valid for the corresponding SMBus bus\r
+                                  protocol.\r
+  @retval EFI_INVALID_PARAMETER   Operation is not defined in EFI_SMBUS_OPERATION.\r
+  @retval EFI_INVALID_PARAMETER   Length/Buffer is NULL for operations except for EfiSmbusQuickRead\r
+                                  and EfiSmbusQuickWrite. Length is outside the range of valid\r
+                                  values.\r
+  @retval EFI_UNSUPPORTED         The SMBus operation or PEC is not supported.\r
+  @retval EFI_BUFFER_TOO_SMALL    Buffer is not sufficient for this operation.\r
+\r
+**/\r
+EFI_STATUS\r
+CheckParameters (\r
+  IN     UINTN                SmBusAddress,\r
+  IN     EFI_SMBUS_OPERATION  Operation,\r
+  IN OUT UINTN                *Length,\r
+  IN     VOID                 *Buffer\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  UINTN                     RequiredLen;\r
+  EFI_SMBUS_DEVICE_COMMAND  Command;\r
+  BOOLEAN                   PecCheck;\r
+\r
+  Command  = SMBUS_LIB_COMMAND (SmBusAddress);\r
+  PecCheck = SMBUS_LIB_PEC (SmBusAddress);\r
+  //\r
+  // Set default value to be 2:\r
+  // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.\r
+  //\r
+  RequiredLen = 2;\r
+  Status      = EFI_SUCCESS;\r
+  switch (Operation) {\r
+    case EfiSmbusQuickRead:\r
+    case EfiSmbusQuickWrite:\r
+      if (PecCheck || (Command != 0)) {\r
+        return EFI_UNSUPPORTED;\r
+      }\r
+\r
+      break;\r
+    case EfiSmbusReceiveByte:\r
+    case EfiSmbusSendByte:\r
+      if (Command != 0) {\r
+        return EFI_UNSUPPORTED;\r
+      }\r
+\r
+    //\r
+    // Cascade to check length parameter.\r
+    //\r
+    case EfiSmbusReadByte:\r
+    case EfiSmbusWriteByte:\r
+      RequiredLen = 1;\r
+    //\r
+    // Cascade to check length parameter.\r
+    //\r
+    case EfiSmbusReadWord:\r
+    case EfiSmbusWriteWord:\r
+    case EfiSmbusProcessCall:\r
+      if ((Buffer == NULL) || (Length == NULL)) {\r
+        return EFI_INVALID_PARAMETER;\r
+      } else if (*Length < RequiredLen) {\r
+        Status = EFI_BUFFER_TOO_SMALL;\r
+      }\r
+\r
+      *Length = RequiredLen;\r
+      break;\r
+    case EfiSmbusReadBlock:\r
+    case EfiSmbusWriteBlock:\r
+    case EfiSmbusBWBRProcessCall:\r
+      if ((Buffer == NULL) ||\r
+          (Length == NULL) ||\r
+          (*Length < MIN_SMBUS_BLOCK_LEN) ||\r
+          (*Length > MAX_SMBUS_BLOCK_LEN))\r
+      {\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+\r
+      break;\r
+    default:\r
+      return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Adds a record for an SMBus command execution into a specified boot script table.\r
 \r
@@ -1109,130 +1631,173 @@ S3BootScriptSavePciCfg2ReadWrite (
                         transactions.\r
   @param Length         A pointer to signify the number of bytes that this operation will do.\r
   @param Buffer         Contains the value of data to execute to the SMBUS slave device.\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveSmbusExecute (\r
-  IN  UINTN                             SmBusAddress, \r
-  IN  EFI_SMBUS_OPERATION               Operation,\r
-  IN  UINTN                             *Length,\r
-  IN  VOID                              *Buffer\r
+  IN  UINTN                SmBusAddress,\r
+  IN  EFI_SMBUS_OPERATION  Operation,\r
+  IN  UINTN                *Length,\r
+  IN  VOID                 *Buffer\r
   )\r
 {\r
-  UINT8                 DataSize;\r
-  UINT8                *Script;\r
+  EFI_STATUS                     Status;\r
+  UINTN                          BufferLength;\r
+  UINT8                          DataSize;\r
+  UINT8                          *Script;\r
   EFI_BOOT_SCRIPT_SMBUS_EXECUTE  ScriptSmbusExecute;\r
 \r
-  DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + (*Length));\r
-  \r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if (Length == NULL) {\r
+    BufferLength = 0;\r
+  } else {\r
+    BufferLength = *Length;\r
+  }\r
+\r
+  Status = CheckParameters (SmBusAddress, Operation, &BufferLength, Buffer);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if (BufferLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + BufferLength);\r
+\r
   Script = S3BootScriptGetEntryAddAddress (DataSize);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
   ScriptSmbusExecute.OpCode       = EFI_BOOT_SCRIPT_SMBUS_EXECUTE_OPCODE;\r
   ScriptSmbusExecute.Length       = DataSize;\r
-  ScriptSmbusExecute.SmBusAddress = (UINT64) SmBusAddress;\r
+  ScriptSmbusExecute.SmBusAddress = (UINT64)SmBusAddress;\r
   ScriptSmbusExecute.Operation    = Operation;\r
-  ScriptSmbusExecute.DataSize     = (UINT32) *Length;\r
+  ScriptSmbusExecute.DataSize     = (UINT32)BufferLength;\r
 \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));\r
   CopyMem (\r
-    (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),\r
+    (VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),\r
     Buffer,\r
-    (*Length)\r
+    BufferLength\r
     );\r
 \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for an execution stall on the processor into a specified boot script table.\r
 \r
   @param Duration   Duration in microseconds of the stall\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveStall (\r
-  IN  UINTN                             Duration\r
+  IN  UINTN  Duration\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
+  UINT8                  Length;\r
+  UINT8                  *Script;\r
   EFI_BOOT_SCRIPT_STALL  ScriptStall;\r
 \r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_STALL));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptStall.OpCode    = EFI_BOOT_SCRIPT_STALL_OPCODE;\r
-  ScriptStall.Length    = Length;\r
-  ScriptStall.Duration  = Duration;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));\r
-  \r
+  ScriptStall.OpCode   = EFI_BOOT_SCRIPT_STALL_OPCODE;\r
+  ScriptStall.Length   = Length;\r
+  ScriptStall.Duration = Duration;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));\r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
 \r
   @param EntryPoint   Entry point of the code to be dispatched.\r
   @param Context      Argument to be passed into the EntryPoint of the code to be dispatched.\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveDispatch2 (\r
-  IN  VOID                      *EntryPoint,\r
-  IN  VOID                      *Context\r
+  IN  VOID  *EntryPoint,\r
+  IN  VOID  *Context\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                 *Script;\r
+  UINT8                       Length;\r
+  UINT8                       *Script;\r
   EFI_BOOT_SCRIPT_DISPATCH_2  ScriptDispatch2;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
   ScriptDispatch2.OpCode     = EFI_BOOT_SCRIPT_DISPATCH_2_OPCODE;\r
   ScriptDispatch2.Length     = Length;\r
   ScriptDispatch2.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
-  ScriptDispatch2.Context =   (EFI_PHYSICAL_ADDRESS)(UINTN)Context;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
-  \r
+  ScriptDispatch2.Context    =   (EFI_PHYSICAL_ADDRESS)(UINTN)Context;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
-\r
 }\r
+\r
 /**\r
   Adds a record for memory reads of the memory location and continues when the exit criteria is\r
   satisfied or after a defined duration.\r
-  \r
+\r
+  Please aware, below interface is different with PI specification, Vol 5:\r
+  EFI_S3_SAVE_STATE_PROTOCOL.Write() for EFI_BOOT_SCRIPT_MEM_POLL_OPCODE.\r
+  "Duration" below is microseconds, while "Delay" in PI specification means\r
+  the number of 100ns units to poll.\r
+\r
   @param Width     The width of the memory operations.\r
   @param Address   The base address of the memory operations.\r
   @param BitMask   A pointer to the bit mask to be AND-ed with the data read from the register.\r
@@ -1241,160 +1806,183 @@ S3BootScriptSaveDispatch2 (
   @param LoopTimes The times of the register polling.\r
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 \r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveMemPoll (\r
-  IN  S3_BOOT_SCRIPT_LIB_WIDTH          Width,\r
-  IN  UINT64                            Address,\r
-  IN  VOID                              *BitMask,\r
-  IN  VOID                              *BitValue,\r
-  IN  UINTN                             Duration,\r
-  IN  UINTN                             LoopTimes\r
+  IN  S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN  UINT64                    Address,\r
+  IN  VOID                      *BitMask,\r
+  IN  VOID                      *BitValue,\r
+  IN  UINTN                     Duration,\r
+  IN  UINT64                    LoopTimes\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
-  UINT8                 WidthInByte; \r
-  EFI_BOOT_SCRIPT_MEM_POLL      ScriptMemPoll; \r
+  UINT8                     Length;\r
+  UINT8                     *Script;\r
+  UINT8                     WidthInByte;\r
+  EFI_BOOT_SCRIPT_MEM_POLL  ScriptMemPoll;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  \r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptMemPoll.OpCode   = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;\r
-  ScriptMemPoll.Length   = Length;\r
-  ScriptMemPoll.Width    = Width;  \r
-  ScriptMemPoll.Address  = Address;\r
-  ScriptMemPoll.Duration = Duration;\r
+  ScriptMemPoll.OpCode    = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;\r
+  ScriptMemPoll.Length    = Length;\r
+  ScriptMemPoll.Width     = Width;\r
+  ScriptMemPoll.Address   = Address;\r
+  ScriptMemPoll.Duration  = Duration;\r
   ScriptMemPoll.LoopTimes = LoopTimes;\r
 \r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL)), BitValue, WidthInByte);\r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + WidthInByte), BitMask, WidthInByte);\r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL)); \r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL)), BitValue, WidthInByte);\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + WidthInByte), BitMask, WidthInByte);\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL));\r
 \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only\r
   used for debugging script issues.\r
-  \r
+\r
   @param InformationLength   Length of the data in bytes\r
   @param Information       Information to be logged in the boot scrpit\r
\r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 \r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveInformation (\r
-  IN  UINT32                                InformationLength, \r
-  IN  VOID                                 *Information\r
+  IN  UINT32  InformationLength,\r
+  IN  VOID    *Information\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                 *Script;\r
+  UINT8                        Length;\r
+  UINT8                        *Script;\r
   EFI_BOOT_SCRIPT_INFORMATION  ScriptInformation;\r
 \r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if (InformationLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_INFORMATION)) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
 \r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptInformation.OpCode     = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;\r
-  ScriptInformation.Length     = Length;\r
-\r
+  ScriptInformation.OpCode = EFI_BOOT_SCRIPT_INFORMATION_OPCODE;\r
+  ScriptInformation.Length = Length;\r
 \r
-  ScriptInformation.InformationLength = InformationLength;  \r
+  ScriptInformation.InformationLength = InformationLength;\r
 \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *)Information, (UINTN)InformationLength);\r
 \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
-\r
 }\r
+\r
 /**\r
   Store a string in the boot script table. This opcode is a no-op on dispatch and is only\r
   used for debugging script issues.\r
-  \r
+\r
   @param String            The string to save to boot script table\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 \r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveInformationAsciiString (\r
-  IN  CONST CHAR8               *String\r
+  IN  CONST CHAR8  *String\r
   )\r
 {\r
-  return S3BootScriptSaveInformation (      \r
-           (UINT32) AsciiStrLen (String) + 1, \r
-           (VOID*) String\r
+  return S3BootScriptSaveInformation (\r
+           (UINT32)AsciiStrLen (String) + 1,\r
+           (VOID *)String\r
            );\r
 }\r
+\r
 /**\r
   Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
 \r
   @param EntryPoint   Entry point of the code to be dispatched.\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveDispatch (\r
-  IN  VOID                              *EntryPoint\r
+  IN  VOID  *EntryPoint\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                *Script;\r
+  UINT8                     Length;\r
+  UINT8                     *Script;\r
   EFI_BOOT_SCRIPT_DISPATCH  ScriptDispatch;\r
-  \r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
   ScriptDispatch.OpCode     = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;\r
   ScriptDispatch.Length     = Length;\r
   ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH)); \r
-  \r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
-\r
 }\r
+\r
 /**\r
   Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a\r
   defined duration.\r
-  \r
-  @param  Width                 The width of the I/O operations. \r
+\r
+  @param  Width                 The width of the I/O operations.\r
   @param  Address               The base address of the I/O operations.\r
   @param  Data                  The comparison value used for the polling exit criteria.\r
   @param  DataMask              Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
@@ -1403,45 +1991,49 @@ S3BootScriptSaveDispatch (
                                 granularity so the delay may be longer.\r
 \r
  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
- @retval RETURN_SUCCESS          Opcode is added.\r
+ @retval RETURN_SUCCESS          Opcode is added or no action is required as ACPI S3 was disabled.\r
 \r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveIoPoll (\r
-  IN S3_BOOT_SCRIPT_LIB_WIDTH       Width,\r
-  IN UINT64                     Address,\r
+  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN UINT64                    Address,\r
   IN VOID                      *Data,\r
-  IN VOID                      *DataMask, \r
-  IN UINT64                     Delay   \r
+  IN VOID                      *DataMask,\r
+  IN UINT64                    Delay\r
   )\r
 {\r
-  UINT8                 WidthInByte;  \r
-  UINT8                *Script;\r
-  UINT8                 Length;\r
+  UINT8                    WidthInByte;\r
+  UINT8                    *Script;\r
+  UINT8                    Length;\r
   EFI_BOOT_SCRIPT_IO_POLL  ScriptIoPoll;\r
-  \r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));  \r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptIoPoll.OpCode   = EFI_BOOT_SCRIPT_IO_POLL_OPCODE;\r
-  ScriptIoPoll.Length   = (UINT8) (sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
-  ScriptIoPoll.Width    = Width;  \r
-  ScriptIoPoll.Address  = Address;\r
-  ScriptIoPoll.Delay    = Delay;\r
+  ScriptIoPoll.OpCode  = EFI_BOOT_SCRIPT_IO_POLL_OPCODE;\r
+  ScriptIoPoll.Length  = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
+  ScriptIoPoll.Width   = Width;\r
+  ScriptIoPoll.Address = Address;\r
+  ScriptIoPoll.Delay   = Delay;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptIoPoll, sizeof (EFI_BOOT_SCRIPT_IO_POLL));\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL)), Data, WidthInByte);\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL) + WidthInByte), DataMask, WidthInByte);\r
 \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptIoPoll, sizeof (EFI_BOOT_SCRIPT_IO_POLL));  \r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL)), Data, WidthInByte);\r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_IO_POLL) + WidthInByte), DataMask, WidthInByte);\r
-  \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
@@ -1451,7 +2043,7 @@ S3BootScriptSaveIoPoll (
   Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
   after a defined duration.\r
 \r
-  @param  Width                 The width of the I/O operations. \r
+  @param  Width                 The width of the I/O operations.\r
   @param  Address               The address within the PCI configuration space.\r
   @param  Data                  The comparison value used for the polling exit criteria.\r
   @param  DataMask              Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
@@ -1460,53 +2052,67 @@ S3BootScriptSaveIoPoll (
                                 granularity so the delay may be longer.\r
 \r
  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
- @retval RETURN_SUCCESS           Opcode is added.\r
+ @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
 \r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSavePciPoll (\r
-   IN S3_BOOT_SCRIPT_LIB_WIDTH   Width,\r
-   IN UINT64                     Address,\r
-   IN VOID                      *Data,\r
-   IN VOID                      *DataMask,\r
-   IN UINT64                     Delay\r
-)\r
+  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN UINT64                    Address,\r
+  IN VOID                      *Data,\r
+  IN VOID                      *DataMask,\r
+  IN UINT64                    Delay\r
+  )\r
 {\r
-  UINT8                   *Script;\r
-  UINT8                    WidthInByte;  \r
-  UINT8                    Length;\r
+  UINT8                            *Script;\r
+  UINT8                            WidthInByte;\r
+  UINT8                            Length;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG_POLL  ScriptPciPoll;\r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
-  \r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if ((Width == S3BootScriptWidthUint64) ||\r
+      (Width == S3BootScriptWidthFifoUint64) ||\r
+      (Width == S3BootScriptWidthFillUint64))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptPciPoll.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE;\r
-  ScriptPciPoll.Length   = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
-  ScriptPciPoll.Width    = Width;  \r
-  ScriptPciPoll.Address  = Address;\r
-  ScriptPciPoll.Delay    = Delay;\r
+  ScriptPciPoll.OpCode  = EFI_BOOT_SCRIPT_PCI_CONFIG_POLL_OPCODE;\r
+  ScriptPciPoll.Length  = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
+  ScriptPciPoll.Width   = Width;\r
+  ScriptPciPoll.Address = Address;\r
+  ScriptPciPoll.Delay   = Delay;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);\r
 \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));\r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);\r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);\r
-  \r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
   after a defined duration.\r
 \r
-  @param  Width                 The width of the I/O operations. \r
+  @param  Width                 The width of the I/O operations.\r
   @param  Segment               The PCI segment number for Address.\r
   @param  Address               The address within the PCI configuration space.\r
   @param  Data                  The comparison value used for the polling exit criteria.\r
@@ -1516,119 +2122,133 @@ S3BootScriptSavePciPoll (
                                 granularity so the delay may be longer.\r
 \r
  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
- @retval RETURN_SUCCESS           Opcode is added.\r
- @note   A known Limitations in the implementation: When interpreting the opcode  EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE\r
-         EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE and EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE, the 'Segment' parameter is assumed as \r
-         Zero, or else, assert.\r
+ @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSavePci2Poll (\r
-   IN S3_BOOT_SCRIPT_LIB_WIDTH      Width,\r
-   IN UINT16                        Segment,\r
-   IN UINT64                        Address,\r
-   IN VOID                         *Data,\r
-   IN VOID                         *DataMask,\r
-  IN UINT64                         Delay\r
-)\r
+  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,\r
+  IN UINT16                    Segment,\r
+  IN UINT64                    Address,\r
+  IN VOID                      *Data,\r
+  IN VOID                      *DataMask,\r
+  IN UINT64                    Delay\r
+  )\r
 {\r
-  UINT8                    WidthInByte;  \r
-  UINT8                   *Script;\r
-  UINT8                    Length;\r
+  UINT8                             WidthInByte;\r
+  UINT8                             *Script;\r
+  UINT8                             Length;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL  ScriptPci2Poll;\r
-  \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
-  \r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  if ((Width == S3BootScriptWidthUint64) ||\r
+      (Width == S3BootScriptWidthFifoUint64) ||\r
+      (Width == S3BootScriptWidthFillUint64))\r
+  {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
+  Length      = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptPci2Poll.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE;\r
-  ScriptPci2Poll.Length   = (UINT8) (sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
-  ScriptPci2Poll.Width    = Width; \r
-  ScriptPci2Poll.Segment  = Segment;\r
-  ScriptPci2Poll.Address  = Address;\r
-  ScriptPci2Poll.Delay    = Delay;\r
-\r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptPci2Poll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));\r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL)), Data, WidthInByte);\r
-  CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + WidthInByte), DataMask, WidthInByte);\r
-  \r
+  ScriptPci2Poll.OpCode  = EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE;\r
+  ScriptPci2Poll.Length  = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
+  ScriptPci2Poll.Width   = Width;\r
+  ScriptPci2Poll.Segment = Segment;\r
+  ScriptPci2Poll.Address = Address;\r
+  ScriptPci2Poll.Delay   = Delay;\r
+\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptPci2Poll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL));\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL)), Data, WidthInByte);\r
+  CopyMem ((UINT8 *)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + WidthInByte), DataMask, WidthInByte);\r
+\r
   SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
   Do the calculation of start address from which a new s3 boot script entry will write into.\r
-  \r
+\r
   @param EntryLength      The new entry length.\r
   @param Position         specifies the position in the boot script table where the opcode will be\r
-                          inserted, either before or after, depending on BeforeOrAfter. \r
+                          inserted, either before or after, depending on BeforeOrAfter.\r
   @param BeforeOrAfter    The flag to indicate to insert the nod before or after the position.\r
                           This parameter is effective when InsertFlag is TRUE\r
   @param Script           return out the position from which the a new s3 boot script entry will write into\r
 **/\r
 VOID\r
 S3BootScriptCalculateInsertAddress (\r
-  IN  UINT8     EntryLength,\r
+  IN  UINT8    EntryLength,\r
   IN  VOID     *Position OPTIONAL,\r
-  IN  BOOLEAN   BeforeOrAfter OPTIONAL,\r
-  OUT UINT8   **Script   \r
+  IN  BOOLEAN  BeforeOrAfter OPTIONAL,\r
+  OUT UINT8    **Script\r
   )\r
 {\r
-   UINTN                            TableLength;\r
-   UINT8                            *S3TableBase;\r
-   UINTN                            PositionOffset; \r
-   EFI_BOOT_SCRIPT_COMMON_HEADER     ScriptHeader;\r
-   //\r
-   // The entry inserting to table is already added to the end of the table\r
-   //\r
-   TableLength =  mS3BootScriptTablePtr->TableLength - EntryLength;\r
-   S3TableBase = mS3BootScriptTablePtr->TableBase ;\r
-   // \r
-   // calculate the Position offset\r
-   //\r
-   if (Position != NULL) {\r
-     PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);\r
-   \r
-     //\r
-     // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.\r
-     //\r
-     if (!BeforeOrAfter) {\r
-        CopyMem ((VOID*)&ScriptHeader, Position, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));  \r
-        PositionOffset += (ScriptHeader.Length);\r
-     }\r
-     //     \r
-     // Insert the node before the adjusted Position\r
-     //\r
-     CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);  \r
-     //\r
-     // calculate the the start address for the new entry. \r
-     //\r
-     *Script = S3TableBase + PositionOffset;\r
-       \r
-   } else {\r
-     if (!BeforeOrAfter) {\r
-       //\r
-       //  Insert the node to the end of the table\r
-       //\r
-       *Script = S3TableBase + TableLength; \r
-     } else {\r
-       // \r
-       // Insert the node to the beginning of the table\r
-       //\r
-       PositionOffset = (UINTN) sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
-       CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset); \r
-       *Script = S3TableBase + PositionOffset; \r
-     }\r
-   }       \r
+  UINTN                          TableLength;\r
+  UINT8                          *S3TableBase;\r
+  UINTN                          PositionOffset;\r
+  EFI_BOOT_SCRIPT_COMMON_HEADER  ScriptHeader;\r
+\r
+  //\r
+  // The entry inserting to table is already added to the end of the table\r
+  //\r
+  TableLength =  mS3BootScriptTablePtr->TableLength - EntryLength;\r
+  S3TableBase = mS3BootScriptTablePtr->TableBase;\r
+  //\r
+  // calculate the Position offset\r
+  //\r
+  if (Position != NULL) {\r
+    PositionOffset = (UINTN)Position - (UINTN)S3TableBase;\r
+\r
+    //\r
+    // If the BeforeOrAfter is FALSE, that means to insert the node right after the node.\r
+    //\r
+    if (!BeforeOrAfter) {\r
+      CopyMem ((VOID *)&ScriptHeader, Position, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));\r
+      PositionOffset += (ScriptHeader.Length);\r
+    }\r
+\r
+    //\r
+    // Insert the node before the adjusted Position\r
+    //\r
+    CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);\r
+    //\r
+    // calculate the the start address for the new entry.\r
+    //\r
+    *Script = S3TableBase + PositionOffset;\r
+  } else {\r
+    if (!BeforeOrAfter) {\r
+      //\r
+      //  Insert the node to the end of the table\r
+      //\r
+      *Script = S3TableBase + TableLength;\r
+    } else {\r
+      //\r
+      // Insert the node to the beginning of the table\r
+      //\r
+      PositionOffset = (UINTN)sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
+      CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);\r
+      *Script = S3TableBase + PositionOffset;\r
+    }\r
+  }\r
 }\r
+\r
 /**\r
-  Move the last boot script entry to the position \r
+  Move the last boot script entry to the position\r
 \r
   @param  BeforeOrAfter         Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
                                 in the boot script table specified by Position. If Position is NULL or points to\r
@@ -1640,26 +2260,30 @@ S3BootScriptCalculateInsertAddress (
 \r
   @retval RETURN_OUT_OF_RESOURCES  The table is not available.\r
   @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
-  @retval RETURN_SUCCESS           Opcode is inserted.\r
+  @retval RETURN_SUCCESS           Opcode is inserted no action is required as ACPI S3 was disabled.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptMoveLastOpcode (\r
-  IN     BOOLEAN                        BeforeOrAfter,\r
-  IN OUT VOID                         **Position OPTIONAL\r
-)\r
+  IN     BOOLEAN  BeforeOrAfter,\r
+  IN OUT VOID     **Position OPTIONAL\r
+  )\r
 {\r
-  UINT8*                Script;\r
-  VOID                  *TempPosition;  \r
-  UINTN                 StartAddress;\r
-  UINT32                TableLength;\r
+  UINT8                          *Script;\r
+  VOID                           *TempPosition;\r
+  UINTN                          StartAddress;\r
+  UINT32                         TableLength;\r
   EFI_BOOT_SCRIPT_COMMON_HEADER  ScriptHeader;\r
-  BOOLEAN               ValidatePosition;\r
-  UINT8*                LastOpcode;\r
-  UINT8                 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];\r
-  \r
+  BOOLEAN                        ValidatePosition;\r
+  UINT8                          *LastOpcode;\r
+  UINT8                          TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   ValidatePosition = FALSE;\r
-  TempPosition = (Position == NULL) ? NULL:(*Position);\r
+  TempPosition     = (Position == NULL) ? NULL : (*Position);\r
 \r
   //\r
   // Check that the script is initialized and synced without adding an entry to the script.\r
@@ -1668,38 +2292,42 @@ S3BootScriptMoveLastOpcode (
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   Script = mS3BootScriptTablePtr->TableBase;\r
 \r
-  StartAddress  = (UINTN) Script;\r
-  TableLength   = mS3BootScriptTablePtr->TableLength;\r
-  Script        = Script + sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
-  LastOpcode    = Script;\r
+  StartAddress = (UINTN)Script;\r
+  TableLength  = mS3BootScriptTablePtr->TableLength;\r
+  Script       = Script + sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
+  LastOpcode   = Script;\r
   //\r
   // Find the last boot Script Entry which is not the terminate node\r
   //\r
-  while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {    \r
-    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));   \r
-    if (TempPosition != NULL && TempPosition == Script) {\r
+  while ((UINTN)Script < (UINTN)(StartAddress + TableLength)) {\r
+    CopyMem ((VOID *)&ScriptHeader, Script, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));\r
+    if ((TempPosition != NULL) && (TempPosition == Script)) {\r
       //\r
-      // If the position is specified, the position must be pointed to a boot script entry start address. \r
+      // If the position is specified, the position must be pointed to a boot script entry start address.\r
       //\r
       ValidatePosition = TRUE;\r
     }\r
+\r
     if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {\r
       LastOpcode = Script;\r
-    } \r
-    Script  = Script + ScriptHeader.Length;\r
+    }\r
+\r
+    Script = Script + ScriptHeader.Length;\r
   }\r
+\r
   //\r
   // If the position is specified, but not the start of a boot script entry, it is a invalid input\r
   //\r
-  if (TempPosition != NULL && !ValidatePosition) {\r
+  if ((TempPosition != NULL) && !ValidatePosition) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
-  \r
-  CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
-  \r
-  CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length); \r
+\r
+  CopyMem ((VOID *)&ScriptHeader, LastOpcode, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));\r
+\r
+  CopyMem ((VOID *)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);\r
   //\r
   // Find the right position to write the node in\r
   //\r
@@ -1707,12 +2335,12 @@ S3BootScriptMoveLastOpcode (
     ScriptHeader.Length,\r
     TempPosition,\r
     BeforeOrAfter,\r
-    &Script   \r
-  );\r
+    &Script\r
+    );\r
   //\r
   // Copy the node to Boot script table\r
   //\r
-  CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length);\r
+  CopyMem ((VOID *)Script, (VOID *)TempBootScriptEntry, ScriptHeader.Length);\r
 \r
   SyncBootScript (Script);\r
 \r
@@ -1722,69 +2350,82 @@ S3BootScriptMoveLastOpcode (
   if (Position != NULL) {\r
     *Position = Script;\r
   }\r
+\r
   return RETURN_SUCCESS;\r
 }\r
+\r
 /**\r
-  Create a Label node in the boot script table. \r
-  \r
+  Create a Label node in the boot script table.\r
+\r
   @param  BeforeOrAfter         Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
                                 in the boot script table specified by Position. If Position is NULL or points to\r
                                 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end\r
                                 of the table (if FALSE).\r
   @param  Position              On entry, specifies the position in the boot script table where the opcode will be\r
                                 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies\r
-                                the position of the inserted opcode in the boot script table.  \r
+                                the position of the inserted opcode in the boot script table.\r
   @param InformationLength      Length of the label in bytes\r
   @param Information            Label to be logged in the boot scrpit\r
\r
+\r
   @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
-  @retval RETURN_SUCCESS           Opcode is added.\r
+  @retval RETURN_SUCCESS           Opcode is added or no action is required as ACPI S3 was disabled.\r
 \r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptLabelInternal (\r
-  IN        BOOLEAN                        BeforeOrAfter,\r
-  IN OUT    VOID                         **Position OPTIONAL, \r
-  IN        UINT32                         InformationLength, \r
-  IN CONST  CHAR8                          *Information\r
+  IN        BOOLEAN  BeforeOrAfter,\r
+  IN OUT    VOID     **Position OPTIONAL,\r
+  IN        UINT32   InformationLength,\r
+  IN CONST  CHAR8    *Information\r
   )\r
 {\r
-  UINT8                 Length;\r
-  UINT8                 *Script;\r
+  UINT8                        Length;\r
+  UINT8                        *Script;\r
   EFI_BOOT_SCRIPT_INFORMATION  ScriptInformation;\r
\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Truncation check\r
+  //\r
+  if (InformationLength > MAX_UINT8 - sizeof (EFI_BOOT_SCRIPT_INFORMATION)) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
-  ScriptInformation.OpCode     = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;\r
-  ScriptInformation.Length     = Length;\r
+  ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;\r
+  ScriptInformation.Length = Length;\r
 \r
+  ScriptInformation.InformationLength = InformationLength;\r
 \r
-  ScriptInformation.InformationLength = InformationLength;  \r
-\r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
-  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
+  CopyMem ((VOID *)Script, (VOID *)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
+  CopyMem ((VOID *)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *)Information, (UINTN)InformationLength);\r
 \r
   SyncBootScript (Script);\r
 \r
   return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);\r
-\r
 }\r
+\r
 /**\r
   Find a label within the boot script table and, if not present, optionally create it.\r
 \r
   @param  BeforeOrAfter         Specifies whether the opcode is stored before (TRUE)\r
-                                or after (FALSE) the position in the boot script table \r
+                                or after (FALSE) the position in the boot script table\r
                                 specified by Position.\r
-  @param  CreateIfNotFound      Specifies whether the label will be created if the label \r
+  @param  CreateIfNotFound      Specifies whether the label will be created if the label\r
                                 does not exists (TRUE) or not (FALSE).\r
   @param  Position              On entry, specifies the position in the boot script table\r
                                 where the opcode will be inserted, either before or after,\r
@@ -1792,42 +2433,48 @@ S3BootScriptLabelInternal (
                                 of the inserted opcode in the boot script table.\r
   @param  Label                 Points to the label which will be inserted in the boot script table.\r
 \r
-  @retval EFI_SUCCESS           The operation succeeded. A record was added into the\r
-                                specified script table.\r
+  @retval EFI_SUCCESS           The operation succeeded or no action is required.\r
+                                A record was added into the specified script table if ACPI S3 was enabled.\r
   @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
-                                If the opcode is unknow or not supported because of the PCD \r
+                                If the opcode is unknow or not supported because of the PCD\r
                                 Feature Flags.\r
   @retval EFI_OUT_OF_RESOURCES  There is insufficient memory to store the boot script.\r
 \r
 **/\r
 RETURN_STATUS\r
-EFIAPI \r
+EFIAPI\r
 S3BootScriptLabel (\r
-  IN       BOOLEAN                      BeforeOrAfter,\r
-  IN       BOOLEAN                      CreateIfNotFound,\r
-  IN OUT   VOID                       **Position OPTIONAL,\r
-  IN CONST CHAR8                       *Label\r
+  IN       BOOLEAN  BeforeOrAfter,\r
+  IN       BOOLEAN  CreateIfNotFound,\r
+  IN OUT   VOID     **Position OPTIONAL,\r
+  IN CONST CHAR8    *Label\r
   )\r
 {\r
-  UINT8*                Script;\r
-  UINTN                 StartAddress;\r
-  UINT32                TableLength;\r
+  UINT8                          *Script;\r
+  UINTN                          StartAddress;\r
+  UINT32                         TableLength;\r
   EFI_BOOT_SCRIPT_COMMON_HEADER  ScriptHeader;\r
   EFI_BOOT_SCRIPT_TABLE_HEADER   TableHeader;\r
   UINT32                         LabelLength;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   //\r
   // Check NULL Label\r
   //\r
   if (Label == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
   //\r
   // Check empty Label\r
   //\r
   if (Label[0] == '\0') {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Check that the script is initialized and synced without adding an entry to the script.\r
   // The code must search for the label first before it knows if a new entry needs\r
@@ -1837,35 +2484,37 @@ S3BootScriptLabel (
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   //\r
   // Check the header and search for existing label.\r
-  // \r
+  //\r
   Script = mS3BootScriptTablePtr->TableBase;\r
-  CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
+  CopyMem ((VOID *)&TableHeader, Script, sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER));\r
   if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  StartAddress  = (UINTN) Script;\r
-  TableLength   = mS3BootScriptTablePtr->TableLength;\r
-  Script    =     Script + TableHeader.Length;\r
-  while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {\r
-    \r
-    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));   \r
+\r
+  StartAddress = (UINTN)Script;\r
+  TableLength  = mS3BootScriptTablePtr->TableLength;\r
+  Script       =     Script + TableHeader.Length;\r
+  while ((UINTN)Script < (UINTN)(StartAddress + TableLength)) {\r
+    CopyMem ((VOID *)&ScriptHeader, Script, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));\r
     if (ScriptHeader.OpCode == S3_BOOT_SCRIPT_LIB_LABEL_OPCODE) {\r
-      if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof(EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {\r
-        (*Position) = Script; \r
+      if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof (EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {\r
+        (*Position) = Script;\r
         return EFI_SUCCESS;\r
       }\r
-    } \r
-    Script  = Script + ScriptHeader.Length;\r
+    }\r
+\r
+    Script = Script + ScriptHeader.Length;\r
   }\r
+\r
   if (CreateIfNotFound) {\r
-    LabelLength = (UINT32)AsciiStrSize(Label);\r
-    return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label);     \r
+    LabelLength = (UINT32)AsciiStrSize (Label);\r
+    return S3BootScriptLabelInternal (BeforeOrAfter, Position, LabelLength, Label);\r
   } else {\r
     return EFI_NOT_FOUND;\r
-  }   \r
+  }\r
 }\r
 \r
 /**\r
@@ -1874,24 +2523,28 @@ S3BootScriptLabel (
   @param  Position2             The positions in the boot script table to compare\r
   @param  RelativePosition      On return, points to the result of the comparison\r
 \r
-  @retval EFI_SUCCESS           The operation succeeded. A record was added into the\r
-                                specified script table.\r
+  @retval EFI_SUCCESS           The operation succeeded or no action is required.\r
+                                A record was added into the specified script table if ACPI S3 was enabled.\r
   @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
-                                If the opcode is unknow or not supported because of the PCD \r
+                                If the opcode is unknow or not supported because of the PCD\r
                                 Feature Flags.\r
   @retval EFI_OUT_OF_RESOURCES  There is insufficient memory to store the boot script.\r
 \r
 **/\r
 RETURN_STATUS\r
-EFIAPI \r
+EFIAPI\r
 S3BootScriptCompare (\r
-  IN  UINT8                       *Position1,\r
-  IN  UINT8                       *Position2,\r
-  OUT UINTN                       *RelativePosition\r
+  IN  UINT8  *Position1,\r
+  IN  UINT8  *Position2,\r
+  OUT UINTN  *RelativePosition\r
   )\r
 {\r
-  UINT8*                    Script;\r
-  UINT32                    TableLength; \r
+  UINT8   *Script;\r
+  UINT32  TableLength;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
 \r
   if (RelativePosition == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1904,20 +2557,22 @@ S3BootScriptCompare (
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   Script = mS3BootScriptTablePtr->TableBase;\r
 \r
   //\r
   // mS3BootScriptTablePtr->TableLength does not include the termination node, so add it up\r
   //\r
   TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
-  if (Position1 < Script || Position1 > Script+TableLength) {\r
+  if ((Position1 < Script) || (Position1 > Script+TableLength)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (Position2 < Script || Position2 > Script+TableLength) {\r
+\r
+  if ((Position2 < Script) || (Position2 > Script+TableLength)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  *RelativePosition = (Position1 < Position2)?-1:((Position1 == Position2)?0:1);\r
-  \r
+\r
+  *RelativePosition = (Position1 < Position2) ? -1 : ((Position1 == Position2) ? 0 : 1);\r
+\r
   return EFI_SUCCESS;\r
 }\r
-\r