]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Library / PlatformBootManagerLib / BdsPlatform.c
index ea0ae1ecd70ff02ed09bf65b6b599f331b30b581..f73c59dfff501b54f94b9bb2cfdf93272ac36ba3 100644 (file)
@@ -1,46 +1,40 @@
 /** @file\r
   Platform BDS customizations.\r
 \r
-  Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "BdsPlatform.h"\r
 #include <Guid/RootBridgesConnectedEventGroup.h>\r
-#include <Library/QemuBootOrderLib.h>\r
-\r
+#include <Guid/SerialPortLibVendor.h>\r
+#include <Protocol/FirmwareVolume2.h>\r
+#include <Library/PlatformBmPrintScLib.h>\r
+#include <Library/Tcg2PhysicalPresenceLib.h>\r
+#include <Library/XenPlatformLib.h>\r
 \r
 //\r
 // Global data\r
 //\r
 \r
-VOID          *mEfiDevPathNotifyReg;\r
-EFI_EVENT     mEfiDevPathEvent;\r
-VOID          *mEmuVariableEventReg;\r
-EFI_EVENT     mEmuVariableEvent;\r
-BOOLEAN       mDetectVgaOnly;\r
-UINT16        mHostBridgeDevId;\r
+VOID       *mEfiDevPathNotifyReg;\r
+EFI_EVENT  mEfiDevPathEvent;\r
+VOID       *mEmuVariableEventReg;\r
+EFI_EVENT  mEmuVariableEvent;\r
+UINT16     mHostBridgeDevId;\r
 \r
 //\r
 // Table of host IRQs matching PCI IRQs A-D\r
 // (for configuring PCI Interrupt Line register)\r
 //\r
-CONST UINT8 PciHostIrqs[] = {\r
-  0x0a, 0x0a, 0x0b, 0x0b\r
+CONST UINT8  PciHostIrqs[] = {\r
+  0x0a, // LNKA, LNKE\r
+  0x0a, // LNKB, LNKF\r
+  0x0b, // LNKC, LNKG\r
+  0x0b  // LNKD, LNKH\r
 };\r
 \r
-//\r
-// Array Size macro\r
-//\r
-#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))\r
-\r
 //\r
 // Type definitions\r
 //\r
@@ -66,7 +60,6 @@ EFI_STATUS
   IN PCI_TYPE00           *Pci\r
   );\r
 \r
-\r
 //\r
 // Function prototypes\r
 //\r
@@ -80,7 +73,7 @@ VisitAllInstancesOfProtocol (
 \r
 EFI_STATUS\r
 VisitAllPciInstancesOfProtocol (\r
-  IN VISIT_PCI_INSTANCE_CALLBACK CallBackFunction\r
+  IN VISIT_PCI_INSTANCE_CALLBACK  CallBackFunction\r
   );\r
 \r
 VOID\r
@@ -88,6 +81,261 @@ InstallDevicePathCallback (
   VOID\r
   );\r
 \r
+VOID\r
+PlatformRegisterFvBootOption (\r
+  EFI_GUID  *FileGuid,\r
+  CHAR16    *Description,\r
+  UINT32    Attributes\r
+  )\r
+{\r
+  EFI_STATUS                         Status;\r
+  INTN                               OptionIndex;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION       NewOption;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION       *BootOptions;\r
+  UINTN                              BootOptionCount;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH  FileNode;\r
+  EFI_LOADED_IMAGE_PROTOCOL          *LoadedImage;\r
+  EFI_DEVICE_PATH_PROTOCOL           *DevicePath;\r
+\r
+  Status = gBS->HandleProtocol (\r
+                  gImageHandle,\r
+                  &gEfiLoadedImageProtocolGuid,\r
+                  (VOID **)&LoadedImage\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  EfiInitializeFwVolDevicepathNode (&FileNode, FileGuid);\r
+  DevicePath = DevicePathFromHandle (LoadedImage->DeviceHandle);\r
+  ASSERT (DevicePath != NULL);\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&FileNode\r
+                 );\r
+  ASSERT (DevicePath != NULL);\r
+\r
+  Status = EfiBootManagerInitializeLoadOption (\r
+             &NewOption,\r
+             LoadOptionNumberUnassigned,\r
+             LoadOptionTypeBoot,\r
+             Attributes,\r
+             Description,\r
+             DevicePath,\r
+             NULL,\r
+             0\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+  FreePool (DevicePath);\r
+\r
+  BootOptions = EfiBootManagerGetLoadOptions (\r
+                  &BootOptionCount,\r
+                  LoadOptionTypeBoot\r
+                  );\r
+\r
+  OptionIndex = EfiBootManagerFindLoadOption (\r
+                  &NewOption,\r
+                  BootOptions,\r
+                  BootOptionCount\r
+                  );\r
+\r
+  if (OptionIndex == -1) {\r
+    Status = EfiBootManagerAddLoadOptionVariable (&NewOption, MAX_UINTN);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  EfiBootManagerFreeLoadOption (&NewOption);\r
+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+}\r
+\r
+/**\r
+  Remove all MemoryMapped(...)/FvFile(...) and Fv(...)/FvFile(...) boot options\r
+  whose device paths do not resolve exactly to an FvFile in the system.\r
+\r
+  This removes any boot options that point to binaries built into the firmware\r
+  and have become stale due to any of the following:\r
+  - DXEFV's base address or size changed (historical),\r
+  - DXEFV's FvNameGuid changed,\r
+  - the FILE_GUID of the pointed-to binary changed,\r
+  - the referenced binary is no longer built into the firmware.\r
+\r
+  EfiBootManagerFindLoadOption() used in PlatformRegisterFvBootOption() only\r
+  avoids exact duplicates.\r
+**/\r
+VOID\r
+RemoveStaleFvFileOptions (\r
+  VOID\r
+  )\r
+{\r
+  EFI_BOOT_MANAGER_LOAD_OPTION  *BootOptions;\r
+  UINTN                         BootOptionCount;\r
+  UINTN                         Index;\r
+\r
+  BootOptions = EfiBootManagerGetLoadOptions (\r
+                  &BootOptionCount,\r
+                  LoadOptionTypeBoot\r
+                  );\r
+\r
+  for (Index = 0; Index < BootOptionCount; ++Index) {\r
+    EFI_DEVICE_PATH_PROTOCOL  *Node1, *Node2, *SearchNode;\r
+    EFI_STATUS                Status;\r
+    EFI_HANDLE                FvHandle;\r
+\r
+    //\r
+    // If the device path starts with neither MemoryMapped(...) nor Fv(...),\r
+    // then keep the boot option.\r
+    //\r
+    Node1 = BootOptions[Index].FilePath;\r
+    if (!((DevicePathType (Node1) == HARDWARE_DEVICE_PATH) &&\r
+          (DevicePathSubType (Node1) == HW_MEMMAP_DP)) &&\r
+        !((DevicePathType (Node1) == MEDIA_DEVICE_PATH) &&\r
+          (DevicePathSubType (Node1) == MEDIA_PIWG_FW_VOL_DP)))\r
+    {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // If the second device path node is not FvFile(...), then keep the boot\r
+    // option.\r
+    //\r
+    Node2 = NextDevicePathNode (Node1);\r
+    if ((DevicePathType (Node2) != MEDIA_DEVICE_PATH) ||\r
+        (DevicePathSubType (Node2) != MEDIA_PIWG_FW_FILE_DP))\r
+    {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Locate the Firmware Volume2 protocol instance that is denoted by the\r
+    // boot option. If this lookup fails (i.e., the boot option references a\r
+    // firmware volume that doesn't exist), then we'll proceed to delete the\r
+    // boot option.\r
+    //\r
+    SearchNode = Node1;\r
+    Status     = gBS->LocateDevicePath (\r
+                        &gEfiFirmwareVolume2ProtocolGuid,\r
+                        &SearchNode,\r
+                        &FvHandle\r
+                        );\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // The firmware volume was found; now let's see if it contains the FvFile\r
+      // identified by GUID.\r
+      //\r
+      EFI_FIRMWARE_VOLUME2_PROTOCOL      *FvProtocol;\r
+      MEDIA_FW_VOL_FILEPATH_DEVICE_PATH  *FvFileNode;\r
+      UINTN                              BufferSize;\r
+      EFI_FV_FILETYPE                    FoundType;\r
+      EFI_FV_FILE_ATTRIBUTES             FileAttributes;\r
+      UINT32                             AuthenticationStatus;\r
+\r
+      Status = gBS->HandleProtocol (\r
+                      FvHandle,\r
+                      &gEfiFirmwareVolume2ProtocolGuid,\r
+                      (VOID **)&FvProtocol\r
+                      );\r
+      ASSERT_EFI_ERROR (Status);\r
+\r
+      FvFileNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)Node2;\r
+      //\r
+      // Buffer==NULL means we request metadata only: BufferSize, FoundType,\r
+      // FileAttributes.\r
+      //\r
+      Status = FvProtocol->ReadFile (\r
+                             FvProtocol,\r
+                             &FvFileNode->FvFileName, // NameGuid\r
+                             NULL,                    // Buffer\r
+                             &BufferSize,\r
+                             &FoundType,\r
+                             &FileAttributes,\r
+                             &AuthenticationStatus\r
+                             );\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // The FvFile was found. Keep the boot option.\r
+        //\r
+        continue;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Delete the boot option.\r
+    //\r
+    Status = EfiBootManagerDeleteLoadOptionVariable (\r
+               BootOptions[Index].OptionNumber,\r
+               LoadOptionTypeBoot\r
+               );\r
+    DEBUG_CODE_BEGIN ();\r
+    CHAR16  *DevicePathString;\r
+\r
+    DevicePathString = ConvertDevicePathToText (\r
+                         BootOptions[Index].FilePath,\r
+                         FALSE,\r
+                         FALSE\r
+                         );\r
+    DEBUG ((\r
+      EFI_ERROR (Status) ? DEBUG_WARN : DEBUG_VERBOSE,\r
+      "%a: removing stale Boot#%04x %s: %r\n",\r
+      __FUNCTION__,\r
+      (UINT32)BootOptions[Index].OptionNumber,\r
+      DevicePathString == NULL ? L"<unavailable>" : DevicePathString,\r
+      Status\r
+      ));\r
+    if (DevicePathString != NULL) {\r
+      FreePool (DevicePathString);\r
+    }\r
+\r
+    DEBUG_CODE_END ();\r
+  }\r
+\r
+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+}\r
+\r
+VOID\r
+PlatformRegisterOptionsAndKeys (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  EFI_INPUT_KEY                 Enter;\r
+  EFI_INPUT_KEY                 F2;\r
+  EFI_INPUT_KEY                 Esc;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION  BootOption;\r
+\r
+  //\r
+  // Register ENTER as CONTINUE key\r
+  //\r
+  Enter.ScanCode    = SCAN_NULL;\r
+  Enter.UnicodeChar = CHAR_CARRIAGE_RETURN;\r
+  Status            = EfiBootManagerRegisterContinueKeyOption (0, &Enter, NULL);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Map F2 to Boot Manager Menu\r
+  //\r
+  F2.ScanCode     = SCAN_F2;\r
+  F2.UnicodeChar  = CHAR_NULL;\r
+  Esc.ScanCode    = SCAN_ESC;\r
+  Esc.UnicodeChar = CHAR_NULL;\r
+  Status          = EfiBootManagerGetBootManagerMenu (&BootOption);\r
+  ASSERT_EFI_ERROR (Status);\r
+  Status = EfiBootManagerAddKeyOptionVariable (\r
+             NULL,\r
+             (UINT16)BootOption.OptionNumber,\r
+             0,\r
+             &F2,\r
+             NULL\r
+             );\r
+  ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);\r
+  Status = EfiBootManagerAddKeyOptionVariable (\r
+             NULL,\r
+             (UINT16)BootOption.OptionNumber,\r
+             0,\r
+             &Esc,\r
+             NULL\r
+             );\r
+  ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);\r
+}\r
+\r
 EFI_STATUS\r
 EFIAPI\r
 ConnectRootBridge (\r
@@ -96,6 +344,15 @@ ConnectRootBridge (
   IN VOID        *Context\r
   );\r
 \r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ConnectVirtioPciRng (\r
+  IN EFI_HANDLE  Handle,\r
+  IN VOID        *Instance,\r
+  IN VOID        *Context\r
+  );\r
+\r
 STATIC\r
 VOID\r
 SaveS3BootScript (\r
@@ -105,34 +362,39 @@ SaveS3BootScript (
 //\r
 // BDS Platform Functions\r
 //\r
+\r
+/**\r
+  Do the platform init, can be customized by OEM/IBV\r
+\r
+  Possible things that can be done in PlatformBootManagerBeforeConsole:\r
+\r
+  > Update console variable: 1. include hot-plug devices;\r
+  >                          2. Clear ConIn and add SOL for AMT\r
+  > Register new Driver#### or Boot####\r
+  > Register new Key####: e.g.: F12\r
+  > Signal ReadyToLock event\r
+  > Authentication action: 1. connect Auth devices;\r
+  >                        2. Identify auto logon user.\r
+**/\r
 VOID\r
 EFIAPI\r
 PlatformBootManagerBeforeConsole (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Platform Bds init. Incude the platform firmware vendor, revision\r
-  and so crc check.\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
 {\r
-  EFI_HANDLE Handle;\r
-  EFI_STATUS Status;\r
+  EFI_HANDLE     Handle;\r
+  EFI_STATUS     Status;\r
+  UINT16         FrontPageTimeout;\r
+  RETURN_STATUS  PcdStatus;\r
 \r
-  DEBUG ((EFI_D_INFO, "PlatformBootManagerBeforeConsole\n"));\r
+  DEBUG ((DEBUG_INFO, "PlatformBootManagerBeforeConsole\n"));\r
   InstallDevicePathCallback ();\r
 \r
-  VisitAllInstancesOfProtocol (&gEfiPciRootBridgeIoProtocolGuid,\r
-    ConnectRootBridge, NULL);\r
+  VisitAllInstancesOfProtocol (\r
+    &gEfiPciRootBridgeIoProtocolGuid,\r
+    ConnectRootBridge,\r
+    NULL\r
+    );\r
 \r
   //\r
   // Signal the ACPI platform driver that it can download QEMU ACPI tables.\r
@@ -148,7 +410,7 @@ Returns:
   //\r
   EfiEventGroupSignal (&gEfiEndOfDxeEventGroupGuid);\r
 \r
-  if (QemuFwCfgS3Enabled ()) {\r
+  if (PcdGetBool (PcdAcpiS3Enable)) {\r
     //\r
     // Save the boot script too. Note that this will require us to emit the\r
     // DxeSmmReadyToLock event just below, which in turn locks down SMM.\r
@@ -156,16 +418,72 @@ Returns:
     SaveS3BootScript ();\r
   }\r
 \r
+  // We need to connect all trusted consoles for TCG PP. Here we treat all\r
+  // consoles in OVMF to be trusted consoles.\r
+  PlatformInitializeConsole (\r
+    XenDetected () ? gXenPlatformConsole : gPlatformConsole\r
+    );\r
+\r
+  //\r
+  // Process TPM PPI request; this may require keyboard input\r
+  //\r
+  Tcg2PhysicalPresenceLibProcessRequest (NULL);\r
+\r
   //\r
   // Prevent further changes to LockBoxes or SMRAM.\r
+  // Any TPM 2 Physical Presence Interface opcode must be handled before.\r
   //\r
   Handle = NULL;\r
-  Status = gBS->InstallProtocolInterface (&Handle,\r
-                  &gEfiDxeSmmReadyToLockProtocolGuid, EFI_NATIVE_INTERFACE,\r
-                  NULL);\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &Handle,\r
+                  &gEfiDxeSmmReadyToLockProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  NULL\r
+                  );\r
   ASSERT_EFI_ERROR (Status);\r
-}\r
 \r
+  //\r
+  // Dispatch deferred images after EndOfDxe event and ReadyToLock\r
+  // installation.\r
+  //\r
+  EfiBootManagerDispatchDeferredImages ();\r
+\r
+  FrontPageTimeout = GetFrontPageTimeoutFromQemu ();\r
+  PcdStatus        = PcdSet16S (PcdPlatformBootTimeOut, FrontPageTimeout);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+  //\r
+  // Reflect the PCD in the standard Timeout variable.\r
+  //\r
+  Status = gRT->SetVariable (\r
+                  EFI_TIME_OUT_VARIABLE_NAME,\r
+                  &gEfiGlobalVariableGuid,\r
+                  (EFI_VARIABLE_NON_VOLATILE |\r
+                   EFI_VARIABLE_BOOTSERVICE_ACCESS |\r
+                   EFI_VARIABLE_RUNTIME_ACCESS),\r
+                  sizeof FrontPageTimeout,\r
+                  &FrontPageTimeout\r
+                  );\r
+  DEBUG ((\r
+    EFI_ERROR (Status) ? DEBUG_ERROR : DEBUG_VERBOSE,\r
+    "%a: SetVariable(%s, %u): %r\n",\r
+    __FUNCTION__,\r
+    EFI_TIME_OUT_VARIABLE_NAME,\r
+    FrontPageTimeout,\r
+    Status\r
+    ));\r
+\r
+  PlatformRegisterOptionsAndKeys ();\r
+\r
+  //\r
+  // Install both VIRTIO_DEVICE_PROTOCOL and (dependent) EFI_RNG_PROTOCOL\r
+  // instances on Virtio PCI RNG devices.\r
+  //\r
+  VisitAllInstancesOfProtocol (\r
+    &gEfiPciIoProtocolGuid,\r
+    ConnectVirtioPciRng,\r
+    NULL\r
+    );\r
+}\r
 \r
 EFI_STATUS\r
 EFIAPI\r
@@ -175,7 +493,7 @@ ConnectRootBridge (
   IN VOID        *Context\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // Make the PCI bus driver connect the root bridge, non-recursively. This\r
@@ -191,29 +509,135 @@ ConnectRootBridge (
   return Status;\r
 }\r
 \r
-\r
+STATIC\r
 EFI_STATUS\r
-PrepareLpcBridgeDevicePath (\r
-  IN EFI_HANDLE                DeviceHandle\r
+EFIAPI\r
+ConnectVirtioPciRng (\r
+  IN EFI_HANDLE  Handle,\r
+  IN VOID        *Instance,\r
+  IN VOID        *Context\r
   )\r
-/*++\r
+{\r
+  EFI_PCI_IO_PROTOCOL  *PciIo;\r
+  EFI_STATUS           Status;\r
+  UINT16               VendorId;\r
+  UINT16               DeviceId;\r
+  UINT8                RevisionId;\r
+  BOOLEAN              Virtio10;\r
+  UINT16               SubsystemId;\r
 \r
-Routine Description:\r
+  PciIo = Instance;\r
 \r
-  Add IsaKeyboard to ConIn,\r
-  add IsaSerial to ConOut, ConIn, ErrOut.\r
-  LPC Bridge: 06 01 00\r
+  //\r
+  // Read and check VendorId.\r
+  //\r
+  Status = PciIo->Pci.Read (\r
+                        PciIo,\r
+                        EfiPciIoWidthUint16,\r
+                        PCI_VENDOR_ID_OFFSET,\r
+                        1,\r
+                        &VendorId\r
+                        );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
 \r
-Arguments:\r
+  if (VendorId != VIRTIO_VENDOR_ID) {\r
+    return EFI_SUCCESS;\r
+  }\r
 \r
-  DeviceHandle            - Handle of PCIIO protocol.\r
+  //\r
+  // Read DeviceId and RevisionId.\r
+  //\r
+  Status = PciIo->Pci.Read (\r
+                        PciIo,\r
+                        EfiPciIoWidthUint16,\r
+                        PCI_DEVICE_ID_OFFSET,\r
+                        1,\r
+                        &DeviceId\r
+                        );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
 \r
-Returns:\r
+  Status = PciIo->Pci.Read (\r
+                        PciIo,\r
+                        EfiPciIoWidthUint8,\r
+                        PCI_REVISION_ID_OFFSET,\r
+                        1,\r
+                        &RevisionId\r
+                        );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
 \r
-  EFI_SUCCESS             - LPC bridge is added to ConOut, ConIn, and ErrOut.\r
-  EFI_STATUS              - No LPC bridge is added.\r
+  //\r
+  // From DeviceId and RevisionId, determine whether the device is a\r
+  // modern-only Virtio 1.0 device. In case of Virtio 1.0, DeviceId can\r
+  // immediately be restricted to VIRTIO_SUBSYSTEM_ENTROPY_SOURCE, and\r
+  // SubsystemId will only play a sanity-check role. Otherwise, DeviceId can\r
+  // only be sanity-checked, and SubsystemId will decide.\r
+  //\r
+  if ((DeviceId == 0x1040 + VIRTIO_SUBSYSTEM_ENTROPY_SOURCE) &&\r
+      (RevisionId >= 0x01))\r
+  {\r
+    Virtio10 = TRUE;\r
+  } else if ((DeviceId >= 0x1000) && (DeviceId <= 0x103F) && (RevisionId == 0x00)) {\r
+    Virtio10 = FALSE;\r
+  } else {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Read and check SubsystemId as dictated by Virtio10.\r
+  //\r
+  Status = PciIo->Pci.Read (\r
+                        PciIo,\r
+                        EfiPciIoWidthUint16,\r
+                        PCI_SUBSYSTEM_ID_OFFSET,\r
+                        1,\r
+                        &SubsystemId\r
+                        );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
+\r
+  if ((Virtio10 && (SubsystemId >= 0x40)) ||\r
+      (!Virtio10 && (SubsystemId == VIRTIO_SUBSYSTEM_ENTROPY_SOURCE)))\r
+  {\r
+    Status = gBS->ConnectController (\r
+                    Handle, // ControllerHandle\r
+                    NULL,   // DriverImageHandle -- connect all drivers\r
+                    NULL,   // RemainingDevicePath -- produce all child handles\r
+                    FALSE   // Recursive -- don't follow child handles\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Error;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
 \r
---*/\r
+Error:\r
+  DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Add IsaKeyboard to ConIn; add IsaSerial to ConOut, ConIn, ErrOut.\r
+\r
+  @param[in] DeviceHandle  Handle of the LPC Bridge device.\r
+\r
+  @retval EFI_SUCCESS  Console devices on the LPC bridge have been added to\r
+                       ConOut, ConIn, and ErrOut.\r
+\r
+  @return              Error codes, due to EFI_DEVICE_PATH_PROTOCOL missing\r
+                       from DeviceHandle.\r
+**/\r
+EFI_STATUS\r
+PrepareLpcBridgeDevicePath (\r
+  IN EFI_HANDLE  DeviceHandle\r
+  )\r
 {\r
   EFI_STATUS                Status;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
@@ -221,46 +645,59 @@ Returns:
   CHAR16                    *DevPathStr;\r
 \r
   DevicePath = NULL;\r
-  Status = gBS->HandleProtocol (\r
-                  DeviceHandle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  (VOID*)&DevicePath\r
-                  );\r
+  Status     = gBS->HandleProtocol (\r
+                      DeviceHandle,\r
+                      &gEfiDevicePathProtocolGuid,\r
+                      (VOID *)&DevicePath\r
+                      );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   TempDevicePath = DevicePath;\r
 \r
   //\r
   // Register Keyboard\r
   //\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode);\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode\r
+                 );\r
 \r
   EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);\r
 \r
   //\r
   // Register COM1\r
   //\r
-  DevicePath = TempDevicePath;\r
+  DevicePath                     = TempDevicePath;\r
   gPnp16550ComPortDeviceNode.UID = 0;\r
 \r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode\r
+                 );\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode\r
+                 );\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode\r
+                 );\r
 \r
   //\r
   // Print Device Path\r
   //\r
-  DevPathStr = DevicePathToStr(DevicePath);\r
+  DevPathStr = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
   if (DevPathStr != NULL) {\r
-    DEBUG((\r
-      EFI_D_INFO,\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
       "BdsPlatform.c+%d: COM%d DevPath: %s\n",\r
-      __LINE__,\r
+      DEBUG_LINE_NUMBER,\r
       gPnp16550ComPortDeviceNode.UID + 1,\r
       DevPathStr\r
       ));\r
-    FreePool(DevPathStr);\r
+    FreePool (DevPathStr);\r
   }\r
 \r
   EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);\r
