]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/PiDxeS3BootScriptLib/BootScriptSave.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / PiDxeS3BootScriptLib / BootScriptSave.c
index 5903b0f6413a5ad5cc49115c958690c62300fbed..c116727531d27ce882ec47f260608fb37fd050f2 100644 (file)
@@ -1,16 +1,9 @@
 /** @file\r
-  Save the S3 data to S3 boot script. \r
\r
-  Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Save the S3 data to S3 boot script.\r
 \r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions\r
-  of the BSD License which accompanies this distribution.  The\r
-  full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 \r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 #include "InternalBootScriptLib.h"\r
 \r
   Data structure usage:\r
 \r
-  +------------------------------+<-- PcdS3BootScriptTablePrivateDataPtr\r
+  +------------------------------+<------- PcdS3BootScriptTablePrivateDataPtr\r
+  | SCRIPT_TABLE_PRIVATE_DATA    |          (mS3BootScriptTablePtr, Before SmmReadyToLock)\r
+  |    TableBase                 |---      PcdS3BootScriptTablePrivateSmmDataPtr\r
+  |    TableLength               |--|--     (mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr, After SmmReadyToLock InSmm)\r
+  |    TableMemoryPageNumber     |--|-|----\r
+  |    AtRuntime                 |  | |   |\r
+  |    InSmm                     |  | |   |\r
+  |    BootTimeScriptLength      |--|-|---|---\r
+  |    SmmLocked                 |  | |   |  |\r
+  |    BackFromS3                |  | |   |  |\r
+  +------------------------------+  | |   |  |\r
+                                    | |   |  |\r
+  +------------------------------+<-- |   |  |\r
+  | EFI_BOOT_SCRIPT_TABLE_HEADER |    |   |  |\r
+  |    TableLength               |----|-- |  |\r
+  +------------------------------+    | | |  |\r
+  |     ......                   |    | | |  |\r
+  +------------------------------+<---- | |  |\r
+  | EFI_BOOT_SCRIPT_TERMINATE    |      | |  |\r
+  +------------------------------+<------ |  |\r
+                                          |  |\r
+                                          |  |\r
+  mBootScriptDataBootTimeGuid LockBox:    |  |\r
+   Used to restore data after back from S3|  |\r
+   to handle potential INSERT boot script |  |\r
+   at runtime.                            |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | Before SmmReadyToLock        |        |  |\r
+  |                              |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | After SmmReadyToLock InSmm   |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+<-------|--|\r
+                                          |  |\r
+                                          |  |\r
+  mBootScriptDataGuid LockBox: (IN_PLACE) |  |\r
+   Used to restore data at S3 resume.     |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | Before SmmReadyToLock        |        |  |\r
+  |                              |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+        |  |\r
+  | Boot Time Boot Script        |        |  |\r
+  | After SmmReadyToLock InSmm   |        |  |\r
+  |                              |        |  |\r
+  +------------------------------+<-------|---\r
+  | Runtime Boot Script          |        |\r
+  | After SmmReadyToLock InSmm   |        |\r
+  +------------------------------+        |\r
+  |     ......                   |        |\r
+  +------------------------------+<--------\r
+\r
+\r
+  mBootScriptTableBaseGuid LockBox: (IN_PLACE)\r
+  +------------------------------+\r
+  | mS3BootScriptTablePtr->      |\r
+  |  TableBase                   |\r
+  +------------------------------+\r
+\r
+\r
+  mBootScriptSmmPrivateDataGuid LockBox: (IN_PLACE)\r
+   SMM private data with BackFromS3 = TRUE\r
+   at runtime. S3 will help restore it to\r
+   tell the Library the system is back from S3.\r
+  +------------------------------+\r
   | SCRIPT_TABLE_PRIVATE_DATA    |\r
-  |    TableBase                 |---\r
-  |    TableLength               |--|--\r
-  |    AtRuntime                 |  | |\r
-  |    InSmm                     |  | |\r
-  +------------------------------+  | |\r
-                                    | |\r
-  +------------------------------+<-- |\r
-  | EFI_BOOT_SCRIPT_TABLE_HEADER |    |\r
-  |    TableLength               |----|--\r
-  +------------------------------+    | |\r
-  |     ......                   |    | |\r
-  +------------------------------+<---- |\r
-  | EFI_BOOT_SCRIPT_TERMINATE    |      |\r
-  +------------------------------+<------\r
+  |    TableBase                 |\r
+  |    TableLength               |\r
+  |    TableMemoryPageNumber     |\r
+  |    AtRuntime                 |\r
+  |    InSmm                     |\r
+  |    BootTimeScriptLength      |\r
+  |    SmmLocked                 |\r
+  |    BackFromS3 = TRUE         |\r
+  +------------------------------+\r
 \r
 **/\r
 \r
 SCRIPT_TABLE_PRIVATE_DATA        *mS3BootScriptTablePtr;\r
-EFI_EVENT                        mEnterRuntimeEvent;\r
+\r
 //\r
-// Allocate local copy in SMM because we can not use mS3BootScriptTablePtr when we AtRuntime in InSmm.\r
+// Allocate SMM copy because we can not use mS3BootScriptTablePtr after SmmReadyToLock in InSmm.\r
 //\r
-SCRIPT_TABLE_PRIVATE_DATA        mS3BootScriptTable;\r
-UINTN                            mLockBoxLength;\r
+SCRIPT_TABLE_PRIVATE_DATA        *mS3BootScriptTableSmmPtr;\r
 \r
 EFI_GUID                         mBootScriptDataGuid = {\r
   0xaea6b965, 0xdcf5, 0x4311, { 0xb4, 0xb8, 0xf, 0x12, 0x46, 0x44, 0x94, 0xd2 }\r
 };\r
 \r
