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 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 extern VARIABLE_STORE_HEADER
*mNvVariableCache
;
21 extern EFI_FIRMWARE_VOLUME_HEADER
*mNvFvHeaderCache
;
22 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
23 EFI_HANDLE mHandle
= NULL
;
24 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
25 EFI_EVENT mFtwRegistration
= NULL
;
26 extern BOOLEAN mEndOfDxe
;
27 VOID
***mVarCheckAddressPointer
= NULL
;
28 UINTN mVarCheckAddressPointerCount
= 0;
29 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
30 EDKII_VAR_CHECK_PROTOCOL mVarCheck
= { VarCheckRegisterSetVariableCheckHandler
,
31 VarCheckVariablePropertySet
,
32 VarCheckVariablePropertyGet
};
35 Return TRUE if ExitBootServices () has been called.
37 @retval TRUE If ExitBootServices () has been called.
44 return EfiAtRuntime ();
49 Initializes a basic mutual exclusion lock.
51 This function initializes a basic mutual exclusion lock to the released state
52 and returns the lock. Each lock provides mutual exclusion access at its task
53 priority level. Since there is no preemption or multiprocessor support in EFI,
54 acquiring the lock only consists of raising to the locks TPL.
55 If Lock is NULL, then ASSERT().
56 If Priority is not a valid TPL value, then ASSERT().
58 @param Lock A pointer to the lock data structure to initialize.
59 @param Priority EFI TPL is associated with the lock.
66 IN OUT EFI_LOCK
*Lock
,
70 return EfiInitializeLock (Lock
, Priority
);
75 Acquires lock only at boot time. Simply returns at runtime.
77 This is a temperary function that will be removed when
78 EfiAcquireLock() in UefiLib can handle the call in UEFI
79 Runtimer driver in RT phase.
80 It calls EfiAcquireLock() at boot time, and simply returns
83 @param Lock A pointer to the lock to acquire.
87 AcquireLockOnlyAtBootTime (
92 EfiAcquireLock (Lock
);
98 Releases lock only at boot time. Simply returns at runtime.
100 This is a temperary function which will be removed when
101 EfiReleaseLock() in UefiLib can handle the call in UEFI
102 Runtimer driver in RT phase.
103 It calls EfiReleaseLock() at boot time and simply returns
106 @param Lock A pointer to the lock to release.
110 ReleaseLockOnlyAtBootTime (
115 EfiReleaseLock (Lock
);
120 Retrive the Fault Tolerent Write protocol interface.
122 @param[out] FtwProtocol The interface of Ftw protocol
124 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
125 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
126 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
131 OUT VOID
**FtwProtocol
137 // Locate Fault Tolerent Write protocol
139 Status
= gBS
->LocateProtocol (
140 &gEfiFaultTolerantWriteProtocolGuid
,
148 Retrive the FVB protocol interface by HANDLE.
150 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
151 reading, writing, and erasing the target block.
152 @param[out] FvBlock The interface of FVB protocol
154 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
155 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
156 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
161 IN EFI_HANDLE FvBlockHandle
,
162 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
166 // To get the FVB protocol interface on the handle
168 return gBS
->HandleProtocol (
170 &gEfiFirmwareVolumeBlockProtocolGuid
,
177 Function returns an array of handles that support the FVB protocol
178 in a buffer allocated from pool.
180 @param[out] NumberHandles The number of handles returned in Buffer.
181 @param[out] Buffer A pointer to the buffer to return the requested
182 array of handles that support FVB protocol.
184 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
185 handles in Buffer was returned in NumberHandles.
186 @retval EFI_NOT_FOUND No FVB handle was found.
187 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
188 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
192 GetFvbCountAndBuffer (
193 OUT UINTN
*NumberHandles
,
194 OUT EFI_HANDLE
**Buffer
200 // Locate all handles of Fvb protocol
202 Status
= gBS
->LocateHandleBuffer (
204 &gEfiFirmwareVolumeBlockProtocolGuid
,
214 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
216 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
217 It convers pointer to new virtual address.
219 @param Event Event whose notification function is being invoked.
220 @param Context Pointer to the notification function's context.
225 VariableClassAddressChangeEvent (
232 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
233 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
235 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
236 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
237 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
238 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
239 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
240 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
241 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
242 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
243 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
244 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
245 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
246 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
247 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
248 EfiConvertPointer (0x0, (VOID
**) &mNvFvHeaderCache
);
250 if (mAuthContextOut
.AddressPointer
!= NULL
) {
251 for (Index
= 0; Index
< mAuthContextOut
.AddressPointerCount
; Index
++) {
252 EfiConvertPointer (0x0, (VOID
**) mAuthContextOut
.AddressPointer
[Index
]);
256 if (mVarCheckAddressPointer
!= NULL
) {
257 for (Index
= 0; Index
< mVarCheckAddressPointerCount
; Index
++) {
258 EfiConvertPointer (0x0, (VOID
**) mVarCheckAddressPointer
[Index
]);
265 Notification function of EVT_GROUP_READY_TO_BOOT event group.
267 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
268 When the Boot Manager is about to load and execute a boot option, it reclaims variable
269 storage if free size is below the threshold.
271 @param Event Event whose notification function is being invoked.
272 @param Context Pointer to the notification function's context.
284 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
287 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
289 // The initialization for variable quota.
291 InitializeVariableQuota ();
294 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
295 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
296 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
298 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
302 gBS
->CloseEvent (Event
);
306 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
308 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
310 @param Event Event whose notification function is being invoked.
311 @param Context Pointer to the notification function's context.
321 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
323 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
325 // The initialization for variable quota.
327 InitializeVariableQuota ();
328 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
332 gBS
->CloseEvent (Event
);
336 Fault Tolerant Write protocol notification event handler.
338 Non-Volatile variable write may needs FTW protocol to reclaim when
341 @param[in] Event Event whose notification function is being invoked.
342 @param[in] Context Pointer to the notification function's context.
347 FtwNotificationEvent (
353 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
354 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
355 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
356 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
357 EFI_PHYSICAL_ADDRESS BaseAddress
;
359 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
360 UINT64 VariableStoreLength
;
361 UINTN FtwMaxBlockSize
;
364 // Ensure FTW protocol is installed.
366 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
367 if (EFI_ERROR (Status
)) {
371 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
372 if (!EFI_ERROR (Status
)) {
373 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
377 // Find the proper FVB protocol for variable.
379 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
380 if (NvStorageVariableBase
== 0) {
381 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
383 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
384 if (EFI_ERROR (Status
)) {
387 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
390 // Mark the variable storage region of the FLASH as RUNTIME.
392 VariableStoreBase
= NvStorageVariableBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageVariableBase
))->HeaderLength
);
393 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
394 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
395 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
396 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
398 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
399 if (EFI_ERROR (Status
)) {
400 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
402 Status
= gDS
->SetMemorySpaceAttributes (
405 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
407 if (EFI_ERROR (Status
)) {
408 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
412 Status
= VariableWriteServiceInitialize ();
413 if (EFI_ERROR (Status
)) {
414 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
418 // Install the Variable Write Architectural protocol.
420 Status
= gBS
->InstallProtocolInterface (
422 &gEfiVariableWriteArchProtocolGuid
,
423 EFI_NATIVE_INTERFACE
,
426 ASSERT_EFI_ERROR (Status
);
429 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
431 gBS
->CloseEvent (Event
);
437 Variable Driver main entry point. The Variable driver places the 4 EFI
438 runtime services in the EFI System Table and installs arch protocols
439 for variable read and write services being available. It also registers
440 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
442 @param[in] ImageHandle The firmware allocated handle for the EFI image.
443 @param[in] SystemTable A pointer to the EFI System Table.
445 @retval EFI_SUCCESS Variable service successfully initialized.
450 VariableServiceInitialize (
451 IN EFI_HANDLE ImageHandle
,
452 IN EFI_SYSTEM_TABLE
*SystemTable
456 EFI_EVENT ReadyToBootEvent
;
457 EFI_EVENT EndOfDxeEvent
;
459 Status
= VariableCommonInitialize ();
460 ASSERT_EFI_ERROR (Status
);
462 Status
= gBS
->InstallMultipleProtocolInterfaces (
464 &gEdkiiVariableLockProtocolGuid
,
468 ASSERT_EFI_ERROR (Status
);
470 Status
= gBS
->InstallMultipleProtocolInterfaces (
472 &gEdkiiVarCheckProtocolGuid
,
476 ASSERT_EFI_ERROR (Status
);
478 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
479 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
480 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
481 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
484 // Now install the Variable Runtime Architectural protocol on a new handle.
486 Status
= gBS
->InstallProtocolInterface (
488 &gEfiVariableArchProtocolGuid
,
489 EFI_NATIVE_INTERFACE
,
492 ASSERT_EFI_ERROR (Status
);
495 // Register FtwNotificationEvent () notify function.
497 EfiCreateProtocolNotifyEvent (
498 &gEfiFaultTolerantWriteProtocolGuid
,
500 FtwNotificationEvent
,
505 Status
= gBS
->CreateEventEx (
508 VariableClassAddressChangeEvent
,
510 &gEfiEventVirtualAddressChangeGuid
,
511 &mVirtualAddressChangeEvent
513 ASSERT_EFI_ERROR (Status
);
516 // Register the event handling function to reclaim variable for OS usage.
518 Status
= EfiCreateEventReadyToBootEx (
524 ASSERT_EFI_ERROR (Status
);
527 // Register the event handling function to set the End Of DXE flag.
529 Status
= gBS
->CreateEventEx (
534 &gEfiEndOfDxeEventGroupGuid
,
537 ASSERT_EFI_ERROR (Status
);