]> 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 9ff5b80e7a3683057f1bfd621b77a1a35f30e535..8c7b58b2f960e5609045eff250199ca4fb4f146c 100644 (file)
@@ -1,16 +1,9 @@
 /** @file\r
   Save the S3 data to S3 boot script.\r
 \r
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\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
-\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
 **/\r
 \r
-SCRIPT_TABLE_PRIVATE_DATA        *mS3BootScriptTablePtr;\r
+SCRIPT_TABLE_PRIVATE_DATA  *mS3BootScriptTablePtr;\r
 \r
 //\r
 // Allocate SMM copy because we can not use mS3BootScriptTablePtr after SmmReadyToLock in InSmm.\r
 //\r
-SCRIPT_TABLE_PRIVATE_DATA        *mS3BootScriptTableSmmPtr;\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                         mBootScriptDataBootTimeGuid = {\r
+EFI_GUID  mBootScriptDataBootTimeGuid = {\r
   0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }\r
 };\r
 \r
-EFI_GUID                         mBootScriptTableBaseGuid = {\r
+EFI_GUID  mBootScriptTableBaseGuid = {\r
   0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }\r
 };\r
 \r
-EFI_GUID                         mBootScriptSmmPrivateDataGuid = {\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
+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
@@ -138,14 +132,15 @@ EFI_SMM_SYSTEM_TABLE2            *mBootScriptSmst = NULL;
 \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
   if (S3TableBase == NULL) {\r
@@ -154,20 +149,19 @@ S3BootScriptInternalCloseTable (
     //\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
   return S3TableBase;\r
   //\r
   // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to\r
@@ -186,7 +180,7 @@ SaveBootScriptDataToLockBox (
   VOID\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // Save whole memory copy into LockBox.\r
@@ -209,7 +203,7 @@ SaveBootScriptDataToLockBox (
   Status = SaveLockBox (\r
              &mBootScriptTableBaseGuid,\r
              (VOID *)&mS3BootScriptTablePtr->TableBase,\r
-             sizeof(mS3BootScriptTablePtr->TableBase)\r
+             sizeof (mS3BootScriptTablePtr->TableBase)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
@@ -231,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
@@ -244,7 +238,7 @@ S3BootScriptEventCallBack (
                   &Interface\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return ;\r
+    return;\r
   }\r
 \r
   //\r
@@ -301,7 +295,7 @@ 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
@@ -309,6 +303,7 @@ S3BootScriptSmmEventCallBack (
     //\r
     mS3BootScriptTableSmmPtr->InSmm = TRUE;\r
   }\r
+\r
   //\r
   // We should not use ACPI Reserved copy, because it is not safe.\r
   //\r
@@ -331,7 +326,7 @@ SaveBootTimeDataToLockBox (
   VOID\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,\r
@@ -350,7 +345,7 @@ SaveBootTimeDataToLockBox (
   //\r
   Status = SaveLockBox (\r
              &mBootScriptDataBootTimeGuid,\r
-             (VOID *) mS3BootScriptTablePtr->TableBase,\r
+             (VOID *)mS3BootScriptTablePtr->TableBase,\r
              mS3BootScriptTablePtr->BootTimeScriptLength\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
@@ -366,17 +361,17 @@ SaveSmmPriviateDataToLockBoxAtRuntime (
   VOID\r
   )\r
 {\r
-  EFI_STATUS    Status;\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
+  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
@@ -401,13 +396,13 @@ SaveSmmPriviateDataToLockBoxAtRuntime (
 EFI_STATUS\r
 EFIAPI\r
 S3BootScriptSmmAtRuntimeCallBack (\r
-  IN CONST EFI_GUID     *Protocol,\r
-  IN VOID               *Interface,\r
-  IN EFI_HANDLE         Handle\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
+    mS3BootScriptTablePtr->BootTimeScriptLength = (UINT32)(mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
     SaveBootTimeDataToLockBox ();\r
 \r
     mS3BootScriptTablePtr->AtRuntime = TRUE;\r
@@ -431,19 +426,25 @@ S3BootScriptSmmAtRuntimeCallBack (
 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_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
@@ -452,16 +453,16 @@ S3BootScriptLibInitialize (
     Status = gBS->AllocatePages (\r
                     AllocateMaxAddress,\r
                     EfiReservedMemoryType,\r
-                    EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)),\r
+                    EFI_SIZE_TO_PAGES (sizeof (SCRIPT_TABLE_PRIVATE_DATA)),\r
                     &Buffer\r
                     );\r
     ASSERT_EFI_ERROR (Status);\r
     mS3BootScriptTableAllocated = TRUE;\r
-    S3TablePtr = (VOID *) (UINTN) Buffer;\r
+    S3TablePtr                  = (VOID *)(UINTN)Buffer;\r
 \r
-    Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr);\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, (UINT64)(UINTN)S3TablePtr);\r
     ASSERT_EFI_ERROR (Status);\r
-    ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));\r
+    ZeroMem (S3TablePtr, sizeof (SCRIPT_TABLE_PRIVATE_DATA));\r
     //\r
     // Create event to notify the library system enter the SmmLocked phase.\r
     //\r
@@ -474,22 +475,26 @@ S3BootScriptLibInitialize (
                                 );\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
@@ -498,22 +503,22 @@ S3BootScriptLibInitialize (
     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 = mBootScriptSmst->SmmAllocatePool (\r
                                 EfiRuntimeServicesData,\r
-                                sizeof(SCRIPT_TABLE_PRIVATE_DATA),\r
-                                (VOID **) &S3TableSmmPtr\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
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, (UINT64)(UINTN)S3TableSmmPtr);\r
     ASSERT_EFI_ERROR (Status);\r
-    ZeroMem (S3TableSmmPtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));\r
+    ZeroMem (S3TableSmmPtr, sizeof (SCRIPT_TABLE_PRIVATE_DATA));\r
 \r
     //\r
     // Register SmmExitBootServices and SmmLegacyBoot notification.\r
@@ -532,6 +537,7 @@ S3BootScriptLibInitialize (
                                 );\r
     ASSERT_EFI_ERROR (Status);\r
   }\r
+\r
   mS3BootScriptTableSmmPtr = S3TableSmmPtr;\r
 \r
   //\r
@@ -563,13 +569,17 @@ S3BootScriptLibInitialize (
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptLibDeinitialize (\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
+  EFI_STATUS  Status;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
 \r
-  DEBUG ((EFI_D_INFO, "%a() in %a module\n", __FUNCTION__, gEfiCallerBaseName));\r
+  DEBUG ((DEBUG_INFO, "%a() in %a module\n", __FUNCTION__, gEfiCallerBaseName));\r
 \r
   if (mEventDxeSmmReadyToLock != NULL) {\r
     //\r
@@ -591,6 +601,7 @@ S3BootScriptLibDeinitialize (
                                   );\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
+\r
     if (mRegistrationSmmLegacyBoot != NULL) {\r
       //\r
       // Unregister SmmLegacyBoot notification.\r
@@ -602,6 +613,7 @@ S3BootScriptLibDeinitialize (
                                   );\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
+\r
     if (mRegistrationSmmReadyToLock != NULL) {\r
       //\r
       // Unregister SmmReadyToLock notification.\r
@@ -619,11 +631,12 @@ S3BootScriptLibDeinitialize (
   // 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
+    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
@@ -644,95 +657,98 @@ S3BootScriptLibDeinitialize (
 \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
-     //\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
+  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
-     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->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 ((UINTN) EFI_PAGES_TO_SIZE ((UINTN) PageNumber) < (UINTN) (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
+    // 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
-     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
+  return NewEntryPtr;\r
 }\r
+\r
 /**\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
@@ -741,26 +757,27 @@ S3BootScriptGetBootTimeEntryAddAddress (
 \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 ((UINTN) (mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE)) <= (UINTN) 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
-   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
@@ -772,18 +789,18 @@ RestoreBootTimeDataFromLockBox (
   VOID\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
-  UINTN         LockBoxLength;\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
+  Status        = RestoreLockBox (\r
+                    &mBootScriptDataBootTimeGuid,\r
+                    (VOID *)mS3BootScriptTablePtr->TableBase,\r
+                    &LockBoxLength\r
+                    );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
@@ -792,7 +809,7 @@ RestoreBootTimeDataFromLockBox (
   Status = UpdateLockBox (\r
              &mBootScriptDataGuid,\r
              0,\r
-             (VOID *) mS3BootScriptTablePtr->TableBase,\r
+             (VOID *)mS3BootScriptTablePtr->TableBase,\r
              LockBoxLength\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
@@ -800,7 +817,7 @@ RestoreBootTimeDataFromLockBox (
   //\r
   // Update TableLength.\r
   //\r
-  mS3BootScriptTablePtr->TableLength = (UINT32) (mS3BootScriptTablePtr->BootTimeScriptLength - sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
+  mS3BootScriptTablePtr->TableLength = (UINT32)(mS3BootScriptTablePtr->BootTimeScriptLength - sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
 }\r
 \r
 /**\r
@@ -810,12 +827,16 @@ RestoreBootTimeDataFromLockBox (
 \r
   @retval the address from which the a new s3 boot script entry will write into\r
  **/\r
-UINT8*\r
+UINT8 *\r
 S3BootScriptGetEntryAddAddress (\r
   UINT8  EntryLength\r
   )\r
 {\r
-  UINT8*                         NewEntryPtr;\r
+  UINT8  *NewEntryPtr;\r
+\r
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return NULL;\r
+  }\r
 \r
   if (mS3BootScriptTablePtr->SmmLocked) {\r
     //\r
@@ -826,7 +847,7 @@ S3BootScriptGetEntryAddAddress (
       // 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 outside SMM after SmmReadyToLock!!!\n"));\r
+      DEBUG ((DEBUG_ERROR, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));\r
       return NULL;\r
     }\r
 \r
@@ -839,12 +860,12 @@ S3BootScriptGetEntryAddAddress (
       mS3BootScriptTablePtr->BackFromS3 = FALSE;\r
     }\r
 \r
-    NewEntryPtr  = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);\r
+    NewEntryPtr = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);\r
   } else {\r
-    NewEntryPtr  = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);\r
+    NewEntryPtr = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);\r
   }\r
-  return NewEntryPtr;\r
 \r
+  return NewEntryPtr;\r
 }\r
 \r
 /**\r
@@ -855,7 +876,7 @@ S3BootScriptGetEntryAddAddress (
 **/\r
 VOID\r
 SyncBootScript (\r
-  IN UINT8      *Script\r
+  IN UINT8  *Script\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -867,12 +888,12 @@ SyncBootScript (
     // 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
+  ScriptOffset = (UINT32)(Script - mS3BootScriptTablePtr->TableBase);\r
 \r
-  TotalScriptLength = (UINT32) (mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
+  TotalScriptLength = (UINT32)(mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
 \r
   //\r
   // Update BootScriptData\r
@@ -924,26 +945,27 @@ SyncBootScript (
   @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
+  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
   }\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
@@ -951,11 +973,12 @@ S3BootScriptCloseTable (
   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
@@ -963,14 +986,15 @@ S3BootScriptCloseTable (
   // 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
   mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
   return Buffer;\r
 }\r
+\r
 /**\r
   Save I/O write to boot script\r
 \r
@@ -980,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
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // save script data\r
   //\r
@@ -1011,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
@@ -1029,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
+  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
@@ -1060,14 +1104,15 @@ S3BootScriptSaveIoReadWrite (
   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
+  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
@@ -1077,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
+  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
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\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
+  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
+  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
@@ -1125,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
+  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
   // 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
+  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
+  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
@@ -1173,53 +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
-  if (Width == S3BootScriptWidthUint64 ||\r
-      Width == S3BootScriptWidthFifoUint64 ||\r
-      Width == S3BootScriptWidthFillUint64) {\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
+  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
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\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
+  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
+  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
@@ -1229,49 +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
-  if (Width == S3BootScriptWidthUint64 ||\r
-      Width == S3BootScriptWidthFifoUint64 ||\r
-      Width == S3BootScriptWidthFillUint64) {\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
+  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
   // 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
+  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 ((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
@@ -1280,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
@@ -1290,55 +1381,72 @@ S3BootScriptSavePciCfgReadWrite (
   @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
 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
+  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
+  UINT8                              Length;\r
+  UINT8                              *Script;\r
+  UINT8                              WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE  ScriptPciWrite2;\r
 \r
-  if (Width == S3BootScriptWidthUint64 ||\r
-      Width == S3BootScriptWidthFifoUint64 ||\r
-      Width == S3BootScriptWidthFillUint64) {\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
+  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
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\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
+  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
+  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
@@ -1349,51 +1457,57 @@ 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
-  if (Width == S3BootScriptWidthUint64 ||\r
-      Width == S3BootScriptWidthFifoUint64 ||\r
-      Width == S3BootScriptWidthFillUint64) {\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
+  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
   // 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
+  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 ((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
@@ -1437,19 +1551,19 @@ S3BootScriptSavePciCfg2ReadWrite (
 **/\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
+  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
+  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
+  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
@@ -1459,32 +1573,35 @@ CheckParameters (
   switch (Operation) {\r
     case EfiSmbusQuickRead:\r
     case EfiSmbusQuickWrite:\r
-      if (PecCheck || Command != 0) {\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
-      // Cascade to check length parameter.\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
+    //\r
+    // Cascade to check length parameter.\r
+    //\r
     case EfiSmbusReadWord:\r
     case EfiSmbusWriteWord:\r
     case EfiSmbusProcessCall:\r
-      if (Buffer == NULL || Length == NULL) {\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
@@ -1493,13 +1610,16 @@ CheckParameters (
       if ((Buffer == NULL) ||\r
           (Length == NULL) ||\r
           (*Length < MIN_SMBUS_BLOCK_LEN) ||\r
-          (*Length > MAX_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
@@ -1513,23 +1633,27 @@ CheckParameters (
   @param Buffer         Contains the value of data to execute to the SMBUS slave device.\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
-  EFI_STATUS            Status;\r
-  UINTN                 BufferLength;\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
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   if (Length == NULL) {\r
     BufferLength = 0;\r
   } else {\r
@@ -1541,24 +1665,32 @@ S3BootScriptSaveSmbusExecute (
     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) BufferLength;\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
     BufferLength\r
     );\r
@@ -1567,43 +1699,50 @@ S3BootScriptSaveSmbusExecute (
 \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
   @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
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\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
+  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
+  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
@@ -1611,39 +1750,45 @@ S3BootScriptSaveStall (
   @param Context      Argument to be passed into the EntryPoint of the code to be dispatched.\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
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\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
+  ScriptDispatch2.Context    =   (EFI_PHYSICAL_ADDRESS)(UINTN)Context;\r
 \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\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
@@ -1661,26 +1806,30 @@ 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
+  WidthInByte = (UINT8)(0x01 << (Width & 0x03));\r
 \r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));\r
 \r
@@ -1688,24 +1837,26 @@ S3BootScriptSaveMemPoll (
   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
@@ -1714,43 +1865,54 @@ S3BootScriptSaveMemPoll (
   @param Information       Information to be logged in the boot scrpit\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
 \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
@@ -1758,44 +1920,50 @@ S3BootScriptSaveInformation (
   @param String            The string to save to boot script table\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
+           (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
   @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
+  if (!mS3BootScriptAcpiS3Enable) {\r
+    return RETURN_SUCCESS;\r
+  }\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
 \r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
+\r
   //\r
   // Build script data\r
   //\r
@@ -1803,13 +1971,13 @@ S3BootScriptSaveDispatch (
   ScriptDispatch.Length     = Length;\r
   ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
 \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH));\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
@@ -1823,44 +1991,48 @@ 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 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
+  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
+  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
   // 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
+  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
@@ -1880,55 +2052,62 @@ 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
-  if (Width == S3BootScriptWidthUint64 ||\r
-      Width == S3BootScriptWidthFifoUint64 ||\r
-      Width == S3BootScriptWidthFillUint64) {\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
+  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
+  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
@@ -1943,57 +2122,64 @@ 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
+ @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
-  if (Width == S3BootScriptWidthUint64 ||\r
-      Width == S3BootScriptWidthFifoUint64 ||\r
-      Width == S3BootScriptWidthFillUint64) {\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
+  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
   // 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
+  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
+  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
@@ -2006,59 +2192,61 @@ S3BootScriptSavePci2Poll (
 **/\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
 \r
@@ -2072,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
+  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
@@ -2100,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
       //\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
+    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
+  CopyMem ((VOID *)&ScriptHeader, LastOpcode, sizeof (EFI_BOOT_SCRIPT_COMMON_HEADER));\r
 \r
-  CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);\r
+  CopyMem ((VOID *)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);\r
   //\r
   // Find the right position to write the node in\r
   //\r
@@ -2140,11 +2336,11 @@ S3BootScriptMoveLastOpcode (
     TempPosition,\r
     BeforeOrAfter,\r
     &Script\r
-  );\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
@@ -2154,8 +2350,10 @@ 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
@@ -2171,45 +2369,56 @@ S3BootScriptMoveLastOpcode (
 \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
+  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     = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;\r
-  ScriptInformation.Length     = Length;\r
-\r
+  ScriptInformation.OpCode = S3_BOOT_SCRIPT_LIB_LABEL_OPCODE;\r
+  ScriptInformation.Length = Length;\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
@@ -2224,8 +2433,8 @@ 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
                                 Feature Flags.\r
@@ -2235,24 +2444,30 @@ S3BootScriptLabelInternal (
 RETURN_STATUS\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
@@ -2274,27 +2489,29 @@ S3BootScriptLabel (
   // Check the header and search for existing label.\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
+  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
+      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
+    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
@@ -2306,8 +2523,8 @@ 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
                                 Feature Flags.\r
@@ -2317,13 +2534,17 @@ S3BootScriptLabel (
 RETURN_STATUS\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
@@ -2336,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
+  *RelativePosition = (Position1 < Position2) ? -1 : ((Position1 == Position2) ? 0 : 1);\r
 \r
   return EFI_SUCCESS;\r
 }\r
-\r