2 Implement all four UEFI Runtime Variable services for the nonvolatile
3 and volatile storage space and install variable architecture protocol.
5 Copyright (C) 2013, Red Hat, Inc.
6 Copyright (c) 2006 - 2015, 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 VOID
***mVarCheckAddressPointer
= NULL
;
26 UINTN mVarCheckAddressPointerCount
= 0;
27 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
28 EDKII_VAR_CHECK_PROTOCOL mVarCheck
= { VarCheckRegisterSetVariableCheckHandler
,
29 VarCheckVariablePropertySet
,
30 VarCheckVariablePropertyGet
};
33 Return TRUE if ExitBootServices () has been called.
35 @retval TRUE If ExitBootServices () has been called.
42 return EfiAtRuntime ();
47 Initializes a basic mutual exclusion lock.
49 This function initializes a basic mutual exclusion lock to the released state
50 and returns the lock. Each lock provides mutual exclusion access at its task
51 priority level. Since there is no preemption or multiprocessor support in EFI,
52 acquiring the lock only consists of raising to the locks TPL.
53 If Lock is NULL, then ASSERT().
54 If Priority is not a valid TPL value, then ASSERT().
56 @param Lock A pointer to the lock data structure to initialize.
57 @param Priority EFI TPL is associated with the lock.
64 IN OUT EFI_LOCK
*Lock
,
68 return EfiInitializeLock (Lock
, Priority
);
73 Acquires lock only at boot time. Simply returns at runtime.
75 This is a temperary function that will be removed when
76 EfiAcquireLock() in UefiLib can handle the call in UEFI
77 Runtimer driver in RT phase.
78 It calls EfiAcquireLock() at boot time, and simply returns
81 @param Lock A pointer to the lock to acquire.
85 AcquireLockOnlyAtBootTime (
90 EfiAcquireLock (Lock
);
96 Releases lock only at boot time. Simply returns at runtime.
98 This is a temperary function which will be removed when
99 EfiReleaseLock() in UefiLib can handle the call in UEFI
100 Runtimer driver in RT phase.
101 It calls EfiReleaseLock() at boot time and simply returns
104 @param Lock A pointer to the lock to release.
108 ReleaseLockOnlyAtBootTime (
113 EfiReleaseLock (Lock
);
118 Retrive the Fault Tolerent Write protocol interface.
120 @param[out] FtwProtocol The interface of Ftw protocol
122 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
123 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
124 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
129 OUT VOID
**FtwProtocol
135 // Locate Fault Tolerent Write protocol
137 Status
= gBS
->LocateProtocol (
138 &gEfiFaultTolerantWriteProtocolGuid
,
146 Retrive the FVB protocol interface by HANDLE.
148 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
149 reading, writing, and erasing the target block.
150 @param[out] FvBlock The interface of FVB protocol
152 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
153 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
154 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
159 IN EFI_HANDLE FvBlockHandle
,
160 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
164 // To get the FVB protocol interface on the handle
166 return gBS
->HandleProtocol (
168 &gEfiFirmwareVolumeBlockProtocolGuid
,
175 Function returns an array of handles that support the FVB protocol
176 in a buffer allocated from pool.
178 @param[out] NumberHandles The number of handles returned in Buffer.
179 @param[out] Buffer A pointer to the buffer to return the requested
180 array of handles that support FVB protocol.
182 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
183 handles in Buffer was returned in NumberHandles.
184 @retval EFI_NOT_FOUND No FVB handle was found.
185 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
186 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
190 GetFvbCountAndBuffer (
191 OUT UINTN
*NumberHandles
,
192 OUT EFI_HANDLE
**Buffer
198 // Locate all handles of Fvb protocol
200 Status
= gBS
->LocateHandleBuffer (
202 &gEfiFirmwareVolumeBlockProtocolGuid
,
212 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
214 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
215 It convers pointer to new virtual address.
217 @param Event Event whose notification function is being invoked.
218 @param Context Pointer to the notification function's context.
223 VariableClassAddressChangeEvent (
230 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
231 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
232 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
233 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
235 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
236 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
237 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
238 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
239 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
240 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
241 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
242 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
243 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
244 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
245 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
247 if (mAuthContextOut
.AddressPointer
!= NULL
) {
248 for (Index
= 0; Index
< mAuthContextOut
.AddressPointerCount
; Index
++) {
249 EfiConvertPointer (0x0, (VOID
**) mAuthContextOut
.AddressPointer
[Index
]);
253 if (mVarCheckAddressPointer
!= NULL
) {
254 for (Index
= 0; Index
< mVarCheckAddressPointerCount
; Index
++) {
255 EfiConvertPointer (0x0, (VOID
**) mVarCheckAddressPointer
[Index
]);
262 Notification function of EVT_GROUP_READY_TO_BOOT event group.
264 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
265 When the Boot Manager is about to load and execute a boot option, it reclaims variable
266 storage if free size is below the threshold.
268 @param Event Event whose notification function is being invoked.
269 @param Context Pointer to the notification function's context.
281 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
284 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
286 // The initialization for variable quota.
288 InitializeVariableQuota ();
291 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
292 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
293 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
295 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
299 gBS
->CloseEvent (Event
);
303 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
305 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
307 @param Event Event whose notification function is being invoked.
308 @param Context Pointer to the notification function's context.
318 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
320 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
322 // The initialization for variable quota.
324 InitializeVariableQuota ();
325 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
329 gBS
->CloseEvent (Event
);
333 Fault Tolerant Write protocol notification event handler.
335 Non-Volatile variable write may needs FTW protocol to reclaim when
338 @param[in] Event Event whose notification function is being invoked.
339 @param[in] Context Pointer to the notification function's context.
344 FtwNotificationEvent (
350 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
351 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
352 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
353 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
354 EFI_PHYSICAL_ADDRESS BaseAddress
;
356 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
357 UINT64 VariableStoreLength
;
358 UINTN FtwMaxBlockSize
;
361 // Ensure FTW protocol is installed.
363 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
364 if (EFI_ERROR (Status
)) {
368 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
369 if (!EFI_ERROR (Status
)) {
370 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
374 // Find the proper FVB protocol for variable.
376 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
377 if (NvStorageVariableBase
== 0) {
378 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
380 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
381 if (EFI_ERROR (Status
)) {
384 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
387 // Mark the variable storage region of the FLASH as RUNTIME.
389 VariableStoreBase
= NvStorageVariableBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageVariableBase
))->HeaderLength
);
390 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
391 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
392 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
393 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
395 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
396 if (EFI_ERROR (Status
)) {
397 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
399 Status
= gDS
->SetMemorySpaceAttributes (
402 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
404 if (EFI_ERROR (Status
)) {
405 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
409 Status
= VariableWriteServiceInitialize ();
410 if (EFI_ERROR (Status
)) {
411 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
415 // Install the Variable Write Architectural protocol.
417 Status
= gBS
->InstallProtocolInterface (
419 &gEfiVariableWriteArchProtocolGuid
,
420 EFI_NATIVE_INTERFACE
,
423 ASSERT_EFI_ERROR (Status
);
426 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
428 gBS
->CloseEvent (Event
);
434 Variable Driver main entry point. The Variable driver places the 4 EFI
435 runtime services in the EFI System Table and installs arch protocols
436 for variable read and write services being available. It also registers
437 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
439 @param[in] ImageHandle The firmware allocated handle for the EFI image.
440 @param[in] SystemTable A pointer to the EFI System Table.
442 @retval EFI_SUCCESS Variable service successfully initialized.
447 VariableServiceInitialize (
448 IN EFI_HANDLE ImageHandle
,
449 IN EFI_SYSTEM_TABLE
*SystemTable
453 EFI_EVENT ReadyToBootEvent
;
454 EFI_EVENT EndOfDxeEvent
;
456 Status
= VariableCommonInitialize ();
457 ASSERT_EFI_ERROR (Status
);
459 Status
= gBS
->InstallMultipleProtocolInterfaces (
461 &gEdkiiVariableLockProtocolGuid
,
465 ASSERT_EFI_ERROR (Status
);
467 Status
= gBS
->InstallMultipleProtocolInterfaces (
469 &gEdkiiVarCheckProtocolGuid
,
473 ASSERT_EFI_ERROR (Status
);
475 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
476 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
477 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
478 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
481 // Now install the Variable Runtime Architectural protocol on a new handle.
483 Status
= gBS
->InstallProtocolInterface (
485 &gEfiVariableArchProtocolGuid
,
486 EFI_NATIVE_INTERFACE
,
489 ASSERT_EFI_ERROR (Status
);
492 // Register FtwNotificationEvent () notify function.
494 EfiCreateProtocolNotifyEvent (
495 &gEfiFaultTolerantWriteProtocolGuid
,
497 FtwNotificationEvent
,
502 Status
= gBS
->CreateEventEx (
505 VariableClassAddressChangeEvent
,
507 &gEfiEventVirtualAddressChangeGuid
,
508 &mVirtualAddressChangeEvent
510 ASSERT_EFI_ERROR (Status
);
513 // Register the event handling function to reclaim variable for OS usage.
515 Status
= EfiCreateEventReadyToBootEx (
521 ASSERT_EFI_ERROR (Status
);
524 // Register the event handling function to set the End Of DXE flag.
526 Status
= gBS
->CreateEventEx (
531 &gEfiEndOfDxeEventGroupGuid
,
534 ASSERT_EFI_ERROR (Status
);