]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
Revert the changes made in 14403 because it incorrectly changed the EOL.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableDxe.c
CommitLineData
e4b7e2c9
RN
1/** @file\r
2\r
3 Implement all four UEFI Runtime Variable services for the nonvolatile\r
4 and volatile storage space and install variable architecture protocol.\r
5 \r
6Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
7This program and the accompanying materials \r
8are licensed and made available under the terms and conditions of the BSD License \r
9which accompanies this distribution. The full text of the license may be found at \r
10http://opensource.org/licenses/bsd-license.php \r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
14\r
15**/\r
16\r
17#include "Variable.h"\r
18\r
19extern VARIABLE_STORE_HEADER *mNvVariableCache;\r
20extern VARIABLE_INFO_ENTRY *gVariableInfo;\r
21EFI_HANDLE mHandle = NULL;\r
22EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
23EFI_EVENT mFtwRegistration = NULL;\r
24extern BOOLEAN mEndOfDxe;\r
25EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock = { VariableLockRequestToLock };\r
26\r
27/**\r
28 Return TRUE if ExitBootServices () has been called.\r
29 \r
30 @retval TRUE If ExitBootServices () has been called.\r
31**/\r
32BOOLEAN\r
33AtRuntime (\r
34 VOID\r
35 )\r
36{\r
37 return EfiAtRuntime ();\r
38}\r
39\r
40\r
41/**\r
42 Initializes a basic mutual exclusion lock.\r
43\r
44 This function initializes a basic mutual exclusion lock to the released state \r
45 and returns the lock. Each lock provides mutual exclusion access at its task \r
46 priority level. Since there is no preemption or multiprocessor support in EFI,\r
47 acquiring the lock only consists of raising to the locks TPL.\r
48 If Lock is NULL, then ASSERT().\r
49 If Priority is not a valid TPL value, then ASSERT().\r
50\r
51 @param Lock A pointer to the lock data structure to initialize.\r
52 @param Priority EFI TPL is associated with the lock.\r
53\r
54 @return The lock.\r
55\r
56**/\r
57EFI_LOCK *\r
58InitializeLock (\r
59 IN OUT EFI_LOCK *Lock,\r
60 IN EFI_TPL Priority\r
61 )\r
62{\r
63 return EfiInitializeLock (Lock, Priority);\r
64}\r
65\r
66\r
67/**\r
68 Acquires lock only at boot time. Simply returns at runtime.\r
69\r
70 This is a temperary function that will be removed when\r
71 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
72 Runtimer driver in RT phase.\r
73 It calls EfiAcquireLock() at boot time, and simply returns\r
74 at runtime.\r
75\r
76 @param Lock A pointer to the lock to acquire.\r
77\r
78**/\r
79VOID\r
80AcquireLockOnlyAtBootTime (\r
81 IN EFI_LOCK *Lock\r
82 )\r
83{\r
84 if (!AtRuntime ()) {\r
85 EfiAcquireLock (Lock);\r
86 }\r
87}\r
88\r
89\r
90/**\r
91 Releases lock only at boot time. Simply returns at runtime.\r
92\r
93 This is a temperary function which will be removed when\r
94 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
95 Runtimer driver in RT phase.\r
96 It calls EfiReleaseLock() at boot time and simply returns\r
97 at runtime.\r
98\r
99 @param Lock A pointer to the lock to release.\r
100\r
101**/\r
102VOID\r
103ReleaseLockOnlyAtBootTime (\r
104 IN EFI_LOCK *Lock\r
105 )\r
106{\r
107 if (!AtRuntime ()) {\r
108 EfiReleaseLock (Lock);\r
109 }\r
110}\r
111\r
112/**\r
113 Retrive the Fault Tolerent Write protocol interface.\r
114\r
115 @param[out] FtwProtocol The interface of Ftw protocol\r
116\r
117 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.\r
118 @retval EFI_NOT_FOUND The FTW protocol instance was not found.\r
119 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
120\r
121**/\r
122EFI_STATUS\r
123GetFtwProtocol (\r
124 OUT VOID **FtwProtocol\r
125 )\r
126{\r
127 EFI_STATUS Status;\r
128\r
129 //\r
130 // Locate Fault Tolerent Write protocol\r
131 //\r
132 Status = gBS->LocateProtocol (\r
133 &gEfiFaultTolerantWriteProtocolGuid,\r
134 NULL,\r
135 FtwProtocol\r
136 ); \r
137 return Status;\r
138}\r
139\r
140/**\r
141 Retrive the FVB protocol interface by HANDLE.\r
142\r
143 @param[in] FvBlockHandle The handle of FVB protocol that provides services for\r
144 reading, writing, and erasing the target block.\r
145 @param[out] FvBlock The interface of FVB protocol\r
146\r
147 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
148 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.\r
149 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
150 \r
151**/\r
152EFI_STATUS\r
153GetFvbByHandle (\r
154 IN EFI_HANDLE FvBlockHandle,\r
155 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
156 )\r
157{\r
158 //\r
159 // To get the FVB protocol interface on the handle\r
160 //\r
161 return gBS->HandleProtocol (\r
162 FvBlockHandle,\r
163 &gEfiFirmwareVolumeBlockProtocolGuid,\r
164 (VOID **) FvBlock\r
165 );\r
166}\r
167\r
168\r
169/**\r
170 Function returns an array of handles that support the FVB protocol\r
171 in a buffer allocated from pool. \r
172\r
173 @param[out] NumberHandles The number of handles returned in Buffer.\r
174 @param[out] Buffer A pointer to the buffer to return the requested\r
175 array of handles that support FVB protocol.\r
176\r
177 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
178 handles in Buffer was returned in NumberHandles.\r
179 @retval EFI_NOT_FOUND No FVB handle was found.\r
180 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
181 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
182 \r
183**/\r
184EFI_STATUS\r
185GetFvbCountAndBuffer (\r
186 OUT UINTN *NumberHandles,\r
187 OUT EFI_HANDLE **Buffer\r
188 )\r
189{\r
190 EFI_STATUS Status;\r
191\r
192 //\r
193 // Locate all handles of Fvb protocol\r
194 //\r
195 Status = gBS->LocateHandleBuffer (\r
196 ByProtocol,\r
197 &gEfiFirmwareVolumeBlockProtocolGuid,\r
198 NULL,\r
199 NumberHandles,\r
200 Buffer\r
201 );\r
202 return Status;\r
203}\r
204\r
205\r
206/**\r
207 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
208\r
209 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
210 It convers pointer to new virtual address.\r
211\r
212 @param Event Event whose notification function is being invoked.\r
213 @param Context Pointer to the notification function's context.\r
214\r
215**/\r
216VOID\r
217EFIAPI\r
218VariableClassAddressChangeEvent (\r
219 IN EFI_EVENT Event,\r
220 IN VOID *Context\r
221 )\r
222{\r
223 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
224 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
225 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);\r
226 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);\r
227 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);\r
228 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);\r
229 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);\r
230 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);\r
231 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);\r
232 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);\r
233 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);\r
234 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
235 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
236 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
237 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r
238 EfiConvertPointer (0x0, (VOID **) &mNvVariableCache); \r
239}\r
240\r
241\r
242/**\r
243 Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
244\r
245 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
246 When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
247 storage if free size is below the threshold.\r
248\r
249 @param Event Event whose notification function is being invoked.\r
250 @param Context Pointer to the notification function's context.\r
251\r
252**/\r
253VOID\r
254EFIAPI\r
255OnReadyToBoot (\r
256 EFI_EVENT Event,\r
257 VOID *Context\r
258 )\r
259{\r
260 //\r
261 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.\r
262 //\r
263 mEndOfDxe = TRUE;\r
264 ReclaimForOS ();\r
265 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
266 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
267 }\r
268}\r
269\r
270/**\r
271 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
272\r
273 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
274\r
275 @param Event Event whose notification function is being invoked.\r
276 @param Context Pointer to the notification function's context.\r
277\r
278**/\r
279VOID\r
280EFIAPI\r
281OnEndOfDxe (\r
282 EFI_EVENT Event,\r
283 VOID *Context\r
284 )\r
285{\r
286 mEndOfDxe = TRUE;\r
287}\r
288\r
289/**\r
290 Fault Tolerant Write protocol notification event handler.\r
291\r
292 Non-Volatile variable write may needs FTW protocol to reclaim when \r
293 writting variable.\r
294\r
295 @param[in] Event Event whose notification function is being invoked.\r
296 @param[in] Context Pointer to the notification function's context.\r
297 \r
298**/\r
299VOID\r
300EFIAPI\r
301FtwNotificationEvent (\r
302 IN EFI_EVENT Event,\r
303 IN VOID *Context\r
304 )\r
305{\r
306 EFI_STATUS Status;\r
307 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
308 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
309 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
310 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
311 EFI_PHYSICAL_ADDRESS BaseAddress;\r
312 UINT64 Length;\r
313 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
314 UINT64 VariableStoreLength;\r
315\r
316 //\r
317 // Ensure FTW protocol is installed.\r
318 //\r
319 Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r
320 if (EFI_ERROR (Status)) {\r
321 return ;\r
322 }\r
323 \r
324 //\r
325 // Find the proper FVB protocol for variable.\r
326 //\r
327 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
328 if (NvStorageVariableBase == 0) {\r
329 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
330 }\r
331 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
332 if (EFI_ERROR (Status)) {\r
333 return ;\r
334 }\r
335 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
336\r
337 //\r
338 // Mark the variable storage region of the FLASH as RUNTIME.\r
339 //\r
340 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
341 VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r
342 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
343 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
344 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
345\r
346 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
347 if (EFI_ERROR (Status)) {\r
348 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
349 } else {\r
350 Status = gDS->SetMemorySpaceAttributes (\r
351 BaseAddress,\r
352 Length,\r
353 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
354 );\r
355 if (EFI_ERROR (Status)) {\r
356 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
357 }\r
358 }\r
359 \r
360 Status = VariableWriteServiceInitialize ();\r
361 ASSERT_EFI_ERROR (Status);\r
362 \r
363 //\r
364 // Install the Variable Write Architectural protocol.\r
365 //\r
366 Status = gBS->InstallProtocolInterface (\r
367 &mHandle,\r
368 &gEfiVariableWriteArchProtocolGuid, \r
369 EFI_NATIVE_INTERFACE,\r
370 NULL\r
371 );\r
372 ASSERT_EFI_ERROR (Status);\r
373 \r
374 //\r
375 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
376 //\r
377 gBS->CloseEvent (Event);\r
378\r
379}\r
380\r
381\r
382/**\r
383 Variable Driver main entry point. The Variable driver places the 4 EFI\r
384 runtime services in the EFI System Table and installs arch protocols \r
385 for variable read and write services being availible. It also registers\r
386 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
387\r
388 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
389 @param[in] SystemTable A pointer to the EFI System Table.\r
390 \r
391 @retval EFI_SUCCESS Variable service successfully initialized.\r
392\r
393**/\r
394EFI_STATUS\r
395EFIAPI\r
396VariableServiceInitialize (\r
397 IN EFI_HANDLE ImageHandle,\r
398 IN EFI_SYSTEM_TABLE *SystemTable\r
399 )\r
400{\r
401 EFI_STATUS Status;\r
402 EFI_EVENT ReadyToBootEvent;\r
403 EFI_EVENT EndOfDxeEvent;\r
404\r
405 Status = VariableCommonInitialize ();\r
406 ASSERT_EFI_ERROR (Status);\r
407\r
408 Status = gBS->InstallMultipleProtocolInterfaces (\r
409 &mHandle,\r
410 &gEdkiiVariableLockProtocolGuid,\r
411 &mVariableLock,\r
412 NULL\r
413 );\r
414 ASSERT_EFI_ERROR (Status);\r
415\r
416 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r
417 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
418 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r
419 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r
420 \r
421 //\r
422 // Now install the Variable Runtime Architectural protocol on a new handle.\r
423 //\r
424 Status = gBS->InstallProtocolInterface (\r
425 &mHandle,\r
426 &gEfiVariableArchProtocolGuid, \r
427 EFI_NATIVE_INTERFACE,\r
428 NULL\r
429 );\r
430 ASSERT_EFI_ERROR (Status);\r
431\r
432 //\r
433 // Register FtwNotificationEvent () notify function.\r
434 // \r
435 EfiCreateProtocolNotifyEvent (\r
436 &gEfiFaultTolerantWriteProtocolGuid,\r
437 TPL_CALLBACK,\r
438 FtwNotificationEvent,\r
439 (VOID *)SystemTable,\r
440 &mFtwRegistration\r
441 );\r
442\r
443 Status = gBS->CreateEventEx (\r
444 EVT_NOTIFY_SIGNAL,\r
445 TPL_NOTIFY,\r
446 VariableClassAddressChangeEvent,\r
447 NULL,\r
448 &gEfiEventVirtualAddressChangeGuid,\r
449 &mVirtualAddressChangeEvent\r
450 );\r
451 ASSERT_EFI_ERROR (Status);\r
452\r
453 //\r
454 // Register the event handling function to reclaim variable for OS usage.\r
455 //\r
456 Status = EfiCreateEventReadyToBootEx (\r
457 TPL_NOTIFY, \r
458 OnReadyToBoot, \r
459 NULL, \r
460 &ReadyToBootEvent\r
461 );\r
462 ASSERT_EFI_ERROR (Status);\r
463\r
464 //\r
465 // Register the event handling function to set the End Of DXE flag.\r
466 //\r
467 Status = gBS->CreateEventEx (\r
468 EVT_NOTIFY_SIGNAL,\r
469 TPL_NOTIFY,\r
470 OnEndOfDxe,\r
471 NULL,\r
472 &gEfiEndOfDxeEventGroupGuid,\r
473 &EndOfDxeEvent\r
474 );\r
475 ASSERT_EFI_ERROR (Status);\r
476\r
477 return EFI_SUCCESS;\r
478}\r
479\r