2 Functions related to the Firmware Volume Block service whose
3 implementation is specific to the runtime DXE driver build.
5 Copyright (C) 2015, Red Hat, Inc.
6 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Guid/EventGroup.h>
18 #include <Library/DebugLib.h>
19 #include <Library/DevicePathLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/MemEncryptSevLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiRuntimeLib.h>
25 #include <Protocol/DevicePath.h>
26 #include <Protocol/FirmwareVolumeBlock.h>
28 #include "FwBlockService.h"
29 #include "QemuFlash.h"
32 InstallProtocolInterfaces (
33 IN EFI_FW_VOL_BLOCK_DEVICE
*FvbDevice
38 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*OldFwbInterface
;
40 ASSERT (!FeaturePcdGet (PcdSmmSmramRequire
));
43 // Find a handle with a matching device path that has supports FW Block
46 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid
,
47 &FvbDevice
->DevicePath
, &FwbHandle
);
48 if (EFI_ERROR (Status
)) {
50 // LocateDevicePath fails so install a new interface and device path
53 DEBUG ((EFI_D_INFO
, "Installing QEMU flash FVB\n"));
54 Status
= gBS
->InstallMultipleProtocolInterfaces (
56 &gEfiFirmwareVolumeBlockProtocolGuid
,
57 &FvbDevice
->FwVolBlockInstance
,
58 &gEfiDevicePathProtocolGuid
,
59 FvbDevice
->DevicePath
,
62 ASSERT_EFI_ERROR (Status
);
63 } else if (IsDevicePathEnd (FvbDevice
->DevicePath
)) {
65 // Device already exists, so reinstall the FVB protocol
67 Status
= gBS
->HandleProtocol (
69 &gEfiFirmwareVolumeBlockProtocolGuid
,
70 (VOID
**)&OldFwbInterface
72 ASSERT_EFI_ERROR (Status
);
74 DEBUG ((EFI_D_INFO
, "Reinstalling FVB for QEMU flash region\n"));
75 Status
= gBS
->ReinstallProtocolInterface (
77 &gEfiFirmwareVolumeBlockProtocolGuid
,
79 &FvbDevice
->FwVolBlockInstance
81 ASSERT_EFI_ERROR (Status
);
84 // There was a FVB protocol on an End Device Path node
94 FvbVirtualAddressChangeEvent (
102 Fixup internal data so that EFI and SAL can be call in virtual mode.
103 Call the passed in Child Notify event and convert the mFvbModuleGlobal
104 date items to there virtual address.
108 (Standard EFI notify event - EFI_EVENT_NOTIFY)
116 EFI_FW_VOL_INSTANCE
*FwhInstance
;
119 FwhInstance
= mFvbModuleGlobal
->FvInstance
;
120 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
->FvInstance
);
123 // Convert the base address of all the instances
126 while (Index
< mFvbModuleGlobal
->NumFv
) {
127 EfiConvertPointer (0x0, (VOID
**) &FwhInstance
->FvBase
);
128 FwhInstance
= (EFI_FW_VOL_INSTANCE
*)
130 (UINTN
) ((UINT8
*) FwhInstance
) +
131 FwhInstance
->VolumeHeader
.HeaderLength
+
132 (sizeof (EFI_FW_VOL_INSTANCE
) - sizeof (EFI_FIRMWARE_VOLUME_HEADER
))
137 EfiConvertPointer (0x0, (VOID
**) &mFvbModuleGlobal
);
138 QemuFlashConvertPointers ();
143 InstallVirtualAddressChangeHandler (
148 EFI_EVENT VirtualAddressChangeEvent
;
150 Status
= gBS
->CreateEventEx (
153 FvbVirtualAddressChangeEvent
,
155 &gEfiEventVirtualAddressChangeGuid
,
156 &VirtualAddressChangeEvent
158 ASSERT_EFI_ERROR (Status
);
162 MarkIoMemoryRangeForRuntimeAccess (
163 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
168 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
171 // Mark flash region as runtime memory
173 Status
= gDS
->RemoveMemorySpace (
178 Status
= gDS
->AddMemorySpace (
179 EfiGcdMemoryTypeMemoryMappedIo
,
182 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
184 ASSERT_EFI_ERROR (Status
);
186 Status
= gDS
->AllocateMemorySpace (
187 EfiGcdAllocateAddress
,
188 EfiGcdMemoryTypeMemoryMappedIo
,
195 ASSERT_EFI_ERROR (Status
);
197 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
198 ASSERT_EFI_ERROR (Status
);
200 Status
= gDS
->SetMemorySpaceAttributes (
203 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
205 ASSERT_EFI_ERROR (Status
);
208 // When SEV is active, AmdSevDxe mapped the BaseAddress with C=0 but
209 // SetMemorySpaceAttributes() remaps the range with C=1. Let's restore
210 // the mapping so that both guest and hyervisor can access the flash
213 if (MemEncryptSevIsEnabled ()) {
214 Status
= MemEncryptSevClearPageEncMask (
217 EFI_SIZE_TO_PAGES (Length
),
220 ASSERT_EFI_ERROR (Status
);