From 0d9d6f987be6d03ac229244feacc0dbd7b80fcb2 Mon Sep 17 00:00:00 2001
From: Mikhail Krichanov <mikhailkrichanov@gmail.com>
Date: Mon, 22 Jul 2024 13:51:38 +0300
Subject: [PATCH] Fixed compilation of all packages tracked by CI after
 rebasing upon edk2-stable202311.

---
 BaseTools/Conf/tools_def.template             |  1 -
 BaseTools/Source/C/Common/Decompress.c        |  5 --
 BaseTools/Source/C/Common/WinNtInclude.h      | 76 +++++++++++++++++++
 BaseTools/Source/C/GenFfs/GenFfs.c            |  3 -
 BaseTools/Source/C/GenFv/GenFvInternalLib.c   |  2 +-
 BaseTools/Source/C/GenSec/GenSec.c            |  4 +-
 BaseTools/Source/C/Include/Common/BaseTypes.h |  4 -
 CryptoPkg/Library/IntrinsicLib/CopyMem.c      |  8 ++
 .../Library/IntrinsicLib/IntrinsicLib.inf     |  8 +-
 .../FspmWrapperPeim/FspmWrapperPeim.inf       |  1 -
 .../FspsWrapperPeim/FspsWrapperPeim.inf       |  1 -
 .../UsbNetwork/NetworkCommon/PxeFunction.c    |  2 +-
 MdeModulePkg/MdeModulePkg.dsc                 |  3 +-
 .../RegularExpressionDxe.inf                  |  2 +-
 MdePkg/Include/IndustryStandard/PeImage2.h    |  2 +
 MdePkg/Library/BaseFdtLib/BaseFdtLib.inf      |  2 +-
 NetworkPkg/NetworkPkg.dsc                     |  1 +
 OvmfPkg/Library/PlatformInitLib/IntelTdx.c    |  1 +
 OvmfPkg/OvmfPkgIa32X64.dsc                    |  1 +
 OvmfPkg/OvmfPkgX64.dsc                        |  1 +
 OvmfPkg/VirtioSerialDxe/VirtioSerial.inf      |  3 +
 .../VariablePolicy.c                          | 23 +-----
 SourceLevelDebugPkg/SourceLevelDebugPkg.dsc   |  1 -
 UefiCpuPkg/SecCore/SecCore.inf                |  1 -
 UefiCpuPkg/SecCore/SecMain.h                  |  1 -
 UefiPayloadPkg/UefiPayloadPkg.dsc             |  1 +
 26 files changed, 105 insertions(+), 53 deletions(-)
 create mode 100644 BaseTools/Source/C/Common/WinNtInclude.h

diff --git a/BaseTools/Conf/tools_def.template b/BaseTools/Conf/tools_def.template
index 5a201f30a9..d900b39dc5 100755
--- a/BaseTools/Conf/tools_def.template
+++ b/BaseTools/Conf/tools_def.template
@@ -1527,7 +1527,6 @@ DEFINE CLANGDWARF_RISCV64_DLINK_FLAGS       = DEF(CLANGDWARF_RISCV64_TARGET) DEF
 *_CLANGDWARF_RISCV64_DLINK2_FLAGS   = DEF(GCC_DLINK2_FLAGS_COMMON) -Wl,--defsym=PECOFF_HEADER_SIZE=0x240
 *_CLANGDWARF_RISCV64_PLATFORM_FLAGS =
 *_CLANGDWARF_RISCV64_PP_FLAGS       = DEF(GCC_PP_FLAGS) DEF(CLANGDWARF_RISCV64_TARGET) $(PLATFORM_FLAGS)
