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 - 2010, 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
;
26 Return TRUE if ExitBootServices () has been called.
28 @retval TRUE If ExitBootServices () has been called.
35 return EfiAtRuntime ();
40 Initializes a basic mutual exclusion lock.
42 This function initializes a basic mutual exclusion lock to the released state
43 and returns the lock. Each lock provides mutual exclusion access at its task
44 priority level. Since there is no preemption or multiprocessor support in EFI,
45 acquiring the lock only consists of raising to the locks TPL.
46 If Lock is NULL, then ASSERT().
47 If Priority is not a valid TPL value, then ASSERT().
49 @param Lock A pointer to the lock data structure to initialize.
50 @param Priority EFI TPL is associated with the lock.
57 IN OUT EFI_LOCK
*Lock
,
61 return EfiInitializeLock (Lock
, Priority
);
66 Acquires lock only at boot time. Simply returns at runtime.
68 This is a temperary function that will be removed when
69 EfiAcquireLock() in UefiLib can handle the call in UEFI
70 Runtimer driver in RT phase.
71 It calls EfiAcquireLock() at boot time, and simply returns
74 @param Lock A pointer to the lock to acquire.
78 AcquireLockOnlyAtBootTime (
83 EfiAcquireLock (Lock
);
89 Releases lock only at boot time. Simply returns at runtime.
91 This is a temperary function which will be removed when
92 EfiReleaseLock() in UefiLib can handle the call in UEFI
93 Runtimer driver in RT phase.
94 It calls EfiReleaseLock() at boot time and simply returns
97 @param Lock A pointer to the lock to release.
101 ReleaseLockOnlyAtBootTime (
106 EfiReleaseLock (Lock
);
111 Retrive the Fault Tolerent Write protocol interface.
113 @param[out] FtwProtocol The interface of Ftw protocol
115 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
116 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
117 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
122 OUT VOID
**FtwProtocol
128 // Locate Fault Tolerent Write protocol
130 Status
= gBS
->LocateProtocol (
131 &gEfiFaultTolerantWriteProtocolGuid
,
139 Retrive the FVB protocol interface by HANDLE.
141 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
142 reading, writing, and erasing the target block.
143 @param[out] FvBlock The interface of FVB protocol
145 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
146 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
147 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
152 IN EFI_HANDLE FvBlockHandle
,
153 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
157 // To get the FVB protocol interface on the handle
159 return gBS
->HandleProtocol (
161 &gEfiFirmwareVolumeBlockProtocolGuid
,
168 Function returns an array of handles that support the FVB protocol
169 in a buffer allocated from pool.
171 @param[out] NumberHandles The number of handles returned in Buffer.
172 @param[out] Buffer A pointer to the buffer to return the requested
173 array of handles that support FVB protocol.
175 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
176 handles in Buffer was returned in NumberHandles.
177 @retval EFI_NOT_FOUND No FVB handle was found.
178 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
179 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
183 GetFvbCountAndBuffer (
184 OUT UINTN
*NumberHandles
,
185 OUT EFI_HANDLE
**Buffer
191 // Locate all handles of Fvb protocol
193 Status
= gBS
->LocateHandleBuffer (
195 &gEfiFirmwareVolumeBlockProtocolGuid
,
205 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
207 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
208 It convers pointer to new virtual address.
210 @param Event Event whose notification function is being invoked.
211 @param Context Pointer to the notification function's context.
216 VariableClassAddressChangeEvent (
221 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
222 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
223 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
224 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
225 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
226 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
227 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
228 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
229 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
230 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
231 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
232 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
233 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
235 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
240 Notification function of EVT_GROUP_READY_TO_BOOT event group.
242 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
243 When the Boot Manager is about to load and execute a boot option, it reclaims variable
244 storage if free size is below the threshold.
246 @param Event Event whose notification function is being invoked.
247 @param Context Pointer to the notification function's context.
258 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
259 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
265 Fault Tolerant Write protocol notification event handler.
267 Non-Volatile variable write may needs FTW protocol to reclaim when
270 @param[in] Event Event whose notification function is being invoked.
271 @param[in] Context Pointer to the notification function's context.
276 FtwNotificationEvent (
282 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
283 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
284 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
285 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
286 EFI_PHYSICAL_ADDRESS BaseAddress
;
288 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
289 UINT64 VariableStoreLength
;
292 // Ensure FTW protocol is installed.
294 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
295 if (EFI_ERROR (Status
)) {
300 // Find the proper FVB protocol for variable.
302 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
303 if (NvStorageVariableBase
== 0) {
304 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
306 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
307 if (EFI_ERROR (Status
)) {
310 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
313 // Mark the variable storage region of the FLASH as RUNTIME.
315 VariableStoreBase
= mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
;
316 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
317 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
318 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
319 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
321 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
322 if (EFI_ERROR (Status
)) {
323 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
325 Status
= gDS
->SetMemorySpaceAttributes (
328 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
330 if (EFI_ERROR (Status
)) {
331 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
335 Status
= VariableWriteServiceInitialize ();
336 ASSERT_EFI_ERROR (Status
);
339 // Install the Variable Write Architectural protocol.
341 Status
= gBS
->InstallProtocolInterface (
343 &gEfiVariableWriteArchProtocolGuid
,
344 EFI_NATIVE_INTERFACE
,
347 ASSERT_EFI_ERROR (Status
);
350 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
352 gBS
->CloseEvent (Event
);
358 Variable Driver main entry point. The Variable driver places the 4 EFI
359 runtime services in the EFI System Table and installs arch protocols
360 for variable read and write services being availible. It also registers
361 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
363 @param[in] ImageHandle The firmware allocated handle for the EFI image.
364 @param[in] SystemTable A pointer to the EFI System Table.
366 @retval EFI_SUCCESS Variable service successfully initialized.
371 VariableServiceInitialize (
372 IN EFI_HANDLE ImageHandle
,
373 IN EFI_SYSTEM_TABLE
*SystemTable
377 EFI_EVENT ReadyToBootEvent
;
379 Status
= VariableCommonInitialize ();
380 ASSERT_EFI_ERROR (Status
);
382 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
383 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
384 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
385 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
388 // Now install the Variable Runtime Architectural protocol on a new handle.
390 Status
= gBS
->InstallProtocolInterface (
392 &gEfiVariableArchProtocolGuid
,
393 EFI_NATIVE_INTERFACE
,
396 ASSERT_EFI_ERROR (Status
);
399 // Register FtwNotificationEvent () notify function.
401 EfiCreateProtocolNotifyEvent (
402 &gEfiFaultTolerantWriteProtocolGuid
,
404 FtwNotificationEvent
,
409 Status
= gBS
->CreateEventEx (
412 VariableClassAddressChangeEvent
,
414 &gEfiEventVirtualAddressChangeGuid
,
415 &mVirtualAddressChangeEvent
417 ASSERT_EFI_ERROR (Status
);
420 // Register the event handling function to reclaim variable for OS usage.
422 Status
= EfiCreateEventReadyToBootEx (