]>
Commit | Line | Data |
---|---|---|
1767877a LE |
1 | /**@file\r |
2 | Functions related to the Firmware Volume Block service whose\r | |
3 | implementation is specific to the runtime DXE driver build.\r | |
4 | \r | |
5 | Copyright (C) 2015, Red Hat, Inc.\r | |
6 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
7 | \r | |
8 | This program and the accompanying materials are licensed and made available\r | |
9 | under the terms and conditions of the BSD License which accompanies this\r | |
10 | distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | **/\r | |
16 | \r | |
17 | #include <Guid/EventGroup.h>\r | |
18 | #include <Library/DebugLib.h>\r | |
19 | #include <Library/DevicePathLib.h>\r | |
3b3d016b | 20 | #include <Library/DxeServicesTableLib.h>\r |
75b7aa95 | 21 | #include <Library/MemEncryptSevLib.h>\r |
b963ec49 | 22 | #include <Library/PcdLib.h>\r |
1767877a LE |
23 | #include <Library/UefiBootServicesTableLib.h>\r |
24 | #include <Library/UefiRuntimeLib.h>\r | |
25 | #include <Protocol/DevicePath.h>\r | |
26 | #include <Protocol/FirmwareVolumeBlock.h>\r | |
27 | \r | |
28 | #include "FwBlockService.h"\r | |
29 | #include "QemuFlash.h"\r | |
30 | \r | |
31 | VOID\r | |
32 | InstallProtocolInterfaces (\r | |
33 | IN EFI_FW_VOL_BLOCK_DEVICE *FvbDevice\r | |
34 | )\r | |
35 | {\r | |
36 | EFI_STATUS Status;\r | |
37 | EFI_HANDLE FwbHandle;\r | |
38 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *OldFwbInterface;\r | |
39 | \r | |
b963ec49 LE |
40 | ASSERT (!FeaturePcdGet (PcdSmmSmramRequire));\r |
41 | \r | |
1767877a LE |
42 | //\r |
43 | // Find a handle with a matching device path that has supports FW Block\r | |
44 | // protocol\r | |
45 | //\r | |
46 | Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid,\r | |
47 | &FvbDevice->DevicePath, &FwbHandle);\r | |
48 | if (EFI_ERROR (Status)) {\r | |
49 | //\r | |
50 | // LocateDevicePath fails so install a new interface and device path\r | |
51 | //\r | |
52 | FwbHandle = NULL;\r | |
53 | DEBUG ((EFI_D_INFO, "Installing QEMU flash FVB\n"));\r | |
54 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
55 | &FwbHandle,\r | |
56 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
57 | &FvbDevice->FwVolBlockInstance,\r | |
58 | &gEfiDevicePathProtocolGuid,\r | |
59 | FvbDevice->DevicePath,\r | |
60 | NULL\r | |
61 | );\r | |
62 | ASSERT_EFI_ERROR (Status);\r | |
63 | } else if (IsDevicePathEnd (FvbDevice->DevicePath)) {\r | |
64 | //\r | |
65 | // Device already exists, so reinstall the FVB protocol\r | |
66 | //\r | |
67 | Status = gBS->HandleProtocol (\r | |
68 | FwbHandle,\r | |
69 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
70 | (VOID**)&OldFwbInterface\r | |
71 | );\r | |
72 | ASSERT_EFI_ERROR (Status);\r | |
73 | \r | |
74 | DEBUG ((EFI_D_INFO, "Reinstalling FVB for QEMU flash region\n"));\r | |
75 | Status = gBS->ReinstallProtocolInterface (\r | |
76 | FwbHandle,\r | |
77 | &gEfiFirmwareVolumeBlockProtocolGuid,\r | |
78 | OldFwbInterface,\r | |
79 | &FvbDevice->FwVolBlockInstance\r | |
80 | );\r | |
81 | ASSERT_EFI_ERROR (Status);\r | |
82 | } else {\r | |
83 | //\r | |
84 | // There was a FVB protocol on an End Device Path node\r | |
85 | //\r | |
86 | ASSERT (FALSE);\r | |
87 | }\r | |
88 | }\r | |
89 | \r | |
90 | \r | |
91 | STATIC\r | |
92 | VOID\r | |
93 | EFIAPI\r | |
94 | FvbVirtualAddressChangeEvent (\r | |
95 | IN EFI_EVENT Event,\r | |
96 | IN VOID *Context\r | |
97 | )\r | |
98 | /*++\r | |
99 | \r | |
100 | Routine Description:\r | |
101 | \r | |
102 | Fixup internal data so that EFI and SAL can be call in virtual mode.\r | |
103 | Call the passed in Child Notify event and convert the mFvbModuleGlobal\r | |
104 | date items to there virtual address.\r | |
105 | \r | |
106 | Arguments:\r | |
107 | \r | |
108 | (Standard EFI notify event - EFI_EVENT_NOTIFY)\r | |
109 | \r | |
110 | Returns:\r | |
111 | \r | |
112 | None\r | |
113 | \r | |
114 | --*/\r | |
115 | {\r | |
116 | EFI_FW_VOL_INSTANCE *FwhInstance;\r | |
117 | UINTN Index;\r | |
118 | \r | |
119 | FwhInstance = mFvbModuleGlobal->FvInstance;\r | |
120 | EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal->FvInstance);\r | |
121 | \r | |
122 | //\r | |
123 | // Convert the base address of all the instances\r | |
124 | //\r | |
125 | Index = 0;\r | |
126 | while (Index < mFvbModuleGlobal->NumFv) {\r | |
127 | EfiConvertPointer (0x0, (VOID **) &FwhInstance->FvBase);\r | |
128 | FwhInstance = (EFI_FW_VOL_INSTANCE *)\r | |
129 | (\r | |
130 | (UINTN) ((UINT8 *) FwhInstance) +\r | |
131 | FwhInstance->VolumeHeader.HeaderLength +\r | |
132 | (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER))\r | |
133 | );\r | |
134 | Index++;\r | |
135 | }\r | |
136 | \r | |
137 | EfiConvertPointer (0x0, (VOID **) &mFvbModuleGlobal);\r | |
138 | QemuFlashConvertPointers ();\r | |
139 | }\r | |
140 | \r | |
141 | \r | |
142 | VOID\r | |
143 | InstallVirtualAddressChangeHandler (\r | |
144 | VOID\r | |
145 | )\r | |
146 | {\r | |
147 | EFI_STATUS Status;\r | |
148 | EFI_EVENT VirtualAddressChangeEvent;\r | |
149 | \r | |
150 | Status = gBS->CreateEventEx (\r | |
151 | EVT_NOTIFY_SIGNAL,\r | |
152 | TPL_NOTIFY,\r | |
153 | FvbVirtualAddressChangeEvent,\r | |
154 | NULL,\r | |
155 | &gEfiEventVirtualAddressChangeGuid,\r | |
156 | &VirtualAddressChangeEvent\r | |
157 | );\r | |
158 | ASSERT_EFI_ERROR (Status);\r | |
159 | }\r | |
3b3d016b BS |
160 | \r |
161 | EFI_STATUS\r | |
162 | MarkIoMemoryRangeForRuntimeAccess (\r | |
163 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
164 | IN UINTN Length\r | |
165 | )\r | |
166 | {\r | |
167 | EFI_STATUS Status;\r | |
168 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r | |
169 | \r | |
170 | //\r | |
171 | // Mark flash region as runtime memory\r | |
172 | //\r | |
173 | Status = gDS->RemoveMemorySpace (\r | |
174 | BaseAddress,\r | |
175 | Length\r | |
176 | );\r | |
177 | \r | |
178 | Status = gDS->AddMemorySpace (\r | |
179 | EfiGcdMemoryTypeMemoryMappedIo,\r | |
180 | BaseAddress,\r | |
181 | Length,\r | |
182 | EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r | |
183 | );\r | |
184 | ASSERT_EFI_ERROR (Status);\r | |
185 | \r | |
186 | Status = gDS->AllocateMemorySpace (\r | |
187 | EfiGcdAllocateAddress,\r | |
188 | EfiGcdMemoryTypeMemoryMappedIo,\r | |
189 | 0,\r | |
190 | Length,\r | |
191 | &BaseAddress,\r | |
192 | gImageHandle,\r | |
193 | NULL\r | |
194 | );\r | |
195 | ASSERT_EFI_ERROR (Status);\r | |
196 | \r | |
197 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
198 | ASSERT_EFI_ERROR (Status);\r | |
199 | \r | |
200 | Status = gDS->SetMemorySpaceAttributes (\r | |
201 | BaseAddress,\r | |
202 | Length,\r | |
203 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
204 | );\r | |
205 | ASSERT_EFI_ERROR (Status);\r | |
206 | \r | |
75b7aa95 BS |
207 | //\r |
208 | // When SEV is active, AmdSevDxe mapped the BaseAddress with C=0 but\r | |
209 | // SetMemorySpaceAttributes() remaps the range with C=1. Let's restore\r | |
210 | // the mapping so that both guest and hyervisor can access the flash\r | |
211 | // memory range.\r | |
212 | //\r | |
213 | if (MemEncryptSevIsEnabled ()) {\r | |
214 | Status = MemEncryptSevClearPageEncMask (\r | |
215 | 0,\r | |
216 | BaseAddress,\r | |
217 | EFI_SIZE_TO_PAGES (Length),\r | |
218 | FALSE\r | |
219 | );\r | |
220 | ASSERT_EFI_ERROR (Status);\r | |
221 | }\r | |
222 | \r | |
3b3d016b BS |
223 | return Status;\r |
224 | }\r |