-*_CLANGDWARF_RISCV64_RC_FLAGS       = DEF(GCC_RISCV64_RC_FLAGS)
 *_CLANGDWARF_RISCV64_VFRPP_FLAGS    = DEF(GCC_VFRPP_FLAGS) DEF(CLANGDWARF_RISCV64_TARGET) $(PLATFORM_FLAGS)
 *_CLANGDWARF_RISCV64_ASLPP_FLAGS    = DEF(GCC_ASLPP_FLAGS) DEF(CLANGDWARF_RISCV64_TARGET)
 *_CLANGDWARF_RISCV64_CC_SECPEIFLAGS = DEF(GCC_RISCV64_CC_SECPEIFLAGS)
diff --git a/BaseTools/Source/C/Common/Decompress.c b/BaseTools/Source/C/Common/Decompress.c
index 0cf0c4a0a8..1b2f40d42b 100644
--- a/BaseTools/Source/C/Common/Decompress.c
+++ b/BaseTools/Source/C/Common/Decompress.c
@@ -15,9 +15,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 //
 // Decompression algorithm begins here
 //
-#ifndef UINT8_MAX
-#define UINT8_MAX 0xff
-#endif
 #define BITBUFSIZ 32
 #define MAXMATCH  256
 #define THRESHOLD 3
@@ -901,5 +898,3 @@ Extract (
 
   return Status;
 }
