[PATCH 2/4] OvmfPkg/AmdSev: add Grub Firmware Volume Package


James Bottomley <jejb@...>
 

This is used to package up the grub bootloader into a firmware volume
where it can be executed as a shell like the UEFI Shell. Grub itself
is built as a minimal entity into a Fv and then added as a boot
option. By default the UEFI shell isn't built but for debugging
purposes it can be enabled and will then be presented as a boot option
(This should never be allowed for secure boot in an external data
centre but may be useful for local debugging). Finally all other boot
options except grub and possibly the shell are stripped and the boot
timeout forced to 0 so the system will not enter a setup menu and will
only boot to grub. This is done by copying the
Library/PlatformBootManagerLib into Library/PlatformBootManagerLibGrub
and then customizing it.

Boot failure is fatal to try to preven secret theft.

Signed-off-by: James Bottomley <jejb@...>
---
OvmfPkg/OvmfPkg.dec | 1 +
OvmfPkg/AmdSev/AmdSevX64.dsc | 14 +-
OvmfPkg/AmdSev/AmdSevX64.fdf | 5 +-
OvmfPkg/AmdSev/Grub/Grub.inf | 37 +
.../PlatformBootManagerLibGrub.inf | 84 +
.../PlatformBootManagerLibGrub/BdsPlatform.h | 179 ++
.../PlatformBootManagerLibGrub/BdsPlatform.c | 1538 +++++++++++++++++
.../PlatformBootManagerLibGrub/PlatformData.c | 213 +++
OvmfPkg/AmdSev/Grub/.gitignore | 1 +
OvmfPkg/AmdSev/Grub/grub.cfg | 35 +
OvmfPkg/AmdSev/Grub/grub.sh | 54 +
11 files changed, 2157 insertions(+), 4 deletions(-)
create mode 100644 OvmfPkg/AmdSev/Grub/Grub.inf
create mode 100644 OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformBoot=
ManagerLibGrub.inf
create mode 100644 OvmfPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.h
create mode 100644 OvmfPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.c
create mode 100644 OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformData=
.c
create mode 100644 OvmfPkg/AmdSev/Grub/.gitignore
create mode 100644 OvmfPkg/AmdSev/Grub/grub.cfg
create mode 100644 OvmfPkg/AmdSev/Grub/grub.sh

diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec
index 6abde4fd93..3fbf7a0ee1 100644
--- a/OvmfPkg/OvmfPkg.dec
+++ b/OvmfPkg/OvmfPkg.dec
@@ -116,6 +116,7 @@
gEfiLegacyDevOrderVariableGuid =3D {0xa56074db, 0x65fe, 0x45f7, {=
0xbd, 0x21, 0x2d, 0x2b, 0xdd, 0x8e, 0x96, 0x52}}=0D
gLinuxEfiInitrdMediaGuid =3D {0x5568e427, 0x68fc, 0x4f3d, {=
0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68}}=0D
gQemuKernelLoaderFsMediaGuid =3D {0x1428f772, 0xb64a, 0x441e, {=
0xb8, 0xc3, 0x9e, 0xbd, 0xd7, 0xf8, 0x93, 0xc7}}=0D
+ gGrubFileGuid =3D {0xb5ae312c, 0xbc8a, 0x43b1, {=
0x9c, 0x62, 0xeb, 0xb8, 0x26, 0xdd, 0x5d, 0x07}}=0D
=0D
[Ppis]=0D
# PPI whose presence in the PPI database signals that the TPM base addre=
ss=0D
diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc
index d1dfb8742f..7d3663150e 100644
--- a/OvmfPkg/AmdSev/AmdSevX64.dsc
+++ b/OvmfPkg/AmdSev/AmdSevX64.dsc
@@ -23,6 +23,7 @@
BUILD_TARGETS =3D NOOPT|DEBUG|RELEASE=0D
SKUID_IDENTIFIER =3D DEFAULT=0D
FLASH_DEFINITION =3D OvmfPkg/AmdSev/AmdSevX64.fdf=0D
+ PREBUILD =3D sh OvmfPkg/AmdSev/Grub/grub.sh=0D
=0D
#=0D
# Defines for default states. These can be changed on the command line.=
=0D
@@ -33,6 +34,7 @@
DEFINE SOURCE_DEBUG_ENABLE =3D FALSE=0D
DEFINE TPM_ENABLE =3D FALSE=0D
DEFINE TPM_CONFIG_ENABLE =3D FALSE=0D
+ DEFINE BUILD_SHELL =3D FALSE=0D
=0D
#=0D
# Network definition=0D
@@ -159,7 +161,6 @@
UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntry=
Point.inf=0D
UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiA=
pplicationEntryPoint.inf=0D
DevicePathLib|MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDev=
icePathLibDevicePathProtocol.inf=0D
- NvVarsFileLib|OvmfPkg/Library/NvVarsFileLib/NvVarsFileLib.inf=0D
FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf=0D
UefiCpuLib|UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf=0D
SecurityManagementLib|MdeModulePkg/Library/DxeSecurityManagementLib/DxeS=
ecurityManagementLib.inf=0D
@@ -213,8 +214,11 @@
TlsLib|CryptoPkg/Library/TlsLib/TlsLib.inf=0D
!endif=0D
=0D
+!if $(BUILD_SHELL) =3D=3D TRUE=0D
ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf=0D
ShellCEntryLib|ShellPkg/Library/UefiShellCEntryLib/UefiShellCEntryLib.in=
f=0D
+!endif=0D
+=0D
S3BootScriptLib|MdeModulePkg/Library/PiDxeS3BootScriptLib/DxeS3BootScrip=
tLib.inf=0D
SmbusLib|MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf=0D
OrderedCollectionLib|MdePkg/Library/BaseOrderedCollectionRedBlackTreeLib=
/BaseOrderedCollectionRedBlackTreeLib.inf=0D
@@ -371,7 +375,7 @@
!else=0D
DebugLib|OvmfPkg/Library/PlatformDebugLibIoPort/PlatformDebugLibIoPort.i=
nf=0D
!endif=0D
- PlatformBootManagerLib|OvmfPkg/Library/PlatformBootManagerLib/PlatformBo=
otManagerLib.inf=0D
+ PlatformBootManagerLib|OvmfPkg/Library/PlatformBootManagerLibGrub/Platfo=
rmBootManagerLibGrub.inf=0D
PlatformBmPrintScLib|OvmfPkg/Library/PlatformBmPrintScLib/PlatformBmPrin=
tScLib.inf=0D
QemuBootOrderLib|OvmfPkg/Library/QemuBootOrderLib/QemuBootOrderLib.inf=0D
CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuE=
xceptionHandlerLib.inf=0D
@@ -566,6 +570,7 @@
# Point to the MdeModulePkg/Application/UiApp/UiApp.inf=0D
gEfiMdeModulePkgTokenSpaceGuid.PcdBootManagerMenuFile|{ 0x21, 0xaa, 0x2c=
, 0x46, 0x14, 0x76, 0x03, 0x45, 0x83, 0x6e, 0x8a, 0xb6, 0xf4, 0x66, 0x23, 0=
x31 }=0D
=0D
+ gEfiMdeModulePkgTokenSpaceGuid.PcdConInConnectOnDemand|TRUE=0D
##########################################################################=
######=0D
#=0D
# Pcd Dynamic Section - list of all EDK II PCD Entries defined by this Pla=
tform=0D
@@ -895,7 +900,7 @@
OvmfPkg/Csm/Csm16/Csm16.inf=0D
!endif=0D
=0D
-!if $(TOOL_CHAIN_TAG) !=3D "XCODE5"=0D
+!if $(TOOL_CHAIN_TAG) !=3D "XCODE5" && $(BUILD_SHELL) =3D=3D TRUE=0D
ShellPkg/DynamicCommand/TftpDynamicCommand/TftpDynamicCommand.inf {=0D
<PcdsFixedAtBuild>=0D
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D
@@ -909,6 +914,8 @@
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D
}=0D
!endif=0D
+ OvmfPkg/AmdSev/Grub/Grub.inf=0D
+!if $(BUILD_SHELL) =3D=3D TRUE=0D
ShellPkg/Application/Shell/Shell.inf {=0D
<LibraryClasses>=0D
ShellCommandLib|ShellPkg/Library/UefiShellCommandLib/UefiShellComman=
dLib.inf=0D
@@ -931,6 +938,7 @@
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D
gEfiMdePkgTokenSpaceGuid.PcdUefiLibMaxPrintBufferSize|8000=0D
}=0D
+!endif=0D
=0D
!if $(SECURE_BOOT_ENABLE) =3D=3D TRUE=0D
SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigDx=
e.inf=0D
diff --git a/OvmfPkg/AmdSev/AmdSevX64.fdf b/OvmfPkg/AmdSev/AmdSevX64.fdf
index e874629a4e..689386612d 100644
--- a/OvmfPkg/AmdSev/AmdSevX64.fdf
+++ b/OvmfPkg/AmdSev/AmdSevX64.fdf
@@ -275,12 +275,15 @@ INF MdeModulePkg/Universal/Acpi/BootGraphicsResource=
TableDxe/BootGraphicsResour
INF FatPkg/EnhancedFatDxe/Fat.inf=0D
INF MdeModulePkg/Universal/Disk/UdfDxe/UdfDxe.inf=0D
=0D
-!if $(TOOL_CHAIN_TAG) !=3D "XCODE5"=0D
+!if $(TOOL_CHAIN_TAG) !=3D "XCODE5" && $(BUILD_SHELL) =3D=3D TRUE=0D
INF ShellPkg/DynamicCommand/TftpDynamicCommand/TftpDynamicCommand.inf=0D
INF ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf=0D
INF OvmfPkg/LinuxInitrdDynamicShellCommand/LinuxInitrdDynamicShellCommand=
.inf=0D
!endif=0D
+INF OvmfPkg/AmdSev/Grub/Grub.inf=0D
+!if $(BUILD_SHELL) =3D=3D TRUE=0D
INF ShellPkg/Application/Shell/Shell.inf=0D
+!endif=0D
=0D
INF MdeModulePkg/Logo/LogoDxe.inf=0D
=0D
diff --git a/OvmfPkg/AmdSev/Grub/Grub.inf b/OvmfPkg/AmdSev/Grub/Grub.inf
new file mode 100644
index 0000000000..a12428668b
--- /dev/null
+++ b/OvmfPkg/AmdSev/Grub/Grub.inf
@@ -0,0 +1,37 @@
+## @file=0D
+# Create a Firmware Volume based Grub Bootloaded=0D
+#=0D
+# Copyright (C) 2020 James Bottomley, IBM Corporation.=0D
+#=0D
+# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+#=0D
+##=0D
+=0D
+[Defines]=0D
+ INF_VERSION =3D 0x00010006=0D
+ BASE_NAME =3D Grub=0D
+ # This is gGrubFileGuid=0D
+ FILE_GUID =3D b5ae312c-bc8a-43b1-9c62-ebb826dd5d07=
=0D
+ MODULE_TYPE =3D UEFI_APPLICATION=0D
+ VERSION_STRING =3D 1.0=0D
+ ENTRY_POINT =3D UefiMain=0D
+=0D
+[Packages]=0D
+ OvmfPkg/OvmfPkg.dec=0D
+=0D
+#=0D
+# The following information is for reference only and not required by=0D
+# the build tools.=0D
+#=0D
+# VALID_ARCHITECTURES =3D IA32 X64 EBC=0D
+#=0D
+=0D
+##=0D
+# Note: The version of grub.efi this picks up can be generated by=0D
+# grub.sh which must be specified as a PREBUILD in the .dsc file or=0D
+# you can simply move a precompiled grub into here and not do the=0D
+# PREBUILD)=0D
+##=0D
+[Binaries]=0D
+ PE32|grub.efi|*=0D
+=0D
diff --git a/OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformBootManager=
LibGrub.inf b/OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformBootManage=
rLibGrub.inf
new file mode 100644
index 0000000000..62707b0bdd
--- /dev/null
+++ b/OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformBootManagerLibGrub=
.inf
@@ -0,0 +1,84 @@
+## @file=0D
+# Platform BDS customizations library.=0D
+#=0D
+# Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>=
=0D
+# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+#=0D
+##=0D
+=0D
+[Defines]=0D
+ INF_VERSION =3D 0x00010005=0D
+ BASE_NAME =3D PlatformBootManagerLibGrub=0D
+ FILE_GUID =3D 3a8f8431-f0c9-4c95-8a1d-04445c582d4e=
=0D
+ MODULE_TYPE =3D DXE_DRIVER=0D
+ VERSION_STRING =3D 1.0=0D
+ LIBRARY_CLASS =3D PlatformBootManagerLib|DXE_DRIVER=0D
+=0D
+#=0D
+# The following information is for reference only and not required by the =
build tools.=0D
+#=0D
+# VALID_ARCHITECTURES =3D IA32 X64 EBC=0D
+#=0D
+=0D
+[Sources]=0D
+ BdsPlatform.c=0D
+ PlatformData.c=0D
+ BdsPlatform.h=0D
+=0D
+[Packages]=0D
+ MdePkg/MdePkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
+ SourceLevelDebugPkg/SourceLevelDebugPkg.dec=0D
+ OvmfPkg/OvmfPkg.dec=0D
+ SecurityPkg/SecurityPkg.dec=0D
+ ShellPkg/ShellPkg.dec=0D
+=0D
+[LibraryClasses]=0D
+ BaseLib=0D
+ MemoryAllocationLib=0D
+ UefiBootServicesTableLib=0D
+ UefiRuntimeServicesTableLib=0D
+ BaseMemoryLib=0D
+ DebugLib=0D
+ PcdLib=0D
+ UefiBootManagerLib=0D
+ BootLogoLib=0D
+ DevicePathLib=0D
+ PciLib=0D
+ QemuFwCfgLib=0D
+ QemuFwCfgS3Lib=0D
+ QemuLoadImageLib=0D
+ QemuBootOrderLib=0D
+ ReportStatusCodeLib=0D
+ UefiLib=0D
+ PlatformBmPrintScLib=0D
+ Tcg2PhysicalPresenceLib=0D
+ XenPlatformLib=0D
+=0D
+[Pcd]=0D
+ gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent=0D
+ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable=0D
+ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfHostBridgePciDevId=0D
+ gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut=0D
+ gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate ## CONSUMES=0D
+ gEfiMdePkgTokenSpaceGuid.PcdUartDefaultDataBits ## CONSUMES=0D
+ gEfiMdePkgTokenSpaceGuid.PcdUartDefaultParity ## CONSUMES=0D
+ gEfiMdePkgTokenSpaceGuid.PcdUartDefaultStopBits ## CONSUMES=0D
+=0D
+[Pcd.IA32, Pcd.X64]=0D
+ gEfiMdePkgTokenSpaceGuid.PcdFSBClock=0D
+=0D
+[Protocols]=0D
+ gEfiDecompressProtocolGuid=0D
+ gEfiPciRootBridgeIoProtocolGuid=0D
+ gEfiS3SaveStateProtocolGuid # PROTOCOL SOMETIMES_CONSU=
MED=0D
+ gEfiDxeSmmReadyToLockProtocolGuid # PROTOCOL SOMETIMES_PRODU=
CED=0D
+ gEfiLoadedImageProtocolGuid # PROTOCOL SOMETIMES_PRODU=
CED=0D
+ gEfiFirmwareVolume2ProtocolGuid # PROTOCOL SOMETIMES_CONSU=
MED=0D
+=0D
+[Guids]=0D
+ gEfiEndOfDxeEventGroupGuid=0D
+ gEfiGlobalVariableGuid=0D
+ gRootBridgesConnectedEventGroupGuid=0D
+ gUefiShellFileGuid=0D
+ gGrubFileGuid=0D
diff --git a/OvmfPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.h b/Ovm=
fPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.h
new file mode 100644
index 0000000000..c6ccf9e52e
--- /dev/null
+++ b/OvmfPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.h
@@ -0,0 +1,179 @@
+/** @file=0D
+ Platform BDS customizations include file.=0D
+=0D
+ Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+Module Name:=0D
+=0D
+ BdsPlatform.h=0D
+=0D
+Abstract:=0D
+=0D
+ Head file for BDS Platform specific code=0D
+=0D
+**/=0D
+=0D
+#ifndef _PLATFORM_SPECIFIC_BDS_PLATFORM_H_=0D
+#define _PLATFORM_SPECIFIC_BDS_PLATFORM_H_=0D
+=0D
+=0D
+#include <PiDxe.h>=0D
+=0D
+#include <IndustryStandard/Pci.h>=0D
+#include <IndustryStandard/Acpi.h>=0D
+#include <IndustryStandard/SmBios.h>=0D
+#include <IndustryStandard/PeImage.h>=0D
+#include <IndustryStandard/Virtio095.h>=0D
+=0D
+#include <Library/DebugLib.h>=0D
+#include <Library/BaseMemoryLib.h>=0D
+#include <Library/UefiBootServicesTableLib.h>=0D
+#include <Library/UefiRuntimeServicesTableLib.h>=0D
+#include <Library/MemoryAllocationLib.h>=0D
+#include <Library/BaseLib.h>=0D
+#include <Library/PcdLib.h>=0D
+#include <Library/PciLib.h>=0D
+#include <Library/UefiBootManagerLib.h>=0D
+#include <Library/BootLogoLib.h>=0D
+#include <Library/HobLib.h>=0D
+#include <Library/UefiLib.h>=0D
+#include <Library/DxeServicesTableLib.h>=0D
+#include <Library/DevicePathLib.h>=0D
+#include <Library/IoLib.h>=0D
+#include <Library/NvVarsFileLib.h>=0D
+#include <Library/QemuFwCfgLib.h>=0D
+#include <Library/QemuFwCfgS3Lib.h>=0D
+#include <Library/QemuBootOrderLib.h>=0D
+=0D
+#include <Protocol/Decompress.h>=0D
+#include <Protocol/PciIo.h>=0D
+#include <Protocol/FirmwareVolume2.h>=0D
+#include <Protocol/SimpleFileSystem.h>=0D
+#include <Protocol/PciRootBridgeIo.h>=0D
+#include <Protocol/S3SaveState.h>=0D
+#include <Protocol/DxeSmmReadyToLock.h>=0D
+#include <Protocol/LoadedImage.h>=0D
+=0D
+#include <Guid/Acpi.h>=0D
+#include <Guid/SmBios.h>=0D
+#include <Guid/HobList.h>=0D
+#include <Guid/GlobalVariable.h>=0D
+#include <Guid/EventGroup.h>=0D
+#include <Guid/DebugAgentGuid.h>=0D
+=0D
+#include <OvmfPlatforms.h>=0D
+=0D
+extern EFI_DEVICE_PATH_PROTOCOL *gPlatformConnectSequence[];=0D
+extern ACPI_HID_DEVICE_PATH gPnpPs2KeyboardDeviceNode;=0D
+extern ACPI_HID_DEVICE_PATH gPnp16550ComPortDeviceNode;=0D
+extern UART_DEVICE_PATH gUartDeviceNode;=0D
+extern VENDOR_DEVICE_PATH gTerminalTypeDeviceNode;=0D
+=0D
+#define PCI_DEVICE_PATH_NODE(Func, Dev) \=0D
+ { \=0D
+ { \=0D
+ HARDWARE_DEVICE_PATH, \=0D
+ HW_PCI_DP, \=0D
+ { \=0D
+ (UINT8) (sizeof (PCI_DEVICE_PATH)), \=0D
+ (UINT8) ((sizeof (PCI_DEVICE_PATH)) >> 8) \=0D
+ } \=0D
+ }, \=0D
+ (Func), \=0D
+ (Dev) \=0D
+ }=0D
+=0D
+#define PNPID_DEVICE_PATH_NODE(PnpId) \=0D
+ { \=0D
+ { \=0D
+ ACPI_DEVICE_PATH, \=0D
+ ACPI_DP, \=0D
+ { \=0D
+ (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)), \=0D
+ (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8) \=0D
+ }, \=0D
+ }, \=0D
+ EISA_PNP_ID((PnpId)), \=0D
+ 0 \=0D
+ }=0D
+=0D
+#define gPciIsaBridge \=0D
+ PCI_DEVICE_PATH_NODE(0, 0x1f)=0D
+=0D
+#define gP2PBridge \=0D
+ PCI_DEVICE_PATH_NODE(0, 0x1e)=0D
+=0D
+#define gPnpPs2Keyboard \=0D
+ PNPID_DEVICE_PATH_NODE(0x0303)=0D
+=0D
+#define gPnp16550ComPort \=0D
+ PNPID_DEVICE_PATH_NODE(0x0501)=0D
+=0D
+#define gUart \=0D
+ { \=0D
+ { \=0D
+ MESSAGING_DEVICE_PATH, \=0D
+ MSG_UART_DP, \=0D
+ { \=0D
+ (UINT8) (sizeof (UART_DEVICE_PATH)), \=0D
+ (UINT8) ((sizeof (UART_DEVICE_PATH)) >> 8) \=0D
+ } \=0D
+ }, \=0D
+ 0, \=0D
+ 115200, \=0D
+ 8, \=0D
+ 1, \=0D
+ 1 \=0D
+ }=0D
+=0D
+#define gPcAnsiTerminal \=0D
+ { \=0D
+ { \=0D
+ MESSAGING_DEVICE_PATH, \=0D
+ MSG_VENDOR_DP, \=0D
+ { \=0D
+ (UINT8) (sizeof (VENDOR_DEVICE_PATH)), \=0D
+ (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8) \=0D
+ } \=0D
+ }, \=0D
+ DEVICE_PATH_MESSAGING_PC_ANSI \=0D
+ }=0D
+=0D
+#define gEndEntire \=0D
+ { \=0D
+ END_DEVICE_PATH_TYPE, \=0D
+ END_ENTIRE_DEVICE_PATH_SUBTYPE, \=0D
+ { \=0D
+ END_DEVICE_PATH_LENGTH, \=0D
+ 0 \=0D
+ } \=0D
+ }=0D
+=0D
+#define PCI_CLASS_SCC 0x07=0D
+#define PCI_SUBCLASS_SERIAL 0x00=0D
+#define PCI_IF_16550 0x02=0D
+#define IS_PCI_16550SERIAL(_p) IS_CLASS3 (_p, PCI_CLASS_SCC, PCI=
_SUBCLASS_SERIAL, PCI_IF_16550)=0D
+#define IS_PCI_ISA_PDECODE(_p) IS_CLASS3 (_p, PCI_CLASS_BRIDGE, PCI=
_CLASS_BRIDGE_ISA_PDECODE, 0)=0D
+=0D
+typedef struct {=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+ UINTN ConnectType;=0D
+} PLATFORM_CONSOLE_CONNECT_ENTRY;=0D
+=0D
+#define CONSOLE_OUT BIT0=0D
+#define CONSOLE_IN BIT1=0D
+#define STD_ERROR BIT2=0D
+extern PLATFORM_CONSOLE_CONNECT_ENTRY gPlatformConsole[];=0D
+extern PLATFORM_CONSOLE_CONNECT_ENTRY gXenPlatformConsole[];=0D
+=0D
+//=0D
+// Platform BDS Functions=0D
+//=0D
+=0D
+VOID=0D
+PlatformInitializeConsole (=0D
+ IN PLATFORM_CONSOLE_CONNECT_ENTRY *PlatformConsole=0D
+ );=0D
+=0D
+#endif // _PLATFORM_SPECIFIC_BDS_PLATFORM_H_=0D
diff --git a/OvmfPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.c b/Ovm=
fPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.c
new file mode 100644
index 0000000000..24c37068a2
--- /dev/null
+++ b/OvmfPkg/Library/PlatformBootManagerLibGrub/BdsPlatform.c
@@ -0,0 +1,1538 @@
+/** @file=0D
+ Platform BDS customizations.=0D
+=0D
+ Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#include "BdsPlatform.h"=0D
+#include <Guid/RootBridgesConnectedEventGroup.h>=0D
+#include <Protocol/FirmwareVolume2.h>=0D
+#include <Library/PlatformBmPrintScLib.h>=0D
+#include <Library/Tcg2PhysicalPresenceLib.h>=0D
+#include <Library/XenPlatformLib.h>=0D
+=0D
+=0D
+//=0D
+// Global data=0D
+//=0D
+=0D
+VOID *mEfiDevPathNotifyReg;=0D
+EFI_EVENT mEfiDevPathEvent;=0D
+UINT16 mHostBridgeDevId;=0D
+=0D
+//=0D
+// Table of host IRQs matching PCI IRQs A-D=0D
+// (for configuring PCI Interrupt Line register)=0D
+//=0D
+CONST UINT8 PciHostIrqs[] =3D {=0D
+ 0x0a, 0x0a, 0x0b, 0x0b=0D
+};=0D
+=0D
+//=0D
+// Type definitions=0D
+//=0D
+=0D
+typedef=0D
+EFI_STATUS=0D
+(EFIAPI *PROTOCOL_INSTANCE_CALLBACK)(=0D
+ IN EFI_HANDLE Handle,=0D
+ IN VOID *Instance,=0D
+ IN VOID *Context=0D
+ );=0D
+=0D
+/**=0D
+ @param[in] Handle - Handle of PCI device instance=0D
+ @param[in] PciIo - PCI IO protocol instance=0D
+ @param[in] Pci - PCI Header register block=0D
+**/=0D
+typedef=0D
+EFI_STATUS=0D
+(EFIAPI *VISIT_PCI_INSTANCE_CALLBACK)(=0D
+ IN EFI_HANDLE Handle,=0D
+ IN EFI_PCI_IO_PROTOCOL *PciIo,=0D
+ IN PCI_TYPE00 *Pci=0D
+ );=0D
+=0D
+=0D
+//=0D
+// Function prototypes=0D
+//=0D
+=0D
+EFI_STATUS=0D
+VisitAllInstancesOfProtocol (=0D
+ IN EFI_GUID *Id,=0D
+ IN PROTOCOL_INSTANCE_CALLBACK CallBackFunction,=0D
+ IN VOID *Context=0D
+ );=0D
+=0D
+EFI_STATUS=0D
+VisitAllPciInstancesOfProtocol (=0D
+ IN VISIT_PCI_INSTANCE_CALLBACK CallBackFunction=0D
+ );=0D
+=0D
+VOID=0D
+InstallDevicePathCallback (=0D
+ VOID=0D
+ );=0D
+=0D
+VOID=0D
+PlatformRegisterFvBootOption (=0D
+ EFI_GUID *FileGuid,=0D
+ CHAR16 *Description,=0D
+ UINT32 Attributes=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ INTN OptionIndex;=0D
+ EFI_BOOT_MANAGER_LOAD_OPTION NewOption;=0D
+ EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;=0D
+ UINTN BootOptionCount;=0D
+ MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;=0D
+ EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+=0D
+ Status =3D gBS->HandleProtocol (=0D
+ gImageHandle,=0D
+ &gEfiLoadedImageProtocolGuid,=0D
+ (VOID **) &LoadedImage=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);=0D
+ DevicePath =3D DevicePathFromHandle (LoadedImage->DeviceHandle);=0D
+ ASSERT (DevicePath !=3D NULL);=0D
+ DevicePath =3D AppendDevicePathNode (=0D
+ DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *) &FileNode=0D
+ );=0D
+ ASSERT (DevicePath !=3D NULL);=0D
+=0D
+ Status =3D EfiBootManagerInitializeLoadOption (=0D
+ &NewOption,=0D
+ LoadOptionNumberUnassigned,=0D
+ LoadOptionTypeBoot,=0D
+ Attributes,=0D
+ Description,=0D
+ DevicePath,=0D
+ NULL,=0D
+ 0=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ FreePool (DevicePath);=0D
+=0D
+ BootOptions =3D EfiBootManagerGetLoadOptions (=0D
+ &BootOptionCount, LoadOptionTypeBoot=0D
+ );=0D
+=0D
+ OptionIndex =3D EfiBootManagerFindLoadOption (=0D
+ &NewOption, BootOptions, BootOptionCount=0D
+ );=0D
+=0D
+ if (OptionIndex =3D=3D -1) {=0D
+ Status =3D EfiBootManagerAddLoadOptionVariable (&NewOption, MAX_UINTN)=
;=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ }=0D
+ EfiBootManagerFreeLoadOption (&NewOption);=0D
+ EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);=0D
+}=0D
+=0D
+/**=0D
+ Remove all MemoryMapped(...)/FvFile(...) and Fv(...)/FvFile(...) boot op=
tions=0D
+ whose device paths do not resolve exactly to an FvFile in the system.=0D
+=0D
+ Also strip out every boot option that is not an FvFile, meaning the syst=
em=0D
+ can only boot either the Grub or (if built) the shell.=0D
+=0D
+ This removes any boot options that point to binaries built into the firm=
ware=0D
+ and have become stale due to any of the following:=0D
+ - DXEFV's base address or size changed (historical),=0D
+ - DXEFV's FvNameGuid changed,=0D
+ - the FILE_GUID of the pointed-to binary changed,=0D
+ - the referenced binary is no longer built into the firmware.=0D
+=0D
+ EfiBootManagerFindLoadOption() used in PlatformRegisterFvBootOption() on=
ly=0D
+ avoids exact duplicates.=0D
+**/=0D
+VOID=0D
+RemoveStaleFvFileOptions (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;=0D
+ UINTN BootOptionCount;=0D
+ UINTN Index;=0D
+=0D
+ BootOptions =3D EfiBootManagerGetLoadOptions (&BootOptionCount,=0D
+ LoadOptionTypeBoot);=0D
+=0D
+ for (Index =3D 0; Index < BootOptionCount; ++Index) {=0D
+ EFI_DEVICE_PATH_PROTOCOL *Node1, *Node2, *SearchNode;=0D
+ EFI_STATUS Status;=0D
+ EFI_HANDLE FvHandle;=0D
+=0D
+ //=0D
+ // If the device path starts with neither MemoryMapped(...) nor Fv(...=
),=0D
+ // then delete the boot option.=0D
+ //=0D
+ Node1 =3D BootOptions[Index].FilePath;=0D
+ if (!(DevicePathType (Node1) =3D=3D HARDWARE_DEVICE_PATH &&=0D
+ DevicePathSubType (Node1) =3D=3D HW_MEMMAP_DP) &&=0D
+ !(DevicePathType (Node1) =3D=3D MEDIA_DEVICE_PATH &&=0D
+ DevicePathSubType (Node1) =3D=3D MEDIA_PIWG_FW_VOL_DP)) {=0D
+ EfiBootManagerDeleteLoadOptionVariable (=0D
+ BootOptions[Index].OptionNumber, LoadOptionTypeBoot);=0D
+ continue;=0D
+ }=0D
+=0D
+ //=0D
+ // If the second device path node is not FvFile(...), then delete the =
boot=0D
+ // option.=0D
+ //=0D
+ Node2 =3D NextDevicePathNode (Node1);=0D
+ if (DevicePathType (Node2) !=3D MEDIA_DEVICE_PATH ||=0D
+ DevicePathSubType (Node2) !=3D MEDIA_PIWG_FW_FILE_DP) {=0D
+ EfiBootManagerDeleteLoadOptionVariable (=0D
+ BootOptions[Index].OptionNumber, LoadOptionTypeBoot);=0D
+ continue;=0D
+ }=0D
+=0D
+ //=0D
+ // Locate the Firmware Volume2 protocol instance that is denoted by th=
e=0D
+ // boot option. If this lookup fails (i.e., the boot option references=
a=0D
+ // firmware volume that doesn't exist), then we'll proceed to delete t=
he=0D
+ // boot option.=0D
+ //=0D
+ SearchNode =3D Node1;=0D
+ Status =3D gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid,=0D
+ &SearchNode, &FvHandle);=0D
+=0D
+ if (!EFI_ERROR (Status)) {=0D
+ //=0D
+ // The firmware volume was found; now let's see if it contains the F=
vFile=0D
+ // identified by GUID.=0D
+ //=0D
+ EFI_FIRMWARE_VOLUME2_PROTOCOL *FvProtocol;=0D
+ MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFileNode;=0D
+ UINTN BufferSize;=0D
+ EFI_FV_FILETYPE FoundType;=0D
+ EFI_FV_FILE_ATTRIBUTES FileAttributes;=0D
+ UINT32 AuthenticationStatus;=0D
+=0D
+ Status =3D gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2Protoc=
olGuid,=0D
+ (VOID **)&FvProtocol);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ FvFileNode =3D (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)Node2;=0D
+ //=0D
+ // Buffer=3D=3DNULL means we request metadata only: BufferSize, Foun=
dType,=0D
+ // FileAttributes.=0D
+ //=0D
+ Status =3D FvProtocol->ReadFile (=0D
+ FvProtocol,=0D
+ &FvFileNode->FvFileName, // NameGuid=0D
+ NULL, // Buffer=0D
+ &BufferSize,=0D
+ &FoundType,=0D
+ &FileAttributes,=0D
+ &AuthenticationStatus=0D
+ );=0D
+ if (!EFI_ERROR (Status)) {=0D
+ //=0D
+ // The FvFile was found. Keep the boot option.=0D
+ //=0D
+ continue;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Delete the boot option.=0D
+ //=0D
+ Status =3D EfiBootManagerDeleteLoadOptionVariable (=0D
+ BootOptions[Index].OptionNumber, LoadOptionTypeBoot);=0D
+ DEBUG_CODE (=0D
+ CHAR16 *DevicePathString;=0D
+=0D
+ DevicePathString =3D ConvertDevicePathToText(BootOptions[Index].File=
Path,=0D
+ FALSE, FALSE);=0D
+ DEBUG ((=0D
+ EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_VERBOSE,=0D
+ "%a: removing stale Boot#%04x %s: %r\n",=0D
+ __FUNCTION__,=0D
+ (UINT32)BootOptions[Index].OptionNumber,=0D
+ DevicePathString =3D=3D NULL ? L"<unavailable>" : DevicePathString=
,=0D
+ Status=0D
+ ));=0D
+ if (DevicePathString !=3D NULL) {=0D
+ FreePool (DevicePathString);=0D
+ }=0D
+ );=0D
+ }=0D
+=0D
+ EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);=0D
+}=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+ConnectRootBridge (=0D
+ IN EFI_HANDLE RootBridgeHandle,=0D
+ IN VOID *Instance,=0D
+ IN VOID *Context=0D
+ );=0D
+=0D
+STATIC=0D
+EFI_STATUS=0D
+EFIAPI=0D
+ConnectVirtioPciRng (=0D
+ IN EFI_HANDLE Handle,=0D
+ IN VOID *Instance,=0D
+ IN VOID *Context=0D
+ );=0D
+=0D
+STATIC=0D
+VOID=0D
+SaveS3BootScript (=0D
+ VOID=0D
+ );=0D
+=0D
+//=0D
+// BDS Platform Functions=0D
+//=0D
+/**=0D
+ Do the platform init, can be customized by OEM/IBV=0D
+=0D
+ Possible things that can be done in PlatformBootManagerBeforeConsole:=0D
+=0D
+ > Update console variable: 1. include hot-plug devices;=0D
+ > 2. Clear ConIn and add SOL for AMT=0D
+ > Register new Driver#### or Boot####=0D
+ > Register new Key####: e.g.: F12=0D
+ > Signal ReadyToLock event=0D
+ > Authentication action: 1. connect Auth devices;=0D
+ > 2. Identify auto logon user.=0D
+**/=0D
+VOID=0D
+EFIAPI=0D
+PlatformBootManagerBeforeConsole (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_HANDLE Handle;=0D
+ EFI_STATUS Status;=0D
+=0D
+ DEBUG ((DEBUG_INFO, "PlatformBootManagerBeforeConsole\n"));=0D
+ InstallDevicePathCallback ();=0D
+=0D
+ VisitAllInstancesOfProtocol (&gEfiPciRootBridgeIoProtocolGuid,=0D
+ ConnectRootBridge, NULL);=0D
+=0D
+ //=0D
+ // Signal the ACPI platform driver that it can download QEMU ACPI tables=
.=0D
+ //=0D
+ EfiEventGroupSignal (&gRootBridgesConnectedEventGroupGuid);=0D
+=0D
+ //=0D
+ // We can't signal End-of-Dxe earlier than this. Namely, End-of-Dxe trig=
gers=0D
+ // the preparation of S3 system information. That logic has a hard depen=
dency=0D
+ // on the presence of the FACS ACPI table. Since our ACPI tables are onl=
y=0D
+ // installed after PCI enumeration completes, we must not trigger the S3=
save=0D
+ // earlier, hence we can't signal End-of-Dxe earlier.=0D
+ //=0D
+ EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);=0D
+=0D
+ if (QemuFwCfgS3Enabled ()) {=0D
+ //=0D
+ // Save the boot script too. Note that this will require us to emit th=
e=0D
+ // DxeSmmReadyToLock event just below, which in turn locks down SMM.=0D
+ //=0D
+ SaveS3BootScript ();=0D
+ }=0D
+=0D
+ //=0D
+ // Prevent further changes to LockBoxes or SMRAM.=0D
+ //=0D
+ Handle =3D NULL;=0D
+ Status =3D gBS->InstallProtocolInterface (&Handle,=0D
+ &gEfiDxeSmmReadyToLockProtocolGuid, EFI_NATIVE_INTERFACE=
,=0D
+ NULL);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ //=0D
+ // Dispatch deferred images after EndOfDxe event and ReadyToLock=0D
+ // installation.=0D
+ //=0D
+ EfiBootManagerDispatchDeferredImages ();=0D
+=0D
+ PlatformInitializeConsole (=0D
+ XenDetected() ? gXenPlatformConsole : gPlatformConsole);=0D
+=0D
+ //=0D
+ // Install both VIRTIO_DEVICE_PROTOCOL and (dependent) EFI_RNG_PROTOCOL=
=0D
+ // instances on Virtio PCI RNG devices.=0D
+ //=0D
+ VisitAllInstancesOfProtocol (&gEfiPciIoProtocolGuid, ConnectVirtioPciRng=
,=0D
+ NULL);=0D
+}=0D
+=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+ConnectRootBridge (=0D
+ IN EFI_HANDLE RootBridgeHandle,=0D
+ IN VOID *Instance,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+=0D
+ //=0D
+ // Make the PCI bus driver connect the root bridge, non-recursively. Thi=
s=0D
+ // will produce a number of child handles with PciIo on them.=0D
+ //=0D
+ Status =3D gBS->ConnectController (=0D
+ RootBridgeHandle, // ControllerHandle=0D
+ NULL, // DriverImageHandle=0D
+ NULL, // RemainingDevicePath -- produce all=
=0D
+ // children=0D
+ FALSE // Recursive=0D
+ );=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+STATIC=0D
+EFI_STATUS=0D
+EFIAPI=0D
+ConnectVirtioPciRng (=0D
+ IN EFI_HANDLE Handle,=0D
+ IN VOID *Instance,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ EFI_PCI_IO_PROTOCOL *PciIo;=0D
+ EFI_STATUS Status;=0D
+ UINT16 VendorId;=0D
+ UINT16 DeviceId;=0D
+ UINT8 RevisionId;=0D
+ BOOLEAN Virtio10;=0D
+ UINT16 SubsystemId;=0D
+=0D
+ PciIo =3D Instance;=0D
+=0D
+ //=0D
+ // Read and check VendorId.=0D
+ //=0D
+ Status =3D PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_VENDOR_ID_OF=
FSET,=0D
+ 1, &VendorId);=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+ if (VendorId !=3D VIRTIO_VENDOR_ID) {=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ //=0D
+ // Read DeviceId and RevisionId.=0D
+ //=0D
+ Status =3D PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_DEVICE_ID_OF=
FSET,=0D
+ 1, &DeviceId);=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+ Status =3D PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, PCI_REVISION_ID_O=
FFSET,=0D
+ 1, &RevisionId);=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+=0D
+ //=0D
+ // From DeviceId and RevisionId, determine whether the device is a=0D
+ // modern-only Virtio 1.0 device. In case of Virtio 1.0, DeviceId can=0D
+ // immediately be restricted to VIRTIO_SUBSYSTEM_ENTROPY_SOURCE, and=0D
+ // SubsystemId will only play a sanity-check role. Otherwise, DeviceId c=
an=0D
+ // only be sanity-checked, and SubsystemId will decide.=0D
+ //=0D
+ if (DeviceId =3D=3D 0x1040 + VIRTIO_SUBSYSTEM_ENTROPY_SOURCE &&=0D
+ RevisionId >=3D 0x01) {=0D
+ Virtio10 =3D TRUE;=0D
+ } else if (DeviceId >=3D 0x1000 && DeviceId <=3D 0x103F && RevisionId =
=3D=3D 0x00) {=0D
+ Virtio10 =3D FALSE;=0D
+ } else {=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ //=0D
+ // Read and check SubsystemId as dictated by Virtio10.=0D
+ //=0D
+ Status =3D PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16,=0D
+ PCI_SUBSYSTEM_ID_OFFSET, 1, &SubsystemId);=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+ if ((Virtio10 && SubsystemId >=3D 0x40) ||=0D
+ (!Virtio10 && SubsystemId =3D=3D VIRTIO_SUBSYSTEM_ENTROPY_SOURCE)) {=
=0D
+ Status =3D gBS->ConnectController (=0D
+ Handle, // ControllerHandle=0D
+ NULL, // DriverImageHandle -- connect all drivers=0D
+ NULL, // RemainingDevicePath -- produce all child ha=
ndles=0D
+ FALSE // Recursive -- don't follow child handles=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+ }=0D
+ return EFI_SUCCESS;=0D
+=0D
+Error:=0D
+ DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Add IsaKeyboard to ConIn; add IsaSerial to ConOut, ConIn, ErrOut.=0D
+=0D
+ @param[in] DeviceHandle Handle of the LPC Bridge device.=0D
+=0D
+ @retval EFI_SUCCESS Console devices on the LPC bridge have been added t=
o=0D
+ ConOut, ConIn, and ErrOut.=0D
+=0D
+ @return Error codes, due to EFI_DEVICE_PATH_PROTOCOL missin=
g=0D
+ from DeviceHandle.=0D
+**/=0D
+EFI_STATUS=0D
+PrepareLpcBridgeDevicePath (=0D
+ IN EFI_HANDLE DeviceHandle=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+ EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;=0D
+ CHAR16 *DevPathStr;=0D
+=0D
+ DevicePath =3D NULL;=0D
+ Status =3D gBS->HandleProtocol (=0D
+ DeviceHandle,=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ (VOID*)&DevicePath=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+ TempDevicePath =3D DevicePath;=0D
+=0D
+ //=0D
+ // Register Keyboard=0D
+ //=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode);=
=0D
+=0D
+ EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);=0D
+=0D
+ //=0D
+ // Register COM1=0D
+ //=0D
+ DevicePath =3D TempDevicePath;=0D
+ gPnp16550ComPortDeviceNode.UID =3D 0;=0D
+=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);=
=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);=0D
+=0D
+ //=0D
+ // Print Device Path=0D
+ //=0D
+ DevPathStr =3D ConvertDevicePathToText (DevicePath, FALSE, FALSE);=0D
+ if (DevPathStr !=3D NULL) {=0D
+ DEBUG((=0D
+ DEBUG_INFO,=0D
+ "BdsPlatform.c+%d: COM%d DevPath: %s\n",=0D
+ __LINE__,=0D
+ gPnp16550ComPortDeviceNode.UID + 1,=0D
+ DevPathStr=0D
+ ));=0D
+ FreePool(DevPathStr);=0D
+ }=0D
+=0D
+ EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);=0D
+ EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);=0D
+ EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);=0D
+=0D
+ //=0D
+ // Register COM2=0D
+ //=0D
+ DevicePath =3D TempDevicePath;=0D
+ gPnp16550ComPortDeviceNode.UID =3D 1;=0D
+=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);=
=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);=0D
+=0D
+ //=0D
+ // Print Device Path=0D
+ //=0D
+ DevPathStr =3D ConvertDevicePathToText (DevicePath, FALSE, FALSE);=0D
+ if (DevPathStr !=3D NULL) {=0D
+ DEBUG((=0D
+ DEBUG_INFO,=0D
+ "BdsPlatform.c+%d: COM%d DevPath: %s\n",=0D
+ __LINE__,=0D
+ gPnp16550ComPortDeviceNode.UID + 1,=0D
+ DevPathStr=0D
+ ));=0D
+ FreePool(DevPathStr);=0D
+ }=0D
+=0D
+ EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);=0D
+ EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);=0D
+ EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+EFI_STATUS=0D
+GetGopDevicePath (=0D
+ IN EFI_DEVICE_PATH_PROTOCOL *PciDevicePath,=0D
+ OUT EFI_DEVICE_PATH_PROTOCOL **GopDevicePath=0D
+ )=0D
+{=0D
+ UINTN Index;=0D
+ EFI_STATUS Status;=0D
+ EFI_HANDLE PciDeviceHandle;=0D
+ EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;=0D
+ EFI_DEVICE_PATH_PROTOCOL *TempPciDevicePath;=0D
+ UINTN GopHandleCount;=0D
+ EFI_HANDLE *GopHandleBuffer;=0D
+=0D
+ if (PciDevicePath =3D=3D NULL || GopDevicePath =3D=3D NULL) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ //=0D
+ // Initialize the GopDevicePath to be PciDevicePath=0D
+ //=0D
+ *GopDevicePath =3D PciDevicePath;=0D
+ TempPciDevicePath =3D PciDevicePath;=0D
+=0D
+ Status =3D gBS->LocateDevicePath (=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ &TempPciDevicePath,=0D
+ &PciDeviceHandle=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ //=0D
+ // Try to connect this handle, so that GOP driver could start on this=0D
+ // device and create child handles with GraphicsOutput Protocol installe=
d=0D
+ // on them, then we get device paths of these child handles and select=0D
+ // them as possible console device.=0D
+ //=0D
+ gBS->ConnectController (PciDeviceHandle, NULL, NULL, FALSE);=0D
+=0D
+ Status =3D gBS->LocateHandleBuffer (=0D
+ ByProtocol,=0D
+ &gEfiGraphicsOutputProtocolGuid,=0D
+ NULL,=0D
+ &GopHandleCount,=0D
+ &GopHandleBuffer=0D
+ );=0D
+ if (!EFI_ERROR (Status)) {=0D
+ //=0D
+ // Add all the child handles as possible Console Device=0D
+ //=0D
+ for (Index =3D 0; Index < GopHandleCount; Index++) {=0D
+ Status =3D gBS->HandleProtocol (GopHandleBuffer[Index],=0D
+ &gEfiDevicePathProtocolGuid, (VOID*)&TempDevicePath)=
;=0D
+ if (EFI_ERROR (Status)) {=0D
+ continue;=0D
+ }=0D
+ if (CompareMem (=0D
+ PciDevicePath,=0D
+ TempDevicePath,=0D
+ GetDevicePathSize (PciDevicePath) - END_DEVICE_PATH_LENGTH=0D
+ ) =3D=3D 0) {=0D
+ //=0D
+ // In current implementation, we only enable one of the child hand=
les=0D
+ // as console device, i.e. sotre one of the child handle's device=
=0D
+ // path to variable "ConOut"=0D
+ // In future, we could select all child handles to be console devi=
ce=0D
+ //=0D
+=0D
+ *GopDevicePath =3D TempDevicePath;=0D
+=0D
+ //=0D
+ // Delete the PCI device's path that added by=0D
+ // GetPlugInPciVgaDevicePath(). Add the integrity GOP device path.=
=0D
+ //=0D
+ EfiBootManagerUpdateConsoleVariable (ConOutDev, NULL, PciDevicePat=
h);=0D
+ EfiBootManagerUpdateConsoleVariable (ConOutDev, TempDevicePath, NU=
LL);=0D
+ }=0D
+ }=0D
+ gBS->FreePool (GopHandleBuffer);=0D
+ }=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+/**=0D
+ Add PCI display to ConOut.=0D
+=0D
+ @param[in] DeviceHandle Handle of the PCI display device.=0D
+=0D
+ @retval EFI_SUCCESS The PCI display device has been added to ConOut.=0D
+=0D
+ @return Error codes, due to EFI_DEVICE_PATH_PROTOCOL missin=
g=0D
+ from DeviceHandle.=0D
+**/=0D
+EFI_STATUS=0D
+PreparePciDisplayDevicePath (=0D
+ IN EFI_HANDLE DeviceHandle=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+ EFI_DEVICE_PATH_PROTOCOL *GopDevicePath;=0D
+=0D
+ DevicePath =3D NULL;=0D
+ GopDevicePath =3D NULL;=0D
+ Status =3D gBS->HandleProtocol (=0D
+ DeviceHandle,=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ (VOID*)&DevicePath=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ GetGopDevicePath (DevicePath, &GopDevicePath);=0D
+ DevicePath =3D GopDevicePath;=0D
+=0D
+ EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+/**=0D
+ Add PCI Serial to ConOut, ConIn, ErrOut.=0D
+=0D
+ @param[in] DeviceHandle Handle of the PCI serial device.=0D
+=0D
+ @retval EFI_SUCCESS The PCI serial device has been added to ConOut, Con=
In,=0D
+ ErrOut.=0D
+=0D
+ @return Error codes, due to EFI_DEVICE_PATH_PROTOCOL missin=
g=0D
+ from DeviceHandle.=0D
+**/=0D
+EFI_STATUS=0D
+PreparePciSerialDevicePath (=0D
+ IN EFI_HANDLE DeviceHandle=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+=0D
+ DevicePath =3D NULL;=0D
+ Status =3D gBS->HandleProtocol (=0D
+ DeviceHandle,=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ (VOID*)&DevicePath=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);=0D
+ DevicePath =3D AppendDevicePathNode (DevicePath,=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);=0D
+=0D
+ EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);=0D
+ EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);=0D
+ EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+EFI_STATUS=0D
+VisitAllInstancesOfProtocol (=0D
+ IN EFI_GUID *Id,=0D
+ IN PROTOCOL_INSTANCE_CALLBACK CallBackFunction,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ UINTN HandleCount;=0D
+ EFI_HANDLE *HandleBuffer;=0D
+ UINTN Index;=0D
+ VOID *Instance;=0D
+=0D
+ //=0D
+ // Start to check all the PciIo to find all possible device=0D
+ //=0D
+ HandleCount =3D 0;=0D
+ HandleBuffer =3D NULL;=0D
+ Status =3D gBS->LocateHandleBuffer (=0D
+ ByProtocol,=0D
+ Id,=0D
+ NULL,=0D
+ &HandleCount,=0D
+ &HandleBuffer=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ for (Index =3D 0; Index < HandleCount; Index++) {=0D
+ Status =3D gBS->HandleProtocol (HandleBuffer[Index], Id, &Instance);=0D
+ if (EFI_ERROR (Status)) {=0D
+ continue;=0D
+ }=0D
+=0D
+ Status =3D (*CallBackFunction) (=0D
+ HandleBuffer[Index],=0D
+ Instance,=0D
+ Context=0D
+ );=0D
+ }=0D
+=0D
+ gBS->FreePool (HandleBuffer);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+VisitingAPciInstance (=0D
+ IN EFI_HANDLE Handle,=0D
+ IN VOID *Instance,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_PCI_IO_PROTOCOL *PciIo;=0D
+ PCI_TYPE00 Pci;=0D
+=0D
+ PciIo =3D (EFI_PCI_IO_PROTOCOL*) Instance;=0D
+=0D
+ //=0D
+ // Check for all PCI device=0D
+ //=0D
+ Status =3D PciIo->Pci.Read (=0D
+ PciIo,=0D
+ EfiPciIoWidthUint32,=0D
+ 0,=0D
+ sizeof (Pci) / sizeof (UINT32),=0D
+ &Pci=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ return (*(VISIT_PCI_INSTANCE_CALLBACK)(UINTN) Context) (=0D
+ Handle,=0D
+ PciIo,=0D
+ &Pci=0D
+ );=0D
+=0D
+}=0D
+=0D
+=0D
+=0D
+EFI_STATUS=0D
+VisitAllPciInstances (=0D
+ IN VISIT_PCI_INSTANCE_CALLBACK CallBackFunction=0D
+ )=0D
+{=0D
+ return VisitAllInstancesOfProtocol (=0D
+ &gEfiPciIoProtocolGuid,=0D
+ VisitingAPciInstance,=0D
+ (VOID*)(UINTN) CallBackFunction=0D
+ );=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Do platform specific PCI Device check and add them to=0D
+ ConOut, ConIn, ErrOut.=0D
+=0D
+ @param[in] Handle - Handle of PCI device instance=0D
+ @param[in] PciIo - PCI IO protocol instance=0D
+ @param[in] Pci - PCI Header register block=0D
+=0D
+ @retval EFI_SUCCESS - PCI Device check and Console variable update=0D
+ successfully.=0D
+ @retval EFI_STATUS - PCI Device check or Console variable update fail.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+DetectAndPreparePlatformPciDevicePath (=0D
+ IN EFI_HANDLE Handle,=0D
+ IN EFI_PCI_IO_PROTOCOL *PciIo,=0D
+ IN PCI_TYPE00 *Pci=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+=0D
+ Status =3D PciIo->Attributes (=0D
+ PciIo,=0D
+ EfiPciIoAttributeOperationEnable,=0D
+ EFI_PCI_DEVICE_ENABLE,=0D
+ NULL=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ //=0D
+ // Here we decide whether it is LPC Bridge=0D
+ //=0D
+ if ((IS_PCI_LPC (Pci)) ||=0D
+ ((IS_PCI_ISA_PDECODE (Pci)) &&=0D
+ (Pci->Hdr.VendorId =3D=3D 0x8086) &&=0D
+ (Pci->Hdr.DeviceId =3D=3D 0x7000)=0D
+ )=0D
+ ) {=0D
+ //=0D
+ // Add IsaKeyboard to ConIn,=0D
+ // add IsaSerial to ConOut, ConIn, ErrOut=0D
+ //=0D
+ DEBUG ((DEBUG_INFO, "Found LPC Bridge device\n"));=0D
+ PrepareLpcBridgeDevicePath (Handle);=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+ //=0D
+ // Here we decide which Serial device to enable in PCI bus=0D
+ //=0D
+ if (IS_PCI_16550SERIAL (Pci)) {=0D
+ //=0D
+ // Add them to ConOut, ConIn, ErrOut.=0D
+ //=0D
+ DEBUG ((DEBUG_INFO, "Found PCI 16550 SERIAL device\n"));=0D
+ PreparePciSerialDevicePath (Handle);=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ //=0D
+ // Here we decide which display device to enable in PCI bus=0D
+ //=0D
+ if (IS_PCI_DISPLAY (Pci)) {=0D
+ //=0D
+ // Add them to ConOut.=0D
+ //=0D
+ DEBUG ((DEBUG_INFO, "Found PCI display device\n"));=0D
+ PreparePciDisplayDevicePath (Handle);=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Connect the predefined platform default console device.=0D
+=0D
+ Always try to find and enable PCI display devices.=0D
+=0D
+ @param[in] PlatformConsole Predefined platform default console device a=
rray.=0D
+**/=0D
+VOID=0D
+PlatformInitializeConsole (=0D
+ IN PLATFORM_CONSOLE_CONNECT_ENTRY *PlatformConsole=0D
+ )=0D
+{=0D
+ UINTN Index;=0D
+=0D
+ //=0D
+ // Do platform specific PCI Device check and add them to ConOut, ConIn,=
=0D
+ // ErrOut=0D
+ //=0D
+ VisitAllPciInstances (DetectAndPreparePlatformPciDevicePath);=0D
+=0D
+ //=0D
+ // Have chance to connect the platform default console,=0D
+ // the platform default console is the minimum device group=0D
+ // the platform should support=0D
+ //=0D
+ for (Index =3D 0; PlatformConsole[Index].DevicePath !=3D NULL; ++Index) =
{=0D
+ //=0D
+ // Update the console variable with the connect type=0D
+ //=0D
+ if ((PlatformConsole[Index].ConnectType & CONSOLE_IN) =3D=3D CONSOLE_I=
N) {=0D
+ EfiBootManagerUpdateConsoleVariable (ConIn,=0D
+ PlatformConsole[Index].DevicePath, NULL);=0D
+ }=0D
+ if ((PlatformConsole[Index].ConnectType & CONSOLE_OUT) =3D=3D CONSOLE_=
OUT) {=0D
+ EfiBootManagerUpdateConsoleVariable (ConOut,=0D
+ PlatformConsole[Index].DevicePath, NULL);=0D
+ }=0D
+ if ((PlatformConsole[Index].ConnectType & STD_ERROR) =3D=3D STD_ERROR)=
{=0D
+ EfiBootManagerUpdateConsoleVariable (ErrOut,=0D
+ PlatformConsole[Index].DevicePath, NULL);=0D
+ }=0D
+ }=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Configure PCI Interrupt Line register for applicable devices=0D
+ Ported from SeaBIOS, src/fw/pciinit.c, *_pci_slot_get_irq()=0D
+=0D
+ @param[in] Handle - Handle of PCI device instance=0D
+ @param[in] PciIo - PCI IO protocol instance=0D
+ @param[in] PciHdr - PCI Header register block=0D
+=0D
+ @retval EFI_SUCCESS - PCI Interrupt Line register configured successfull=
y.=0D
+=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+SetPciIntLine (=0D
+ IN EFI_HANDLE Handle,=0D
+ IN EFI_PCI_IO_PROTOCOL *PciIo,=0D
+ IN PCI_TYPE00 *PciHdr=0D
+ )=0D
+{=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevPathNode;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevPath;=0D
+ UINTN RootSlot;=0D
+ UINTN Idx;=0D
+ UINT8 IrqLine;=0D
+ EFI_STATUS Status;=0D
+ UINT32 RootBusNumber;=0D
+=0D
+ Status =3D EFI_SUCCESS;=0D
+=0D
+ if (PciHdr->Device.InterruptPin !=3D 0) {=0D
+=0D
+ DevPathNode =3D DevicePathFromHandle (Handle);=0D
+ ASSERT (DevPathNode !=3D NULL);=0D
+ DevPath =3D DevPathNode;=0D
+=0D
+ RootBusNumber =3D 0;=0D
+ if (DevicePathType (DevPathNode) =3D=3D ACPI_DEVICE_PATH &&=0D
+ DevicePathSubType (DevPathNode) =3D=3D ACPI_DP &&=0D
+ ((ACPI_HID_DEVICE_PATH *)DevPathNode)->HID =3D=3D EISA_PNP_ID(0x0A=
03)) {=0D
+ RootBusNumber =3D ((ACPI_HID_DEVICE_PATH *)DevPathNode)->UID;=0D
+ }=0D
+=0D
+ //=0D
+ // Compute index into PciHostIrqs[] table by walking=0D
+ // the device path and adding up all device numbers=0D
+ //=0D
+ Status =3D EFI_NOT_FOUND;=0D
+ RootSlot =3D 0;=0D
+ Idx =3D PciHdr->Device.InterruptPin - 1;=0D
+ while (!IsDevicePathEnd (DevPathNode)) {=0D
+ if (DevicePathType (DevPathNode) =3D=3D HARDWARE_DEVICE_PATH &&=0D
+ DevicePathSubType (DevPathNode) =3D=3D HW_PCI_DP) {=0D
+=0D
+ Idx +=3D ((PCI_DEVICE_PATH *)DevPathNode)->Device;=0D
+=0D
+ //=0D
+ // Unlike SeaBIOS, which starts climbing from the leaf device=0D
+ // up toward the root, we traverse the device path starting at=0D
+ // the root moving toward the leaf node.=0D
+ // The slot number of the top-level parent bridge is needed for=0D
+ // Q35 cases with more than 24 slots on the root bus.=0D
+ //=0D
+ if (Status !=3D EFI_SUCCESS) {=0D
+ Status =3D EFI_SUCCESS;=0D
+ RootSlot =3D ((PCI_DEVICE_PATH *)DevPathNode)->Device;=0D
+ }=0D
+ }=0D
+=0D
+ DevPathNode =3D NextDevicePathNode (DevPathNode);=0D
+ }=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+ if (RootBusNumber =3D=3D 0 && RootSlot =3D=3D 0) {=0D
+ DEBUG((=0D
+ DEBUG_ERROR,=0D
+ "%a: PCI host bridge (00:00.0) should have no interrupts!\n",=0D
+ __FUNCTION__=0D
+ ));=0D
+ ASSERT (FALSE);=0D
+ }=0D
+=0D
+ //=0D
+ // Final PciHostIrqs[] index calculation depends on the platform=0D
+ // and should match SeaBIOS src/fw/pciinit.c *_pci_slot_get_irq()=0D
+ //=0D
+ switch (mHostBridgeDevId) {=0D
+ case INTEL_82441_DEVICE_ID:=0D
+ Idx -=3D 1;=0D
+ break;=0D
+ case INTEL_Q35_MCH_DEVICE_ID:=0D
+ //=0D
+ // SeaBIOS contains the following comment:=0D
+ // "Slots 0-24 rotate slot:pin mapping similar to piix above, but=
=0D
+ // with a different starting index - see q35-acpi-dsdt.dsl.=0D
+ //=0D
+ // Slots 25-31 all use LNKA mapping (or LNKE, but A:D =3D E:H)"=0D
+ //=0D
+ if (RootSlot > 24) {=0D
+ //=0D
+ // in this case, subtract back out RootSlot from Idx=0D
+ // (SeaBIOS never adds it to begin with, but that would make our=
=0D
+ // device path traversal loop above too awkward)=0D
+ //=0D
+ Idx -=3D RootSlot;=0D
+ }=0D
+ break;=0D
+ default:=0D
+ ASSERT (FALSE); // should never get here=0D
+ }=0D
+ Idx %=3D ARRAY_SIZE (PciHostIrqs);=0D
+ IrqLine =3D PciHostIrqs[Idx];=0D
+=0D
+ DEBUG_CODE_BEGIN ();=0D
+ {=0D
+ CHAR16 *DevPathString;=0D
+ STATIC CHAR16 Fallback[] =3D L"<failed to convert>";=0D
+ UINTN Segment, Bus, Device, Function;=0D
+=0D
+ DevPathString =3D ConvertDevicePathToText (DevPath, FALSE, FALSE);=0D
+ if (DevPathString =3D=3D NULL) {=0D
+ DevPathString =3D Fallback;=0D
+ }=0D
+ Status =3D PciIo->GetLocation (PciIo, &Segment, &Bus, &Device, &Func=
tion);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ DEBUG ((DEBUG_VERBOSE, "%a: [%02x:%02x.%x] %s -> 0x%02x\n", __FUNCTI=
ON__,=0D
+ (UINT32)Bus, (UINT32)Device, (UINT32)Function, DevPathString,=0D
+ IrqLine));=0D
+=0D
+ if (DevPathString !=3D Fallback) {=0D
+ FreePool (DevPathString);=0D
+ }=0D
+ }=0D
+ DEBUG_CODE_END ();=0D
+=0D
+ //=0D
+ // Set PCI Interrupt Line register for this device to PciHostIrqs[Idx]=
=0D
+ //=0D
+ Status =3D PciIo->Pci.Write (=0D
+ PciIo,=0D
+ EfiPciIoWidthUint8,=0D
+ PCI_INT_LINE_OFFSET,=0D
+ 1,=0D
+ &IrqLine=0D
+ );=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+=0D
+VOID=0D
+PciAcpiInitialization (=0D
+ )=0D
+{=0D
+ UINTN Pmba;=0D
+=0D
+ //=0D
+ // Query Host Bridge DID to determine platform type=0D
+ //=0D
+ mHostBridgeDevId =3D PcdGet16 (PcdOvmfHostBridgePciDevId);=0D
+ switch (mHostBridgeDevId) {=0D
+ case INTEL_82441_DEVICE_ID:=0D
+ Pmba =3D POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);=0D
+ //=0D
+ // 00:01.0 ISA Bridge (PIIX4) LNK routing targets=0D
+ //=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x60), 0x0b); // A=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x61), 0x0b); // B=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x62), 0x0a); // C=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x63), 0x0a); // D=0D
+ break;=0D
+ case INTEL_Q35_MCH_DEVICE_ID:=0D
+ Pmba =3D POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);=0D
+ //=0D
+ // 00:1f.0 LPC Bridge (Q35) LNK routing targets=0D
+ //=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x60), 0x0a); // A=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x61), 0x0a); // B=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x62), 0x0b); // C=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x63), 0x0b); // D=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x68), 0x0a); // E=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x69), 0x0a); // F=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x6a), 0x0b); // G=0D
+ PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x6b), 0x0b); // H=0D
+ break;=0D
+ default:=0D
+ if (XenDetected ()) {=0D
+ //=0D
+ // There is no PCI bus in this case.=0D
+ //=0D
+ return;=0D
+ }=0D
+ DEBUG ((DEBUG_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",=
=0D
+ __FUNCTION__, mHostBridgeDevId));=0D
+ ASSERT (FALSE);=0D
+ return;=0D
+ }=0D
+=0D
+ //=0D
+ // Initialize PCI_INTERRUPT_LINE for applicable present PCI devices=0D
+ //=0D
+ VisitAllPciInstances (SetPciIntLine);=0D
+=0D
+ //=0D
+ // Set ACPI SCI_EN bit in PMCNTRL=0D
+ //=0D
+ IoOr16 ((PciRead32 (Pmba) & ~BIT0) + 4, BIT0);=0D
+}=0D
+=0D
+EFI_STATUS=0D
+EFIAPI=0D
+ConnectRecursivelyIfPciMassStorage (=0D
+ IN EFI_HANDLE Handle,=0D
+ IN EFI_PCI_IO_PROTOCOL *Instance,=0D
+ IN PCI_TYPE00 *PciHeader=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;=0D
+ CHAR16 *DevPathStr;=0D
+=0D
+ //=0D
+ // Recognize PCI Mass Storage, and Xen PCI devices=0D
+ //=0D
+ if (IS_CLASS1 (PciHeader, PCI_CLASS_MASS_STORAGE) ||=0D
+ (XenDetected() && IS_CLASS2 (PciHeader, 0xFF, 0x80))) {=0D
+ DevicePath =3D NULL;=0D
+ Status =3D gBS->HandleProtocol (=0D
+ Handle,=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ (VOID*)&DevicePath=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ //=0D
+ // Print Device Path=0D
+ //=0D
+ DevPathStr =3D ConvertDevicePathToText (DevicePath, FALSE, FALSE);=0D
+ if (DevPathStr !=3D NULL) {=0D
+ DEBUG((=0D
+ DEBUG_INFO,=0D
+ "Found %s device: %s\n",=0D
+ (IS_CLASS1 (PciHeader, PCI_CLASS_MASS_STORAGE) ?=0D
+ L"Mass Storage" :=0D
+ L"Xen"=0D
+ ),=0D
+ DevPathStr=0D
+ ));=0D
+ FreePool(DevPathStr);=0D
+ }=0D
+=0D
+ Status =3D gBS->ConnectController (Handle, NULL, NULL, TRUE);=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ }=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Connect with predefined platform connect sequence.=0D
+=0D
+ The OEM/IBV can customize with their own connect sequence.=0D
+**/=0D
+VOID=0D
+PlatformBdsConnectSequence (=0D
+ VOID=0D
+ )=0D
+{=0D
+ UINTN Index;=0D
+ RETURN_STATUS Status;=0D
+=0D
+ DEBUG ((DEBUG_INFO, "PlatformBdsConnectSequence\n"));=0D
+=0D
+ Index =3D 0;=0D
+=0D
+ //=0D
+ // Here we can get the customized platform connect sequence=0D
+ // Notes: we can connect with new variable which record the=0D
+ // last time boots connect device path sequence=0D
+ //=0D
+ while (gPlatformConnectSequence[Index] !=3D NULL) {=0D
+ //=0D
+ // Build the platform boot option=0D
+ //=0D
+ EfiBootManagerConnectDevicePath (gPlatformConnectSequence[Index], NULL=
);=0D
+ Index++;=0D
+ }=0D
+=0D
+ Status =3D ConnectDevicesFromQemu ();=0D
+ if (RETURN_ERROR (Status)) {=0D
+ //=0D
+ // Just use the simple policy to connect all devices=0D
+ //=0D
+ DEBUG ((DEBUG_INFO, "EfiBootManagerConnectAll\n"));=0D
+ EfiBootManagerConnectAll ();=0D
+ }=0D
+}=0D
+=0D
+/**=0D
+ Save the S3 boot script.=0D
+=0D
+ Note that DxeSmmReadyToLock must be signaled after this function returns=
;=0D
+ otherwise the script wouldn't be saved actually.=0D
+**/=0D
+STATIC=0D
+VOID=0D
+SaveS3BootScript (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_S3_SAVE_STATE_PROTOCOL *BootScript;=0D
+ STATIC CONST UINT8 Info[] =3D { 0xDE, 0xAD, 0xBE, 0xEF };=0D
+=0D
+ Status =3D gBS->LocateProtocol (&gEfiS3SaveStateProtocolGuid, NULL,=0D
+ (VOID **) &BootScript);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ //=0D
+ // Despite the opcode documentation in the PI spec, the protocol=0D
+ // implementation embeds a deep copy of the info in the boot script, rat=
her=0D
+ // than storing just a pointer to runtime or NVS storage.=0D
+ //=0D
+ Status =3D BootScript->Write(BootScript, EFI_BOOT_SCRIPT_INFORMATION_OPC=
ODE,=0D
+ (UINT32) sizeof Info,=0D
+ (EFI_PHYSICAL_ADDRESS)(UINTN) &Info);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Do the platform specific action after the console is ready=0D
+=0D
+ Possible things that can be done in PlatformBootManagerAfterConsole:=0D
+=0D
+ > Console post action:=0D
+ > Dynamically switch output mode from 100x31 to 80x25 for certain sena=
rino=0D
+ > Signal console ready platform customized event=0D
+ > Run diagnostics like memory testing=0D
+ > Connect certain devices=0D
+ > Dispatch aditional option roms=0D
+ > Special boot: e.g.: USB boot, enter UI=0D
+**/=0D
+VOID=0D
+EFIAPI=0D
+PlatformBootManagerAfterConsole (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_BOOT_MODE BootMode;=0D
+=0D
+ DEBUG ((DEBUG_INFO, "PlatformBootManagerAfterConsole\n"));=0D
+=0D
+ //=0D
+ // Get current Boot Mode=0D
+ //=0D
+ BootMode =3D GetBootModeHob ();=0D
+ DEBUG ((DEBUG_INFO, "Boot Mode:%x\n", BootMode));=0D
+=0D
+ //=0D
+ // Go the different platform policy with different boot mode=0D
+ // Notes: this part code can be change with the table policy=0D
+ //=0D
+ ASSERT (BootMode =3D=3D BOOT_WITH_FULL_CONFIGURATION);=0D
+=0D
+ //=0D
+ // Logo show=0D
+ //=0D
+ BootLogoEnableLogo ();=0D
+=0D
+ //=0D
+ // Set PCI Interrupt Line registers and ACPI SCI_EN=0D
+ //=0D
+ PciAcpiInitialization ();=0D
+=0D
+ //=0D
+ // Process TPM PPI request=0D
+ //=0D
+ Tcg2PhysicalPresenceLibProcessRequest (NULL);=0D
+=0D
+ //=0D
+ // Perform some platform specific connect sequence=0D
+ //=0D
+ PlatformBdsConnectSequence ();=0D
+=0D
+ EfiBootManagerRefreshAllBootOption ();=0D
+=0D
+ //=0D
+ // Register UEFI Shell (Will be removed if the Shell isn't built=0D
+ // which is the default)=0D
+ //=0D
+ PlatformRegisterFvBootOption (=0D
+ &gUefiShellFileGuid, L"EFI Internal Shell", LOAD_OPTION_ACTIVE=0D
+ );=0D
+=0D
+ //=0D
+ // Register Grub=0D
+ //=0D
+ PlatformRegisterFvBootOption (=0D
+ &gGrubFileGuid, L"Grub Bootloader", LOAD_OPTION_ACTIVE=0D
+ );=0D
+=0D
+ RemoveStaleFvFileOptions ();=0D
+=0D
+ PlatformBmPrintScRegisterHandler ();=0D
+}=0D
+=0D
+/**=0D
+ This notification function is invoked when an instance of the=0D
+ EFI_DEVICE_PATH_PROTOCOL is produced.=0D
+=0D
+ @param Event The event that occurred=0D
+ @param Context For EFI compatibility. Not used.=0D
+=0D
+**/=0D
+VOID=0D
+EFIAPI=0D
+NotifyDevPath (=0D
+ IN EFI_EVENT Event,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ EFI_HANDLE Handle;=0D
+ EFI_STATUS Status;=0D
+ UINTN BufferSize;=0D
+ EFI_DEVICE_PATH_PROTOCOL *DevPathNode;=0D
+ ATAPI_DEVICE_PATH *Atapi;=0D
+=0D
+ //=0D
+ // Examine all new handles=0D
+ //=0D
+ for (;;) {=0D
+ //=0D
+ // Get the next handle=0D
+ //=0D
+ BufferSize =3D sizeof (Handle);=0D
+ Status =3D gBS->LocateHandle (=0D
+ ByRegisterNotify,=0D
+ NULL,=0D
+ mEfiDevPathNotifyReg,=0D
+ &BufferSize,=0D
+ &Handle=0D
+ );=0D
+=0D
+ //=0D
+ // If not found, we're done=0D
+ //=0D
+ if (EFI_NOT_FOUND =3D=3D Status) {=0D
+ break;=0D
+ }=0D
+=0D
+ if (EFI_ERROR (Status)) {=0D
+ continue;=0D
+ }=0D
+=0D
+ //=0D
+ // Get the DevicePath protocol on that handle=0D
+ //=0D
+ Status =3D gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid,=0D
+ (VOID **)&DevPathNode);=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ while (!IsDevicePathEnd (DevPathNode)) {=0D
+ //=0D
+ // Find the handler to dump this device path node=0D
+ //=0D
+ if (=0D
+ (DevicePathType(DevPathNode) =3D=3D MESSAGING_DEVICE_PATH) &&=0D
+ (DevicePathSubType(DevPathNode) =3D=3D MSG_ATAPI_DP)=0D
+ ) {=0D
+ Atapi =3D (ATAPI_DEVICE_PATH*) DevPathNode;=0D
+ PciOr16 (=0D
+ PCI_LIB_ADDRESS (=0D
+ 0,=0D
+ 1,=0D
+ 1,=0D
+ (Atapi->PrimarySecondary =3D=3D 1) ? 0x42: 0x40=0D
+ ),=0D
+ BIT15=0D
+ );=0D
+ }=0D
+=0D
+ //=0D
+ // Next device path node=0D
+ //=0D
+ DevPathNode =3D NextDevicePathNode (DevPathNode);=0D
+ }=0D
+ }=0D
+=0D
+ return;=0D
+}=0D
+=0D
+=0D
+VOID=0D
+InstallDevicePathCallback (=0D
+ VOID=0D
+ )=0D
+{=0D
+ DEBUG ((DEBUG_INFO, "Registered NotifyDevPath Event\n"));=0D
+ mEfiDevPathEvent =3D EfiCreateProtocolNotifyEvent (=0D
+ &gEfiDevicePathProtocolGuid,=0D
+ TPL_CALLBACK,=0D
+ NotifyDevPath,=0D
+ NULL,=0D
+ &mEfiDevPathNotifyReg=0D
+ );=0D
+}=0D
+=0D
+/**=0D
+ This function is called each second during the boot manager waits the=0D
+ timeout.=0D
+=0D
+ @param TimeoutRemain The remaining timeout.=0D
+**/=0D
+VOID=0D
+EFIAPI=0D
+PlatformBootManagerWaitCallback (=0D
+ UINT16 TimeoutRemain=0D
+ )=0D
+{=0D
+ //=0D
+ // Since the timeout should be forced to zero we should never=0D
+ // Get here=0D
+ //=0D
+ ASSERT (FALSE);=0D
+}=0D
+=0D
+/**=0D
+ The function is called when no boot option could be launched,=0D
+ including platform recovery options and options pointing to applications=
=0D
+ built into firmware volumes.=0D
+=0D
+ If this function returns, BDS attempts to enter an infinite loop.=0D
+**/=0D
+VOID=0D
+EFIAPI=0D
+PlatformBootManagerUnableToBoot (=0D
+ VOID=0D
+ )=0D
+{=0D
+ //=0D
+ // If we get here something failed about the grub boot but since=0D
+ // We're privy to the secret we must panic and not retry or loop=0D
+ //=0D
+ ASSERT (FALSE);=0D
+}=0D
diff --git a/OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformData.c b/Ov=
mfPkg/Library/PlatformBootManagerLibGrub/PlatformData.c
new file mode 100644
index 0000000000..2858c3dfd5
--- /dev/null
+++ b/OvmfPkg/Library/PlatformBootManagerLibGrub/PlatformData.c
@@ -0,0 +1,213 @@
+/** @file=0D
+ Defined the platform specific device path which will be used by=0D
+ platform Bbd to perform the platform policy connect.=0D
+=0D
+ Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#include "BdsPlatform.h"=0D
+#include <Guid/QemuRamfb.h>=0D
+#include <Guid/SerialPortLibVendor.h>=0D
+=0D
+//=0D
+// Vendor UART Device Path structure=0D
+//=0D
+#pragma pack (1)=0D
+typedef struct {=0D
+ VENDOR_DEVICE_PATH VendorHardware;=0D
+ UART_DEVICE_PATH Uart;=0D
+ VENDOR_DEVICE_PATH TerminalType;=0D
+ EFI_DEVICE_PATH_PROTOCOL End;=0D
+} VENDOR_UART_DEVICE_PATH;=0D
+#pragma pack ()=0D
+=0D
+//=0D
+// USB Keyboard Device Path structure=0D
+//=0D
+#pragma pack (1)=0D
+typedef struct {=0D
+ USB_CLASS_DEVICE_PATH Keyboard;=0D
+ EFI_DEVICE_PATH_PROTOCOL End;=0D
+} USB_KEYBOARD_DEVICE_PATH;=0D
+#pragma pack ()=0D
+=0D
+//=0D
+// QemuRamfb Device Path structure=0D
+//=0D
+#pragma pack (1)=0D
+typedef struct {=0D
+ VENDOR_DEVICE_PATH Vendor;=0D
+ ACPI_ADR_DEVICE_PATH AcpiAdr;=0D
+ EFI_DEVICE_PATH_PROTOCOL End;=0D
+} VENDOR_RAMFB_DEVICE_PATH;=0D
+#pragma pack ()=0D
+=0D
+ACPI_HID_DEVICE_PATH gPnpPs2KeyboardDeviceNode =3D gPnpPs2Keyboard;=
=0D
+ACPI_HID_DEVICE_PATH gPnp16550ComPortDeviceNode =3D gPnp16550ComPort=
;=0D
+UART_DEVICE_PATH gUartDeviceNode =3D gUart;=0D
+VENDOR_DEVICE_PATH gTerminalTypeDeviceNode =3D gPcAnsiTerminal;=
=0D
+=0D
+//=0D
+// Platform specific keyboard device path=0D
+//=0D
+=0D
+=0D
+//=0D
+// Debug Agent UART Device Path=0D
+//=0D
+VENDOR_UART_DEVICE_PATH gDebugAgentUartDevicePath =3D {=0D
+ {=0D
+ {=0D
+ HARDWARE_DEVICE_PATH,=0D
+ HW_VENDOR_DP,=0D
+ {=0D
+ (UINT8) (sizeof (VENDOR_DEVICE_PATH)),=0D
+ (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)=0D
+ }=0D
+ },=0D
+ EFI_DEBUG_AGENT_GUID,=0D
+ },=0D
+ {=0D
+ {=0D
+ MESSAGING_DEVICE_PATH,=0D
+ MSG_UART_DP,=0D
+ {=0D
+ (UINT8) (sizeof (UART_DEVICE_PATH)),=0D
+ (UINT8) ((sizeof (UART_DEVICE_PATH)) >> 8)=0D
+ }=0D
+ },=0D
+ 0, // Reserved=0D
+ 0, // BaudRate - Default=0D
+ 0, // DataBits - Default=0D
+ 0, // Parity - Default=0D
+ 0, // StopBits - Default=0D
+ },=0D
+ gPcAnsiTerminal,=0D
+ gEndEntire=0D
+};=0D
+=0D
+STATIC USB_KEYBOARD_DEVICE_PATH gUsbKeyboardDevicePath =3D {=0D
+ {=0D
+ {=0D
+ MESSAGING_DEVICE_PATH,=0D
+ MSG_USB_CLASS_DP,=0D
+ {=0D
+ (UINT8)sizeof (USB_CLASS_DEVICE_PATH),=0D
+ (UINT8)(sizeof (USB_CLASS_DEVICE_PATH) >> 8)=0D
+ }=0D
+ },=0D
+ 0xFFFF, // VendorId: any=0D
+ 0xFFFF, // ProductId: any=0D
+ 3, // DeviceClass: HID=0D
+ 1, // DeviceSubClass: boot=0D
+ 1 // DeviceProtocol: keyboard=0D
+ },=0D
+ gEndEntire=0D
+};=0D
+=0D
+STATIC VENDOR_RAMFB_DEVICE_PATH gQemuRamfbDevicePath =3D {=0D
+ {=0D
+ {=0D
+ HARDWARE_DEVICE_PATH,=0D
+ HW_VENDOR_DP,=0D
+ {=0D
+ (UINT8) (sizeof (VENDOR_DEVICE_PATH)),=0D
+ (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)=0D
+ }=0D
+ },=0D
+ QEMU_RAMFB_GUID,=0D
+ },=0D
+ {=0D
+ {=0D
+ ACPI_DEVICE_PATH,=0D
+ ACPI_ADR_DP,=0D
+ {=0D
+ (UINT8) (sizeof (ACPI_ADR_DEVICE_PATH)),=0D
+ (UINT8) ((sizeof (ACPI_ADR_DEVICE_PATH)) >> 8)=0D
+ }=0D
+ },=0D
+ ACPI_DISPLAY_ADR (=0D
+ 1, // DeviceIdScheme=0D
+ 0, // HeadId=0D
+ 0, // NonVgaOutput=0D
+ 1, // BiosCanDetect=0D
+ 0, // VendorInfo=0D
+ ACPI_ADR_DISPLAY_TYPE_EXTERNAL_DIGITAL, // Type=0D
+ 0, // Port=0D
+ 0 // Index=0D
+ ),=0D
+ },=0D
+ gEndEntire=0D
+};=0D
+=0D
+STATIC VENDOR_UART_DEVICE_PATH gXenConsoleDevicePath =3D {=0D
+ {=0D
+ {=0D
+ HARDWARE_DEVICE_PATH,=0D
+ HW_VENDOR_DP,=0D
+ {=0D
+ (UINT8) (sizeof (VENDOR_DEVICE_PATH)),=0D
+ (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)=0D
+ }=0D
+ },=0D
+ EDKII_SERIAL_PORT_LIB_VENDOR_GUID=0D
+ },=0D
+ {=0D
+ {=0D
+ MESSAGING_DEVICE_PATH,=0D
+ MSG_UART_DP,=0D
+ {=0D
+ (UINT8) (sizeof (UART_DEVICE_PATH)),=0D
+ (UINT8) ((sizeof (UART_DEVICE_PATH)) >> 8)=0D
+ }=0D
+ },=0D
+ 0,=0D
+ FixedPcdGet64 (PcdUartDefaultBaudRate),=0D
+ FixedPcdGet8 (PcdUartDefaultDataBits),=0D
+ FixedPcdGet8 (PcdUartDefaultParity),=0D
+ FixedPcdGet8 (PcdUartDefaultStopBits),=0D
+ },=0D
+ gPcAnsiTerminal,=0D
+ gEndEntire=0D
+};=0D
+=0D
+//=0D
+// Predefined platform default console device path=0D
+//=0D
+PLATFORM_CONSOLE_CONNECT_ENTRY gPlatformConsole[] =3D {=0D
+ {=0D
+ (EFI_DEVICE_PATH_PROTOCOL *) &gDebugAgentUartDevicePath,=0D
+ (CONSOLE_OUT | CONSOLE_IN | STD_ERROR)=0D
+ },=0D
+ {=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gUsbKeyboardDevicePath,=0D
+ CONSOLE_IN=0D
+ },=0D
+ {=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gQemuRamfbDevicePath,=0D
+ CONSOLE_OUT=0D
+ },=0D
+ {=0D
+ NULL,=0D
+ 0=0D
+ }=0D
+};=0D
+=0D
+PLATFORM_CONSOLE_CONNECT_ENTRY gXenPlatformConsole[] =3D {=0D
+ {=0D
+ (EFI_DEVICE_PATH_PROTOCOL *)&gXenConsoleDevicePath,=0D
+ (CONSOLE_OUT | CONSOLE_IN | STD_ERROR)=0D
+ },=0D
+ {=0D
+ NULL,=0D
+ 0=0D
+ }=0D
+};=0D
+=0D
+//=0D
+// Predefined platform connect sequence=0D
+//=0D
+EFI_DEVICE_PATH_PROTOCOL *gPlatformConnectSequence[] =3D { NULL };=0D
+=0D
diff --git a/OvmfPkg/AmdSev/Grub/.gitignore b/OvmfPkg/AmdSev/Grub/.gitignore
new file mode 100644
index 0000000000..7e3b30951f
--- /dev/null
+++ b/OvmfPkg/AmdSev/Grub/.gitignore
@@ -0,0 +1 @@
+grub.efi
diff --git a/OvmfPkg/AmdSev/Grub/grub.cfg b/OvmfPkg/AmdSev/Grub/grub.cfg
new file mode 100644
index 0000000000..5c8fd1e547
--- /dev/null
+++ b/OvmfPkg/AmdSev/Grub/grub.cfg
@@ -0,0 +1,35 @@
+echo "Entering grub config"
+sevsecret
+if [ $? -ne 0 ]; then
+ echo "Failed to locate anything in the SEV secret area, prompting for =
password"
+ cryptomount -a
+else
+ cryptomount -s
+ if [ $? -ne 0 ]; then
+ echo "Failed to mount root securely, retrying with password prompt"
+ cryptomount -a
+ fi
+fi
+set root=3D
+for f in (crypto*); do
+ if [ -e $f/boot/grub/grub.cfg ]; then
+ set root=3D$f
+ set prefix=3D($root)/boot/grub
+ break;
+ fi
+done
+if [ x$root =3D x ]; then
+ echo "Failed to find any grub configuration on the encrypted volume"
+ sleep 5
+ reboot
+fi
+# rest of modules to get boot to work
+set modules=3D"
+ boot
+ loadenv
+ "
+for f in $modules; do
+ insmod $f
+done
+echo "Transferring to ${prefix}/grub.cfg"
+source $prefix/grub.cfg
diff --git a/OvmfPkg/AmdSev/Grub/grub.sh b/OvmfPkg/AmdSev/Grub/grub.sh
new file mode 100644
index 0000000000..91fac11ac9
--- /dev/null
+++ b/OvmfPkg/AmdSev/Grub/grub.sh
@@ -0,0 +1,54 @@
+GRUB_MODULES=3D"
+ part_msdos
+ part_gpt
+ cryptodisk
+ luks
+ gcry_rijndael
+ gcry_sha256
+ ext2
+ btrfs
+ xfs
+ fat
+ configfile
+ memdisk
+ sleep
+ normal
+ echo
+ test
+ regexp
+ linux
+ linuxefi
+ reboot
+ sevsecret
+ "
+basedir=3D`dirname $0`
+##
+# different distributions have different names for grub-mkimage, so
+# search all the known ones
+##
+for b in grub2-mkimage grub-mkimage; do
+ if which $b > /dev/null 2>&1; then
+ mkimage=3D$b
+ fi
+done
+if [ -z "$mkimage" ]; then
+ echo "Can't find grub mkimage"
+ exit 1
+fi
+
+# GRUB's rescue parser doesn't understand 'if'.
+echo 'normal (memdisk)/grub.cfg' >"${basedir}/grub-bootstrap.cfg"
+
+# Now build a memdisk with the correct grub.cfg
+rm -f ${basedir}/disk.fat
+mkfs.msdos -C ${basedir}/disk.fat 64 || exit 1
+mcopy -i ${basedir}/disk.fat ${basedir}/grub.cfg ::grub.cfg || exit 1
+
+
+${mkimage} -O x86_64-efi -p '(crypto0)' -c ${basedir}/grub-bootstrap.cfg -=
m ${basedir}/disk.fat -o ${basedir}/grub.efi ${GRUB_MODULES} || exit 1
+
+# remove the intermediates
+for f in disk.fat grub-bootstrap.cfg; do
+ rm -f ${basedir}/$f
+done
+echo "grub.efi generated in ${basedir}"
--=20
2.26.2

Join devel@edk2.groups.io to automatically receive all group messages.