]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FwBlockServiceDxe.c
OvmfPkg: replace old EFI_D_ debug levels with new DEBUG_ ones
[mirror_edk2.git] / OvmfPkg / QemuFlashFvbServicesRuntimeDxe / FwBlockServiceDxe.c
index c11f598b5a6b4559424e93c717563cb8233537ae..1f285e008372b069dd26bf991342ff247d26abb0 100644 (file)
@@ -5,18 +5,15 @@
   Copyright (C) 2015, Red Hat, Inc.\r
   Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
 \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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 **/\r
 \r
 #include <Guid/EventGroup.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/DevicePathLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/MemEncryptSevLib.h>\r
+#include <Library/PcdLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/UefiRuntimeLib.h>\r
 #include <Protocol/DevicePath.h>\r
@@ -34,6 +31,8 @@ InstallProtocolInterfaces (
   EFI_HANDLE                         FwbHandle;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;\r
 \r
+  ASSERT (!FeaturePcdGet (PcdSmmSmramRequire));\r
+\r
   //\r
   // Find a handle with a matching device path that has supports FW Block\r
   // protocol\r
@@ -45,7 +44,7 @@ InstallProtocolInterfaces (
     // LocateDevicePath fails so install a new interface and device path\r
     //\r
     FwbHandle = NULL;\r
-    DEBUG ((EFI_D_INFO, "Installing QEMU flash FVB\n"));\r
+    DEBUG ((DEBUG_INFO, "Installing QEMU flash FVB\n"));\r
     Status = gBS->InstallMultipleProtocolInterfaces (\r
                     &FwbHandle,\r
                     &gEfiFirmwareVolumeBlockProtocolGuid,\r
@@ -66,7 +65,7 @@ InstallProtocolInterfaces (
                     );\r
     ASSERT_EFI_ERROR (Status);\r
 \r
-    DEBUG ((EFI_D_INFO, "Reinstalling FVB for QEMU flash region\n"));\r
+    DEBUG ((DEBUG_INFO, "Reinstalling FVB for QEMU flash region\n"));\r
     Status = gBS->ReinstallProtocolInterface (\r
                     FwbHandle,\r
                     &gEfiFirmwareVolumeBlockProtocolGuid,\r
@@ -152,3 +151,95 @@ InstallVirtualAddressChangeHandler (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
+\r
+EFI_STATUS\r
+MarkIoMemoryRangeForRuntimeAccess (\r
+  IN EFI_PHYSICAL_ADDRESS                BaseAddress,\r
+  IN UINTN                               Length\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR     GcdDescriptor;\r
+\r
+  //\r
+  // Mark flash region as runtime memory\r
+  //\r
+  Status = gDS->RemoveMemorySpace (\r
+                  BaseAddress,\r
+                  Length\r
+                  );\r
+\r
+  Status = gDS->AddMemorySpace (\r
+                  EfiGcdMemoryTypeMemoryMappedIo,\r
+                  BaseAddress,\r
+                  Length,\r
+                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gDS->AllocateMemorySpace (\r
+                  EfiGcdAllocateAddress,\r
+                  EfiGcdMemoryTypeMemoryMappedIo,\r
+                  0,\r
+                  Length,\r
+                  &BaseAddress,\r
+                  gImageHandle,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gDS->SetMemorySpaceAttributes (\r
+                  BaseAddress,\r
+                  Length,\r
+                  GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // When SEV is active, AmdSevDxe mapped the BaseAddress with C=0 but\r
+  // SetMemorySpaceAttributes() remaps the range with C=1. Let's restore\r
+  // the mapping so that both guest and hyervisor can access the flash\r
+  // memory range.\r
+  //\r
+  if (MemEncryptSevIsEnabled ()) {\r
+    Status = MemEncryptSevClearPageEncMask (\r
+               0,\r
+               BaseAddress,\r
+               EFI_SIZE_TO_PAGES (Length),\r
+               FALSE\r
+               );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+VOID\r
+SetPcdFlashNvStorageBaseAddresses (\r
+  VOID\r
+  )\r
+{\r
+  RETURN_STATUS PcdStatus;\r
+\r
+  //\r
+  // Set several PCD values to point to flash\r
+  //\r
+  PcdStatus = PcdSet64S (\r
+    PcdFlashNvStorageVariableBase64,\r
+    (UINTN) PcdGet32 (PcdOvmfFlashNvStorageVariableBase)\r
+    );\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+  PcdStatus = PcdSet32S (\r
+    PcdFlashNvStorageFtwWorkingBase,\r
+    PcdGet32 (PcdOvmfFlashNvStorageFtwWorkingBase)\r
+    );\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+  PcdStatus = PcdSet32S (\r
+    PcdFlashNvStorageFtwSpareBase,\r
+    PcdGet32 (PcdOvmfFlashNvStorageFtwSpareBase)\r
+    );\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+}\r