]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FwBlockServiceDxe.c
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / OvmfPkg / QemuFlashFvbServicesRuntimeDxe / FwBlockServiceDxe.c
1 /**@file
2 Functions related to the Firmware Volume Block service whose
3 implementation is specific to the runtime DXE driver build.
4
5 Copyright (C) 2015, Red Hat, Inc.
6 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
7
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
12
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.
15 **/
16
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>
27
28 #include "FwBlockService.h"
29 #include "QemuFlash.h"
30
31 VOID
32 InstallProtocolInterfaces (
33 IN EFI_FW_VOL_BLOCK_DEVICE *FvbDevice
34 )
35 {
36 EFI_STATUS Status;
37 EFI_HANDLE FwbHandle;
38 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;
39
40 ASSERT (!FeaturePcdGet (PcdSmmSmramRequire));
41
42 //
43 // Find a handle with a matching device path that has supports FW Block
44 // protocol
45 //
46 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid,
47 &FvbDevice->DevicePath, &FwbHandle);
48 if (EFI_ERROR (Status)) {
49 //
50 // LocateDevicePath fails so install a new interface and device path
51 //
52 FwbHandle = NULL;
53 DEBUG ((EFI_D_INFO, "Installing QEMU flash FVB\n"));
54 Status = gBS->InstallMultipleProtocolInterfaces (
55 &FwbHandle,
56 &gEfiFirmwareVolumeBlockProtocolGuid,
57 &FvbDevice->FwVolBlockInstance,
58 &gEfiDevicePathProtocolGuid,
59 FvbDevice->DevicePath,
60 NULL
61 );
62 ASSERT_EFI_ERROR (Status);
63 } else if (IsDevicePathEnd (FvbDevice->DevicePath)) {
64 //
65 // Device already exists, so reinstall the FVB protocol
66 //
67 Status = gBS->HandleProtocol (
68 FwbHandle,
69 &gEfiFirmwareVolumeBlockProtocolGuid,
70 (VOID**)&OldFwbInterface
71 );
72 ASSERT_EFI_ERROR (Status);
73
74 DEBUG ((EFI_D_INFO, "Reinstalling FVB for QEMU flash region\n"));
75 Status = gBS->ReinstallProtocolInterface (
76 FwbHandle,
77 &gEfiFirmwareVolumeBlockProtocolGuid,
78 OldFwbInterface,
79 &FvbDevice->FwVolBlockInstance
80 );
81 ASSERT_EFI_ERROR (Status);
82 } else {
83 //
84 // There was a FVB protocol on an End Device Path node
85 //
86 ASSERT (FALSE);
87 }
88 }
89
90
91 STATIC
92 VOID
93 EFIAPI
94 FvbVirtualAddressChangeEvent (
95 IN EFI_EVENT Event,
96 IN VOID *Context
97 )
98 /*++
99
100 Routine Description:
101
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.
105
106 Arguments:
107
108 (Standard EFI notify event - EFI_EVENT_NOTIFY)
109
110 Returns:
111
112 None
113
114 --*/
115 {
116 EFI_FW_VOL_INSTANCE *FwhInstance;
117 UINTN Index;
118
119 FwhInstance = mFvbModuleGlobal->FvInstance;
120 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance);
121
122 //
123 // Convert the base address of all the instances
124 //
125 Index = 0;
126 while (Index < mFvbModuleGlobal->NumFv) {
127 EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase);
128 FwhInstance = (EFI_FW_VOL_INSTANCE *)
129 (
130 (UINTN) ((UINT8 *) FwhInstance) +
131 FwhInstance->VolumeHeader.HeaderLength +
132 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))
133 );
134 Index++;
135 }
136
137 EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);
138 QemuFlashConvertPointers ();
139 }
140
141
142 VOID
143 InstallVirtualAddressChangeHandler (
144 VOID
145 )
146 {
147 EFI_STATUS Status;
148 EFI_EVENT VirtualAddressChangeEvent;
149
150 Status = gBS->CreateEventEx (
151 EVT_NOTIFY_SIGNAL,
152 TPL_NOTIFY,
153 FvbVirtualAddressChangeEvent,
154 NULL,
155 &gEfiEventVirtualAddressChangeGuid,
156 &VirtualAddressChangeEvent
157 );
158 ASSERT_EFI_ERROR (Status);
159 }
160
161 EFI_STATUS
162 MarkIoMemoryRangeForRuntimeAccess (
163 IN EFI_PHYSICAL_ADDRESS BaseAddress,
164 IN UINTN Length
165 )
166 {
167 EFI_STATUS Status;
168 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
169
170 //
171 // Mark flash region as runtime memory
172 //
173 Status = gDS->RemoveMemorySpace (
174 BaseAddress,
175 Length
176 );
177
178 Status = gDS->AddMemorySpace (
179 EfiGcdMemoryTypeMemoryMappedIo,
180 BaseAddress,
181 Length,
182 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
183 );
184 ASSERT_EFI_ERROR (Status);
185
186 Status = gDS->AllocateMemorySpace (
187 EfiGcdAllocateAddress,
188 EfiGcdMemoryTypeMemoryMappedIo,
189 0,
190 Length,
191 &BaseAddress,
192 gImageHandle,
193 NULL
194 );
195 ASSERT_EFI_ERROR (Status);
196
197 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
198 ASSERT_EFI_ERROR (Status);
199
200 Status = gDS->SetMemorySpaceAttributes (
201 BaseAddress,
202 Length,
203 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
204 );
205 ASSERT_EFI_ERROR (Status);
206
207 //
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
211 // memory range.
212 //
213 if (MemEncryptSevIsEnabled ()) {
214 Status = MemEncryptSevClearPageEncMask (
215 0,
216 BaseAddress,
217 EFI_SIZE_TO_PAGES (Length),
218 FALSE
219 );
220 ASSERT_EFI_ERROR (Status);
221 }
222
223 return Status;
224 }