-
-
diff --git a/BaseTools/Source/C/Common/WinNtInclude.h b/BaseTools/Source/C/Common/WinNtInclude.h
new file mode 100644
index 0000000000..c549dc7d73
--- /dev/null
+++ b/BaseTools/Source/C/Common/WinNtInclude.h
@@ -0,0 +1,76 @@
+/** @file
+Include file for the WinNt Library
+
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef __WIN_NT_INCLUDE_H__
+#define __WIN_NT_INCLUDE_H__
+
+#define GUID  _WINNT_DUP_GUID_____
+#define _LIST_ENTRY  _WINNT_DUP_LIST_ENTRY_FORWARD
+#define LIST_ENTRY   _WINNT_DUP_LIST_ENTRY
+
+#if (_MSC_VER < 1800)
+#define InterlockedIncrement _WINNT_DUP_InterlockedIncrement
+#define InterlockedDecrement _WINNT_DUP_InterlockedDecrement
+#define InterlockedCompareExchange64 _WINNT_DUP_InterlockedCompareExchange64
+#endif
+
+#undef UNALIGNED
+#undef CONST
+#undef VOID
+
+#ifndef __GNUC__
+#include "windows.h"
+
+//
+// Win32 include files do not compile clean with /W4, so we use the warning
+// pragma to suppress the warnings for Win32 only. This way our code can still
+// compile at /W4 (highest warning level) with /WX (warnings cause build
+// errors).
+//
+#pragma warning(disable : 4115)
+#pragma warning(disable : 4201)
+#pragma warning(disable : 4214)
+#pragma warning(disable : 4028)
+#pragma warning(disable : 4133)
+
+//
+// Set the warnings back on as the EFI code must be /W4.
+//
+#pragma warning(default : 4115)
+#pragma warning(default : 4201)
+
+#endif
+
+#undef GUID
+#undef _LIST_ENTRY
+#undef LIST_ENTRY
+#undef InterlockedIncrement
+#undef InterlockedDecrement
+#undef InterlockedCompareExchange64
+#undef InterlockedCompareExchangePointer
+
+#define VOID void
+
+//
+// Prevent collisions with Windows API name macros that deal with Unicode/Not issues
+//
+#undef LoadImage
+#undef CreateEvent
+
+//
+// FAR is used as a struct member name by DebugSupport.h.
+//
+#undef FAR
+
+//
+// These two macros collide with re-definitions and case labels.
+//
+#undef ERROR
+#undef MAX_PATH
+
+#endif
diff --git a/BaseTools/Source/C/GenFfs/GenFfs.c b/BaseTools/Source/C/GenFfs/GenFfs.c
index d38b39236a..12f9641ed8 100644
--- a/BaseTools/Source/C/GenFfs/GenFfs.c
+++ b/BaseTools/Source/C/GenFfs/GenFfs.c
@@ -9,9 +9,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "WinNtInclude.h"
 
 #ifndef __GNUC__
-#define RUNTIME_FUNCTION  _WINNT_DUP_RUNTIME_FUNCTION
-#include <windows.h>
-#undef RUNTIME_FUNCTION
 #include <io.h>
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/BaseTools/Source/C/GenFv/GenFvInternalLib.c b/BaseTools/Source/C/GenFv/GenFvInternalLib.c
index 2756cc65cc..411ec2d4e1 100644
--- a/BaseTools/Source/C/GenFv/GenFvInternalLib.c
+++ b/BaseTools/Source/C/GenFv/GenFvInternalLib.c
@@ -3604,7 +3604,7 @@ Returns:
     Machine = UefiImageGetMachine (&ImageContext);
 
     if ( (Machine == IMAGE_FILE_MACHINE_ARMTHUMB_MIXED) ||
-         (Machine == IMAGE_FILE_MACHINE_AARCH64) ) {
+         (Machine == IMAGE_FILE_MACHINE_ARM64) ) {
       mArm = TRUE;
     }
 
diff --git a/BaseTools/Source/C/GenSec/GenSec.c b/BaseTools/Source/C/GenSec/GenSec.c
index 3965f9bdac..01383c9530 100644
--- a/BaseTools/Source/C/GenSec/GenSec.c
+++ b/BaseTools/Source/C/GenSec/GenSec.c
@@ -5,12 +5,10 @@ Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
+
 #include "WinNtInclude.h"
 
 #ifndef __GNUC__
-#define RUNTIME_FUNCTION  _WINNT_DUP_RUNTIME_FUNCTION
-#include <windows.h>
-#undef RUNTIME_FUNCTION
 #include <io.h>
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/BaseTools/Source/C/Include/Common/BaseTypes.h b/BaseTools/Source/C/Include/Common/BaseTypes.h
index f81449c91b..95b87498b7 100644
--- a/BaseTools/Source/C/Include/Common/BaseTypes.h
+++ b/BaseTools/Source/C/Include/Common/BaseTypes.h
@@ -13,10 +13,6 @@
 #ifndef __BT_BASE_TYPES_H__
 #define __BT_BASE_TYPES_H__
 
-//
-// To be able to safely include Windows headers, we need to include
-// WinNtInclude.h first.
-//
 #include "WinNtInclude.h"
 
 #include <stdarg.h>
diff --git a/CryptoPkg/Library/IntrinsicLib/CopyMem.c b/CryptoPkg/Library/IntrinsicLib/CopyMem.c
index 14a213d1da..530d092c8b 100644
--- a/CryptoPkg/Library/IntrinsicLib/CopyMem.c
+++ b/CryptoPkg/Library/IntrinsicLib/CopyMem.c
@@ -44,4 +44,12 @@ memcpy (
   return CopyMem (dest, src, (UINTN)count);
 }
 
+void
+__bzero (
+  void          *src,
+  unsigned int  count
+  )
+{
+  ZeroMem (src, count);
+}
 #endif
diff --git a/CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf b/CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
index 8602a513b0..80aead02fa 100644
--- a/CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
+++ b/CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
@@ -23,10 +23,9 @@
 
 [Sources]
   MemoryIntrinsics.c
-
-[Sources.IA32]
   CopyMem.c
 
+[Sources.IA32]
   Ia32/MathDivU64xU64Rem.c  | MSFT
   Ia32/MathLShiftS64.c      | MSFT
   Ia32/MathRShiftU64.c      | MSFT
@@ -36,7 +35,6 @@
   Ia32/MathUlldiv.asm       | MSFT
   Ia32/MathUlldvrm.asm      | MSFT
   Ia32/MathLldiv.asm        | MSFT
-  Ia32/MathUllrem.asm       | MSFT
 
   Ia32/MathDivU64xU64Rem.c  | INTEL
   Ia32/MathLShiftS64.c      | INTEL
@@ -49,10 +47,6 @@
   Ia32/MathDivS64x64.c      | GCC
   Ia32/MathDivU64x64.c      | GCC
   Ia32/MathModU64x64.c      | GCC
-[Sources.X64]
-  CopyMem.c
-[Sources.RISCV64]
-  CopyMem.c
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/IntelFsp2WrapperPkg/FspmWrapperPeim/FspmWrapperPeim.inf b/IntelFsp2WrapperPkg/FspmWrapperPeim/FspmWrapperPeim.inf
index 6b0b422773..39b0d810b4 100644
--- a/IntelFsp2WrapperPkg/FspmWrapperPeim/FspmWrapperPeim.inf
+++ b/IntelFsp2WrapperPkg/FspmWrapperPeim/FspmWrapperPeim.inf
@@ -38,7 +38,6 @@
   FspWrapperPlatformLib
   FspWrapperHobProcessLib
   CpuLib
-  UefiCpuLib
   UefiImageExtraActionLib
   PerformanceLib
   TimerLib
diff --git a/IntelFsp2WrapperPkg/FspsWrapperPeim/FspsWrapperPeim.inf b/IntelFsp2WrapperPkg/FspsWrapperPeim/FspsWrapperPeim.inf
index bc5be2f4cc..e9a8a19532 100644
--- a/IntelFsp2WrapperPkg/FspsWrapperPeim/FspsWrapperPeim.inf
+++ b/IntelFsp2WrapperPkg/FspsWrapperPeim/FspsWrapperPeim.inf
@@ -39,7 +39,6 @@
   FspWrapperPlatformLib
   FspWrapperHobProcessLib
   CpuLib
-  UefiCpuLib
   UefiImageExtraActionLib
   PerformanceLib
   FspWrapperApiLib
diff --git a/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c b/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c
index 62df4e92ea..049618b6c6 100644
--- a/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c
+++ b/MdeModulePkg/Bus/Usb/UsbNetwork/NetworkCommon/PxeFunction.c
@@ -760,7 +760,7 @@ UndiReceiveFilter (
           Cdb->StatFlags = PXE_STATFLAGS_COMMAND_FAILED;
         }
 
-        if ((Cdb->CPBsize == 0)) {
+        if (Cdb->CPBsize == 0) {
           Cdb->StatFlags = PXE_STATFLAGS_COMMAND_FAILED;
         }
       }
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 55f83dde5a..4efb102874 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -107,6 +107,7 @@
   VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
   IpmiCommandLib|MdeModulePkg/Library/BaseIpmiCommandLibNull/BaseIpmiCommandLibNull.inf
   SpiHcPlatformLib|MdeModulePkg/Library/BaseSpiHcPlatformLibNull/BaseSpiHcPlatformLibNull.inf
+  IntrinsicLib|CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
 
 [LibraryClasses.EBC.PEIM]
   IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf
@@ -132,10 +133,10 @@
   CpuArchLib|UefiCpuPkg/Library/CpuArchLib/CpuArchLib.inf
   MtrrLib|UefiCpuPkg/Library/MtrrLib/MtrrLib.inf
   LocalApicLib|UefiCpuPkg/Library/BaseXApicX2ApicLib/BaseXApicX2ApicLib.inf
-  UefiCpuLib|UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   MicrocodeLib|UefiCpuPkg/Library/MicrocodeLib/MicrocodeLib.inf
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 [LibraryClasses.IA32.DXE_CORE]
   CcExitLib|UefiCpuPkg/Library/CcExitLibNull/CcExitLibNull.inf
diff --git a/MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf b/MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
index 04149b19c6..8d91355507 100644
--- a/MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
+++ b/MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
@@ -127,7 +127,7 @@
 
   # Onigurama: corrects build for llvm-15
   CLANGPDB:*_*_*_CC_FLAGS = -Wno-deprecated-non-prototype
-  CLANGGCC:*_*_*_CC_FLAGS = -Wno-deprecated-non-prototype
+  GCC:*_CLANGDWARF_*_CC_FLAGS = -Wno-deprecated-non-prototype
 
   # Not add -Wno-error=maybe-uninitialized option for XCODE
   # XCODE doesn't know this option
diff --git a/MdePkg/Include/IndustryStandard/PeImage2.h b/MdePkg/Include/IndustryStandard/PeImage2.h
index 29c4e706e5..c17104722d 100644
--- a/MdePkg/Include/IndustryStandard/PeImage2.h
+++ b/MdePkg/Include/IndustryStandard/PeImage2.h
@@ -40,6 +40,8 @@
 #define IMAGE_FILE_MACHINE_RISCV32         0x5032
 #define IMAGE_FILE_MACHINE_RISCV64         0x5064
 #define IMAGE_FILE_MACHINE_RISCV128        0x5128
+#define IMAGE_FILE_MACHINE_LOONGARCH32     0x6232
+#define IMAGE_FILE_MACHINE_LOONGARCH64     0x6264
 
 //
 // EXE file formats
diff --git a/MdePkg/Library/BaseFdtLib/BaseFdtLib.inf b/MdePkg/Library/BaseFdtLib/BaseFdtLib.inf
index b5815380c5..a850816042 100644
--- a/MdePkg/Library/BaseFdtLib/BaseFdtLib.inf
+++ b/MdePkg/Library/BaseFdtLib/BaseFdtLib.inf
@@ -62,4 +62,4 @@
 # in BaseFdtLib\libfdt\libfdt\fdt_rw.c (wait for sub module update to remove this)
   MSFT:*_*_IA32_CC_FLAGS = /wd4146 /wd4245 /wd4706
   MSFT:*_*_X64_CC_FLAGS  = /wd4146 /wd4244 /wd4245 /wd4267 /wd4706
-
+  XCODE:*_*_X64_CC_FLAGS = -U__APPLE__
diff --git a/NetworkPkg/NetworkPkg.dsc b/NetworkPkg/NetworkPkg.dsc
index 6880255c9f..9488a9b384 100644
--- a/NetworkPkg/NetworkPkg.dsc
+++ b/NetworkPkg/NetworkPkg.dsc
@@ -35,6 +35,7 @@
   UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
   UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
   UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
+  SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
   UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
   UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
   UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf
diff --git a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c
index 12e4501c5b..79b822985e 100644
--- a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c
+++ b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c
@@ -18,6 +18,7 @@
 #include <IndustryStandard/IntelTdx.h>
 #include <Library/PeiServicesLib.h>
 #include <Pi/PiHob.h>
+#include <Library/TdxLib.h>
 #include <WorkArea.h>
 #include <ConfidentialComputingGuestAttr.h>
 
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index 6919357a14..ed2d12e42e 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -331,6 +331,7 @@
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf
   CpuArchLib|UefiCpuPkg/Library/CpuArchLib/CpuArchLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 [LibraryClasses.common.DXE_RUNTIME_DRIVER]
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index f72ca4e95e..40e76a68e1 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -353,6 +353,7 @@
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf
   CpuArchLib|UefiCpuPkg/Library/CpuArchLib/CpuArchLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 [LibraryClasses.common.DXE_RUNTIME_DRIVER]
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf
diff --git a/OvmfPkg/VirtioSerialDxe/VirtioSerial.inf b/OvmfPkg/VirtioSerialDxe/VirtioSerial.inf
index d63a08b928..c852385cdd 100644
--- a/OvmfPkg/VirtioSerialDxe/VirtioSerial.inf
+++ b/OvmfPkg/VirtioSerialDxe/VirtioSerial.inf
@@ -38,3 +38,6 @@
 [Protocols]
   gVirtioDeviceProtocolGuid        ## TO_START
   gEfiSerialIoProtocolGuid
+
+[BuildOptions]
+  MSFT:*_*_*_CC_FLAGS = /wd4204 
diff --git a/ShellPkg/DynamicCommand/VariablePolicyDynamicCommand/VariablePolicy.c b/ShellPkg/DynamicCommand/VariablePolicyDynamicCommand/VariablePolicy.c
index ed991be4ed..6d019746a1 100644
--- a/ShellPkg/DynamicCommand/VariablePolicyDynamicCommand/VariablePolicy.c
+++ b/ShellPkg/DynamicCommand/VariablePolicyDynamicCommand/VariablePolicy.c
@@ -859,32 +859,15 @@ InitializeHiiPackage (
   IN  EFI_HANDLE  ImageHandle
   )
 {
-  EFI_STATUS                   Status;
-  EFI_HII_PACKAGE_LIST_HEADER  *PackageList;
-  EFI_HII_HANDLE               HiiHandle;
-
-  //
-  // Retrieve HII package list from ImageHandle
-  //
-  Status = gBS->OpenProtocol (
-                  ImageHandle,
-                  &gEfiHiiPackageListProtocolGuid,
-                  (VOID **)&PackageList,
-                  ImageHandle,
-                  NULL,
-                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
-                  );
-  ASSERT_EFI_ERROR (Status);
-  if (EFI_ERROR (Status)) {
-    return NULL;
-  }
+  EFI_STATUS      Status;
+  EFI_HII_HANDLE  HiiHandle;
 
   //
   // Publish HII package list to HII Database.
   //
   Status = gHiiDatabase->NewPackageList (
                            gHiiDatabase,
-                           PackageList,
+                           &gModuleHiiPackageList->Header,
                            NULL,
                            &HiiHandle
                            );
diff --git a/SourceLevelDebugPkg/SourceLevelDebugPkg.dsc b/SourceLevelDebugPkg/SourceLevelDebugPkg.dsc
index 744b9527b4..11870b8ae6 100644
--- a/SourceLevelDebugPkg/SourceLevelDebugPkg.dsc
+++ b/SourceLevelDebugPkg/SourceLevelDebugPkg.dsc
@@ -35,7 +35,6 @@
   IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
   SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
   LocalApicLib|UefiCpuPkg/Library/BaseXApicLib/BaseXApicLib.inf
-  UefiCpuLib|UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf
   SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf
   UefiImageExtraActionLib|SourceLevelDebugPkg/Library/UefiImageExtraActionLibDebug/UefiImageExtraActionLibDebug.inf
   TimerLib|UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/SecPeiDxeTimerLibUefiCpu.inf
diff --git a/UefiCpuPkg/SecCore/SecCore.inf b/UefiCpuPkg/SecCore/SecCore.inf
index 7d3a80cf04..7c8fccb2d6 100644
--- a/UefiCpuPkg/SecCore/SecCore.inf
+++ b/UefiCpuPkg/SecCore/SecCore.inf
@@ -48,7 +48,6 @@
   PcdLib
   DebugAgentLib
   CpuLib
-  UefiCpuLib
   UefiImageExtraActionLib
   CpuExceptionHandlerLib
   ReportStatusCodeLib
diff --git a/UefiCpuPkg/SecCore/SecMain.h b/UefiCpuPkg/SecCore/SecMain.h
index f26d337843..fb2537998b 100644
--- a/UefiCpuPkg/SecCore/SecMain.h
+++ b/UefiCpuPkg/SecCore/SecMain.h
@@ -25,7 +25,6 @@
 #include <Library/BaseMemoryLib.h>
 #include <Library/PlatformSecLib.h>
 #include <Library/CpuLib.h>
-#include <Library/UefiCpuLib.h>
 
 #include <Library/UefiImageExtraActionLib.h>
 #include <Library/DebugAgentLib.h>
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 8eab1e00d8..4a6b647877 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -383,6 +383,7 @@
 !endif
   CpuArchLib|UefiCpuPkg/Library/CpuArchLib/CpuArchLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 [LibraryClasses.common.DXE_DRIVER]
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf