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 extern BOOLEAN mEnableLocking
;
26 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
29 Return TRUE if ExitBootServices () has been called.
31 @retval TRUE If ExitBootServices () has been called.
38 return EfiAtRuntime ();
43 Initializes a basic mutual exclusion lock.
45 This function initializes a basic mutual exclusion lock to the released state
46 and returns the lock. Each lock provides mutual exclusion access at its task
47 priority level. Since there is no preemption or multiprocessor support in EFI,
48 acquiring the lock only consists of raising to the locks TPL.
49 If Lock is NULL, then ASSERT().
50 If Priority is not a valid TPL value, then ASSERT().
52 @param Lock A pointer to the lock data structure to initialize.
53 @param Priority EFI TPL is associated with the lock.
60 IN OUT EFI_LOCK
*Lock
,
64 return EfiInitializeLock (Lock
, Priority
);
69 Acquires lock only at boot time. Simply returns at runtime.
71 This is a temperary function that will be removed when
72 EfiAcquireLock() in UefiLib can handle the call in UEFI
73 Runtimer driver in RT phase.
74 It calls EfiAcquireLock() at boot time, and simply returns
77 @param Lock A pointer to the lock to acquire.
81 AcquireLockOnlyAtBootTime (
86 EfiAcquireLock (Lock
);
92 Releases lock only at boot time. Simply returns at runtime.
94 This is a temperary function which will be removed when
95 EfiReleaseLock() in UefiLib can handle the call in UEFI
96 Runtimer driver in RT phase.
97 It calls EfiReleaseLock() at boot time and simply returns
100 @param Lock A pointer to the lock to release.
104 ReleaseLockOnlyAtBootTime (
109 EfiReleaseLock (Lock
);
114 Retrive the Fault Tolerent Write protocol interface.
116 @param[out] FtwProtocol The interface of Ftw protocol
118 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
119 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
120 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
125 OUT VOID
**FtwProtocol
131 // Locate Fault Tolerent Write protocol
133 Status
= gBS
->LocateProtocol (
134 &gEfiFaultTolerantWriteProtocolGuid
,
142 Retrive the FVB protocol interface by HANDLE.
144 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
145 reading, writing, and erasing the target block.
146 @param[out] FvBlock The interface of FVB protocol
148 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
149 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
150 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
155 IN EFI_HANDLE FvBlockHandle
,
156 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
160 // To get the FVB protocol interface on the handle
162 return gBS
->HandleProtocol (
164 &gEfiFirmwareVolumeBlockProtocolGuid
,
171 Function returns an array of handles that support the FVB protocol
172 in a buffer allocated from pool.
174 @param[out] NumberHandles The number of handles returned in Buffer.
175 @param[out] Buffer A pointer to the buffer to return the requested
176 array of handles that support FVB protocol.
178 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
179 handles in Buffer was returned in NumberHandles.
180 @retval EFI_NOT_FOUND No FVB handle was found.
181 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
182 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
186 GetFvbCountAndBuffer (
187 OUT UINTN
*NumberHandles
,
188 OUT EFI_HANDLE
**Buffer
194 // Locate all handles of Fvb protocol
196 Status
= gBS
->LocateHandleBuffer (
198 &gEfiFirmwareVolumeBlockProtocolGuid
,
208 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
210 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
211 It convers pointer to new virtual address.
213 @param Event Event whose notification function is being invoked.
214 @param Context Pointer to the notification function's context.
219 VariableClassAddressChangeEvent (
224 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
225 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
226 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
227 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
228 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
229 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
230 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
231 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
232 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
233 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
235 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
236 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
237 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
238 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
239 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
244 Notification function of EVT_GROUP_READY_TO_BOOT event group.
246 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
247 When the Boot Manager is about to load and execute a boot option, it reclaims variable
248 storage if free size is below the threshold.
250 @param Event Event whose notification function is being invoked.
251 @param Context Pointer to the notification function's context.
262 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
266 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
267 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
272 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
274 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
276 @param Event Event whose notification function is being invoked.
277 @param Context Pointer to the notification function's context.
291 Fault Tolerant Write protocol notification event handler.
293 Non-Volatile variable write may needs FTW protocol to reclaim when
296 @param[in] Event Event whose notification function is being invoked.
297 @param[in] Context Pointer to the notification function's context.
302 FtwNotificationEvent (
308 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
309 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
310 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
311 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
312 EFI_PHYSICAL_ADDRESS BaseAddress
;
314 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
315 UINT64 VariableStoreLength
;
318 // Ensure FTW protocol is installed.
320 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
321 if (EFI_ERROR (Status
)) {
326 // Find the proper FVB protocol for variable.
328 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
329 if (NvStorageVariableBase
== 0) {
330 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
332 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
333 if (EFI_ERROR (Status
)) {
336 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
339 // Mark the variable storage region of the FLASH as RUNTIME.
341 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
342 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
343 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
344 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
345 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
347 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
348 if (EFI_ERROR (Status
)) {
349 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
351 Status
= gDS
->SetMemorySpaceAttributes (
354 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
356 if (EFI_ERROR (Status
)) {
357 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
361 Status
= VariableWriteServiceInitialize ();
362 ASSERT_EFI_ERROR (Status
);
365 // Install the Variable Write Architectural protocol.
367 Status
= gBS
->InstallProtocolInterface (
369 &gEfiVariableWriteArchProtocolGuid
,
370 EFI_NATIVE_INTERFACE
,
373 ASSERT_EFI_ERROR (Status
);
376 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
378 gBS
->CloseEvent (Event
);
384 Variable Driver main entry point. The Variable driver places the 4 EFI
385 runtime services in the EFI System Table and installs arch protocols
386 for variable read and write services being availible. It also registers
387 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
389 @param[in] ImageHandle The firmware allocated handle for the EFI image.
390 @param[in] SystemTable A pointer to the EFI System Table.
392 @retval EFI_SUCCESS Variable service successfully initialized.
397 VariableServiceInitialize (
398 IN EFI_HANDLE ImageHandle
,
399 IN EFI_SYSTEM_TABLE
*SystemTable
403 EFI_EVENT ReadyToBootEvent
;
404 EFI_EVENT EndOfDxeEvent
;
406 Status
= VariableCommonInitialize ();
407 ASSERT_EFI_ERROR (Status
);
409 Status
= gBS
->InstallMultipleProtocolInterfaces (
411 &gEdkiiVariableLockProtocolGuid
,
415 ASSERT_EFI_ERROR (Status
);
417 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
418 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
419 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
420 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
423 // Now install the Variable Runtime Architectural protocol on a new handle.
425 Status
= gBS
->InstallProtocolInterface (
427 &gEfiVariableArchProtocolGuid
,
428 EFI_NATIVE_INTERFACE
,
431 ASSERT_EFI_ERROR (Status
);
434 // Register FtwNotificationEvent () notify function.
436 EfiCreateProtocolNotifyEvent (
437 &gEfiFaultTolerantWriteProtocolGuid
,
439 FtwNotificationEvent
,
444 Status
= gBS
->CreateEventEx (
447 VariableClassAddressChangeEvent
,
449 &gEfiEventVirtualAddressChangeGuid
,
450 &mVirtualAddressChangeEvent
452 ASSERT_EFI_ERROR (Status
);
455 // Register the event handling function to reclaim variable for OS usage.
457 Status
= EfiCreateEventReadyToBootEx (
463 ASSERT_EFI_ERROR (Status
);
466 // Register the event handling function to set the End Of DXE flag.
468 Status
= gBS
->CreateEventEx (
473 &gEfiEndOfDxeEventGroupGuid
,
476 ASSERT_EFI_ERROR (Status
);