]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
OvmfPkg/PlatformBds: Implement PlatformBootManagerUnableToBoot
[mirror_edk2.git] / OvmfPkg / Library / PlatformBootManagerLib / BdsPlatform.c
index b5af58367e32197821f6d75e9cc376a13ce6ddc9..b2faa797c61bab7267992f08d06789b9a6f3dcc5 100644 (file)
@@ -1,19 +1,22 @@
 /** @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
+  Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  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
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
 #include "BdsPlatform.h"\r
+#include <Guid/XenInfo.h>\r
 #include <Guid/RootBridgesConnectedEventGroup.h>\r
+#include <Protocol/FirmwareVolume2.h>\r
+#include <Library/Tcg2PhysicalPresenceLib.h>\r
 \r
 \r
 //\r
@@ -24,7 +27,6 @@ VOID          *mEfiDevPathNotifyReg;
 EFI_EVENT     mEfiDevPathEvent;\r
 VOID          *mEmuVariableEventReg;\r
 EFI_EVENT     mEmuVariableEvent;\r
-BOOLEAN       mDetectVgaOnly;\r
 UINT16        mHostBridgeDevId;\r
 \r
 //\r
@@ -35,11 +37,6 @@ CONST UINT8 PciHostIrqs[] = {
   0x0a, 0x0a, 0x0b, 0x0b\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
@@ -148,6 +145,134 @@ PlatformRegisterFvBootOption (
   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 (&BootOptionCount,\r
+                  LoadOptionTypeBoot);\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
+      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
+      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 (&gEfiFirmwareVolume2ProtocolGuid,\r
+                    &SearchNode, &FvHandle);\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 (FvHandle, &gEfiFirmwareVolume2ProtocolGuid,\r
+                      (VOID **)&FvProtocol);\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, LoadOptionTypeBoot);\r
+    DEBUG_CODE (\r
+      CHAR16 *DevicePathString;\r
+\r
+      DevicePathString = ConvertDevicePathToText(BootOptions[Index].FilePath,\r
+                           FALSE, FALSE);\r
+      DEBUG ((\r
+        EFI_ERROR (Status) ? EFI_D_WARN : EFI_D_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
+  }\r
+\r
+  EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
+}\r
+\r
 VOID\r
 PlatformRegisterOptionsAndKeys (\r
   VOID\r
@@ -194,6 +319,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
@@ -203,28 +337,28 @@ SaveS3BootScript (
 //\r
 // BDS Platform Functions\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
+  RETURN_STATUS PcdStatus;\r
 \r
   DEBUG ((EFI_D_INFO, "PlatformBootManagerBeforeConsole\n"));\r
   InstallDevicePathCallback ();\r
@@ -263,10 +397,25 @@ Returns:
                   NULL);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Dispatch deferred images after EndOfDxe event and ReadyToLock\r
+  // installation.\r
+  //\r
+  EfiBootManagerDispatchDeferredImages ();\r
+\r
   PlatformInitializeConsole (gPlatformConsole);\r
-  PcdSet16 (PcdPlatformBootTimeOut, GetFrontPageTimeoutFromQemu ());\r
+  PcdStatus = PcdSet16S (PcdPlatformBootTimeOut,\r
+                GetFrontPageTimeoutFromQemu ());\r
+  ASSERT_RETURN_ERROR (PcdStatus);\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 (&gEfiPciIoProtocolGuid, ConnectVirtioPciRng,\r
+    NULL);\r
 }\r
 \r
 \r
@@ -295,28 +444,110 @@ ConnectRootBridge (
 }\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
+  PciIo = Instance;\r
 \r
-Routine Description:\r
+  //\r
+  // Read and check VendorId.\r
+  //\r
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_VENDOR_ID_OFFSET,\r
+                        1, &VendorId);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
+  if (VendorId != VIRTIO_VENDOR_ID) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Read DeviceId and RevisionId.\r
+  //\r
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint16, PCI_DEVICE_ID_OFFSET,\r
+                        1, &DeviceId);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
+  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint8, PCI_REVISION_ID_OFFSET,\r
+                        1, &RevisionId);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
+\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
+    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 (PciIo, EfiPciIoWidthUint16,\r
+                        PCI_SUBSYSTEM_ID_OFFSET, 1, &SubsystemId);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
+  if ((Virtio10 && SubsystemId >= 0x40) ||\r
+      (!Virtio10 && SubsystemId == VIRTIO_SUBSYSTEM_ENTROPY_SOURCE)) {\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
+  return EFI_SUCCESS;\r
 \r
-  Add IsaKeyboard to ConIn,\r
-  add IsaSerial to ConOut, ConIn, ErrOut.\r
-  LPC Bridge: 06 01 00\r
+Error:\r
+  DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, Status));\r
+  return Status;\r
+}\r
 \r
-Arguments:\r
 \r
-  DeviceHandle            - Handle of PCIIO protocol.\r
+/**\r
+  Add IsaKeyboard to ConIn; add IsaSerial to ConOut, ConIn, ErrOut.\r
 \r
-Returns:\r
+  @param[in] DeviceHandle  Handle of the LPC Bridge device.\r
 \r
-  EFI_SUCCESS             - LPC bridge is added to ConOut, ConIn, and ErrOut.\r
-  EFI_STATUS              - No LPC bridge is added.\r
+  @retval EFI_SUCCESS  Console devices on the LPC bridge have been added to\r
+                       ConOut, ConIn, and ErrOut.\r
 \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
@@ -337,7 +568,8 @@ Returns:
   //\r
   // Register Keyboard\r
   //\r
-  DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode);\r
+  DevicePath = AppendDevicePathNode (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gPnpPs2KeyboardDeviceNode);\r
 \r
   EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);\r
 \r
@@ -347,9 +579,12 @@ Returns:
   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 (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);\r
+  DevicePath = AppendDevicePathNode (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);\r
+  DevicePath = AppendDevicePathNode (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);\r
 \r
   //\r
   // Print Device Path\r
@@ -376,9 +611,12 @@ Returns:
   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 (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gPnp16550ComPortDeviceNode);\r
+  DevicePath = AppendDevicePathNode (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);\r
+  DevicePath = AppendDevicePathNode (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);\r
 \r
   //\r
   // Print Device Path\r
@@ -436,7 +674,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
@@ -455,7 +693,8 @@ 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 (GopHandleBuffer[Index],\r
+                      &gEfiDevicePathProtocolGuid, (VOID*)&TempDevicePath);\r
       if (EFI_ERROR (Status)) {\r
         continue;\r
       }\r
@@ -468,14 +707,14 @@ GetGopDevicePath (
         // 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
@@ -487,27 +726,20 @@ GetGopDevicePath (
   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
@@ -532,27 +764,21 @@ 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
+  @param[in] DeviceHandle  Handle of the PCI serial device.\r
 \r
-Returns:\r
+  @retval EFI_SUCCESS  The PCI serial device has been added to ConOut, ConIn,\r
+                       ErrOut.\r
 \r
-  EFI_SUCCESS             - PCI Serial is added to ConOut, ConIn, and ErrOut.\r
-  EFI_STATUS              - No PCI Serial device is added.\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
@@ -567,8 +793,10 @@ Returns:
     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 (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gUartDeviceNode);\r
+  DevicePath = AppendDevicePathNode (DevicePath,\r
+                 (EFI_DEVICE_PATH_PROTOCOL *)&gTerminalTypeDeviceNode);\r
 \r
   EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);\r
   EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);\r
@@ -684,7 +912,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
@@ -706,46 +935,44 @@ DetectAndPreparePlatformPciDevicePath (
     );\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
-    // 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 ((EFI_D_INFO, "Found LPC Bridge device\n"));\r
+    PrepareLpcBridgeDevicePath (Handle);\r
+    return EFI_SUCCESS;\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 ((EFI_D_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 ((EFI_D_INFO, "Found PCI display device\n"));\r
+    PreparePciDisplayDevicePath (Handle);\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -754,80 +981,46 @@ DetectAndPreparePlatformPciDevicePath (
 \r
 \r
 /**\r
-  Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut\r
+  Connect the predefined platform default console device.\r
 \r
-  @param[in]  DetectVgaOnly - Only detect VGA device if it's TRUE.\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
-  )\r
-{\r
-  mDetectVgaOnly = DetectVgaOnly;\r
-  return VisitAllPciInstances (DetectAndPreparePlatformPciDevicePath);\r
-}\r
-\r
-\r
 VOID\r
 PlatformInitializeConsole (\r
   IN PLATFORM_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
-\r
-Arguments:\r
-\r
-  PlatformConsole         - Predfined platform default console device array.\r
---*/\r
 {\r
   UINTN                              Index;\r
-  EFI_DEVICE_PATH_PROTOCOL           *VarConout;\r
-  EFI_DEVICE_PATH_PROTOCOL           *VarConin;\r
 \r
   //\r
-  // Connect RootBridge\r
+  // Do platform specific PCI Device check and add them to ConOut, ConIn,\r
+  // ErrOut\r
   //\r
-  GetEfiGlobalVariable2 (EFI_CON_OUT_VARIABLE_NAME, (VOID **) &VarConout, NULL);\r
-  GetEfiGlobalVariable2 (EFI_CON_IN_VARIABLE_NAME, (VOID **) &VarConin, NULL);\r
-\r
-  if (VarConout == NULL || VarConin == NULL) {\r
-    //\r
-    // Do platform specific PCI Device check and add them to ConOut, ConIn, ErrOut\r
-    //\r
-    DetectAndPreparePlatformPciDevicePaths (FALSE);\r
+  VisitAllPciInstances (DetectAndPreparePlatformPciDevicePath);\r
 \r
+  //\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
+  for (Index = 0; PlatformConsole[Index].DevicePath != NULL; ++Index) {\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
+    // Update the console variable with the connect type\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_IN) == CONSOLE_IN) {\r
+      EfiBootManagerUpdateConsoleVariable (ConIn,\r
+        PlatformConsole[Index].DevicePath, NULL);\r
+    }\r
+    if ((PlatformConsole[Index].ConnectType & CONSOLE_OUT) == CONSOLE_OUT) {\r
+      EfiBootManagerUpdateConsoleVariable (ConOut,\r
+        PlatformConsole[Index].DevicePath, NULL);\r
+    }\r
+    if ((PlatformConsole[Index].ConnectType & STD_ERROR) == STD_ERROR) {\r
+      EfiBootManagerUpdateConsoleVariable (ErrOut,\r
+        PlatformConsole[Index].DevicePath, NULL);\r
     }\r
-  } else {\r
-    //\r
-    // Only detect VGA device and add them to ConOut\r
-    //\r
-    DetectAndPreparePlatformPciDevicePaths (TRUE);\r
   }\r
 }\r
 \r
