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 - 2017, 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 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).
36 Record their initial State when variable write service is ready.
41 RecordSecureBootPolicyVarData(
46 Return TRUE if ExitBootServices () has been called.
48 @retval TRUE If ExitBootServices () has been called.
55 return EfiAtRuntime ();
60 Initializes a basic mutual exclusion lock.
62 This function initializes a basic mutual exclusion lock to the released state
63 and returns the lock. Each lock provides mutual exclusion access at its task
64 priority level. Since there is no preemption or multiprocessor support in EFI,
65 acquiring the lock only consists of raising to the locks TPL.
66 If Lock is NULL, then ASSERT().
67 If Priority is not a valid TPL value, then ASSERT().
69 @param Lock A pointer to the lock data structure to initialize.
70 @param Priority EFI TPL is associated with the lock.
77 IN OUT EFI_LOCK
*Lock
,
81 return EfiInitializeLock (Lock
, Priority
);
86 Acquires lock only at boot time. Simply returns at runtime.
88 This is a temperary function that will be removed when
89 EfiAcquireLock() in UefiLib can handle the call in UEFI
90 Runtimer driver in RT phase.
91 It calls EfiAcquireLock() at boot time, and simply returns
94 @param Lock A pointer to the lock to acquire.
98 AcquireLockOnlyAtBootTime (
103 EfiAcquireLock (Lock
);
109 Releases lock only at boot time. Simply returns at runtime.
111 This is a temperary function which will be removed when
112 EfiReleaseLock() in UefiLib can handle the call in UEFI
113 Runtimer driver in RT phase.
114 It calls EfiReleaseLock() at boot time and simply returns
117 @param Lock A pointer to the lock to release.
121 ReleaseLockOnlyAtBootTime (
126 EfiReleaseLock (Lock
);
131 Retrieve the Fault Tolerent Write protocol interface.
133 @param[out] FtwProtocol The interface of Ftw protocol
135 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
136 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
137 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
142 OUT VOID
**FtwProtocol
148 // Locate Fault Tolerent Write protocol
150 Status
= gBS
->LocateProtocol (
151 &gEfiFaultTolerantWriteProtocolGuid
,
159 Retrieve the FVB protocol interface by HANDLE.
161 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
162 reading, writing, and erasing the target block.
163 @param[out] FvBlock The interface of FVB protocol
165 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
166 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
167 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
172 IN EFI_HANDLE FvBlockHandle
,
173 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
177 // To get the FVB protocol interface on the handle
179 return gBS
->HandleProtocol (
181 &gEfiFirmwareVolumeBlockProtocolGuid
,
188 Function returns an array of handles that support the FVB protocol
189 in a buffer allocated from pool.
191 @param[out] NumberHandles The number of handles returned in Buffer.
192 @param[out] Buffer A pointer to the buffer to return the requested
193 array of handles that support FVB protocol.
195 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
196 handles in Buffer was returned in NumberHandles.
197 @retval EFI_NOT_FOUND No FVB handle was found.
198 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
199 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
203 GetFvbCountAndBuffer (
204 OUT UINTN
*NumberHandles
,
205 OUT EFI_HANDLE
**Buffer
211 // Locate all handles of Fvb protocol
213 Status
= gBS
->LocateHandleBuffer (
215 &gEfiFirmwareVolumeBlockProtocolGuid
,
225 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
227 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
228 It convers pointer to new virtual address.
230 @param Event Event whose notification function is being invoked.
231 @param Context Pointer to the notification function's context.
236 VariableClassAddressChangeEvent (
243 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
244 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
245 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
246 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
247 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
248 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
249 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
250 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
251 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
252 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
253 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
254 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
255 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
256 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
257 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
258 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
259 EfiConvertPointer (0x0, (VOID
**) &mNvFvHeaderCache
);
261 if (mAuthContextOut
.AddressPointer
!= NULL
) {
262 for (Index
= 0; Index
< mAuthContextOut
.AddressPointerCount
; Index
++) {
263 EfiConvertPointer (0x0, (VOID
**) mAuthContextOut
.AddressPointer
[Index
]);
267 if (mVarCheckAddressPointer
!= NULL
) {
268 for (Index
= 0; Index
< mVarCheckAddressPointerCount
; Index
++) {
269 EfiConvertPointer (0x0, (VOID
**) mVarCheckAddressPointer
[Index
]);
276 Notification function of EVT_GROUP_READY_TO_BOOT event group.
278 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
279 When the Boot Manager is about to load and execute a boot option, it reclaims variable
280 storage if free size is below the threshold.
282 @param Event Event whose notification function is being invoked.
283 @param Context Pointer to the notification function's context.
295 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
298 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
300 // The initialization for variable quota.
302 InitializeVariableQuota ();
305 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
306 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
307 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
309 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
313 gBS
->CloseEvent (Event
);
317 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
319 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
321 @param Event Event whose notification function is being invoked.
322 @param Context Pointer to the notification function's context.
332 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
334 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
336 // The initialization for variable quota.
338 InitializeVariableQuota ();
339 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
343 gBS
->CloseEvent (Event
);
347 Fault Tolerant Write protocol notification event handler.
349 Non-Volatile variable write may needs FTW protocol to reclaim when
352 @param[in] Event Event whose notification function is being invoked.
353 @param[in] Context Pointer to the notification function's context.
358 FtwNotificationEvent (
364 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
365 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
366 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
367 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
368 EFI_PHYSICAL_ADDRESS BaseAddress
;
370 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
371 UINT64 VariableStoreLength
;
372 UINTN FtwMaxBlockSize
;
375 // Ensure FTW protocol is installed.
377 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
378 if (EFI_ERROR (Status
)) {
382 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
383 if (!EFI_ERROR (Status
)) {
384 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
388 // Find the proper FVB protocol for variable.
390 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
391 if (NvStorageVariableBase
== 0) {
392 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
394 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
395 if (EFI_ERROR (Status
)) {
398 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
401 // Mark the variable storage region of the FLASH as RUNTIME.
403 VariableStoreBase
= NvStorageVariableBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageVariableBase
))->HeaderLength
);
404 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
405 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
406 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
407 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
409 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
410 if (EFI_ERROR (Status
)) {
411 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
413 Status
= gDS
->SetMemorySpaceAttributes (
416 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
418 if (EFI_ERROR (Status
)) {
419 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
423 Status
= VariableWriteServiceInitialize ();
424 if (EFI_ERROR (Status
)) {
425 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
429 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
430 // Secure Boot Policy Variable change. Record their initial value.
432 RecordSecureBootPolicyVarData();
435 // Install the Variable Write Architectural protocol.
437 Status
= gBS
->InstallProtocolInterface (
439 &gEfiVariableWriteArchProtocolGuid
,
440 EFI_NATIVE_INTERFACE
,
443 ASSERT_EFI_ERROR (Status
);
446 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
448 gBS
->CloseEvent (Event
);
454 Variable Driver main entry point. The Variable driver places the 4 EFI
455 runtime services in the EFI System Table and installs arch protocols
456 for variable read and write services being available. It also registers
457 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
459 @param[in] ImageHandle The firmware allocated handle for the EFI image.
460 @param[in] SystemTable A pointer to the EFI System Table.
462 @retval EFI_SUCCESS Variable service successfully initialized.
467 VariableServiceInitialize (
468 IN EFI_HANDLE ImageHandle
,
469 IN EFI_SYSTEM_TABLE
*SystemTable
473 EFI_EVENT ReadyToBootEvent
;
474 EFI_EVENT EndOfDxeEvent
;
476 Status
= VariableCommonInitialize ();
477 ASSERT_EFI_ERROR (Status
);
479 Status
= gBS
->InstallMultipleProtocolInterfaces (
481 &gEdkiiVariableLockProtocolGuid
,
485 ASSERT_EFI_ERROR (Status
);
487 Status
= gBS
->InstallMultipleProtocolInterfaces (
489 &gEdkiiVarCheckProtocolGuid
,
493 ASSERT_EFI_ERROR (Status
);
495 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
496 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
497 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
498 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
501 // Now install the Variable Runtime Architectural protocol on a new handle.
503 Status
= gBS
->InstallProtocolInterface (
505 &gEfiVariableArchProtocolGuid
,
506 EFI_NATIVE_INTERFACE
,
509 ASSERT_EFI_ERROR (Status
);
512 // Register FtwNotificationEvent () notify function.
514 EfiCreateProtocolNotifyEvent (
515 &gEfiFaultTolerantWriteProtocolGuid
,
517 FtwNotificationEvent
,
522 Status
= gBS
->CreateEventEx (
525 VariableClassAddressChangeEvent
,
527 &gEfiEventVirtualAddressChangeGuid
,
528 &mVirtualAddressChangeEvent
530 ASSERT_EFI_ERROR (Status
);
533 // Register the event handling function to reclaim variable for OS usage.
535 Status
= EfiCreateEventReadyToBootEx (
541 ASSERT_EFI_ERROR (Status
);
544 // Register the event handling function to set the End Of DXE flag.
546 Status
= gBS
->CreateEventEx (
551 &gEfiEndOfDxeEventGroupGuid
,
554 ASSERT_EFI_ERROR (Status
);