-EFI_GUID                         mBootScriptHeaderDataGuid = {\r
+EFI_GUID                         mBootScriptDataBootTimeGuid = {\r
+  0xb5af1d7a, 0xb8cf, 0x4eb3, { 0x89, 0x25, 0xa8, 0x20, 0xe1, 0x6b, 0x68, 0x7d }\r
+};\r
+\r
+EFI_GUID                         mBootScriptTableBaseGuid = {\r
   0x1810ab4a, 0x2314, 0x4df6, { 0x81, 0xeb, 0x67, 0xc6, 0xec, 0x5, 0x85, 0x91 }\r
 };\r
 \r
-EFI_GUID                         mBootScriptInformationGuid = {\r
-  0x2c680508, 0x2b87, 0x46ab, { 0xb9, 0x8a, 0x49, 0xfc, 0x23, 0xf9, 0xf5, 0x95 }\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
+\r
 /**\r
-  This is an internal function to add a terminate node the entry, recalculate the table \r
-  length and fill into the table. \r
-  \r
-  @return the base address of the boot script tble.   \r
+  This is an internal function to add a terminate node the entry, recalculate the table\r
+  length and fill into the table.\r
+\r
+  @return the base address of the boot script table.\r
  **/\r
 UINT8*\r
 S3BootScriptInternalCloseTable (\r
@@ -73,7 +140,7 @@ S3BootScriptInternalCloseTable (
   EFI_BOOT_SCRIPT_TERMINATE      ScriptTerminate;\r
   EFI_BOOT_SCRIPT_TABLE_HEADER   *ScriptTableInfo;\r
   S3TableBase = mS3BootScriptTablePtr->TableBase;\r
-  \r
+\r
   if (S3TableBase == NULL) {\r
     //\r
     // the table is not exist\r
@@ -91,131 +158,21 @@ S3BootScriptInternalCloseTable (
   //\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
-  // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE). Because \r
-  // maybe in runtime, we still need add entries into the table, and the runtime entry should be\r
-  // added start before this TERMINATE node.\r
-  //\r
-}  \r
-\r
-/**\r
-  This function return the total size of INFORMATION OPCODE in boot script table.\r
-\r
-  @return InformationBufferSize The total size of INFORMATION OPCODE in boot script table.\r
-**/\r
-UINTN\r
-GetBootScriptInformationBufferSize (\r
-  VOID\r
-  )\r
-{\r
-  UINT8                          *S3TableBase;\r
-  UINT8                          *Script;\r
-  UINTN                          TableLength;\r
-  EFI_BOOT_SCRIPT_COMMON_HEADER  ScriptHeader;\r
-  EFI_BOOT_SCRIPT_TABLE_HEADER   TableHeader;\r
-  EFI_BOOT_SCRIPT_INFORMATION    Information;\r
-  UINTN                          InformationBufferSize;\r
 \r
-  InformationBufferSize = 0;\r
 \r
-  S3TableBase   = mS3BootScriptTablePtr->TableBase;\r
-  Script        = S3TableBase;\r
-  CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
-  TableLength   = TableHeader.TableLength;\r
-\r
-  //\r
-  // Go through the ScriptTable\r
-  //\r
-  while ((UINTN) Script < (UINTN) (S3TableBase + TableLength)) {\r
-    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));\r
-    switch (ScriptHeader.OpCode) {\r
-    case EFI_BOOT_SCRIPT_INFORMATION_OPCODE:\r
-      CopyMem ((VOID*)&Information, (VOID*)Script, sizeof(Information));\r
-      InformationBufferSize += Information.InformationLength;\r
-      break;\r
-    default:\r
-      break;\r
-    }\r
-    Script  = Script + ScriptHeader.Length;\r
-  }\r
-\r
-  return InformationBufferSize;\r
-}\r
-\r
-/**\r
-  This function fix INFORMATION OPCODE in boot script table.\r
-  Originally, the Information buffer is pointer to EfiRuntimeServicesCode,\r
-  EfiRuntimeServicesData, or EfiACPIMemoryNVS. They are seperated.\r
-  Now, in order to save it to LockBox, we allocate a big EfiACPIMemoryNVS,\r
-  and fix the pointer for INFORMATION opcode InformationBuffer.\r
-\r
-  @param InformationBuffer     The address of new Information buffer.\r
-  @param InformationBufferSize The size of new Information buffer.\r
-**/\r
-VOID\r
-FixBootScriptInformation (\r
-  IN VOID  *InformationBuffer,\r
-  IN UINTN InformationBufferSize\r
-  )\r
-{\r
-  UINT8                          *S3TableBase;\r
-  UINT8                          *Script;\r
-  UINTN                          TableLength;\r
-  EFI_BOOT_SCRIPT_COMMON_HEADER  ScriptHeader;\r
-  EFI_BOOT_SCRIPT_TABLE_HEADER   TableHeader;\r
-  EFI_BOOT_SCRIPT_INFORMATION    Information;\r
-  UINTN                          FixedInformationBufferSize;\r
-\r
-  FixedInformationBufferSize = 0;\r
-\r
-  S3TableBase   = mS3BootScriptTablePtr->TableBase;\r
-  Script        = S3TableBase;\r
-  CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
-  TableLength   = TableHeader.TableLength;\r
 \r
+  return S3TableBase;\r
   //\r
-  // Go through the ScriptTable\r
+  // NOTE: Here we did NOT adjust the mS3BootScriptTablePtr->TableLength to\r
+  // mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE).\r
+  // Because maybe after SmmReadyToLock, we still need add entries into the table,\r
+  // and the entry should be added start before this TERMINATE node.\r
   //\r
-  while ((UINTN) Script < (UINTN) (S3TableBase + TableLength)) {\r
-    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));\r
-    switch (ScriptHeader.OpCode) {\r
-    case EFI_BOOT_SCRIPT_INFORMATION_OPCODE:\r
-      CopyMem ((VOID*)&Information, (VOID*)Script, sizeof(Information));\r
-\r
-      CopyMem (\r
-        (VOID *)((UINTN)InformationBuffer + FixedInformationBufferSize),\r
-        (VOID *)(UINTN)Information.Information,\r
-        Information.InformationLength\r
-        );\r
-      gBS->FreePool ((VOID *)(UINTN)Information.Information);\r
-      Information.Information = (EFI_PHYSICAL_ADDRESS)((UINTN)InformationBuffer + FixedInformationBufferSize);\r
-\r
-      CopyMem ((VOID*)Script, (VOID*)&Information, sizeof(Information));\r
-\r
-      FixedInformationBufferSize += Information.InformationLength;\r
-      break;\r
-    default:\r
-      break;\r
-    }\r
-    Script  = Script + ScriptHeader.Length;\r
-  }\r
-\r
-  ASSERT (FixedInformationBufferSize == InformationBufferSize);\r
-\r
-  return ;\r
 }\r
 \r
 /**\r
   This function save boot script data to LockBox.\r
-  1. BootSriptPrivate data, BootScript data - Image and DispatchContext are handled by platform.\r
-  2. BootScriptExecutor, BootScriptExecutor context\r
-  - ACPI variable - (PI version) sould be handled by SMM driver. S3 Page table is handled here.\r
-  - ACPI variable - framework version is already handled by Framework CPU driver.\r
+\r
 **/\r
 VOID\r
 SaveBootScriptDataToLockBox (\r
@@ -223,52 +180,15 @@ SaveBootScriptDataToLockBox (
   )\r
 {\r
   EFI_STATUS            Status;\r
-  EFI_PHYSICAL_ADDRESS  InformationBuffer;\r
-  UINTN                 InformationBufferSize;\r
 \r
   //\r
-  // We need save BootScriptInformation to LockBox, because it is in\r
-  // EfiRuntimeServicesCode, EfiRuntimeServicesData, or EfiACPIMemoryNVS.\r
-  // \r
-  //\r
-  InformationBufferSize = GetBootScriptInformationBufferSize ();\r
-  if (InformationBufferSize != 0) {\r
-    InformationBuffer = 0xFFFFFFFF;\r
-    Status = gBS->AllocatePages (\r
-                    AllocateMaxAddress,\r
-                    EfiACPIMemoryNVS,\r
-                    EFI_SIZE_TO_PAGES(InformationBufferSize),\r
-                    &InformationBuffer\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    //\r
-    // Fix BootScript information pointer\r
-    //\r
-    FixBootScriptInformation ((VOID *)(UINTN)InformationBuffer, InformationBufferSize);\r
-\r
-    //\r
-    // Save BootScript information to lockbox\r
-    //\r
-    Status = SaveLockBox (\r
-               &mBootScriptInformationGuid,\r
-               (VOID *)(UINTN)InformationBuffer,\r
-               InformationBufferSize\r
-               );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    Status = SetLockBoxAttributes (&mBootScriptInformationGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-\r
-  //\r
-  // mS3BootScriptTablePtr->TableLength does not include EFI_BOOT_SCRIPT_TERMINATE, because we need add entry at runtime.\r
-  // Save all info here, just in case that no one will add boot script entry in SMM.\r
+  // Save whole memory copy into LockBox.\r
+  // It will be used to restore data at S3 resume.\r
   //\r
   Status = SaveLockBox (\r
              &mBootScriptDataGuid,\r
              (VOID *)mS3BootScriptTablePtr->TableBase,\r
-             mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE)\r
+             EFI_PAGES_TO_SIZE (mS3BootScriptTablePtr->TableMemoryPageNumber)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
@@ -280,22 +200,22 @@ SaveBootScriptDataToLockBox (
   // Do not update other field because they will NOT be used in S3.\r
   //\r
   Status = SaveLockBox (\r
-             &mBootScriptHeaderDataGuid,\r
+             &mBootScriptTableBaseGuid,\r
              (VOID *)&mS3BootScriptTablePtr->TableBase,\r
              sizeof(mS3BootScriptTablePtr->TableBase)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  Status = SetLockBoxAttributes (&mBootScriptHeaderDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
+  Status = SetLockBoxAttributes (&mBootScriptTableBaseGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r
   This is the Event call back function to notify the Library the system is entering\r
-  run time phase.\r
-  \r
+  SmmLocked phase.\r
+\r
   @param  Event   Pointer to this event\r
-  @param  Context Event hanlder private data \r
+  @param  Context Event handler private data\r
  **/\r
 VOID\r
 EFIAPI\r
@@ -321,28 +241,29 @@ S3BootScriptEventCallBack (
   }\r
 \r
   //\r
-  // Here we should tell the library that we are enter into runtime phase. and \r
-  // the memory page number occupied by the table should not grow anymore.\r
+  // Here we should tell the library that we are entering SmmLocked phase.\r
+  // and the memory page number occupied by the table should not grow anymore.\r
   //\r
-  if (!mS3BootScriptTablePtr->AtRuntime) {\r
+  if (!mS3BootScriptTablePtr->SmmLocked) {\r
     //\r
-    // In boot time, we need not write the terminate node when adding a node to boot scipt table\r
-    // or else, that will impact the performance. However, in runtime, we should append terminate\r
-    // node on every add to boot script table\r
+    // Before SmmReadyToLock, we need not write the terminate node when adding a node to boot scipt table\r
+    // or else, that will impact the performance. However, after SmmReadyToLock, we should append terminate\r
+    // node on every add to boot script table.\r
     //\r
     S3BootScriptInternalCloseTable ();\r
-    mS3BootScriptTablePtr->AtRuntime = TRUE;\r
+    mS3BootScriptTablePtr->SmmLocked = TRUE;\r
 \r
     //\r
     // Save BootScript data to lockbox\r
     //\r
     SaveBootScriptDataToLockBox ();\r
   }\r
-} \r
+}\r
+\r
 /**\r
-  This is the Event call back function is triggered in SMM to notify the Library the system is entering\r
-  run time phase and set InSmm flag.\r
-  \r
+  This is the Event call back function is triggered in SMM to notify the Library\r
+  the system is entering SmmLocked phase and set InSmm flag.\r
+\r
   @param  Protocol   Points to the protocol's unique identifier\r
   @param  Interface  Points to the interface instance\r
   @param  Handle     The handle on which the interface was installed\r
@@ -360,48 +281,145 @@ S3BootScriptSmmEventCallBack (
   //\r
   // Check if it is already done\r
   //\r
-  if (mS3BootScriptTablePtr == &mS3BootScriptTable) {\r
+  if (mS3BootScriptTablePtr == mS3BootScriptTableSmmPtr) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
   //\r
-  // Last chance to call-out, just make sure AtRuntime is set\r
+  // Last chance to call-out, just make sure SmmLocked is set.\r
   //\r
   S3BootScriptEventCallBack (NULL, NULL);\r
 \r
   //\r
-  // Save a local copy\r
+  // 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
+\r
+    //\r
+    // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.\r
+    // InSmm will only be checked if SmmLocked is TRUE.\r
+    //\r
+    mS3BootScriptTableSmmPtr->InSmm = TRUE;\r
+  }\r
+  //\r
+  // We should not use ACPI Reserved copy, because it is not safe.\r
+  //\r
+  mS3BootScriptTablePtr = mS3BootScriptTableSmmPtr;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This function is to save boot time boot script data to LockBox.\r
+\r
+  Because there may be INSERT boot script at runtime in SMM.\r
+  The boot time copy will be used to restore data after back from S3.\r
+  Otherwise the data inserted may cause some boot time boot script data lost\r
+  if only BootScriptData used.\r
+\r
+**/\r
+VOID\r
+SaveBootTimeDataToLockBox (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  //\r
+  // ACPI Reserved copy is not safe, restore from BootScriptData LockBox first,\r
+  // and then save the data to BootScriptDataBootTime LockBox.\r
   //\r
-  CopyMem (&mS3BootScriptTable, mS3BootScriptTablePtr, sizeof(*mS3BootScriptTablePtr));\r
+  Status = RestoreLockBox (\r
+             &mBootScriptDataGuid,\r
+             NULL,\r
+             NULL\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   //\r
-  // We should not use ACPINvs copy, because it is not safe.\r
+  // Save BootScriptDataBootTime\r
+  // It will be used to restore data after back from S3.\r
   //\r
-  mS3BootScriptTablePtr = &mS3BootScriptTable;\r
+  Status = SaveLockBox (\r
+             &mBootScriptDataBootTimeGuid,\r
+             (VOID *) mS3BootScriptTablePtr->TableBase,\r
+             mS3BootScriptTablePtr->BootTimeScriptLength\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+  This function save boot script SMM private data to LockBox with BackFromS3 = TRUE at runtime.\r
+  S3 resume will help restore it to tell the Library the system is back from S3.\r
+\r
+**/\r
+VOID\r
+SaveSmmPriviateDataToLockBoxAtRuntime (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
 \r
   //\r
-  // Set InSmm, we allow boot script update when InSmm, but not allow boot script outside SMM.\r
-  // InSmm will only be checked if AtRuntime is TRUE.\r
+  // Save boot script SMM private data with BackFromS3 = TRUE.\r
   //\r
-  mS3BootScriptTablePtr->InSmm = TRUE;\r
+  mS3BootScriptTablePtr->BackFromS3 = TRUE;\r
+  Status = SaveLockBox (\r
+             &mBootScriptSmmPrivateDataGuid,\r
+             (VOID *) mS3BootScriptTablePtr,\r
+             sizeof (SCRIPT_TABLE_PRIVATE_DATA)\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = SetLockBoxAttributes (&mBootScriptSmmPrivateDataGuid, LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE);\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
-  // Record LockBoxLength\r
+  // Set BackFromS3 flag back to FALSE to indicate that now is not back from S3.\r
   //\r
-  mLockBoxLength = mS3BootScriptTable.TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE);\r
+  mS3BootScriptTablePtr->BackFromS3 = FALSE;\r
+}\r
+\r
+/**\r
+  This is the Event call back function is triggered in SMM to notify the Library\r
+  the system is entering runtime phase.\r
+\r
+  @param[in] Protocol   Points to the protocol's unique identifier\r
+  @param[in] Interface  Points to the interface instance\r
+  @param[in] Handle     The handle on which the interface was installed\r
+\r
+  @retval EFI_SUCCESS SmmAtRuntimeCallBack runs successfully\r
+ **/\r
+EFI_STATUS\r
+EFIAPI\r
+S3BootScriptSmmAtRuntimeCallBack (\r
+  IN CONST EFI_GUID     *Protocol,\r
+  IN VOID               *Interface,\r
+  IN EFI_HANDLE         Handle\r
+  )\r
+{\r
+  if (!mS3BootScriptTablePtr->AtRuntime) {\r
+    mS3BootScriptTablePtr->BootTimeScriptLength = (UINT32) (mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
+    SaveBootTimeDataToLockBox ();\r
+\r
+    mS3BootScriptTablePtr->AtRuntime = TRUE;\r
+    SaveSmmPriviateDataToLockBoxAtRuntime ();\r
+  }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   Library Constructor.\r
-  this function just identify it is a smm driver or non-smm driver linked against \r
-  with the library   \r
+  this function just identify it is a smm driver or non-smm driver linked against\r
+  with the library\r
 \r
   @param  ImageHandle   The firmware allocated handle for the EFI image.\r
   @param  SystemTable   A pointer to the EFI System Table.\r
 \r
-  @retval  RETURN_SUCCESS            Allocate the global memory space to store S3 boot script table private data\r
-  @retval  RETURN_OUT_OF_RESOURCES   No enough memory to allocated.\r
+  @retval RETURN_SUCCESS    The constructor always returns RETURN_SUCCESS.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -412,10 +430,10 @@ S3BootScriptLibInitialize (
 {\r
   EFI_STATUS                      Status;\r
   SCRIPT_TABLE_PRIVATE_DATA      *S3TablePtr;\r
+  SCRIPT_TABLE_PRIVATE_DATA      *S3TableSmmPtr;\r
   VOID                           *Registration;\r
   EFI_SMM_BASE2_PROTOCOL         *SmmBase2;\r
   BOOLEAN                        InSmm;\r
-  EFI_SMM_SYSTEM_TABLE2          *Smst;\r
   EFI_PHYSICAL_ADDRESS           Buffer;\r
 \r
   S3TablePtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateDataPtr);\r
@@ -426,29 +444,29 @@ S3BootScriptLibInitialize (
     Buffer = SIZE_4GB - 1;\r
     Status = gBS->AllocatePages (\r
                     AllocateMaxAddress,\r
-                    EfiACPIMemoryNVS,\r
+                    EfiReservedMemoryType,\r
                     EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)),\r
                     &Buffer\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      return RETURN_OUT_OF_RESOURCES;\r
-    }\r
+    ASSERT_EFI_ERROR (Status);\r
+    mS3BootScriptTableAllocated = TRUE;\r
     S3TablePtr = (VOID *) (UINTN) Buffer;\r
 \r
-    PcdSet64 (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr); \r
-    ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));  \r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, (UINT64) (UINTN)S3TablePtr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    ZeroMem (S3TablePtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));\r
     //\r
-    // create event to notify the library system enter the runtime phase\r
+    // Create event to notify the library system enter the SmmLocked phase.\r
     //\r
-    mEnterRuntimeEvent = EfiCreateProtocolNotifyEvent  (\r
-                          &gEfiDxeSmmReadyToLockProtocolGuid,\r
-                          TPL_CALLBACK,\r
-                          S3BootScriptEventCallBack,\r
-                          NULL,\r
-                          &Registration\r
-                          );\r
-    ASSERT (mEnterRuntimeEvent != NULL);\r
-  } \r
+    mEventDxeSmmReadyToLock = EfiCreateProtocolNotifyEvent (\r
+                                &gEfiDxeSmmReadyToLockProtocolGuid,\r
+                                TPL_CALLBACK,\r
+                                S3BootScriptEventCallBack,\r
+                                NULL,\r
+                                &Registration\r
+                                );\r
+    ASSERT (mEventDxeSmmReadyToLock != NULL);\r
+  }\r
   mS3BootScriptTablePtr = S3TablePtr;\r
 \r
   //\r
@@ -468,33 +486,156 @@ S3BootScriptLibInitialize (
   //\r
   // Good, we are in SMM\r
   //\r
-  Status = SmmBase2->GetSmstLocation (SmmBase2, &Smst);\r
+  Status = SmmBase2->GetSmstLocation (SmmBase2, &mBootScriptSmst);\r
   if (EFI_ERROR (Status)) {\r
     return RETURN_SUCCESS;\r
   }\r
 \r
+  S3TableSmmPtr = (SCRIPT_TABLE_PRIVATE_DATA*)(UINTN)PcdGet64(PcdS3BootScriptTablePrivateSmmDataPtr);\r
   //\r
-  // Then register event after lock\r
+  // The Boot script private data in SMM is not be initialized. create it\r
   //\r
-  Registration = NULL;\r
-  Status = Smst->SmmRegisterProtocolNotify (\r
-                   &gEfiSmmReadyToLockProtocolGuid,\r
-                   S3BootScriptSmmEventCallBack,\r
-                   &Registration\r
-                   );\r
+  if (S3TableSmmPtr == 0) {\r
+    Status = mBootScriptSmst->SmmAllocatePool (\r
+                                EfiRuntimeServicesData,\r
+                                sizeof(SCRIPT_TABLE_PRIVATE_DATA),\r
+                                (VOID **) &S3TableSmmPtr\r
+                                );\r
+    ASSERT_EFI_ERROR (Status);\r
+    mS3BootScriptTableSmmAllocated = TRUE;\r
+\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, (UINT64) (UINTN)S3TableSmmPtr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    ZeroMem (S3TableSmmPtr, sizeof(SCRIPT_TABLE_PRIVATE_DATA));\r
+\r
+    //\r
+    // Register SmmExitBootServices and SmmLegacyBoot notification.\r
+    //\r
+    Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                &gEdkiiSmmExitBootServicesProtocolGuid,\r
+                                S3BootScriptSmmAtRuntimeCallBack,\r
+                                &mRegistrationSmmExitBootServices\r
+                                );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                &gEdkiiSmmLegacyBootProtocolGuid,\r
+                                S3BootScriptSmmAtRuntimeCallBack,\r
+                                &mRegistrationSmmLegacyBoot\r
+                                );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  mS3BootScriptTableSmmPtr = S3TableSmmPtr;\r
+\r
+  //\r
+  // Register SmmReadyToLock notification.\r
+  //\r
+  Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                              &gEfiSmmReadyToLockProtocolGuid,\r
+                              S3BootScriptSmmEventCallBack,\r
+                              &mRegistrationSmmReadyToLock\r
+                              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Library Destructor to free the resources allocated by\r
+  S3BootScriptLibInitialize() and unregister callbacks.\r
+\r
+  NOTICE: The destructor doesn't support unloading as a separate action, and it\r
+  only supports unloading if the containing driver's entry point function fails.\r
+\r
+  @param ImageHandle        The firmware allocated handle for the EFI image.\r
+  @param SystemTable        A pointer to the EFI System Table.\r
+\r
+  @retval RETURN_SUCCESS    The destructor always returns RETURN_SUCCESS.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+S3BootScriptLibDeinitialize (\r
+  IN EFI_HANDLE             ImageHandle,\r
+  IN EFI_SYSTEM_TABLE       *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+\r
+  DEBUG ((EFI_D_INFO, "%a() in %a module\n", __FUNCTION__, gEfiCallerBaseName));\r
+\r
+  if (mEventDxeSmmReadyToLock != NULL) {\r
+    //\r
+    // Close the DxeSmmReadyToLock event.\r
+    //\r
+    Status = gBS->CloseEvent (mEventDxeSmmReadyToLock);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  if (mBootScriptSmst != NULL) {\r
+    if (mRegistrationSmmExitBootServices != NULL) {\r
+      //\r
+      // Unregister SmmExitBootServices notification.\r
+      //\r
+      Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                  &gEdkiiSmmExitBootServicesProtocolGuid,\r
+                                  NULL,\r
+                                  &mRegistrationSmmExitBootServices\r
+                                  );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+    if (mRegistrationSmmLegacyBoot != NULL) {\r
+      //\r
+      // Unregister SmmLegacyBoot notification.\r
+      //\r
+      Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                  &gEdkiiSmmLegacyBootProtocolGuid,\r
+                                  NULL,\r
+                                  &mRegistrationSmmLegacyBoot\r
+                                  );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+    if (mRegistrationSmmReadyToLock != NULL) {\r
+      //\r
+      // Unregister SmmReadyToLock notification.\r
+      //\r
+      Status = mBootScriptSmst->SmmRegisterProtocolNotify (\r
+                                  &gEfiSmmReadyToLockProtocolGuid,\r
+                                  NULL,\r
+                                  &mRegistrationSmmReadyToLock\r
+                                  );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  }\r
+\r
+  //\r
+  // Free the resources allocated and set PCDs to 0.\r
+  //\r
+  if (mS3BootScriptTableAllocated) {\r
+    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) mS3BootScriptTablePtr, EFI_SIZE_TO_PAGES(sizeof(SCRIPT_TABLE_PRIVATE_DATA)));\r
+    ASSERT_EFI_ERROR (Status);\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateDataPtr, 0);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  if ((mBootScriptSmst != NULL) && mS3BootScriptTableSmmAllocated) {\r
+    Status = mBootScriptSmst->SmmFreePool (mS3BootScriptTableSmmPtr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    Status = PcdSet64S (PcdS3BootScriptTablePrivateSmmDataPtr, 0);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
 /**\r
   To get the start address from which a new boot time s3 boot script entry will write into.\r
   If the table is not exist, the functio will first allocate a buffer for the table\r
-  If the table buffer is not enough for the new entry, in non-smm mode, the funtion will \r
+  If the table buffer is not enough for the new entry, in non-smm mode, the funtion will\r
   invoke reallocate to enlarge buffer.\r
-  \r
+\r
   @param EntryLength      the new entry length.\r
-  \r
-  @retval the address from which the a new s3 boot script entry will write into \r
+\r
+  @retval the address from which the a new s3 boot script entry will write into\r
  **/\r
 UINT8*\r
 S3BootScriptGetBootTimeEntryAddAddress (\r
@@ -508,21 +649,21 @@ S3BootScriptGetBootTimeEntryAddAddress (
    UINT16                            PageNumber;\r
    EFI_STATUS                        Status;\r
    EFI_BOOT_SCRIPT_TABLE_HEADER      *ScriptTableInfo;\r
-   \r
+\r
    S3TableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)(mS3BootScriptTablePtr->TableBase);\r
    if (S3TableBase == 0) {\r
-     // The table is not exist. This is the first to add entry. \r
-     // Allocate ACPI script table space under 4G memory. We need it to save\r
-     // some settings done by CSM, which runs after normal script table closed\r
+     //\r
+     // 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
-                  EfiACPIMemoryNVS,\r
+                  EfiReservedMemoryType,\r
                   2 + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),\r
                   (EFI_PHYSICAL_ADDRESS*)&S3TableBase\r
                   );\r
-     \r
+\r
      if (EFI_ERROR(Status)) {\r
        ASSERT_EFI_ERROR (Status);\r
        return 0;\r
@@ -533,64 +674,65 @@ S3BootScriptGetBootTimeEntryAddAddress (
      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
+\r
    // Here we do not count the reserved memory for runtime script table.\r
-   PageNumber   = (UINT16)(mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));   \r
+   PageNumber = (UINT16) (mS3BootScriptTablePtr->TableMemoryPageNumber - PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));\r
    TableLength =  mS3BootScriptTablePtr->TableLength;\r
-   if ((UINT32)(PageNumber * EFI_PAGE_SIZE) < (TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE))) {\r
-     // \r
+   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
-                  EfiACPIMemoryNVS,\r
+                  EfiReservedMemoryType,\r
                   2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber),\r
                   (EFI_PHYSICAL_ADDRESS*)&NewS3TableBase\r
                   );\r
-   \r
+\r
      if (EFI_ERROR(Status)) {\r
        ASSERT_EFI_ERROR (Status);\r
        return 0;\r
      }\r
-     \r
+\r
      CopyMem ((VOID*)(UINTN)NewS3TableBase, (VOID*)(UINTN)S3TableBase, TableLength);\r
      gBS->FreePages (S3TableBase, mS3BootScriptTablePtr->TableMemoryPageNumber);\r
-         \r
+\r
      mS3BootScriptTablePtr->TableBase = (UINT8*)(UINTN)NewS3TableBase;\r
-     mS3BootScriptTablePtr->TableMemoryPageNumber =  (UINT16) (2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber)); \r
+     mS3BootScriptTablePtr->TableMemoryPageNumber =  (UINT16) (2 + PageNumber + PcdGet16(PcdS3BootScriptRuntimeTableReservePageNumber));\r
    }\r
    //\r
-   // calculate the the start address for the new entry. \r
+   // calculate the the start address for the new entry.\r
    //\r
    NewEntryPtr = mS3BootScriptTablePtr->TableBase + TableLength;\r
-   \r
+\r
    //\r
    // update the table lenghth\r
    //\r
    mS3BootScriptTablePtr->TableLength =  TableLength + EntryLength;\r
-   \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
+   // table until the callers think there is no boot time data that should be added and\r
+   // it is caller's responsibility to explicit call the CloseTable.\r
    //\r
    //\r
-  \r
-   return NewEntryPtr;    \r
+\r
+   return NewEntryPtr;\r
 }\r
 /**\r
-  To get the start address from which a new runtime s3 boot script entry will write into.\r
+  To get the start address from which a new runtime(after SmmReadyToLock) s3 boot script entry will write into.\r
   In this case, it should be ensured that there is enough buffer to hold the entry.\r
-  \r
+\r
   @param EntryLength      the new entry length.\r
-  \r
-  @retval the address from which the a new s3 runtime script entry will write into\r
+\r
+  @retval the address from which the a new s3 runtime(after SmmReadyToLock) script entry will write into\r
  **/\r
 UINT8*\r
 S3BootScriptGetRuntimeEntryAddAddress (\r
@@ -598,138 +740,180 @@ S3BootScriptGetRuntimeEntryAddAddress (
   )\r
 {\r
    UINT8     *NewEntryPtr;\r
-   \r
-   NewEntryPtr = NULL;   \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
+   // Check if the memory range reserved for S3 Boot Script table is large enough to hold the node.\r
    //\r
-   if (mS3BootScriptTablePtr->TableLength + EntryLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE) <= EFI_PAGES_TO_SIZE((UINT32)(mS3BootScriptTablePtr->TableMemoryPageNumber))) {\r
-     NewEntryPtr = mS3BootScriptTablePtr->TableBase + mS3BootScriptTablePtr->TableLength;   \r
+   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
-   return (UINT8*)NewEntryPtr;    \r
+   return (UINT8*)NewEntryPtr;\r
+}\r
+\r
+/**\r
+  This function is to restore boot time boot script data from LockBox.\r
+\r
+**/\r
+VOID\r
+RestoreBootTimeDataFromLockBox (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+  UINTN         LockBoxLength;\r
+\r
+  //\r
+  // Restore boot time boot script data from LockBox.\r
+  //\r
+  LockBoxLength = mS3BootScriptTablePtr->BootTimeScriptLength;\r
+  Status = RestoreLockBox (\r
+             &mBootScriptDataBootTimeGuid,\r
+             (VOID *) mS3BootScriptTablePtr->TableBase,\r
+             &LockBoxLength\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Update the data to BootScriptData LockBox.\r
+  //\r
+  Status = UpdateLockBox (\r
+             &mBootScriptDataGuid,\r
+             0,\r
+             (VOID *) mS3BootScriptTablePtr->TableBase,\r
+             LockBoxLength\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Update TableLength.\r
+  //\r
+  mS3BootScriptTablePtr->TableLength = (UINT32) (mS3BootScriptTablePtr->BootTimeScriptLength - sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
 }\r
+\r
 /**\r
   To get the start address from which a new s3 boot script entry will write into.\r
-  \r
+\r
   @param EntryLength      the new entry length.\r
-  \r
-  @retval the address from which the a new s3 runtime script entry will write into \r
- **/ \r
-UINT8* \r
+\r
+  @retval the address from which the a new s3 boot script entry will write into\r
+ **/\r
+UINT8*\r
 S3BootScriptGetEntryAddAddress (\r
   UINT8  EntryLength\r
   )\r
 {\r
   UINT8*                         NewEntryPtr;\r
-  EFI_BOOT_SCRIPT_TABLE_HEADER   TableHeader;\r
-  EFI_STATUS                     Status;\r
 \r
-  if (mS3BootScriptTablePtr->AtRuntime) {\r
+  if (mS3BootScriptTablePtr->SmmLocked) {\r
     //\r
-    // We need check InSmm when AtRuntime, because after SmmReadyToLock, only SMM driver is allowed to write boot script.\r
+    // We need check InSmm, because after SmmReadyToLock, only SMM driver is allowed to write boot script.\r
     //\r
     if (!mS3BootScriptTablePtr->InSmm) {\r
       //\r
-      // Add DEBUG ERROR, so that we can find it at boot time.\r
-      // Do not use ASSERT, because we may have test invoke this interface.\r
+      // Add DEBUG ERROR, so that we can find it after SmmReadyToLock.\r
+      // Do not use ASSERT, because we may have test to invoke this interface.\r
       //\r
-      DEBUG ((EFI_D_ERROR, "FATAL ERROR: Set boot script after ReadyToLock!!!\n"));\r
+      DEBUG ((EFI_D_ERROR, "FATAL ERROR: Set boot script outside SMM after SmmReadyToLock!!!\n"));\r
       return NULL;\r
     }\r
 \r
-    //\r
-    // NOTE: OS will restore ACPINvs data. After S3, the table length in mS3BootScriptTable (SMM) is different with\r
-    // table length in BootScriptTable header (ACPINvs).\r
-    // So here we need sync them. We choose ACPINvs table length, because we want to override the boot script saved\r
-    // in SMM every time.\r
-    //\r
-    ASSERT (mS3BootScriptTablePtr == &mS3BootScriptTable);\r
-    CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
-    if (mS3BootScriptTablePtr->TableLength + sizeof(EFI_BOOT_SCRIPT_TERMINATE) != TableHeader.TableLength) {\r
-      //\r
-      // Restore it to use original value\r
-      //\r
-      RestoreLockBox (&mBootScriptDataGuid, NULL, NULL);\r
+    if (mS3BootScriptTablePtr->BackFromS3) {\r
       //\r
-      // Copy it again to get original value\r
-      // NOTE: We should NOT use TableHeader.TableLength, because it is already updated to be whole length.\r
+      // Back from S3, restore boot time boot script data from LockBox\r
+      // and set BackFromS3 flag back to FALSE.\r
       //\r
-      mS3BootScriptTablePtr->TableLength = (UINT32)(mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE));\r
+      RestoreBootTimeDataFromLockBox ();\r
+      mS3BootScriptTablePtr->BackFromS3 = FALSE;\r
     }\r
 \r
     NewEntryPtr  = S3BootScriptGetRuntimeEntryAddAddress (EntryLength);\r
-    //\r
-    // Now the length field is updated, need sync to lockbox.\r
-    // So in S3 resume, the data can be restored correctly.\r
-    //\r
-    CopyMem ((VOID*)&TableHeader, (VOID*)mS3BootScriptTablePtr->TableBase, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
-    Status = UpdateLockBox (\r
-               &mBootScriptDataGuid,\r
-               OFFSET_OF(EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),\r
-               &TableHeader.TableLength,\r
-               sizeof(TableHeader.TableLength)\r
-               );\r
-    ASSERT_EFI_ERROR (Status);\r
-  } else {   \r
+  } else {\r
     NewEntryPtr  = S3BootScriptGetBootTimeEntryAddAddress (EntryLength);\r
-  }  \r
+  }\r
   return NewEntryPtr;\r
-  \r
-}  \r
+\r
+}\r
 \r
 /**\r
   Sync BootScript LockBox data.\r
+\r
+  @param Script           The address from where the boot script has been added or updated.\r
+\r
 **/\r
 VOID\r
 SyncBootScript (\r
-  VOID\r
+  IN UINT8      *Script\r
   )\r
 {\r
   EFI_STATUS  Status;\r
+  UINT32      ScriptOffset;\r
+  UINT32      TotalScriptLength;\r
 \r
-  if (!mS3BootScriptTablePtr->AtRuntime || !mS3BootScriptTablePtr->InSmm) {\r
+  if (!mS3BootScriptTablePtr->SmmLocked || !mS3BootScriptTablePtr->InSmm) {\r
+    //\r
+    // If it is not after SmmReadyToLock in SMM,\r
+    // just return.\r
+    //\r
     return ;\r
   }\r
+\r
+  ScriptOffset = (UINT32) (Script - mS3BootScriptTablePtr->TableBase);\r
+\r
+  TotalScriptLength = (UINT32) (mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE));\r
+\r
   //\r
-  // Update Terminate\r
+  // Update BootScriptData\r
   // So in S3 resume, the data can be restored correctly.\r
   //\r
   Status = UpdateLockBox (\r
              &mBootScriptDataGuid,\r
-             mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE),\r
-             (VOID *)((UINTN)mS3BootScriptTablePtr->TableBase + mLockBoxLength - sizeof(EFI_BOOT_SCRIPT_TERMINATE)),\r
-             sizeof(EFI_BOOT_SCRIPT_TERMINATE)\r
+             ScriptOffset,\r
+             (VOID *)((UINTN)mS3BootScriptTablePtr->TableBase + ScriptOffset),\r
+             TotalScriptLength - ScriptOffset\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Now the length field is updated, need sync to lockbox.\r
+  // So at S3 resume, the data can be restored correctly.\r
+  //\r
+  Status = UpdateLockBox (\r
+             &mBootScriptDataGuid,\r
+             OFFSET_OF (EFI_BOOT_SCRIPT_TABLE_HEADER, TableLength),\r
+             &TotalScriptLength,\r
+             sizeof (TotalScriptLength)\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
-/** \r
-  This is an function to close the S3 boot script table. The function could only be called in \r
-  BOOT time phase. To comply with the Framework spec definition on \r
+/**\r
+  This is an function to close the S3 boot script table. The function could only be called in\r
+  BOOT time phase. To comply with the Framework spec definition on\r
   EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable(), this function will fulfill following things:\r
   1. Closes the specified boot script table\r
-  2. It allocates a new memory pool to duplicate all the boot scripts in the specified table. \r
-     Once this function is called, the table maintained by the library will be destroyed \r
+  2. It allocates a new memory pool to duplicate all the boot scripts in the specified table.\r
+     Once this function is called, the table maintained by the library will be destroyed\r
      after it is copied into the allocated pool.\r
-  3. Any attempts to add a script record after calling this function will cause a new table \r
+  3. Any attempts to add a script record after calling this function will cause a new table\r
      to be created by the library.\r
-  4. The base address of the allocated pool will be returned in Address. Note that after \r
+  4. The base address of the allocated pool will be returned in Address. Note that after\r
      using the boot script table, the CALLER is responsible for freeing the pool that is allocated\r
-     by this function. \r
+     by this function.\r
 \r
-  In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is \r
+  In Spec PI1.1, this EFI_BOOT_SCRIPT_SAVE_PROTOCOL.CloseTable() is retired. To provides this API for now is\r
   for Framework Spec compatibility.\r
-  \r
-  If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out \r
-  how to get the script to run on an S3 resume because the boot script maintained by the lib will be \r
+\r
+  If anyone does call CloseTable() on a real platform, then the caller is responsible for figuring out\r
+  how to get the script to run at S3 resume because the boot script maintained by the lib will be\r
   destroyed.\r
\r
-  @return the base address of the new copy of the boot script tble.   \r
+\r
+  @return the base address of the new copy of the boot script table.\r
   @note this function could only called in boot time phase\r
 \r
 **/\r
@@ -744,10 +928,10 @@ S3BootScriptCloseTable (
   UINT8                          *Buffer;\r
   EFI_STATUS                      Status;\r
   EFI_BOOT_SCRIPT_TABLE_HEADER      *ScriptTableInfo;\r
-  \r
-  S3TableBase =    mS3BootScriptTablePtr->TableBase;    \r
+\r
+  S3TableBase =    mS3BootScriptTablePtr->TableBase;\r
   if (S3TableBase == 0) {\r
-    return 0; \r
+    return 0;\r
   }\r
   //\r
   // Append the termination record the S3 boot script table\r
@@ -755,7 +939,7 @@ S3BootScriptCloseTable (
   S3BootScriptInternalCloseTable();\r
   TableLength = mS3BootScriptTablePtr->TableLength + sizeof (EFI_BOOT_SCRIPT_TERMINATE);\r
   //\r
-  // Allocate the buffer and copy the boot script to the buffer. \r
+  // Allocate the buffer and copy the boot script to the buffer.\r
   //\r
   Status = gBS->AllocatePool (\r
                   EfiBootServicesData,\r
@@ -763,12 +947,12 @@ S3BootScriptCloseTable (
                   (VOID **) &Buffer\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-        return 0; \r
+        return 0;\r
   }\r
   CopyMem (Buffer, S3TableBase, TableLength);\r
-  \r
+\r
   //\r
-  // Destroy the table maintained by the library so that the next write operation \r
+  // Destroy the table maintained by the library so that the next write operation\r
   // will write the record to the first entry of the table.\r
   //\r
   // Fill the table header.\r
@@ -776,12 +960,12 @@ S3BootScriptCloseTable (
   ScriptTableInfo->OpCode      = S3_BOOT_SCRIPT_LIB_TABLE_OPCODE;\r
   ScriptTableInfo->Length      = (UINT8) sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
   ScriptTableInfo->TableLength = 0;   // will be calculate at close the table\r
-  \r
+\r
   mS3BootScriptTablePtr->TableLength = sizeof (EFI_BOOT_SCRIPT_TABLE_HEADER);\r
   return Buffer;\r
 }\r
 /**\r
-  Save I/O write to boot script \r
+  Save I/O write to boot script\r
 \r
   @param Width   The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
   @param Address The base address of the I/O operations.\r
@@ -808,7 +992,7 @@ S3BootScriptSaveIoWrite (
 \r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
@@ -824,7 +1008,7 @@ S3BootScriptSaveIoWrite (
   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 ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -856,7 +1040,7 @@ S3BootScriptSaveIoReadWrite (
 \r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
@@ -868,12 +1052,12 @@ S3BootScriptSaveIoReadWrite (
   ScriptIoReadWrite.Length  = Length;\r
   ScriptIoReadWrite.Width   = Width;\r
   ScriptIoReadWrite.Address = Address;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptIoReadWrite, sizeof(EFI_BOOT_SCRIPT_IO_READ_WRITE));\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE)), Data, WidthInByte);\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_IO_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
 \r
-  SyncBootScript ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -901,14 +1085,14 @@ S3BootScriptSaveMemWrite (
   UINT8                *Script;\r
   UINT8                 WidthInByte;\r
   EFI_BOOT_SCRIPT_MEM_WRITE  ScriptMemWrite;\r
-  \r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
   //\r
@@ -917,11 +1101,11 @@ S3BootScriptSaveMemWrite (
   ScriptMemWrite.Width    = Width;\r
   ScriptMemWrite.Address  = Address;\r
   ScriptMemWrite.Count    = (UINT32) Count;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptMemWrite, sizeof(EFI_BOOT_SCRIPT_MEM_WRITE));\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_WRITE)), Buffer, WidthInByte * Count);\r
-  \r
-  SyncBootScript ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -949,27 +1133,27 @@ S3BootScriptSaveMemReadWrite (
   UINT8                *Script;\r
   UINT8                 WidthInByte;\r
   EFI_BOOT_SCRIPT_MEM_READ_WRITE  ScriptMemReadWrite;\r
\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
   //\r
   // Build script data\r
-  //    \r
+  //\r
   ScriptMemReadWrite.OpCode   = EFI_BOOT_SCRIPT_MEM_READ_WRITE_OPCODE;\r
   ScriptMemReadWrite.Length   = Length;\r
   ScriptMemReadWrite.Width    = Width;\r
   ScriptMemReadWrite.Address  = Address;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptMemReadWrite , sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE));\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE)), Data, WidthInByte);\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_MEM_READ_WRITE) + WidthInByte), DataMask, WidthInByte);\r
 \r
-  SyncBootScript ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -983,6 +1167,8 @@ S3BootScriptSaveMemReadWrite (
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -998,13 +1184,19 @@ S3BootScriptSavePciCfgWrite (
   UINT8                 WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE  ScriptPciWrite;\r
 \r
+  if (Width == S3BootScriptWidthUint64 ||\r
+      Width == S3BootScriptWidthFifoUint64 ||\r
+      Width == S3BootScriptWidthFillUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
   //\r
   // Build script data\r
   //\r
@@ -1013,11 +1205,11 @@ S3BootScriptSavePciCfgWrite (
   ScriptPciWrite.Width    = Width;\r
   ScriptPciWrite.Address  = Address;\r
   ScriptPciWrite.Count    = (UINT32) Count;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite,  sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE));\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_WRITE)), Buffer, WidthInByte * Count);\r
-  \r
-  SyncBootScript ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -1031,6 +1223,8 @@ S3BootScriptSavePciCfgWrite (
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN__SUCCESS           Opcode is added.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -1046,21 +1240,27 @@ S3BootScriptSavePciCfgReadWrite (
   UINT8                 WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE  ScriptPciReadWrite;\r
 \r
+  if (Width == S3BootScriptWidthUint64 ||\r
+      Width == S3BootScriptWidthFifoUint64 ||\r
+      Width == S3BootScriptWidthFillUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
-  // \r
+  //\r
   ScriptPciReadWrite.OpCode   = EFI_BOOT_SCRIPT_PCI_CONFIG_READ_WRITE_OPCODE;\r
   ScriptPciReadWrite.Length   = Length;\r
   ScriptPciReadWrite.Width    = Width;\r
   ScriptPciReadWrite.Address  = Address;\r
-  \r
+\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
@@ -1069,12 +1269,12 @@ S3BootScriptSavePciCfgReadWrite (
     WidthInByte\r
     );\r
 \r
-  SyncBootScript ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 /**\r
-  Adds a record for a PCI configuration space modify operation into a specified boot script table.\r
+  Adds a record for a PCI configuration 2 space write operation into a specified boot script table.\r
 \r
   @param Width     The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
   @param Segment   The PCI segment number for Address.\r
@@ -1084,6 +1284,8 @@ S3BootScriptSavePciCfgReadWrite (
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -1099,14 +1301,20 @@ S3BootScriptSavePciCfg2Write (
   UINT8                *Script;\r
   UINT8                 WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE  ScriptPciWrite2;\r
-  \r
+\r
+  if (Width == S3BootScriptWidthUint64 ||\r
+      Width == S3BootScriptWidthFifoUint64 ||\r
+      Width == S3BootScriptWidthFillUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE) + (WidthInByte * Count));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
   //\r
@@ -1116,16 +1324,16 @@ S3BootScriptSavePciCfg2Write (
   ScriptPciWrite2.Address  = Address;\r
   ScriptPciWrite2.Segment  = Segment;\r
   ScriptPciWrite2.Count    = (UINT32)Count;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptPciWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE));\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE)), Buffer, WidthInByte * Count);\r
 \r
-  SyncBootScript ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 /**\r
-  Adds a record for a PCI configuration space modify operation into a specified boot script table.\r
+  Adds a record for a PCI configuration space modify operation into a specified boot script table.\r
 \r
   @param Width     The width of the I/O operations.Enumerated in S3_BOOT_SCRIPT_LIB_WIDTH.\r
   @param Segment   The PCI segment number for Address.\r
@@ -1135,6 +1343,8 @@ S3BootScriptSavePciCfg2Write (
 \r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -1150,14 +1360,20 @@ S3BootScriptSavePciCfg2ReadWrite (
   UINT8                *Script;\r
   UINT8                 WidthInByte;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE  ScriptPciReadWrite2;\r
-  \r
+\r
+  if (Width == S3BootScriptWidthUint64 ||\r
+      Width == S3BootScriptWidthFifoUint64 ||\r
+      Width == S3BootScriptWidthFillUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
   //\r
@@ -1166,7 +1382,7 @@ S3BootScriptSavePciCfg2ReadWrite (
   ScriptPciReadWrite2.Width    = Width;\r
   ScriptPciReadWrite2.Segment  = Segment;\r
   ScriptPciReadWrite2.Address  = Address;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptPciReadWrite2, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE));\r
   CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE)), Data, WidthInByte);\r
   CopyMem (\r
@@ -1174,11 +1390,112 @@ S3BootScriptSavePciCfg2ReadWrite (
     DataMask,\r
     WidthInByte\r
     );\r
-  \r
-  SyncBootScript ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Checks the parameter of S3BootScriptSaveSmbusExecute().\r
+\r
+  This function checks the input parameters of SmbusExecute().  If the input parameters are valid\r
+  for certain SMBus bus protocol, it will return EFI_SUCCESS; otherwise, it will return certain\r
+  error code based on the input SMBus bus protocol.\r
+\r
+  @param  SmBusAddress            Address that encodes the SMBUS Slave Address, SMBUS Command, SMBUS Data Length,\r
+                                  and PEC.\r
+  @param  Operation               Signifies which particular SMBus hardware protocol instance that\r
+                                  it will use to execute the SMBus transactions. This SMBus\r
+                                  hardware protocol is defined by the SMBus Specification and is\r
+                                  not related to EFI.\r
+  @param  Length                  Signifies the number of bytes that this operation will do. The\r
+                                  maximum number of bytes can be revision specific and operation\r
+                                  specific. This field will contain the actual number of bytes that\r
+                                  are executed for this operation. Not all operations require this\r
+                                  argument.\r
+  @param  Buffer                  Contains the value of data to execute to the SMBus slave device.\r
+                                  Not all operations require this argument. The length of this\r
+                                  buffer is identified by Length.\r
+\r
+  @retval EFI_SUCCESS             All the parameters are valid for the corresponding SMBus bus\r
+                                  protocol.\r
+  @retval EFI_INVALID_PARAMETER   Operation is not defined in EFI_SMBUS_OPERATION.\r
+  @retval EFI_INVALID_PARAMETER   Length/Buffer is NULL for operations except for EfiSmbusQuickRead\r
+                                  and EfiSmbusQuickWrite. Length is outside the range of valid\r
+                                  values.\r
+  @retval EFI_UNSUPPORTED         The SMBus operation or PEC is not supported.\r
+  @retval EFI_BUFFER_TOO_SMALL    Buffer is not sufficient for this operation.\r
+\r
+**/\r
+EFI_STATUS\r
+CheckParameters (\r
+  IN     UINTN                    SmBusAddress,\r
+  IN     EFI_SMBUS_OPERATION      Operation,\r
+  IN OUT UINTN                    *Length,\r
+  IN     VOID                     *Buffer\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       RequiredLen;\r
+  EFI_SMBUS_DEVICE_COMMAND Command;\r
+  BOOLEAN                  PecCheck;\r
+\r
+  Command      = SMBUS_LIB_COMMAND (SmBusAddress);\r
+  PecCheck     = SMBUS_LIB_PEC (SmBusAddress);\r
+  //\r
+  // Set default value to be 2:\r
+  // for SmbusReadWord, SmbusWriteWord and SmbusProcessCall.\r
+  //\r
+  RequiredLen = 2;\r
+  Status      = EFI_SUCCESS;\r
+  switch (Operation) {\r
+    case EfiSmbusQuickRead:\r
+    case EfiSmbusQuickWrite:\r
+      if (PecCheck || Command != 0) {\r
+        return EFI_UNSUPPORTED;\r
+      }\r
+      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
+    case EfiSmbusReadByte:\r
+    case EfiSmbusWriteByte:\r
+      RequiredLen = 1;\r
+      //\r
+      // Cascade to check length parameter.\r
+      //\r
+    case EfiSmbusReadWord:\r
+    case EfiSmbusWriteWord:\r
+    case EfiSmbusProcessCall:\r
+      if (Buffer == NULL || Length == NULL) {\r
+        return EFI_INVALID_PARAMETER;\r
+      } else if (*Length < RequiredLen) {\r
+        Status = EFI_BUFFER_TOO_SMALL;\r
+      }\r
+      *Length = RequiredLen;\r
+      break;\r
+    case EfiSmbusReadBlock:\r
+    case EfiSmbusWriteBlock:\r
+    case EfiSmbusBWBRProcessCall:\r
+      if ((Buffer == NULL) ||\r
+          (Length == NULL) ||\r
+          (*Length < MIN_SMBUS_BLOCK_LEN) ||\r
+          (*Length > MAX_SMBUS_BLOCK_LEN)) {\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+      break;\r
+    default:\r
+      return EFI_INVALID_PARAMETER;\r
+  }\r
+  return Status;\r
+}\r
+\r
 /**\r
   Adds a record for an SMBus command execution into a specified boot script table.\r
 \r
@@ -1187,25 +1504,38 @@ S3BootScriptSavePciCfg2ReadWrite (
                         transactions.\r
   @param Length         A pointer to signify the number of bytes that this operation will do.\r
   @param Buffer         Contains the value of data to execute to the SMBUS slave device.\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveSmbusExecute (\r
-  IN  UINTN                             SmBusAddress, \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_BOOT_SCRIPT_SMBUS_EXECUTE  ScriptSmbusExecute;\r
 \r
-  DataSize = (UINT8)(sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE) + (*Length));\r
-  \r
+  if (Length == NULL) {\r
+    BufferLength = 0;\r
+  } else {\r
+    BufferLength = *Length;\r
+  }\r
+\r
+  Status = CheckParameters (SmBusAddress, Operation, &BufferLength, Buffer);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  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
@@ -1217,16 +1547,16 @@ S3BootScriptSaveSmbusExecute (
   ScriptSmbusExecute.Length       = DataSize;\r
   ScriptSmbusExecute.SmBusAddress = (UINT64) SmBusAddress;\r
   ScriptSmbusExecute.Operation    = Operation;\r
-  ScriptSmbusExecute.DataSize     = (UINT32) *Length;\r
+  ScriptSmbusExecute.DataSize     = (UINT32) BufferLength;\r
 \r
   CopyMem ((VOID*)Script, (VOID*)&ScriptSmbusExecute, sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE));\r
   CopyMem (\r
     (VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_SMBUS_EXECUTE)),\r
     Buffer,\r
-    (*Length)\r
+    BufferLength\r
     );\r
 \r
-  SyncBootScript ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -1234,7 +1564,7 @@ S3BootScriptSaveSmbusExecute (
   Adds a record for an execution stall on the processor into a specified boot script table.\r
 \r
   @param Duration   Duration in microseconds of the stall\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
 **/\r
@@ -1249,30 +1579,30 @@ S3BootScriptSaveStall (
   EFI_BOOT_SCRIPT_STALL  ScriptStall;\r
 \r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_STALL));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
   //\r
   ScriptStall.OpCode    = EFI_BOOT_SCRIPT_STALL_OPCODE;\r
   ScriptStall.Length    = Length;\r
   ScriptStall.Duration  = Duration;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptStall, sizeof (EFI_BOOT_SCRIPT_STALL));\r
-  \r
-  SyncBootScript ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 /**\r
-  Adds a record for an execution stall on the processor into a specified boot script table.\r
+  Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
 \r
   @param EntryPoint   Entry point of the code to be dispatched.\r
   @param Context      Argument to be passed into the EntryPoint of the code to be dispatched.\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
 **/\r
@@ -1287,11 +1617,11 @@ S3BootScriptSaveDispatch2 (
   UINT8                 *Script;\r
   EFI_BOOT_SCRIPT_DISPATCH_2  ScriptDispatch2;\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
   //\r
@@ -1299,10 +1629,10 @@ S3BootScriptSaveDispatch2 (
   ScriptDispatch2.Length     = Length;\r
   ScriptDispatch2.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
   ScriptDispatch2.Context =   (EFI_PHYSICAL_ADDRESS)(UINTN)Context;\r
-  \r
+\r
   CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch2, sizeof (EFI_BOOT_SCRIPT_DISPATCH_2));\r
-  \r
-  SyncBootScript ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 \r
@@ -1310,7 +1640,12 @@ S3BootScriptSaveDispatch2 (
 /**\r
   Adds a record for memory reads of the memory location and continues when the exit criteria is\r
   satisfied or after a defined duration.\r
-  \r
+\r
+  Please aware, below interface is different with PI specification, Vol 5:\r
+  EFI_S3_SAVE_STATE_PROTOCOL.Write() for EFI_BOOT_SCRIPT_MEM_POLL_OPCODE.\r
+  "Duration" below is microseconds, while "Delay" in PI specification means\r
+  the number of 100ns units to poll.\r
+\r
   @param Width     The width of the memory operations.\r
   @param Address   The base address of the memory operations.\r
   @param BitMask   A pointer to the bit mask to be AND-ed with the data read from the register.\r
@@ -1330,18 +1665,18 @@ S3BootScriptSaveMemPoll (
   IN  VOID                              *BitMask,\r
   IN  VOID                              *BitValue,\r
   IN  UINTN                             Duration,\r
-  IN  UINT                            LoopTimes\r
+  IN  UINT64                            LoopTimes\r
   )\r
 {\r
   UINT8                 Length;\r
   UINT8                *Script;\r
-  UINT8                 WidthInByte; \r
-  EFI_BOOT_SCRIPT_MEM_POLL      ScriptMemPoll; \r
+  UINT8                 WidthInByte;\r
+  EFI_BOOT_SCRIPT_MEM_POLL      ScriptMemPoll;\r
 \r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
-  \r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_MEM_POLL) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
@@ -1351,27 +1686,26 @@ S3BootScriptSaveMemPoll (
   //\r
   ScriptMemPoll.OpCode   = EFI_BOOT_SCRIPT_MEM_POLL_OPCODE;\r
   ScriptMemPoll.Length   = Length;\r
-  ScriptMemPoll.Width    = Width;  \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 ((VOID*)Script, (VOID*)&ScriptMemPoll, sizeof (EFI_BOOT_SCRIPT_MEM_POLL));\r
 \r
-  SyncBootScript ();\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 /**\r
   Store arbitrary information in the boot script table. This opcode is a no-op on dispatch and is only\r
   used for debugging script issues.\r
-  \r
+\r
   @param InformationLength   Length of the data in bytes\r
   @param Information       Information to be logged in the boot scrpit\r
\r
-  @retval RETURN_UNSUPPORTED       If  entering runtime, this method will not support.\r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
 \r
@@ -1379,34 +1713,16 @@ S3BootScriptSaveMemPoll (
 RETURN_STATUS\r
 EFIAPI\r
 S3BootScriptSaveInformation (\r
-  IN  UINT32                                InformationLength, \r
+  IN  UINT32                                InformationLength,\r
   IN  VOID                                 *Information\r
   )\r
 {\r
-  RETURN_STATUS         Status;\r
   UINT8                 Length;\r
   UINT8                 *Script;\r
-  VOID                  *Buffer;\r
   EFI_BOOT_SCRIPT_INFORMATION  ScriptInformation;\r
 \r
-  if (mS3BootScriptTablePtr->AtRuntime) {\r
-    return RETURN_UNSUPPORTED;\r
-  }\r
-  Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
-  \r
-  //\r
-  // Use BootServicesData to hold the data, just in case caller free it.\r
-  // It will be copied into ACPINvs later.\r
-  //\r
-  Status = gBS->AllocatePool (\r
-                  EfiBootServicesData,\r
-                  InformationLength,\r
-                  &Buffer\r
-                  );\r
-  if (EFI_ERROR (Status)) {\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
@@ -1418,21 +1734,22 @@ S3BootScriptSaveInformation (
   ScriptInformation.Length     = Length;\r
 \r
 \r
-  ScriptInformation.InformationLength = InformationLength;  \r
+  ScriptInformation.InformationLength = InformationLength;\r
+\r
+  CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
+  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
+\r
+  SyncBootScript (Script);\r
 \r
-  CopyMem ((VOID *)(UINTN)Buffer, Information,(UINTN) InformationLength);  \r
-  ScriptInformation.Information = (EFI_PHYSICAL_ADDRESS) (UINTN) Buffer;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));  \r
   return RETURN_SUCCESS;\r
 \r
 }\r
 /**\r
   Store a string in the boot script table. This opcode is a no-op on dispatch and is only\r
   used for debugging script issues.\r
-  \r
+\r
   @param String            The string to save to boot script table\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
 \r
@@ -1443,8 +1760,8 @@ S3BootScriptSaveInformationAsciiString (
   IN  CONST CHAR8               *String\r
   )\r
 {\r
-  return S3BootScriptSaveInformation (      \r
-           (UINT32) AsciiStrLen (String) + 1, \r
+  return S3BootScriptSaveInformation (\r
+           (UINT32) AsciiStrLen (String) + 1,\r
            (VOID*) String\r
            );\r
 }\r
@@ -1452,7 +1769,7 @@ S3BootScriptSaveInformationAsciiString (
   Adds a record for dispatching specified arbitrary code into a specified boot script table.\r
 \r
   @param EntryPoint   Entry point of the code to be dispatched.\r
-  \r
+\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
 **/\r
@@ -1465,23 +1782,23 @@ S3BootScriptSaveDispatch (
   UINT8                 Length;\r
   UINT8                *Script;\r
   EFI_BOOT_SCRIPT_DISPATCH  ScriptDispatch;\r
-  \r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   //\r
   // Build script data\r
   //\r
   ScriptDispatch.OpCode     = EFI_BOOT_SCRIPT_DISPATCH_OPCODE;\r
   ScriptDispatch.Length     = Length;\r
   ScriptDispatch.EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)EntryPoint;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH)); \r
-  \r
-  SyncBootScript ();\r
+\r
+  CopyMem ((VOID*)Script, (VOID*)&ScriptDispatch, sizeof (EFI_BOOT_SCRIPT_DISPATCH));\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 \r
@@ -1489,8 +1806,8 @@ S3BootScriptSaveDispatch (
 /**\r
   Adds a record for I/O reads the I/O location and continues when the exit criteria is satisfied or after a\r
   defined duration.\r
-  \r
-  @param  Width                 The width of the I/O operations. \r
+\r
+  @param  Width                 The width of the I/O operations.\r
   @param  Address               The base address of the I/O operations.\r
   @param  Data                  The comparison value used for the polling exit criteria.\r
   @param  DataMask              Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
@@ -1508,37 +1825,37 @@ S3BootScriptSaveIoPoll (
   IN S3_BOOT_SCRIPT_LIB_WIDTH       Width,\r
   IN UINT64                     Address,\r
   IN VOID                      *Data,\r
-  IN VOID                      *DataMask, \r
-  IN UINT64                     Delay   \r
+  IN VOID                      *DataMask,\r
+  IN UINT64                     Delay\r
   )\r
 {\r
-  UINT8                 WidthInByte;  \r
+  UINT8                 WidthInByte;\r
   UINT8                *Script;\r
   UINT8                 Length;\r
   EFI_BOOT_SCRIPT_IO_POLL  ScriptIoPoll;\r
-  \r
 \r
-  WidthInByte = (UINT8) (0x01 << (Width & 0x03));  \r
+\r
+  WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_IO_POLL) + (WidthInByte * 2));\r
\r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
-  } \r
+  }\r
   //\r
   // Build script data\r
   //\r
   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.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 ((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 ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -1547,7 +1864,7 @@ S3BootScriptSaveIoPoll (
   Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
   after a defined duration.\r
 \r
-  @param  Width                 The width of the I/O operations. \r
+  @param  Width                 The width of the I/O operations.\r
   @param  Address               The address within the PCI configuration space.\r
   @param  Data                  The comparison value used for the polling exit criteria.\r
   @param  DataMask              Mask used for the polling criteria. The bits in the bytes below Width which are zero\r
@@ -1557,6 +1874,7 @@ S3BootScriptSaveIoPoll (
 \r
  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
  @retval RETURN_SUCCESS           Opcode is added.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
 \r
 **/\r
 RETURN_STATUS\r
@@ -1570,13 +1888,19 @@ S3BootScriptSavePciPoll (
 )\r
 {\r
   UINT8                   *Script;\r
-  UINT8                    WidthInByte;  \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
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + (WidthInByte * 2));\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
@@ -1586,15 +1910,15 @@ S3BootScriptSavePciPoll (
   //\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.Width    = Width;\r
   ScriptPciPoll.Address  = Address;\r
   ScriptPciPoll.Delay    = Delay;\r
 \r
   CopyMem ((VOID*)Script, (VOID*)&ScriptPciPoll, sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL));\r
   CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL)), Data, WidthInByte);\r
   CopyMem ((UINT8 *) (Script + sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG_POLL) + WidthInByte), DataMask, WidthInByte);\r
-  \r
-  SyncBootScript ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -1602,7 +1926,7 @@ S3BootScriptSavePciPoll (
   Adds a record for PCI configuration space reads and continues when the exit criteria is satisfied or\r
   after a defined duration.\r
 \r
-  @param  Width                 The width of the I/O operations. \r
+  @param  Width                 The width of the I/O operations.\r
   @param  Segment               The PCI segment number for Address.\r
   @param  Address               The address within the PCI configuration space.\r
   @param  Data                  The comparison value used for the polling exit criteria.\r
@@ -1613,9 +1937,8 @@ S3BootScriptSavePciPoll (
 \r
  @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
  @retval RETURN_SUCCESS           Opcode is added.\r
- @note   A known Limitations in the implementation: When interpreting the opcode  EFI_BOOT_SCRIPT_PCI_CONFIG2_WRITE_OPCODE\r
-         EFI_BOOT_SCRIPT_PCI_CONFIG2_READ_WRITE_OPCODE and EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL_OPCODE, the 'Segment' parameter is assumed as \r
-         Zero, or else, assert.\r
+  @note  A known Limitations in the implementation which is 64bits operations are not supported.\r
+\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -1628,24 +1951,30 @@ S3BootScriptSavePci2Poll (
   IN UINT64                         Delay\r
 )\r
 {\r
-  UINT8                    WidthInByte;  \r
+  UINT8                    WidthInByte;\r
   UINT8                   *Script;\r
   UINT8                    Length;\r
   EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL  ScriptPci2Poll;\r
-  \r
+\r
+  if (Width == S3BootScriptWidthUint64 ||\r
+      Width == S3BootScriptWidthFifoUint64 ||\r
+      Width == S3BootScriptWidthFillUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   WidthInByte = (UINT8) (0x01 << (Width & 0x03));\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_PCI_CONFIG2_POLL) + (WidthInByte * 2));\r
-  \r
+\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.Width    = Width;\r
   ScriptPci2Poll.Segment  = Segment;\r
   ScriptPci2Poll.Address  = Address;\r
   ScriptPci2Poll.Delay    = Delay;\r
@@ -1653,17 +1982,17 @@ S3BootScriptSavePci2Poll (
   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 ();\r
+\r
+  SyncBootScript (Script);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 /**\r
   Do the calculation of start address from which a new s3 boot script entry will write into.\r
-  \r
+\r
   @param EntryLength      The new entry length.\r
   @param Position         specifies the position in the boot script table where the opcode will be\r
-                          inserted, either before or after, depending on BeforeOrAfter. \r
+                          inserted, either before or after, depending on BeforeOrAfter.\r
   @param BeforeOrAfter    The flag to indicate to insert the nod before or after the position.\r
                           This parameter is effective when InsertFlag is TRUE\r
   @param Script           return out the position from which the a new s3 boot script entry will write into\r
@@ -1673,58 +2002,58 @@ S3BootScriptCalculateInsertAddress (
   IN  UINT8     EntryLength,\r
   IN  VOID     *Position OPTIONAL,\r
   IN  BOOLEAN   BeforeOrAfter OPTIONAL,\r
-  OUT UINT8   **Script   \r
+  OUT UINT8   **Script\r
   )\r
 {\r
    UINTN                            TableLength;\r
    UINT8                            *S3TableBase;\r
-   UINTN                            PositionOffset; \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
+   //\r
    // calculate the Position offset\r
    //\r
    if (Position != NULL) {\r
-     PositionOffset = (UINTN) ((UINT8 *)Position - S3TableBase);\r
-   \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
+        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
+     CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);\r
      //\r
-     // calculate the the start address for the new entry. \r
+     // calculate the the start address for the new entry.\r
      //\r
      *Script = S3TableBase + PositionOffset;\r
-       \r
+\r
    } else {\r
      if (!BeforeOrAfter) {\r
        //\r
        //  Insert the node to the end of the table\r
        //\r
-       *Script = S3TableBase + TableLength; \r
+       *Script = S3TableBase + TableLength;\r
      } else {\r
-       // \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
+       CopyMem (S3TableBase+PositionOffset+EntryLength, S3TableBase+PositionOffset, TableLength - PositionOffset);\r
+       *Script = S3TableBase + PositionOffset;\r
      }\r
-   }       \r
+   }\r
 }\r
 /**\r
-  Move the last boot script entry to the position \r
+  Move the last boot script entry to the position\r
 \r
   @param  BeforeOrAfter         Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
                                 in the boot script table specified by Position. If Position is NULL or points to\r
@@ -1746,20 +2075,26 @@ S3BootScriptMoveLastOpcode (
 )\r
 {\r
   UINT8*                Script;\r
-  VOID                  *TempPosition;  \r
+  VOID                  *TempPosition;\r
   UINTN                 StartAddress;\r
   UINT32                TableLength;\r
   EFI_BOOT_SCRIPT_COMMON_HEADER  ScriptHeader;\r
   BOOLEAN               ValidatePosition;\r
   UINT8*                LastOpcode;\r
   UINT8                 TempBootScriptEntry[BOOT_SCRIPT_NODE_MAX_LENGTH];\r
-  \r
+\r
   ValidatePosition = FALSE;\r
   TempPosition = (Position == NULL) ? NULL:(*Position);\r
-  Script = mS3BootScriptTablePtr->TableBase;\r
-  if (Script == 0) {    \r
-    return EFI_OUT_OF_RESOURCES;\r
+\r
+  //\r
+  // Check that the script is initialized and synced without adding an entry to the script.\r
+  //\r
+  Script = S3BootScriptGetEntryAddAddress (0);\r
+  if (Script == NULL) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
   }\r
+  Script = mS3BootScriptTablePtr->TableBase;\r
+\r
   StartAddress  = (UINTN) Script;\r
   TableLength   = mS3BootScriptTablePtr->TableLength;\r
   Script        = Script + sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER);\r
@@ -1767,17 +2102,17 @@ S3BootScriptMoveLastOpcode (
   //\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
+  while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {\r
+    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));\r
     if (TempPosition != NULL && TempPosition == Script) {\r
       //\r
-      // If the position is specified, the position must be pointed to a boot script entry start address. \r
+      // If the position is specified, the position must be pointed to a boot script entry start address.\r
       //\r
       ValidatePosition = TRUE;\r
     }\r
     if (ScriptHeader.OpCode != S3_BOOT_SCRIPT_LIB_TERMINATE_OPCODE) {\r
       LastOpcode = Script;\r
-    } \r
+    }\r
     Script  = Script + ScriptHeader.Length;\r
   }\r
   //\r
@@ -1786,10 +2121,10 @@ S3BootScriptMoveLastOpcode (
   if (TempPosition != NULL && !ValidatePosition) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
-  \r
-  CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER)); \r
-  \r
-  CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length); \r
+\r
+  CopyMem ((VOID*)&ScriptHeader, LastOpcode, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));\r
+\r
+  CopyMem((VOID*)TempBootScriptEntry, LastOpcode, ScriptHeader.Length);\r
   //\r
   // Find the right position to write the node in\r
   //\r
@@ -1797,12 +2132,15 @@ S3BootScriptMoveLastOpcode (
     ScriptHeader.Length,\r
     TempPosition,\r
     BeforeOrAfter,\r
-    &Script   \r
+    &Script\r
   );\r
   //\r
   // Copy the node to Boot script table\r
   //\r
-  CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length); \r
+  CopyMem((VOID*)Script, (VOID*)TempBootScriptEntry, ScriptHeader.Length);\r
+\r
+  SyncBootScript (Script);\r
+\r
   //\r
   // return out the Position\r
   //\r
@@ -1812,18 +2150,18 @@ S3BootScriptMoveLastOpcode (
   return RETURN_SUCCESS;\r
 }\r
 /**\r
-  Create a Label node in the boot script table. \r
-  \r
+  Create a Label node in the boot script table.\r
+\r
   @param  BeforeOrAfter         Specifies whether the opcode is stored before (TRUE) or after (FALSE) the position\r
                                 in the boot script table specified by Position. If Position is NULL or points to\r
                                 NULL then the new opcode is inserted at the beginning of the table (if TRUE) or end\r
                                 of the table (if FALSE).\r
   @param  Position              On entry, specifies the position in the boot script table where the opcode will be\r
                                 inserted, either before or after, depending on BeforeOrAfter. On exit, specifies\r
-                                the position of the inserted opcode in the boot script table.  \r
+                                the position of the inserted opcode in the boot script table.\r
   @param InformationLength      Length of the label in bytes\r
   @param Information            Label to be logged in the boot scrpit\r
\r
+\r
   @retval RETURN_INVALID_PARAMETER The Position is not a valid position in the boot script table.\r
   @retval RETURN_OUT_OF_RESOURCES  Not enough memory for the table do operation.\r
   @retval RETURN_SUCCESS           Opcode is added.\r
@@ -1833,23 +2171,21 @@ RETURN_STATUS
 EFIAPI\r
 S3BootScriptLabelInternal (\r
   IN        BOOLEAN                        BeforeOrAfter,\r
-  IN OUT    VOID                         **Position OPTIONAL, \r
-  IN        UINT32                         InformationLength, \r
+  IN OUT    VOID                         **Position OPTIONAL,\r
+  IN        UINT32                         InformationLength,\r
   IN CONST  CHAR8                          *Information\r
   )\r
 {\r
   UINT8                 Length;\r
   UINT8                 *Script;\r
-  VOID                  *Buffer;\r
   EFI_BOOT_SCRIPT_INFORMATION  ScriptInformation;\r
\r
+\r
   Length = (UINT8)(sizeof (EFI_BOOT_SCRIPT_INFORMATION) + InformationLength);\r
-  \r
+\r
   Script = S3BootScriptGetEntryAddAddress (Length);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
-  Buffer =  Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION);\r
   //\r
   // Build script data\r
   //\r
@@ -1857,13 +2193,13 @@ S3BootScriptLabelInternal (
   ScriptInformation.Length     = Length;\r
 \r
 \r
-  ScriptInformation.InformationLength = InformationLength;  \r
+  ScriptInformation.InformationLength = InformationLength;\r
+\r
+  CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));\r
+  CopyMem ((VOID*)(Script + sizeof (EFI_BOOT_SCRIPT_INFORMATION)), (VOID *) Information, (UINTN) InformationLength);\r
+\r
+  SyncBootScript (Script);\r
 \r
-  AsciiStrnCpy (Buffer, Information,(UINTN) InformationLength);  \r
-  ScriptInformation.Information = (EFI_PHYSICAL_ADDRESS) (UINTN) Buffer;\r
-  \r
-  CopyMem ((VOID*)Script, (VOID*)&ScriptInformation, sizeof (EFI_BOOT_SCRIPT_INFORMATION));  \r
-  \r
   return S3BootScriptMoveLastOpcode (BeforeOrAfter, Position);\r
 \r
 }\r
@@ -1871,9 +2207,9 @@ S3BootScriptLabelInternal (
   Find a label within the boot script table and, if not present, optionally create it.\r
 \r
   @param  BeforeOrAfter         Specifies whether the opcode is stored before (TRUE)\r
-                                or after (FALSE) the position in the boot script table \r
+                                or after (FALSE) the position in the boot script table\r
                                 specified by Position.\r
-  @param  CreateIfNotFound      Specifies whether the label will be created if the label \r
+  @param  CreateIfNotFound      Specifies whether the label will be created if the label\r
                                 does not exists (TRUE) or not (FALSE).\r
   @param  Position              On entry, specifies the position in the boot script table\r
                                 where the opcode will be inserted, either before or after,\r
@@ -1884,13 +2220,13 @@ S3BootScriptLabelInternal (
   @retval EFI_SUCCESS           The operation succeeded. A record was added into the\r
                                 specified script table.\r
   @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
-                                If the opcode is unknow or not supported because of the PCD \r
+                                If the opcode is unknow or not supported because of the PCD\r
                                 Feature Flags.\r
   @retval EFI_OUT_OF_RESOURCES  There is insufficient memory to store the boot script.\r
 \r
 **/\r
 RETURN_STATUS\r
-EFIAPI \r
+EFIAPI\r
 S3BootScriptLabel (\r
   IN       BOOLEAN                      BeforeOrAfter,\r
   IN       BOOLEAN                      CreateIfNotFound,\r
@@ -1916,20 +2252,20 @@ S3BootScriptLabel (
   if (Label[0] == '\0') {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
-  // Check that the script is initialized without adding an entry to the script.\r
-  // The code must search for the label first befor it knows if a new entry needs\r
+  // Check that the script is initialized and synced without adding an entry to the script.\r
+  // The code must search for the label first before it knows if a new entry needs\r
   // to be added.\r
   //\r
   Script = S3BootScriptGetEntryAddAddress (0);\r
   if (Script == NULL) {\r
     return RETURN_OUT_OF_RESOURCES;\r
   }\r
-  \r
+\r
   //\r
   // Check the header and search for existing label.\r
-  // \r
+  //\r
   Script = mS3BootScriptTablePtr->TableBase;\r
   CopyMem ((VOID*)&TableHeader, Script, sizeof(EFI_BOOT_SCRIPT_TABLE_HEADER));\r
   if (TableHeader.OpCode != S3_BOOT_SCRIPT_LIB_TABLE_OPCODE) {\r
@@ -1939,22 +2275,22 @@ S3BootScriptLabel (
   TableLength   = mS3BootScriptTablePtr->TableLength;\r
   Script    =     Script + TableHeader.Length;\r
   while ((UINTN) Script < (UINTN) (StartAddress + TableLength)) {\r
-    \r
-    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));   \r
+\r
+    CopyMem ((VOID*)&ScriptHeader, Script, sizeof(EFI_BOOT_SCRIPT_COMMON_HEADER));\r
     if (ScriptHeader.OpCode == S3_BOOT_SCRIPT_LIB_LABEL_OPCODE) {\r
       if (AsciiStrCmp ((CHAR8 *)(UINTN)(Script+sizeof(EFI_BOOT_SCRIPT_INFORMATION)), Label) == 0) {\r
-        (*Position) = Script; \r
+        (*Position) = Script;\r
         return EFI_SUCCESS;\r
       }\r
-    } \r
+    }\r
     Script  = Script + ScriptHeader.Length;\r
   }\r
   if (CreateIfNotFound) {\r
     LabelLength = (UINT32)AsciiStrSize(Label);\r
-    return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label);     \r
+    return S3BootScriptLabelInternal (BeforeOrAfter,Position, LabelLength, Label);\r
   } else {\r
     return EFI_NOT_FOUND;\r
-  }   \r
+  }\r
 }\r
 \r
 /**\r
@@ -1966,13 +2302,13 @@ S3BootScriptLabel (
   @retval EFI_SUCCESS           The operation succeeded. A record was added into the\r
                                 specified script table.\r
   @retval EFI_INVALID_PARAMETER The parameter is illegal or the given boot script is not supported.\r
-                                If the opcode is unknow or not supported because of the PCD \r
+                                If the opcode is unknow or not supported because of the PCD\r
                                 Feature Flags.\r
   @retval EFI_OUT_OF_RESOURCES  There is insufficient memory to store the boot script.\r
 \r
 **/\r
 RETURN_STATUS\r
-EFIAPI \r
+EFIAPI\r
 S3BootScriptCompare (\r
   IN  UINT8                       *Position1,\r
   IN  UINT8                       *Position2,\r
@@ -1980,22 +2316,25 @@ S3BootScriptCompare (
   )\r
 {\r
   UINT8*                    Script;\r
-  UINT32                    TableLength; \r
+  UINT32                    TableLength;\r
 \r
-  Script = mS3BootScriptTablePtr->TableBase;\r
-  if (Script == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
   if (RelativePosition == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  TableLength = ((EFI_BOOT_SCRIPT_TABLE_HEADER*)Script)->TableLength;\r
+\r
   //\r
-  // If in boot time, TableLength does not include the termination node. so add it up \r
+  // Check that the script is initialized and synced without adding an entry to the script.\r
   //\r
-  if (!mS3BootScriptTablePtr->AtRuntime) {\r
-    TableLength += sizeof(EFI_BOOT_SCRIPT_TERMINATE);\r
+  Script = S3BootScriptGetEntryAddAddress (0);\r
+  if (Script == NULL) {\r
+    return RETURN_OUT_OF_RESOURCES;\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
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -2003,7 +2342,7 @@ S3BootScriptCompare (
     return EFI_INVALID_PARAMETER;\r
   }\r
   *RelativePosition = (Position1 < Position2)?-1:((Position1 == Position2)?0:1);\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r