@@ -270,26 +707,35 @@ Returns:
   //\r
   // Register COM2\r
   //\r
-  DevicePath = TempDevicePath;\r
+  DevicePath                     = TempDevicePath;\r
   gPnp16550ComPortDeviceNode.UID = 1;\r
 \r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode\r
+                 );\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode\r
+                 );\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode\r
+                 );\r
 \r
   //\r
   // Print Device Path\r
   //\r
-  DevPathStr = DevicePathToStr(DevicePath);\r
+  DevPathStr = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
   if (DevPathStr != NULL) {\r
-    DEBUG((\r
-      EFI_D_INFO,\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
       "BdsPlatform.c+%d: COM%d DevPath: %s\n",\r
-      __LINE__,\r
+      DEBUG_LINE_NUMBER,\r
       gPnp16550ComPortDeviceNode.UID + 1,\r
       DevPathStr\r
       ));\r
-    FreePool(DevPathStr);\r
+    FreePool (DevPathStr);\r
   }\r
 \r
   EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);\r
@@ -299,21 +745,64 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+typedef struct {\r
+  VENDOR_DEVICE_PATH          Guid;\r
+  EFI_DEVICE_PATH_PROTOCOL    End;\r
+} SERIAL_DEVICE_PATH;\r
+\r
+SERIAL_DEVICE_PATH  serialDevicePath = {\r
+  {\r
+    { HARDWARE_DEVICE_PATH, HW_VENDOR_DP,                   { sizeof (VENDOR_DEVICE_PATH),       0 }\r
+    },\r
+    EDKII_SERIAL_PORT_LIB_VENDOR_GUID\r
+  },\r
+  { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }\r
+  }\r
+};\r
+\r
+VOID\r
+PrepareMicrovmDevicePath (\r
+  VOID\r
+  )\r
+{\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  UINT16                    HostBridgeDevId;\r
+\r
+  HostBridgeDevId = PcdGet16 (PcdOvmfHostBridgePciDevId);\r
+  if (HostBridgeDevId != MICROVM_PSEUDO_DEVICE_ID) {\r
+    return;\r
+  }\r
+\r
+  DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)&serialDevicePath;\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode\r
+                 );\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode\r
+                 );\r
+\r
+  EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);\r
+  EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);\r
+  EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);\r
+}\r
+\r
 EFI_STATUS\r
 GetGopDevicePath (\r
-   IN  EFI_DEVICE_PATH_PROTOCOL *PciDevicePath,\r
-   OUT EFI_DEVICE_PATH_PROTOCOL **GopDevicePath\r
-   )\r
+  IN  EFI_DEVICE_PATH_PROTOCOL  *PciDevicePath,\r
+  OUT EFI_DEVICE_PATH_PROTOCOL  **GopDevicePath\r
+  )\r
 {\r
-  UINTN                           Index;\r
-  EFI_STATUS                      Status;\r
-  EFI_HANDLE                      PciDeviceHandle;\r
-  EFI_DEVICE_PATH_PROTOCOL        *TempDevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL        *TempPciDevicePath;\r
-  UINTN                           GopHandleCount;\r
-  EFI_HANDLE                      *GopHandleBuffer;\r
-\r
-  if (PciDevicePath == NULL || GopDevicePath == NULL) {\r
+  UINTN                     Index;\r
+  EFI_STATUS                Status;\r
+  EFI_HANDLE                PciDeviceHandle;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *TempPciDevicePath;\r
+  UINTN                     GopHandleCount;\r
+  EFI_HANDLE                *GopHandleBuffer;\r
+\r
+  if ((PciDevicePath == NULL) || (GopDevicePath == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -333,7 +822,7 @@ GetGopDevicePath (
   }\r
 \r
   //\r
-  // Try to connect this handle, so that GOP dirver could start on this\r
+  // Try to connect this handle, so that GOP driver could start on this\r
   // device and create child handles with GraphicsOutput Protocol installed\r
   // on them, then we get device paths of these child handles and select\r
   // them as possible console device.\r
@@ -352,59 +841,59 @@ GetGopDevicePath (
     // Add all the child handles as possible Console Device\r
     //\r
     for (Index = 0; Index < GopHandleCount; Index++) {\r
-      Status = gBS->HandleProtocol (GopHandleBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID*)&TempDevicePath);\r
+      Status = gBS->HandleProtocol (\r
+                      GopHandleBuffer[Index],\r
+                      &gEfiDevicePathProtocolGuid,\r
+                      (VOID *)&TempDevicePath\r
+                      );\r
       if (EFI_ERROR (Status)) {\r
         continue;\r
       }\r
+\r
       if (CompareMem (\r
             PciDevicePath,\r
             TempDevicePath,\r
             GetDevicePathSize (PciDevicePath) - END_DEVICE_PATH_LENGTH\r
-            ) == 0) {\r
+            ) == 0)\r
+      {\r
         //\r
         // In current implementation, we only enable one of the child handles\r
         // as console device, i.e. sotre one of the child handle's device\r
         // path to variable "ConOut"\r
-        // In futhure, we could select all child handles to be console device\r
+        // In future, we could select all child handles to be console device\r
         //\r
 \r
         *GopDevicePath = TempDevicePath;\r
 \r
         //\r
-        // Delete the PCI device's path that added by GetPlugInPciVgaDevicePath()\r
-        // Add the integrity GOP device path.\r
+        // Delete the PCI device's path that added by\r
+        // GetPlugInPciVgaDevicePath(). Add the integrity GOP device path.\r
         //\r
         EfiBootManagerUpdateConsoleVariable (ConOutDev, NULL, PciDevicePath);\r
         EfiBootManagerUpdateConsoleVariable (ConOutDev, TempDevicePath, NULL);\r
       }\r
     }\r
+\r
     gBS->FreePool (GopHandleBuffer);\r
   }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-PreparePciVgaDevicePath (\r
-  IN EFI_HANDLE                DeviceHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Add PCI VGA to ConOut.\r
-  PCI VGA: 03 00 00\r
-\r
-Arguments:\r
-\r
-  DeviceHandle            - Handle of PCIIO protocol.\r
+/**\r
+  Add PCI display to ConOut.\r
 \r
-Returns:\r
+  @param[in] DeviceHandle  Handle of the PCI display device.\r
 \r
-  EFI_SUCCESS             - PCI VGA is added to ConOut.\r
-  EFI_STATUS              - No PCI VGA device is added.\r
+  @retval EFI_SUCCESS  The PCI display device has been added to ConOut.\r
 \r
---*/\r
+  @return              Error codes, due to EFI_DEVICE_PATH_PROTOCOL missing\r
+                       from DeviceHandle.\r
+**/\r
+EFI_STATUS\r
+PreparePciDisplayDevicePath (\r
+  IN EFI_HANDLE  DeviceHandle\r
+  )\r
 {\r
   EFI_STATUS                Status;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
@@ -412,11 +901,11 @@ Returns:
 \r
   DevicePath    = NULL;\r
   GopDevicePath = NULL;\r
-  Status = gBS->HandleProtocol (\r
-                  DeviceHandle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  (VOID*)&DevicePath\r
-                  );\r
+  Status        = gBS->HandleProtocol (\r
+                         DeviceHandle,\r
+                         &gEfiDevicePathProtocolGuid,\r
+                         (VOID *)&DevicePath\r
+                         );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -429,43 +918,43 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-PreparePciSerialDevicePath (\r
-  IN EFI_HANDLE                DeviceHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Add PCI Serial to ConOut, ConIn, ErrOut.\r
-  PCI Serial: 07 00 02\r
-\r
-Arguments:\r
-\r
-  DeviceHandle            - Handle of PCIIO protocol.\r
 \r
-Returns:\r
+  @param[in] DeviceHandle  Handle of the PCI serial device.\r
 \r
-  EFI_SUCCESS             - PCI Serial is added to ConOut, ConIn, and ErrOut.\r
-  EFI_STATUS              - No PCI Serial device is added.\r
+  @retval EFI_SUCCESS  The PCI serial device has been added to ConOut, ConIn,\r
+                       ErrOut.\r
 \r
---*/\r
+  @return              Error codes, due to EFI_DEVICE_PATH_PROTOCOL missing\r
+                       from DeviceHandle.\r
+**/\r
+EFI_STATUS\r
+PreparePciSerialDevicePath (\r
+  IN EFI_HANDLE  DeviceHandle\r
+  )\r
 {\r
   EFI_STATUS                Status;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
 \r
   DevicePath = NULL;\r
-  Status = gBS->HandleProtocol (\r
-                  DeviceHandle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  (VOID*)&DevicePath\r
-                  );\r
+  Status     = gBS->HandleProtocol (\r
+                      DeviceHandle,\r
+                      &gEfiDevicePathProtocolGuid,\r
+                      (VOID *)&DevicePath\r
+                      );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode\r
+                 );\r
+  DevicePath = AppendDevicePathNode (\r
+                 DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode\r
+                 );\r
 \r
   EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);\r
   EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);\r
@@ -481,24 +970,24 @@ VisitAllInstancesOfProtocol (
   IN VOID                        *Context\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
-  UINTN                     HandleCount;\r
-  EFI_HANDLE                *HandleBuffer;\r
-  UINTN                     Index;\r
-  VOID                      *Instance;\r
+  EFI_STATUS  Status;\r
+  UINTN       HandleCount;\r
+  EFI_HANDLE  *HandleBuffer;\r
+  UINTN       Index;\r
+  VOID        *Instance;\r
 \r
   //\r
   // Start to check all the PciIo to find all possible device\r
   //\r
-  HandleCount = 0;\r
+  HandleCount  = 0;\r
   HandleBuffer = NULL;\r
-  Status = gBS->LocateHandleBuffer (\r
-                  ByProtocol,\r
-                  Id,\r
-                  NULL,\r
-                  &HandleCount,\r
-                  &HandleBuffer\r
-                  );\r
+  Status       = gBS->LocateHandleBuffer (\r
+                        ByProtocol,\r
+                        Id,\r
+                        NULL,\r
+                        &HandleCount,\r
+                        &HandleBuffer\r
+                        );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -509,11 +998,11 @@ VisitAllInstancesOfProtocol (
       continue;\r
     }\r
 \r
-    Status = (*CallBackFunction) (\r
-               HandleBuffer[Index],\r
-               Instance,\r
-               Context\r
-               );\r
+    Status = (*CallBackFunction)(\r
+  HandleBuffer[Index],\r
+  Instance,\r
+  Context\r
+  );\r
   }\r
 \r
   gBS->FreePool (HandleBuffer);\r
@@ -521,7 +1010,6 @@ VisitAllInstancesOfProtocol (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 EFI_STATUS\r
 EFIAPI\r
 VisitingAPciInstance (\r
@@ -530,49 +1018,45 @@ VisitingAPciInstance (
   IN VOID        *Context\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
-  EFI_PCI_IO_PROTOCOL       *PciIo;\r
-  PCI_TYPE00                Pci;\r
+  EFI_STATUS           Status;\r
+  EFI_PCI_IO_PROTOCOL  *PciIo;\r
+  PCI_TYPE00           Pci;\r
 \r
-  PciIo = (EFI_PCI_IO_PROTOCOL*) Instance;\r
+  PciIo = (EFI_PCI_IO_PROTOCOL *)Instance;\r
 \r
   //\r
   // Check for all PCI device\r
   //\r
   Status = PciIo->Pci.Read (\r
-                    PciIo,\r
-                    EfiPciIoWidthUint32,\r
-                    0,\r
-                    sizeof (Pci) / sizeof (UINT32),\r
-                    &Pci\r
-                    );\r
+                        PciIo,\r
+                        EfiPciIoWidthUint32,\r
+                        0,\r
+                        sizeof (Pci) / sizeof (UINT32),\r
+                        &Pci\r
+                        );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  return (*(VISIT_PCI_INSTANCE_CALLBACK)(UINTN) Context) (\r
-           Handle,\r
-           PciIo,\r
-           &Pci\r
-           );\r
-\r
+  return (*(VISIT_PCI_INSTANCE_CALLBACK)(UINTN)Context)(\r
+  Handle,\r
+  PciIo,\r
+  &Pci\r
+  );\r
 }\r
 \r
-\r
-\r
 EFI_STATUS\r
 VisitAllPciInstances (\r
-  IN VISIT_PCI_INSTANCE_CALLBACK CallBackFunction\r
+  IN VISIT_PCI_INSTANCE_CALLBACK  CallBackFunction\r
   )\r
 {\r
   return VisitAllInstancesOfProtocol (\r
            &gEfiPciIoProtocolGuid,\r
            VisitingAPciInstance,\r
-           (VOID*)(UINTN) CallBackFunction\r
+           (VOID *)(UINTN)CallBackFunction\r
            );\r
 }\r
 \r
-\r
 /**\r
   Do platform specific PCI Device check and add them to\r
   ConOut, ConIn, ErrOut.\r
@@ -581,7 +1065,8 @@ VisitAllPciInstances (
   @param[in]  PciIo - PCI IO protocol instance\r
   @param[in]  Pci - PCI Header register block\r
 \r
-  @retval EFI_SUCCESS - PCI Device check and Console variable update successfully.\r
+  @retval EFI_SUCCESS - PCI Device check and Console variable update\r
+                        successfully.\r
   @retval EFI_STATUS - PCI Device check or Console variable update fail.\r
 \r
 **/\r
@@ -593,172 +1078,119 @@ DetectAndPreparePlatformPciDevicePath (
   IN PCI_TYPE00           *Pci\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
+  EFI_STATUS  Status;\r
 \r
   Status = PciIo->Attributes (\r
-    PciIo,\r
-    EfiPciIoAttributeOperationEnable,\r
-    EFI_PCI_DEVICE_ENABLE,\r
-    NULL\r
-    );\r
+                    PciIo,\r
+                    EfiPciIoAttributeOperationEnable,\r
+                    EFI_PCI_DEVICE_ENABLE,\r
+                    NULL\r
+                    );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  if (!mDetectVgaOnly) {\r
+  //\r
+  // Here we decide whether it is LPC Bridge\r
+  //\r
+  if ((IS_PCI_LPC (Pci)) ||\r
+      ((IS_PCI_ISA_PDECODE (Pci)) &&\r
+       (Pci->Hdr.VendorId == 0x8086) &&\r
+       (Pci->Hdr.DeviceId == 0x7000)\r
+      )\r
+      )\r
+  {\r
     //\r
-    // Here we decide whether it is LPC Bridge\r
+    // Add IsaKeyboard to ConIn,\r
+    // add IsaSerial to ConOut, ConIn, ErrOut\r
     //\r
-    if ((IS_PCI_LPC (Pci)) ||\r
-        ((IS_PCI_ISA_PDECODE (Pci)) &&\r
-         (Pci->Hdr.VendorId == 0x8086) &&\r
-         (Pci->Hdr.DeviceId == 0x7000)\r
-        )\r
-       ) {\r
-      //\r
-      // Add IsaKeyboard to ConIn,\r
-      // add IsaSerial to ConOut, ConIn, ErrOut\r
-      //\r
-      DEBUG ((EFI_D_INFO, "Found LPC Bridge device\n"));\r
-      PrepareLpcBridgeDevicePath (Handle);\r
-      return EFI_SUCCESS;\r
-    }\r
+    DEBUG ((DEBUG_INFO, "Found LPC Bridge device\n"));\r
+    PrepareLpcBridgeDevicePath (Handle);\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Here we decide which Serial device to enable in PCI bus\r
+  //\r
+  if (IS_PCI_16550SERIAL (Pci)) {\r
     //\r
-    // Here we decide which Serial device to enable in PCI bus\r
+    // Add them to ConOut, ConIn, ErrOut.\r
     //\r
-    if (IS_PCI_16550SERIAL (Pci)) {\r
-      //\r
-      // Add them to ConOut, ConIn, ErrOut.\r
-      //\r
-      DEBUG ((EFI_D_INFO, "Found PCI 16550 SERIAL device\n"));\r
-      PreparePciSerialDevicePath (Handle);\r
-      return EFI_SUCCESS;\r
-    }\r
+    DEBUG ((DEBUG_INFO, "Found PCI 16550 SERIAL device\n"));\r
+    PreparePciSerialDevicePath (Handle);\r
+    return EFI_SUCCESS;\r
   }\r
 \r
   //\r
-  // Here we decide which VGA device to enable in PCI bus\r
+  // Here we decide which display device to enable in PCI bus\r
   //\r
-  if (IS_PCI_VGA (Pci)) {\r
+  if (IS_PCI_DISPLAY (Pci)) {\r
     //\r
     // Add them to ConOut.\r
     //\r
-    DEBUG ((EFI_D_INFO, "Found PCI VGA device\n"));\r
-    PreparePciVgaDevicePath (Handle);\r
+    DEBUG ((DEBUG_INFO, "Found PCI display device\n"));\r
+    PreparePciDisplayDevicePath (Handle);\r
     return EFI_SUCCESS;\r
   }\r
 \r
   return Status;\r
 }\r
 \r
-\r
 /**\r
-  Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut\r
-\r
-  @param[in]  DetectVgaOnly - Only detect VGA device if it's TRUE.\r
+  Connect the predefined platform default console device.\r
 \r
-  @retval EFI_SUCCESS - PCI Device check and Console variable update successfully.\r
-  @retval EFI_STATUS - PCI Device check or Console variable update fail.\r
+  Always try to find and enable PCI display devices.\r
 \r
+  @param[in] PlatformConsole  Predefined platform default console device array.\r
 **/\r
-EFI_STATUS\r
-DetectAndPreparePlatformPciDevicePaths (\r
-  BOOLEAN DetectVgaOnly\r
+VOID\r
+PlatformInitializeConsole (\r
+  IN PLATFORM_CONSOLE_CONNECT_ENTRY  *PlatformConsole\r
   )\r
 {\r
-  mDetectVgaOnly = DetectVgaOnly;\r
-  return VisitAllPciInstances (DetectAndPreparePlatformPciDevicePath);\r
-}\r
-\r
-\r
-EFI_STATUS\r
-PlatformBdsConnectConsole (\r
-  IN BDS_CONSOLE_CONNECT_ENTRY   *PlatformConsole\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Connect the predefined platform default console device. Always try to find\r
-  and enable the vga device if have.\r
+  UINTN  Index;\r
 \r
-Arguments:\r
-\r
-  PlatformConsole         - Predfined platform default console device array.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS             - Success connect at least one ConIn and ConOut\r
-                            device, there must have one ConOut device is\r
-                            active vga device.\r
-\r
-  EFI_STATUS              - Return the status of\r
-                            BdsLibConnectAllDefaultConsoles ()\r
+  //\r
+  // Do platform specific PCI Device check and add them to ConOut, ConIn,\r
+  // ErrOut\r
+  //\r
+  VisitAllPciInstances (DetectAndPreparePlatformPciDevicePath);\r
 \r
---*/\r
-{\r
-  EFI_STATUS                         Status;\r
-  UINTN                              Index;\r
-  EFI_DEVICE_PATH_PROTOCOL           *VarConout;\r
-  EFI_DEVICE_PATH_PROTOCOL           *VarConin;\r
-  UINTN                              DevicePathSize;\r
+  PrepareMicrovmDevicePath ();\r
 \r
   //\r
-  // Connect RootBridge\r
+  // Have chance to connect the platform default console,\r
+  // the platform default console is the minimum device group\r
+  // the platform should support\r
   //\r
-  VarConout = BdsLibGetVariableAndSize (\r
-                VarConsoleOut,\r
-                &gEfiGlobalVariableGuid,\r
-                &DevicePathSize\r
-                );\r
-  VarConin = BdsLibGetVariableAndSize (\r
-               VarConsoleInp,\r
-               &gEfiGlobalVariableGuid,\r
-               &DevicePathSize\r
-               );\r
-\r
-  if (VarConout == NULL || VarConin == NULL) {\r
+  for (Index = 0; PlatformConsole[Index].DevicePath != NULL; ++Index) {\r
     //\r
-    // Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut\r
+    // Update the console variable with the connect type\r
     //\r
-    DetectAndPreparePlatformPciDevicePaths (FALSE);\r
+    if ((PlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {\r
+      EfiBootManagerUpdateConsoleVariable (\r
+        ConIn,\r
+        PlatformConsole[Index].DevicePath,\r
+        NULL\r
+        );\r
+    }\r
 \r
-    //\r
-    // Have chance to connect the platform default console,\r
-    // the platform default console is the minimue device group\r
-    // the platform should support\r
-    //\r
-    for (Index = 0; PlatformConsole[Index].DevicePath != NULL; ++Index) {\r
-      //\r
-      // Update the console variable with the connect type\r
-      //\r
-      if ((PlatformConsole[Index].ConnectType & CONSOLE_IN) == CONSOLE_IN) {\r
-        EfiBootManagerUpdateConsoleVariable (ConIn, PlatformConsole[Index].DevicePath, NULL);\r
-      }\r
-      if ((PlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {\r
-        EfiBootManagerUpdateConsoleVariable (ConOut, PlatformConsole[Index].DevicePath, NULL);\r
-      }\r
-      if ((PlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {\r
-        EfiBootManagerUpdateConsoleVariable (ErrOut, PlatformConsole[Index].DevicePath, NULL);\r
-      }\r
+    if ((PlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {\r
+      EfiBootManagerUpdateConsoleVariable (\r
+        ConOut,\r
+        PlatformConsole[Index].DevicePath,\r
+        NULL\r
+        );\r
     }\r
-  } else {\r
-    //\r
-    // Only detect VGA device and add them to ConOut\r
-    //\r
-    DetectAndPreparePlatformPciDevicePaths (TRUE);\r
-  }\r
 \r
-  //\r
-  // Connect the all the default console with current cosole variable\r
-  //\r
-  Status = BdsLibConnectAllDefaultConsoles ();\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    if ((PlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {\r
+      EfiBootManagerUpdateConsoleVariable (\r
+        ErrOut,\r
+        PlatformConsole[Index].DevicePath,\r
+        NULL\r
+        );\r
+    }\r
   }\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Configure PCI Interrupt Line register for applicable devices\r
   Ported from SeaBIOS, src/fw/pciinit.c, *_pci_slot_get_irq()\r
@@ -789,15 +1221,15 @@ SetPciIntLine (
   Status = EFI_SUCCESS;\r
 \r
   if (PciHdr->Device.InterruptPin != 0) {\r
-\r
     DevPathNode = DevicePathFromHandle (Handle);\r
     ASSERT (DevPathNode != NULL);\r
     DevPath = DevPathNode;\r
 \r
     RootBusNumber = 0;\r
-    if (DevicePathType (DevPathNode) == ACPI_DEVICE_PATH &&\r
-        DevicePathSubType (DevPathNode) == ACPI_DP &&\r
-        ((ACPI_HID_DEVICE_PATH *)DevPathNode)->HID == EISA_PNP_ID(0x0A03)) {\r
+    if ((DevicePathType (DevPathNode) == ACPI_DEVICE_PATH) &&\r
+        (DevicePathSubType (DevPathNode) == ACPI_DP) &&\r
+        (((ACPI_HID_DEVICE_PATH *)DevPathNode)->HID == EISA_PNP_ID (0x0A03)))\r
+    {\r
       RootBusNumber = ((ACPI_HID_DEVICE_PATH *)DevPathNode)->UID;\r
     }\r
 \r
@@ -805,13 +1237,13 @@ SetPciIntLine (
     // Compute index into PciHostIrqs[] table by walking\r
     // the device path and adding up all device numbers\r
     //\r
-    Status = EFI_NOT_FOUND;\r
+    Status   = EFI_NOT_FOUND;\r
     RootSlot = 0;\r
-    Idx = PciHdr->Device.InterruptPin - 1;\r
+    Idx      = PciHdr->Device.InterruptPin - 1;\r
     while (!IsDevicePathEnd (DevPathNode)) {\r
-      if (DevicePathType (DevPathNode) == HARDWARE_DEVICE_PATH &&\r
-          DevicePathSubType (DevPathNode) == HW_PCI_DP) {\r
-\r
+      if ((DevicePathType (DevPathNode) == HARDWARE_DEVICE_PATH) &&\r
+          (DevicePathSubType (DevPathNode) == HW_PCI_DP))\r
+      {\r
         Idx += ((PCI_DEVICE_PATH *)DevPathNode)->Device;\r
 \r
         //\r
@@ -822,19 +1254,21 @@ SetPciIntLine (
         // Q35 cases with more than 24 slots on the root bus.\r
         //\r
         if (Status != EFI_SUCCESS) {\r
-          Status = EFI_SUCCESS;\r
+          Status   = EFI_SUCCESS;\r
           RootSlot = ((PCI_DEVICE_PATH *)DevPathNode)->Device;\r
         }\r
       }\r
 \r
       DevPathNode = NextDevicePathNode (DevPathNode);\r
     }\r
+\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    if (RootBusNumber == 0 && RootSlot == 0) {\r
-      DEBUG((\r
-        EFI_D_ERROR,\r
+\r
+    if ((RootBusNumber == 0) && (RootSlot == 0)) {\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
         "%a: PCI host bridge (00:00.0) should have no interrupts!\n",\r
         __FUNCTION__\r
         ));\r
@@ -865,29 +1299,39 @@ SetPciIntLine (
           //\r
           Idx -= RootSlot;\r
         }\r
+\r
         break;\r
       default:\r
         ASSERT (FALSE); // should never get here\r
     }\r
-    Idx %= ARRAY_SIZE (PciHostIrqs);\r
+\r
+    Idx    %= ARRAY_SIZE (PciHostIrqs);\r
     IrqLine = PciHostIrqs[Idx];\r
 \r
     DEBUG_CODE_BEGIN ();\r
     {\r
-      CHAR16        *DevPathString;\r
-      STATIC CHAR16 Fallback[] = L"<failed to convert>";\r
-      UINTN         Segment, Bus, Device, Function;\r
+      CHAR16         *DevPathString;\r
+      STATIC CHAR16  Fallback[] = L"<failed to convert>";\r
+      UINTN          Segment, Bus, Device, Function;\r
 \r
       DevPathString = ConvertDevicePathToText (DevPath, FALSE, FALSE);\r
       if (DevPathString == NULL) {\r
         DevPathString = Fallback;\r
       }\r
+\r
       Status = PciIo->GetLocation (PciIo, &Segment, &Bus, &Device, &Function);\r
       ASSERT_EFI_ERROR (Status);\r
 \r
-      DEBUG ((EFI_D_VERBOSE, "%a: [%02x:%02x.%x] %s -> 0x%02x\n", __FUNCTION__,\r
-        (UINT32)Bus, (UINT32)Device, (UINT32)Function, DevPathString,\r
-        IrqLine));\r
+      DEBUG ((\r
+        DEBUG_VERBOSE,\r
+        "%a: [%02x:%02x.%x] %s -> 0x%02x\n",\r
+        __FUNCTION__,\r
+        (UINT32)Bus,\r
+        (UINT32)Device,\r
+        (UINT32)Function,\r
+        DevPathString,\r
+        IrqLine\r
+        ));\r
 \r
       if (DevPathString != Fallback) {\r
         FreePool (DevPathString);\r
@@ -899,18 +1343,17 @@ SetPciIntLine (
     // Set PCI Interrupt Line register for this device to PciHostIrqs[Idx]\r
     //\r
     Status = PciIo->Pci.Write (\r
-               PciIo,\r
-               EfiPciIoWidthUint8,\r
-               PCI_INT_LINE_OFFSET,\r
-               1,\r
-               &IrqLine\r
-               );\r
+                          PciIo,\r
+                          EfiPciIoWidthUint8,\r
+                          PCI_INT_LINE_OFFSET,\r
+                          1,\r
+                          &IrqLine\r
+                          );\r
   }\r
 \r
   return Status;\r
 }\r
 \r
-\r
 VOID\r
 PciAcpiInitialization (\r
   )\r
@@ -927,28 +1370,41 @@ PciAcpiInitialization (
       //\r
       // 00:01.0 ISA Bridge (PIIX4) LNK routing targets\r
       //\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x60), 0x0b); // A\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x61), 0x0b); // B\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x62), 0x0a); // C\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x63), 0x0a); // D\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x60), PciHostIrqs[0]); // A\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x61), PciHostIrqs[1]); // B\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x62), PciHostIrqs[2]); // C\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 1, 0, 0x63), PciHostIrqs[3]); // D\r
       break;\r
     case INTEL_Q35_MCH_DEVICE_ID:\r
       Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
       //\r
       // 00:1f.0 LPC Bridge (Q35) LNK routing targets\r
       //\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x60), 0x0a); // A\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x61), 0x0a); // B\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x62), 0x0b); // C\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x63), 0x0b); // D\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x68), 0x0a); // E\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x69), 0x0a); // F\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x6a), 0x0b); // G\r
-      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x6b), 0x0b); // H\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x60), PciHostIrqs[0]); // A\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x61), PciHostIrqs[1]); // B\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x62), PciHostIrqs[2]); // C\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x63), PciHostIrqs[3]); // D\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x68), PciHostIrqs[0]); // E\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x69), PciHostIrqs[1]); // F\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x6a), PciHostIrqs[2]); // G\r
+      PciWrite8 (PCI_LIB_ADDRESS (0, 0x1f, 0, 0x6b), PciHostIrqs[3]); // H\r
       break;\r
+    case MICROVM_PSEUDO_DEVICE_ID:\r
+      return;\r
     default:\r
-      DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
-        __FUNCTION__, mHostBridgeDevId));\r
+      if (XenDetected ()) {\r
+        //\r
+        // There is no PCI bus in this case.\r
+        //\r
+        return;\r
+      }\r
+\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
+        __FUNCTION__,\r
+        mHostBridgeDevId\r
+        ));\r
       ASSERT (FALSE);\r
       return;\r
   }\r
@@ -964,7 +1420,6 @@ PciAcpiInitialization (
   IoOr16 ((PciRead32 (Pmba) & ~BIT0) + 4, BIT0);\r
 }\r
 \r
-\r
 EFI_STATUS\r
 EFIAPI\r
 ConnectRecursivelyIfPciMassStorage (\r
@@ -977,13 +1432,18 @@ ConnectRecursivelyIfPciMassStorage (
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
   CHAR16                    *DevPathStr;\r
 \r
-  if (IS_CLASS1 (PciHeader, PCI_CLASS_MASS_STORAGE)) {\r
+  //\r
+  // Recognize PCI Mass Storage, and Xen PCI devices\r
+  //\r
+  if (IS_CLASS1 (PciHeader, PCI_CLASS_MASS_STORAGE) ||\r
+      (XenDetected () && IS_CLASS2 (PciHeader, 0xFF, 0x80)))\r
+  {\r
     DevicePath = NULL;\r
-    Status = gBS->HandleProtocol (\r
-                    Handle,\r
-                    &gEfiDevicePathProtocolGuid,\r
-                    (VOID*)&DevicePath\r
-                    );\r
+    Status     = gBS->HandleProtocol (\r
+                        Handle,\r
+                        &gEfiDevicePathProtocolGuid,\r
+                        (VOID *)&DevicePath\r
+                        );\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -991,47 +1451,48 @@ ConnectRecursivelyIfPciMassStorage (
     //\r
     // Print Device Path\r
     //\r
-    DevPathStr = DevicePathToStr (DevicePath);\r
+    DevPathStr = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
     if (DevPathStr != NULL) {\r
-      DEBUG((\r
-        EFI_D_INFO,\r
-        "Found Mass Storage device: %s\n",\r
+      DEBUG ((\r
+        DEBUG_INFO,\r
+        "Found %s device: %s\n",\r
+        (IS_CLASS1 (PciHeader, PCI_CLASS_MASS_STORAGE) ?\r
+         L"Mass Storage" :\r
+         L"Xen"\r
+        ),\r
         DevPathStr\r
         ));\r
-      FreePool(DevPathStr);\r
+      FreePool (DevPathStr);\r
     }\r
 \r
     Status = gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-\r
   }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   This notification function is invoked when the\r
   EMU Variable FVB has been changed.\r
 \r
-  @param  Event                 The event that occured\r
-  @param  Context               For EFI compatiblity.  Not used.\r
+  @param  Event                 The event that occurred\r
+  @param  Context               For EFI compatibility.  Not used.\r
 \r
 **/\r
 VOID\r
 EFIAPI\r
 EmuVariablesUpdatedCallback (\r
-  IN  EFI_EVENT Event,\r
-  IN  VOID      *Context\r
+  IN  EFI_EVENT  Event,\r
+  IN  VOID       *Context\r
   )\r
 {\r
-  DEBUG ((EFI_D_INFO, "EmuVariablesUpdatedCallback\n"));\r
+  DEBUG ((DEBUG_INFO, "EmuVariablesUpdatedCallback\n"));\r
   UpdateNvVarsOnFileSystem ();\r
 }\r
 \r
-\r
 EFI_STATUS\r
 EFIAPI\r
 VisitingFileSystemInstance (\r
@@ -1042,6 +1503,7 @@ VisitingFileSystemInstance (
 {\r
   EFI_STATUS      Status;\r
   STATIC BOOLEAN  ConnectedToFileSystem = FALSE;\r
+  RETURN_STATUS   PcdStatus;\r
 \r
   if (ConnectedToFileSystem) {\r
     return EFI_ALREADY_STARTED;\r
@@ -1053,7 +1515,7 @@ VisitingFileSystemInstance (
   }\r
 \r
   ConnectedToFileSystem = TRUE;\r
-  mEmuVariableEvent =\r
+  mEmuVariableEvent     =\r
     EfiCreateProtocolNotifyEvent (\r
       &gEfiDevicePathProtocolGuid,\r
       TPL_CALLBACK,\r
@@ -1061,12 +1523,15 @@ VisitingFileSystemInstance (
       NULL,\r
       &mEmuVariableEventReg\r
       );\r
-  PcdSet64 (PcdEmuVariableEvent, (UINT64)(UINTN) mEmuVariableEvent);\r
+  PcdStatus = PcdSet64S (\r
+                PcdEmuVariableEvent,\r
+                (UINT64)(UINTN)mEmuVariableEvent\r
+                );\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 VOID\r
 PlatformBdsRestoreNvVarsFromHardDisk (\r
   )\r
@@ -1077,34 +1542,22 @@ PlatformBdsRestoreNvVarsFromHardDisk (
     VisitingFileSystemInstance,\r
     NULL\r
     );\r
-\r
 }\r
 \r
+/**\r
+  Connect with predefined platform connect sequence.\r
 \r
+  The OEM/IBV can customize with their own connect sequence.\r
+**/\r
 VOID\r
 PlatformBdsConnectSequence (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Connect with predeined platform connect sequence,\r
-  the OEM/IBV can customize with their own connect sequence.\r
-\r
-Arguments:\r
-\r
-  None.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
 {\r
-  UINTN Index;\r
+  UINTN          Index;\r
+  RETURN_STATUS  Status;\r
 \r
-  DEBUG ((EFI_D_INFO, "PlatformBdsConnectSequence\n"));\r
+  DEBUG ((DEBUG_INFO, "PlatformBdsConnectSequence\n"));\r
 \r
   Index = 0;\r
 \r
@@ -1117,104 +1570,20 @@ Returns:
     //\r
     // Build the platform boot option\r
     //\r
-    BdsLibConnectDevicePath (gPlatformConnectSequence[Index]);\r
+    EfiBootManagerConnectDevicePath (gPlatformConnectSequence[Index], NULL);\r
     Index++;\r
   }\r
 \r
-  //\r
-  // Just use the simple policy to connect all devices\r
-  //\r
-  BdsLibConnectAll ();\r
-\r
-  PciAcpiInitialization ();\r
-\r
-  //\r
-  // Clear the logo after all devices are connected.\r
-  //\r
-  gST->ConOut->ClearScreen (gST->ConOut);\r
-}\r
-\r
-VOID\r
-PlatformBdsGetDriverOption (\r
-  IN OUT LIST_ENTRY              *BdsDriverLists\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Load the predefined driver option, OEM/IBV can customize this\r
-  to load their own drivers\r
-\r
-Arguments:\r
-\r
-  BdsDriverLists  - The header of the driver option link list.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
-{\r
-  DEBUG ((EFI_D_INFO, "PlatformBdsGetDriverOption\n"));\r
-  return;\r
-}\r
-\r
-VOID\r
-PlatformBdsDiagnostics (\r
-  IN EXTENDMEM_COVERAGE_LEVEL    MemoryTestLevel,\r
-  IN BOOLEAN                     QuietBoot,\r
-  IN BASEM_MEMORY_TEST           BaseMemoryTest\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Perform the platform diagnostic, such like test memory. OEM/IBV also\r
-  can customize this fuction to support specific platform diagnostic.\r
-\r
-Arguments:\r
-\r
-  MemoryTestLevel  - The memory test intensive level\r
-\r
-  QuietBoot        - Indicate if need to enable the quiet boot\r
-\r
-  BaseMemoryTest   - A pointer to BaseMemoryTest()\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  DEBUG ((EFI_D_INFO, "PlatformBdsDiagnostics\n"));\r
-\r
-  //\r
-  // Here we can decide if we need to show\r
-  // the diagnostics screen\r
-  // Notes: this quiet boot code should be remove\r
-  // from the graphic lib\r
-  //\r
-  if (QuietBoot) {\r
-    EnableQuietBoot (PcdGetPtr(PcdLogoFile));\r
+  Status = ConnectDevicesFromQemu ();\r
+  if (RETURN_ERROR (Status)) {\r
     //\r
-    // Perform system diagnostic\r
+    // Just use the simple policy to connect all devices\r
     //\r
-    Status = BaseMemoryTest (MemoryTestLevel);\r
-    if (EFI_ERROR (Status)) {\r
-      DisableQuietBoot ();\r
-    }\r
-\r
-    return ;\r
+    DEBUG ((DEBUG_INFO, "EfiBootManagerConnectAll\n"));\r
+    EfiBootManagerConnectAll ();\r
   }\r
-  //\r
-  // Perform system diagnostic\r
-  //\r
-  Status = BaseMemoryTest (MemoryTestLevel);\r
 }\r
 \r
-\r
 /**\r
   Save the S3 boot script.\r
 \r
@@ -1227,12 +1596,15 @@ SaveS3BootScript (
   VOID\r
   )\r
 {\r
-  EFI_STATUS                 Status;\r
-  EFI_S3_SAVE_STATE_PROTOCOL *BootScript;\r
-  STATIC CONST UINT8         Info[] = { 0xDE, 0xAD, 0xBE, 0xEF };\r
+  EFI_STATUS                  Status;\r
+  EFI_S3_SAVE_STATE_PROTOCOL  *BootScript;\r
+  STATIC CONST UINT8          Info[] = { 0xDE, 0xAD, 0xBE, 0xEF };\r
 \r
-  Status = gBS->LocateProtocol (&gEfiS3SaveStateProtocolGuid, NULL,\r
-                  (VOID **) &BootScript);\r
+  Status = gBS->LocateProtocol (\r
+                  &gEfiS3SaveStateProtocolGuid,\r
+                  NULL,\r
+                  (VOID **)&BootScript\r
+                  );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
@@ -1240,36 +1612,44 @@ SaveS3BootScript (
   // implementation embeds a deep copy of the info in the boot script, rather\r
   // than storing just a pointer to runtime or NVS storage.\r
   //\r
-  Status = BootScript->Write(BootScript, EFI_BOOT_SCRIPT_INFORMATION_OPCODE,\r
-                         (UINT32) sizeof Info,\r
-                         (EFI_PHYSICAL_ADDRESS)(UINTN) &Info);\r
+  Status = BootScript->Write (\r
+                         BootScript,\r
+                         EFI_BOOT_SCRIPT_INFORMATION_OPCODE,\r
+                         (UINT32)sizeof Info,\r
+                         (EFI_PHYSICAL_ADDRESS)(UINTN)&Info\r
+                         );\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
+/**\r
+  Do the platform specific action after the console is ready\r
+\r
+  Possible things that can be done in PlatformBootManagerAfterConsole:\r
 \r
+  > Console post action:\r
+    > Dynamically switch output mode from 100x31 to 80x25 for certain senarino\r
+    > Signal console ready platform customized event\r
+  > Run diagnostics like memory testing\r
+  > Connect certain devices\r
+  > Dispatch aditional option roms\r
+  > Special boot: e.g.: USB boot, enter UI\r
+**/\r
 VOID\r
 EFIAPI\r
 PlatformBootManagerAfterConsole (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The function will excute with as the platform policy, current policy\r
-  is driven by boot mode. IBV/OEM can customize this code for their specific\r
-  policy action.\r
-\r
---*/\r
 {\r
-  EFI_STATUS                         Status;\r
-  EFI_BOOT_MODE                      BootMode;\r
+  EFI_BOOT_MODE  BootMode;\r
 \r
-  DEBUG ((EFI_D_INFO, "PlatformBootManagerAfterConsole\n"));\r
+  DEBUG ((DEBUG_INFO, "PlatformBootManagerAfterConsole\n"));\r
 \r
   if (PcdGetBool (PcdOvmfFlashVariablesEnable)) {\r
-    DEBUG ((EFI_D_INFO, "PlatformBdsPolicyBehavior: not restoring NvVars "\r
-      "from disk since flash variables appear to be supported.\n"));\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "PlatformBdsPolicyBehavior: not restoring NvVars "\r
+      "from disk since flash variables appear to be supported.\n"\r
+      ));\r
   } else {\r
     //\r
     // Try to restore variables from the hard disk early so\r
@@ -1278,217 +1658,91 @@ Routine Description:
     PlatformBdsRestoreNvVarsFromHardDisk ();\r
   }\r
 \r
-  //\r
-  // Load the driver option as the driver option list\r
-  //\r
-  PlatformBdsGetDriverOption (DriverOptionList);\r
-\r
   //\r
   // Get current Boot Mode\r
   //\r
-  Status = BdsLibGetBootMode (&BootMode);\r
-  DEBUG ((EFI_D_ERROR, "Boot Mode:%x\n", BootMode));\r
+  BootMode = GetBootModeHob ();\r
+  DEBUG ((DEBUG_INFO, "Boot Mode:%x\n", BootMode));\r
 \r
   //\r
   // Go the different platform policy with different boot mode\r
   // Notes: this part code can be change with the table policy\r
   //\r
   ASSERT (BootMode == BOOT_WITH_FULL_CONFIGURATION);\r
-  //\r
-  // Connect platform console\r
-  //\r
-  Status = PlatformBdsConnectConsole (gPlatformConsole);\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // Here OEM/IBV can customize with defined action\r
-    //\r
-    PlatformBdsNoConsoleAction ();\r
-  }\r
 \r
   //\r
-  // Memory test and Logo show\r
+  // Logo show\r
   //\r
-  PlatformBdsDiagnostics (IGNORE, TRUE, BaseMemoryTest);\r
+  BootLogoEnableLogo ();\r
 \r
   //\r
-  // Perform some platform specific connect sequence\r
+  // Set PCI Interrupt Line registers and ACPI SCI_EN\r
   //\r
-  PlatformBdsConnectSequence ();\r
+  PciAcpiInitialization ();\r
 \r
   //\r
   // Process QEMU's -kernel command line option\r
   //\r
   TryRunningQemuKernel ();\r
 \r
-  DEBUG ((EFI_D_INFO, "BdsLibConnectAll\n"));\r
-  BdsLibConnectAll ();\r
-  BdsLibEnumerateAllBootOption (BootOptionList);\r
-\r
-  SetBootOrderFromQemu (BootOptionList);\r
-  //\r
-  // The BootOrder variable may have changed, reload the in-memory list with\r
-  // it.\r
-  //\r
-  BdsLibBuildOptionFromVar (BootOptionList, L"BootOrder");\r
-\r
-  PlatformBdsEnterFrontPage (GetFrontPageTimeoutFromQemu(), TRUE);\r
-}\r
-\r
-VOID\r
-EFIAPI\r
-PlatformBdsBootSuccess (\r
-  IN  BDS_COMMON_OPTION   *Option\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Hook point after a boot attempt succeeds. We don't expect a boot option to\r
-  return, so the EFI 1.0 specification defines that you will default to an\r
-  interactive mode and stop processing the BootOrder list in this case. This\r
-  is alos a platform implementation and can be customized by IBV/OEM.\r
-\r
-Arguments:\r
-\r
-  Option - Pointer to Boot Option that succeeded to boot.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
-{\r
-  CHAR16  *TmpStr;\r
-\r
-  DEBUG ((EFI_D_INFO, "PlatformBdsBootSuccess\n"));\r
   //\r
-  // If Boot returned with EFI_SUCCESS and there is not in the boot device\r
-  // select loop then we need to pop up a UI and wait for user input.\r
+  // Perform some platform specific connect sequence\r
   //\r
-  TmpStr = Option->StatusString;\r
-  if (TmpStr != NULL) {\r
-    BdsLibOutputStrings (gST->ConOut, TmpStr, Option->Description, L"\n\r", NULL);\r
-    FreePool (TmpStr);\r
-  }\r
-}\r
-\r
-VOID\r
-EFIAPI\r
-PlatformBdsBootFail (\r
-  IN  BDS_COMMON_OPTION  *Option,\r
-  IN  EFI_STATUS         Status,\r
-  IN  CHAR16             *ExitData,\r
-  IN  UINTN              ExitDataSize\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Hook point after a boot attempt fails.\r
-\r
-Arguments:\r
-\r
-  Option - Pointer to Boot Option that failed to boot.\r
-\r
-  Status - Status returned from failed boot.\r
-\r
-  ExitData - Exit data returned from failed boot.\r
-\r
-  ExitDataSize - Exit data size returned from failed boot.\r
-\r
-Returns:\r
-\r
-  None.\r
-\r
---*/\r
-{\r
-  CHAR16  *TmpStr;\r
+  PlatformBdsConnectSequence ();\r
 \r
-  DEBUG ((EFI_D_INFO, "PlatformBdsBootFail\n"));\r
+  EfiBootManagerRefreshAllBootOption ();\r
 \r
   //\r
-  // If Boot returned with failed status then we need to pop up a UI and wait\r
-  // for user input.\r
+  // Register UEFI Shell\r
   //\r
-  TmpStr = Option->StatusString;\r
-  if (TmpStr != NULL) {\r
-    BdsLibOutputStrings (gST->ConOut, TmpStr, Option->Description, L"\n\r", NULL);\r
-    FreePool (TmpStr);\r
-  }\r
-}\r
-\r
-EFI_STATUS\r
-PlatformBdsNoConsoleAction (\r
-  VOID\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This function is remained for IBV/OEM to do some platform action,\r
-  if there no console device can be connected.\r
-\r
-Arguments:\r
-\r
-  None.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS      - Direct return success now.\r
+  PlatformRegisterFvBootOption (\r
+    &gUefiShellFileGuid,\r
+    L"EFI Internal Shell",\r
+    LOAD_OPTION_ACTIVE\r
+    );\r
 \r
---*/\r
-{\r
-  DEBUG ((EFI_D_INFO, "PlatformBdsNoConsoleAction\n"));\r
-  return EFI_SUCCESS;\r
-}\r
+  RemoveStaleFvFileOptions ();\r
+  SetBootOrderFromQemu ();\r
 \r
-VOID\r
-EFIAPI\r
-PlatformBdsLockNonUpdatableFlash (\r
-  VOID\r
-  )\r
-{\r
-  DEBUG ((EFI_D_INFO, "PlatformBdsLockNonUpdatableFlash\n"));\r
-  return;\r
+  PlatformBmPrintScRegisterHandler ();\r
 }\r
 \r
-\r
 /**\r
   This notification function is invoked when an instance of the\r
   EFI_DEVICE_PATH_PROTOCOL is produced.\r
 \r
-  @param  Event                 The event that occured\r
-  @param  Context               For EFI compatiblity.  Not used.\r
+  @param  Event                 The event that occurred\r
+  @param  Context               For EFI compatibility.  Not used.\r
 \r
 **/\r
 VOID\r
 EFIAPI\r
 NotifyDevPath (\r
-  IN  EFI_EVENT Event,\r
-  IN  VOID      *Context\r
+  IN  EFI_EVENT  Event,\r
+  IN  VOID       *Context\r
   )\r
 {\r
-  EFI_HANDLE                            Handle;\r
-  EFI_STATUS                            Status;\r
-  UINTN                                 BufferSize;\r
-  EFI_DEVICE_PATH_PROTOCOL             *DevPathNode;\r
-  ATAPI_DEVICE_PATH                    *Atapi;\r
+  EFI_HANDLE                Handle;\r
+  EFI_STATUS                Status;\r
+  UINTN                     BufferSize;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevPathNode;\r
+  ATAPI_DEVICE_PATH         *Atapi;\r
 \r
   //\r
   // Examine all new handles\r
   //\r
-  for (;;) {\r
+  for ( ; ;) {\r
     //\r
     // Get the next handle\r
     //\r
     BufferSize = sizeof (Handle);\r
-    Status = gBS->LocateHandle (\r
-              ByRegisterNotify,\r
-              NULL,\r
-              mEfiDevPathNotifyReg,\r
-              &BufferSize,\r
-              &Handle\r
-              );\r
+    Status     = gBS->LocateHandle (\r
+                        ByRegisterNotify,\r
+                        NULL,\r
+                        mEfiDevPathNotifyReg,\r
+                        &BufferSize,\r
+                        &Handle\r
+                        );\r
 \r
     //\r
     // If not found, we're done\r
@@ -1504,7 +1758,11 @@ NotifyDevPath (
     //\r
     // Get the DevicePath protocol on that handle\r
     //\r
-    Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevPathNode);\r
+    Status = gBS->HandleProtocol (\r
+                    Handle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    (VOID **)&DevPathNode\r
+                    );\r
     ASSERT_EFI_ERROR (Status);\r
 \r
     while (!IsDevicePathEnd (DevPathNode)) {\r
@@ -1512,16 +1770,17 @@ NotifyDevPath (
       // Find the handler to dump this device path node\r
       //\r
       if (\r
-           (DevicePathType(DevPathNode) == MESSAGING_DEVICE_PATH) &&\r
-           (DevicePathSubType(DevPathNode) == MSG_ATAPI_DP)\r
-         ) {\r
-        Atapi = (ATAPI_DEVICE_PATH*) DevPathNode;\r
+          (DevicePathType (DevPathNode) == MESSAGING_DEVICE_PATH) &&\r
+          (DevicePathSubType (DevPathNode) == MSG_ATAPI_DP)\r
+          )\r
+      {\r
+        Atapi = (ATAPI_DEVICE_PATH *)DevPathNode;\r
         PciOr16 (\r
           PCI_LIB_ADDRESS (\r
             0,\r
             1,\r
             1,\r
-            (Atapi->PrimarySecondary == 1) ? 0x42: 0x40\r
+            (Atapi->PrimarySecondary == 1) ? 0x42 : 0x40\r
             ),\r
           BIT15\r
           );\r
@@ -1537,52 +1796,118 @@ NotifyDevPath (
   return;\r
 }\r
 \r
-\r
 VOID\r
 InstallDevicePathCallback (\r
   VOID\r
   )\r
 {\r
-  DEBUG ((EFI_D_INFO, "Registered NotifyDevPath Event\n"));\r
+  DEBUG ((DEBUG_INFO, "Registered NotifyDevPath Event\n"));\r
   mEfiDevPathEvent = EfiCreateProtocolNotifyEvent (\r
-                          &gEfiDevicePathProtocolGuid,\r
-                          TPL_CALLBACK,\r
-                          NotifyDevPath,\r
-                          NULL,\r
-                          &mEfiDevPathNotifyReg\r
-                          );\r
+                       &gEfiDevicePathProtocolGuid,\r
+                       TPL_CALLBACK,\r
+                       NotifyDevPath,\r
+                       NULL,\r
+                       &mEfiDevPathNotifyReg\r
+                       );\r
 }\r
 \r
 /**\r
-  This function is called each second during the boot manager waits the timeout.\r
+  This function is called each second during the boot manager waits the\r
+  timeout.\r
 \r
   @param TimeoutRemain  The remaining timeout.\r
 **/\r
 VOID\r
 EFIAPI\r
 PlatformBootManagerWaitCallback (\r
-  UINT16          TimeoutRemain\r
+  UINT16  TimeoutRemain\r
   )\r
 {\r
-}\r
+  EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION  Black;\r
+  EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION  White;\r
+  UINT16                               TimeoutInitial;\r
 \r
-/**\r
-  Lock the ConsoleIn device in system table. All key\r
-  presses will be ignored until the Password is typed in. The only way to\r
-  disable the password is to type it in to a ConIn device.\r
+  TimeoutInitial = PcdGet16 (PcdPlatformBootTimeOut);\r
 \r
-  @param  Password        Password used to lock ConIn device.\r
+  //\r
+  // If PcdPlatformBootTimeOut is set to zero, then we consider\r
+  // that no progress update should be enacted (since we'd only\r
+  // ever display a one-shot progress of either 0% or 100%).\r
+  //\r
+  if (TimeoutInitial == 0) {\r
+    return;\r
+  }\r
+\r
+  Black.Raw = 0x00000000;\r
+  White.Raw = 0x00FFFFFF;\r
 \r
-  @retval EFI_SUCCESS     lock the Console In Spliter virtual handle successfully.\r
-  @retval EFI_UNSUPPORTED Password not found\r
+  BootLogoUpdateProgress (\r
+    White.Pixel,\r
+    Black.Pixel,\r
+    L"Start boot option",\r
+    White.Pixel,\r
+    (TimeoutInitial - TimeoutRemain) * 100 / TimeoutInitial,\r
+    0\r
+    );\r
+}\r
+\r
+/**\r
+  The function is called when no boot option could be launched,\r
+  including platform recovery options and options pointing to applications\r
+  built into firmware volumes.\r
 \r
+  If this function returns, BDS attempts to enter an infinite loop.\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
-LockKeyboards (\r
-  IN  CHAR16    *Password\r
+PlatformBootManagerUnableToBoot (\r
+  VOID\r
   )\r
 {\r
-    return EFI_UNSUPPORTED;\r
-}\r
+  EFI_STATUS                    Status;\r
+  EFI_INPUT_KEY                 Key;\r
+  EFI_BOOT_MANAGER_LOAD_OPTION  BootManagerMenu;\r
+  UINTN                         Index;\r
+\r
+  //\r
+  // BootManagerMenu doesn't contain the correct information when return status\r
+  // is EFI_NOT_FOUND.\r
+  //\r
+  Status = EfiBootManagerGetBootManagerMenu (&BootManagerMenu);\r
+  if (EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Normally BdsDxe does not print anything to the system console, but this is\r
+  // a last resort -- the end-user will likely not see any DEBUG messages\r
+  // logged in this situation.\r
+  //\r
+  // AsciiPrint() will NULL-check gST->ConOut internally. We check gST->ConIn\r
+  // here to see if it makes sense to request and wait for a keypress.\r
+  //\r
+  if (gST->ConIn != NULL) {\r
+    AsciiPrint (\r
+      "%a: No bootable option or device was found.\n"\r
+      "%a: Press any key to enter the Boot Manager Menu.\n",\r
+      gEfiCallerBaseName,\r
+      gEfiCallerBaseName\r
+      );\r
+    Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &Index);\r
+    ASSERT_EFI_ERROR (Status);\r
+    ASSERT (Index == 0);\r
+\r
+    //\r
+    // Drain any queued keys.\r
+    //\r
+    while (!EFI_ERROR (gST->ConIn->ReadKeyStroke (gST->ConIn, &Key))) {\r
+      //\r
+      // just throw away Key\r
+      //\r
+    }\r
+  }\r
 \r
+  for ( ; ;) {\r
+    EfiBootManagerBoot (&BootManagerMenu);\r
+  }\r
+}\r