@@ -1037,6 +1230,37 @@ PciAcpiInitialization (
   IoOr16 ((PciRead32 (Pmba) & ~BIT0) + 4, BIT0);\r
 }\r
 \r
+/**\r
+  This function detects if OVMF is running on Xen.\r
+\r
+**/\r
+STATIC\r
+BOOLEAN\r
+XenDetected (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE         *GuidHob;\r
+  STATIC INTN               FoundHob = -1;\r
+\r
+  if (FoundHob == 0) {\r
+    return FALSE;\r
+  } else if (FoundHob == 1) {\r
+    return TRUE;\r
+  }\r
+\r
+  //\r
+  // See if a XenInfo HOB is available\r
+  //\r
+  GuidHob = GetFirstGuidHob (&gEfiXenInfoGuid);\r
+  if (GuidHob == NULL) {\r
+    FoundHob = 0;\r
+    return FALSE;\r
+  }\r
+\r
+  FoundHob = 1;\r
+  return TRUE;\r
+}\r
 \r
 EFI_STATUS\r
 EFIAPI\r
@@ -1050,7 +1274,11 @@ 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
     DevicePath = NULL;\r
     Status = gBS->HandleProtocol (\r
                     Handle,\r
@@ -1068,7 +1296,11 @@ ConnectRecursivelyIfPciMassStorage (
     if (DevPathStr != NULL) {\r
       DEBUG((\r
         EFI_D_INFO,\r
-        "Found Mass Storage device: %s\n",\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
@@ -1089,8 +1321,8 @@ ConnectRecursivelyIfPciMassStorage (
   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
@@ -1115,6 +1347,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
@@ -1134,7 +1367,9 @@ VisitingFileSystemInstance (
       NULL,\r
       &mEmuVariableEventReg\r
       );\r
-  PcdSet64 (PcdEmuVariableEvent, (UINT64)(UINTN) mEmuVariableEvent);\r
+  PcdStatus = PcdSet64S (PcdEmuVariableEvent,\r
+                (UINT64)(UINTN) mEmuVariableEvent);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1153,28 +1388,18 @@ PlatformBdsRestoreNvVarsFromHardDisk (
 \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
 \r
@@ -1193,18 +1418,14 @@ Returns:
     Index++;\r
   }\r
 \r
-  //\r
-  // Just use the simple policy to connect all devices\r
-  //\r
-  DEBUG ((EFI_D_INFO, "EfiBootManagerConnectAll\n"));\r
-  EfiBootManagerConnectAll ();\r
-\r
-  PciAcpiInitialization ();\r
-\r
-  //\r
-  // Clear the logo after all devices are connected.\r
-  //\r
-  gST->ConOut->ClearScreen (gST->ConOut);\r
+  Status = ConnectDevicesFromQemu ();\r
+  if (RETURN_ERROR (Status)) {\r
+    //\r
+    // Just use the simple policy to connect all devices\r
+    //\r
+    DEBUG ((DEBUG_INFO, "EfiBootManagerConnectAll\n"));\r
+    EfiBootManagerConnectAll ();\r
+  }\r
 }\r
 \r
 /**\r
@@ -1239,20 +1460,24 @@ SaveS3BootScript (
 }\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_BOOT_MODE                      BootMode;\r
 \r
@@ -1273,7 +1498,7 @@ Routine Description:
   // Get current Boot Mode\r
   //\r
   BootMode = GetBootModeHob ();\r
-  DEBUG ((EFI_D_ERROR, "Boot Mode:%x\n", BootMode));\r
+  DEBUG ((DEBUG_INFO, "Boot Mode:%x\n", BootMode));\r
 \r
   //\r
   // Go the different platform policy with different boot mode\r
@@ -1284,24 +1509,28 @@ Routine Description:
   //\r
   // Logo show\r
   //\r
-  BootLogoEnableLogo (\r
-    ImageFormatBmp,                          // ImageFormat\r
-    PcdGetPtr (PcdLogoFile),                 // Logo\r
-    EdkiiPlatformLogoDisplayAttributeCenter, // Attribute\r
-    0,                                       // OffsetX\r
-    0                                        // OffsetY\r
-    );\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 TPM PPI request\r
+  //\r
+  Tcg2PhysicalPresenceLibProcessRequest (NULL);\r
 \r
   //\r
   // Process QEMU's -kernel command line option\r
   //\r
   TryRunningQemuKernel ();\r
 \r
+  //\r
+  // Perform some platform specific connect sequence\r
+  //\r
+  PlatformBdsConnectSequence ();\r
+\r
   EfiBootManagerRefreshAllBootOption ();\r
 \r
   //\r
@@ -1311,6 +1540,7 @@ Routine Description:
     PcdGetPtr (PcdShellFile), L"EFI Internal Shell", LOAD_OPTION_ACTIVE\r
     );\r
 \r
+  RemoveStaleFvFileOptions ();\r
   SetBootOrderFromQemu ();\r
 }\r
 \r
@@ -1318,8 +1548,8 @@ Routine Description:
   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
@@ -1365,7 +1595,8 @@ NotifyDevPath (
     //\r
     // Get the DevicePath protocol on that handle\r
     //\r
-    Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevPathNode);\r
+    Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid,\r
+                    (VOID **)&DevPathNode);\r
     ASSERT_EFI_ERROR (Status);\r
 \r
     while (!IsDevicePathEnd (DevPathNode)) {\r
@@ -1415,7 +1646,8 @@ InstallDevicePathCallback (
 }\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
@@ -1444,3 +1676,62 @@ PlatformBootManagerWaitCallback (
     );\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
+VOID\r
+EFIAPI\r
+PlatformBootManagerUnableToBoot (\r
+  VOID\r
+  )\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
+  // 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