3 Implement all four UEFI Runtime Variable services for the nonvolatile
4 and volatile storage space and install variable architecture protocol.
6 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 extern VARIABLE_STORE_HEADER
*mNvVariableCache
;
20 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
21 EFI_HANDLE mHandle
= NULL
;
22 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
23 EFI_EVENT mFtwRegistration
= NULL
;
24 extern BOOLEAN mEndOfDxe
;
25 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
28 Return TRUE if ExitBootServices () has been called.
30 @retval TRUE If ExitBootServices () has been called.
37 return EfiAtRuntime ();
42 Initializes a basic mutual exclusion lock.
44 This function initializes a basic mutual exclusion lock to the released state
45 and returns the lock. Each lock provides mutual exclusion access at its task
46 priority level. Since there is no preemption or multiprocessor support in EFI,
47 acquiring the lock only consists of raising to the locks TPL.
48 If Lock is NULL, then ASSERT().
49 If Priority is not a valid TPL value, then ASSERT().
51 @param Lock A pointer to the lock data structure to initialize.
52 @param Priority EFI TPL is associated with the lock.
59 IN OUT EFI_LOCK
*Lock
,
63 return EfiInitializeLock (Lock
, Priority
);
68 Acquires lock only at boot time. Simply returns at runtime.
70 This is a temperary function that will be removed when
71 EfiAcquireLock() in UefiLib can handle the call in UEFI
72 Runtimer driver in RT phase.
73 It calls EfiAcquireLock() at boot time, and simply returns
76 @param Lock A pointer to the lock to acquire.
80 AcquireLockOnlyAtBootTime (
85 EfiAcquireLock (Lock
);
91 Releases lock only at boot time. Simply returns at runtime.
93 This is a temperary function which will be removed when
94 EfiReleaseLock() in UefiLib can handle the call in UEFI
95 Runtimer driver in RT phase.
96 It calls EfiReleaseLock() at boot time and simply returns
99 @param Lock A pointer to the lock to release.
103 ReleaseLockOnlyAtBootTime (
108 EfiReleaseLock (Lock
);
113 Retrive the Fault Tolerent Write protocol interface.
115 @param[out] FtwProtocol The interface of Ftw protocol
117 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
118 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
119 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
124 OUT VOID
**FtwProtocol
130 // Locate Fault Tolerent Write protocol
132 Status
= gBS
->LocateProtocol (
133 &gEfiFaultTolerantWriteProtocolGuid
,
141 Retrive the FVB protocol interface by HANDLE.
143 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
144 reading, writing, and erasing the target block.
145 @param[out] FvBlock The interface of FVB protocol
147 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
148 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
149 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
154 IN EFI_HANDLE FvBlockHandle
,
155 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
159 // To get the FVB protocol interface on the handle
161 return gBS
->HandleProtocol (
163 &gEfiFirmwareVolumeBlockProtocolGuid
,
170 Function returns an array of handles that support the FVB protocol
171 in a buffer allocated from pool.
173 @param[out] NumberHandles The number of handles returned in Buffer.
174 @param[out] Buffer A pointer to the buffer to return the requested
175 array of handles that support FVB protocol.
177 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
178 handles in Buffer was returned in NumberHandles.
179 @retval EFI_NOT_FOUND No FVB handle was found.
180 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
181 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
185 GetFvbCountAndBuffer (
186 OUT UINTN
*NumberHandles
,
187 OUT EFI_HANDLE
**Buffer
193 // Locate all handles of Fvb protocol
195 Status
= gBS
->LocateHandleBuffer (
197 &gEfiFirmwareVolumeBlockProtocolGuid
,
207 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
209 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
210 It convers pointer to new virtual address.
212 @param Event Event whose notification function is being invoked.
213 @param Context Pointer to the notification function's context.
218 VariableClassAddressChangeEvent (
223 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
224 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
225 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
226 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
227 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
228 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
229 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
230 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
231 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
232 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
233 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
235 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
236 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
237 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
238 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
243 Notification function of EVT_GROUP_READY_TO_BOOT event group.
245 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
246 When the Boot Manager is about to load and execute a boot option, it reclaims variable
247 storage if free size is below the threshold.
249 @param Event Event whose notification function is being invoked.
250 @param Context Pointer to the notification function's context.
261 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
265 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
266 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
271 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
273 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
275 @param Event Event whose notification function is being invoked.
276 @param Context Pointer to the notification function's context.
290 Fault Tolerant Write protocol notification event handler.
292 Non-Volatile variable write may needs FTW protocol to reclaim when
295 @param[in] Event Event whose notification function is being invoked.
296 @param[in] Context Pointer to the notification function's context.
301 FtwNotificationEvent (
307 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
308 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
309 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
310 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
311 EFI_PHYSICAL_ADDRESS BaseAddress
;
313 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
314 UINT64 VariableStoreLength
;
317 // Ensure FTW protocol is installed.
319 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
320 if (EFI_ERROR (Status
)) {
325 // Find the proper FVB protocol for variable.
327 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
328 if (NvStorageVariableBase
== 0) {
329 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
331 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
332 if (EFI_ERROR (Status
)) {
335 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
338 // Mark the variable storage region of the FLASH as RUNTIME.
340 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
341 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
342 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
343 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
344 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
346 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
347 if (EFI_ERROR (Status
)) {
348 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
350 Status
= gDS
->SetMemorySpaceAttributes (
353 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
355 if (EFI_ERROR (Status
)) {
356 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
360 Status
= VariableWriteServiceInitialize ();
361 ASSERT_EFI_ERROR (Status
);
364 // Install the Variable Write Architectural protocol.
366 Status
= gBS
->InstallProtocolInterface (
368 &gEfiVariableWriteArchProtocolGuid
,
369 EFI_NATIVE_INTERFACE
,
372 ASSERT_EFI_ERROR (Status
);
375 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
377 gBS
->CloseEvent (Event
);
383 Variable Driver main entry point. The Variable driver places the 4 EFI
384 runtime services in the EFI System Table and installs arch protocols
385 for variable read and write services being availible. It also registers
386 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
388 @param[in] ImageHandle The firmware allocated handle for the EFI image.
389 @param[in] SystemTable A pointer to the EFI System Table.
391 @retval EFI_SUCCESS Variable service successfully initialized.
396 VariableServiceInitialize (
397 IN EFI_HANDLE ImageHandle
,
398 IN EFI_SYSTEM_TABLE
*SystemTable
402 EFI_EVENT ReadyToBootEvent
;
403 EFI_EVENT EndOfDxeEvent
;
405 Status
= VariableCommonInitialize ();
406 ASSERT_EFI_ERROR (Status
);
408 Status
= gBS
->InstallMultipleProtocolInterfaces (
410 &gEdkiiVariableLockProtocolGuid
,
414 ASSERT_EFI_ERROR (Status
);
416 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
417 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
418 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
419 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
422 // Now install the Variable Runtime Architectural protocol on a new handle.
424 Status
= gBS
->InstallProtocolInterface (
426 &gEfiVariableArchProtocolGuid
,
427 EFI_NATIVE_INTERFACE
,
430 ASSERT_EFI_ERROR (Status
);
433 // Register FtwNotificationEvent () notify function.
435 EfiCreateProtocolNotifyEvent (
436 &gEfiFaultTolerantWriteProtocolGuid
,
438 FtwNotificationEvent
,
443 Status
= gBS
->CreateEventEx (
446 VariableClassAddressChangeEvent
,
448 &gEfiEventVirtualAddressChangeGuid
,
449 &mVirtualAddressChangeEvent
451 ASSERT_EFI_ERROR (Status
);
454 // Register the event handling function to reclaim variable for OS usage.
456 Status
= EfiCreateEventReadyToBootEx (
462 ASSERT_EFI_ERROR (Status
);
465 // Register the event handling function to set the End Of DXE flag.
467 Status
= gBS
->CreateEventEx (
472 &gEfiEndOfDxeEventGroupGuid
,
475 ASSERT_EFI_ERROR (Status
);