]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/PlatformBootManagerLib/BdsPlatform.c
OvmfPkg/MemEncryptSevLib: find pages of initial SMRAM save state map
[mirror_edk2.git] / OvmfPkg / Library / PlatformBootManagerLib / BdsPlatform.c
index 722776b2b538e750c53ae5eb5aabefe2872035c5..025252e72b3905453adef678aa7acb62bec39289 100644 (file)
@@ -13,8 +13,9 @@
 **/\r
 \r
 #include "BdsPlatform.h"\r
+#include <Guid/XenInfo.h>\r
 #include <Guid/RootBridgesConnectedEventGroup.h>\r
-#include <Library/QemuBootOrderLib.h>\r
+#include <Protocol/FirmwareVolume2.h>\r
 \r
 \r
 //\r
@@ -36,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
@@ -149,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
@@ -185,12 +309,6 @@ PlatformRegisterOptionsAndKeys (
              NULL, (UINT16) BootOption.OptionNumber, 0, &Esc, NULL\r
              );\r
   ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);\r
-  //\r
-  // Register UEFI Shell\r
-  //\r
-  PlatformRegisterFvBootOption (\r
-    PcdGetPtr (PcdShellFile), L"EFI Internal Shell", LOAD_OPTION_ACTIVE\r
-    );\r
 }\r
 \r
 EFI_STATUS\r
@@ -219,7 +337,7 @@ PlatformBootManagerBeforeConsole (
 \r
 Routine Description:\r
 \r
-  Platform Bds init. Incude the platform firmware vendor, revision\r
+  Platform Bds init. Include the platform firmware vendor, revision\r
   and so crc check.\r
 \r
 Arguments:\r
@@ -230,8 +348,9 @@ Returns:
 \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
@@ -270,8 +389,15 @@ Returns:
                   NULL);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Dispatch deferred images after EndOfDxe event and ReadyToLock 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
@@ -443,7 +569,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
@@ -475,7 +601,7 @@ 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
@@ -495,7 +621,7 @@ GetGopDevicePath (
 }\r
 \r
 EFI_STATUS\r
-PreparePciVgaDevicePath (\r
+PreparePciDisplayDevicePath (\r
   IN EFI_HANDLE                DeviceHandle\r
   )\r
 /*++\r
@@ -745,14 +871,14 @@ DetectAndPreparePlatformPciDevicePath (
   }\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
@@ -792,7 +918,7 @@ Routine Description:
 \r
 Arguments:\r
 \r
-  PlatformConsole         - Predfined platform default console device array.\r
+  PlatformConsole         - Predefined platform default console device array.\r
 --*/\r
 {\r
   UINTN                              Index;\r
@@ -813,7 +939,7 @@ Arguments:
 \r
     //\r
     // Have chance to connect the platform default console,\r
-    // the platform default console is the minimue device group\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
@@ -1044,6 +1170,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
@@ -1057,7 +1214,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
@@ -1075,7 +1236,8 @@ 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) ? L"Mass Storage" : L"Xen",\r
         DevPathStr\r
         ));\r
       FreePool(DevPathStr);\r
@@ -1096,8 +1258,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
@@ -1122,6 +1284,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
@@ -1141,7 +1304,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
@@ -1168,7 +1333,7 @@ PlatformBdsConnectSequence (
 \r
 Routine Description:\r
 \r
-  Connect with predeined platform connect sequence,\r
+  Connect with predefined platform connect sequence,\r
   the OEM/IBV can customize with their own connect sequence.\r
 \r
 Arguments:\r
@@ -1207,94 +1372,8 @@ Returns:
   EfiBootManagerConnectAll ();\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
-    //\r
-    // Perform system diagnostic\r
-    //\r
-    Status = BaseMemoryTest (MemoryTestLevel);\r
-    if (EFI_ERROR (Status)) {\r
-      DisableQuietBoot ();\r
-    }\r
-\r
-    return ;\r
-  }\r
-  //\r
-  // Perform system diagnostic\r
-  //\r
-  Status = BaseMemoryTest (MemoryTestLevel);\r
-}\r
-\r
-\r
 /**\r
   Save the S3 boot script.\r
 \r
@@ -1336,13 +1415,12 @@ PlatformBootManagerAfterConsole (
 \r
 Routine Description:\r
 \r
-  The function will excute with as the platform policy, current policy\r
+  The function will execute 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
 \r
   DEBUG ((EFI_D_INFO, "PlatformBootManagerAfterConsole\n"));\r
@@ -1358,16 +1436,11 @@ 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
@@ -1376,9 +1449,9 @@ Routine Description:
   ASSERT (BootMode == BOOT_WITH_FULL_CONFIGURATION);\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
@@ -1389,22 +1462,26 @@ Routine Description:
   // Process QEMU's -kernel command line option\r
   //\r
   TryRunningQemuKernel ();\r
-  BdsLibEnumerateAllBootOption (BootOptionList);\r
 \r
-  SetBootOrderFromQemu (BootOptionList);\r
+  EfiBootManagerRefreshAllBootOption ();\r
+\r
   //\r
-  // The BootOrder variable may have changed, reload the in-memory list with\r
-  // it.\r
+  // Register UEFI Shell\r
   //\r
-  BdsLibBuildOptionFromVar (BootOptionList, L"BootOrder");\r
+  PlatformRegisterFvBootOption (\r
+    PcdGetPtr (PcdShellFile), L"EFI Internal Shell", LOAD_OPTION_ACTIVE\r
+    );\r
+\r
+  RemoveStaleFvFileOptions ();\r
+  SetBootOrderFromQemu ();\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
@@ -1510,5 +1587,22 @@ PlatformBootManagerWaitCallback (
   UINT16          TimeoutRemain\r
   )\r
 {\r
+  EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION Black;\r
+  EFI_GRAPHICS_OUTPUT_BLT_PIXEL_UNION White;\r
+  UINT16                              Timeout;\r
+\r
+  Timeout = PcdGet16 (PcdPlatformBootTimeOut);\r
+\r
+  Black.Raw = 0x00000000;\r
+  White.Raw = 0x00FFFFFF;\r
+\r
+  BootLogoUpdateProgress (\r
+    White.Pixel,\r
+    Black.Pixel,\r
+    L"Start boot option",\r
+    White.Pixel,\r
+    (Timeout - TimeoutRemain) * 100 / Timeout,\r
+    0\r
+    );\r
 }\r
 \r