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 - 2019, 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 EFI_HANDLE mHandle
= NULL
;
21 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
22 EFI_EVENT mFtwRegistration
= NULL
;
23 VOID
***mVarCheckAddressPointer
= NULL
;
24 UINTN mVarCheckAddressPointerCount
= 0;
25 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
26 EDKII_VAR_CHECK_PROTOCOL mVarCheck
= { VarCheckRegisterSetVariableCheckHandler
,
27 VarCheckVariablePropertySet
,
28 VarCheckVariablePropertyGet
};
31 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).
32 Record their initial State when variable write service is ready.
37 RecordSecureBootPolicyVarData(
42 Return TRUE if ExitBootServices () has been called.
44 @retval TRUE If ExitBootServices () has been called.
51 return EfiAtRuntime ();
56 Initializes a basic mutual exclusion lock.
58 This function initializes a basic mutual exclusion lock to the released state
59 and returns the lock. Each lock provides mutual exclusion access at its task
60 priority level. Since there is no preemption or multiprocessor support in EFI,
61 acquiring the lock only consists of raising to the locks TPL.
62 If Lock is NULL, then ASSERT().
63 If Priority is not a valid TPL value, then ASSERT().
65 @param Lock A pointer to the lock data structure to initialize.
66 @param Priority EFI TPL is associated with the lock.
73 IN OUT EFI_LOCK
*Lock
,
77 return EfiInitializeLock (Lock
, Priority
);
82 Acquires lock only at boot time. Simply returns at runtime.
84 This is a temperary function that will be removed when
85 EfiAcquireLock() in UefiLib can handle the call in UEFI
86 Runtimer driver in RT phase.
87 It calls EfiAcquireLock() at boot time, and simply returns
90 @param Lock A pointer to the lock to acquire.
94 AcquireLockOnlyAtBootTime (
99 EfiAcquireLock (Lock
);
105 Releases lock only at boot time. Simply returns at runtime.
107 This is a temperary function which will be removed when
108 EfiReleaseLock() in UefiLib can handle the call in UEFI
109 Runtimer driver in RT phase.
110 It calls EfiReleaseLock() at boot time and simply returns
113 @param Lock A pointer to the lock to release.
117 ReleaseLockOnlyAtBootTime (
122 EfiReleaseLock (Lock
);
127 Retrieve the Fault Tolerent Write protocol interface.
129 @param[out] FtwProtocol The interface of Ftw protocol
131 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
132 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
133 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
138 OUT VOID
**FtwProtocol
144 // Locate Fault Tolerent Write protocol
146 Status
= gBS
->LocateProtocol (
147 &gEfiFaultTolerantWriteProtocolGuid
,
155 Retrieve the FVB protocol interface by HANDLE.
157 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
158 reading, writing, and erasing the target block.
159 @param[out] FvBlock The interface of FVB protocol
161 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
162 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
163 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
168 IN EFI_HANDLE FvBlockHandle
,
169 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
173 // To get the FVB protocol interface on the handle
175 return gBS
->HandleProtocol (
177 &gEfiFirmwareVolumeBlockProtocolGuid
,
184 Function returns an array of handles that support the FVB protocol
185 in a buffer allocated from pool.
187 @param[out] NumberHandles The number of handles returned in Buffer.
188 @param[out] Buffer A pointer to the buffer to return the requested
189 array of handles that support FVB protocol.
191 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
192 handles in Buffer was returned in NumberHandles.
193 @retval EFI_NOT_FOUND No FVB handle was found.
194 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
195 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
199 GetFvbCountAndBuffer (
200 OUT UINTN
*NumberHandles
,
201 OUT EFI_HANDLE
**Buffer
207 // Locate all handles of Fvb protocol
209 Status
= gBS
->LocateHandleBuffer (
211 &gEfiFirmwareVolumeBlockProtocolGuid
,
221 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
223 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
224 It convers pointer to new virtual address.
226 @param Event Event whose notification function is being invoked.
227 @param Context Pointer to the notification function's context.
232 VariableClassAddressChangeEvent (
239 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
240 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
241 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
242 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
243 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
244 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
245 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
246 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
247 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
249 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
250 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
251 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
252 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
253 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
254 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
255 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
256 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
257 EfiConvertPointer (0x0, (VOID
**) &mNvFvHeaderCache
);
259 if (mAuthContextOut
.AddressPointer
!= NULL
) {
260 for (Index
= 0; Index
< mAuthContextOut
.AddressPointerCount
; Index
++) {
261 EfiConvertPointer (0x0, (VOID
**) mAuthContextOut
.AddressPointer
[Index
]);
265 if (mVarCheckAddressPointer
!= NULL
) {
266 for (Index
= 0; Index
< mVarCheckAddressPointerCount
; Index
++) {
267 EfiConvertPointer (0x0, (VOID
**) mVarCheckAddressPointer
[Index
]);
274 Notification function of EVT_GROUP_READY_TO_BOOT event group.
276 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
277 When the Boot Manager is about to load and execute a boot option, it reclaims variable
278 storage if free size is below the threshold.
280 @param Event Event whose notification function is being invoked.
281 @param Context Pointer to the notification function's context.
292 MorLockInitAtEndOfDxe ();
294 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
297 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
299 // The initialization for variable quota.
301 InitializeVariableQuota ();
304 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
305 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
306 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
308 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
312 gBS
->CloseEvent (Event
);
316 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
318 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
320 @param Event Event whose notification function is being invoked.
321 @param Context Pointer to the notification function's context.
331 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
332 MorLockInitAtEndOfDxe ();
334 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
336 // The initialization for variable quota.
338 InitializeVariableQuota ();
339 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
343 gBS
->CloseEvent (Event
);
347 Initializes variable write service for DXE.
351 VariableWriteServiceInitializeDxe (
357 Status
= VariableWriteServiceInitialize ();
358 if (EFI_ERROR (Status
)) {
359 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
363 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
364 // Secure Boot Policy Variable change. Record their initial value.
366 RecordSecureBootPolicyVarData();
369 // Install the Variable Write Architectural protocol.
371 Status
= gBS
->InstallProtocolInterface (
373 &gEfiVariableWriteArchProtocolGuid
,
374 EFI_NATIVE_INTERFACE
,
377 ASSERT_EFI_ERROR (Status
);
381 Fault Tolerant Write protocol notification event handler.
383 Non-Volatile variable write may needs FTW protocol to reclaim when
386 @param[in] Event Event whose notification function is being invoked.
387 @param[in] Context Pointer to the notification function's context.
392 FtwNotificationEvent (
398 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
399 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
400 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
401 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
402 EFI_PHYSICAL_ADDRESS BaseAddress
;
404 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
405 UINT64 VariableStoreLength
;
406 UINTN FtwMaxBlockSize
;
409 // Ensure FTW protocol is installed.
411 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
412 if (EFI_ERROR (Status
)) {
416 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
417 if (!EFI_ERROR (Status
)) {
418 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
421 NvStorageVariableBase
= NV_STORAGE_VARIABLE_BASE
;
422 VariableStoreBase
= NvStorageVariableBase
+ mNvFvHeaderCache
->HeaderLength
;
425 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
427 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
430 // Find the proper FVB protocol for variable.
432 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
433 if (EFI_ERROR (Status
)) {
436 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
439 // Mark the variable storage region of the FLASH as RUNTIME.
441 VariableStoreLength
= mNvVariableCache
->Size
;
442 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
443 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
444 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
446 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
447 if (EFI_ERROR (Status
)) {
448 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
450 if ((GcdDescriptor
.Attributes
& EFI_MEMORY_RUNTIME
) == 0) {
451 Status
= gDS
->SetMemorySpaceAttributes (
454 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
456 if (EFI_ERROR (Status
)) {
457 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
463 // Initializes variable write service after FTW was ready.
465 VariableWriteServiceInitializeDxe ();
468 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
470 gBS
->CloseEvent (Event
);
476 Variable Driver main entry point. The Variable driver places the 4 EFI
477 runtime services in the EFI System Table and installs arch protocols
478 for variable read and write services being available. It also registers
479 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
481 @param[in] ImageHandle The firmware allocated handle for the EFI image.
482 @param[in] SystemTable A pointer to the EFI System Table.
484 @retval EFI_SUCCESS Variable service successfully initialized.
489 VariableServiceInitialize (
490 IN EFI_HANDLE ImageHandle
,
491 IN EFI_SYSTEM_TABLE
*SystemTable
495 EFI_EVENT ReadyToBootEvent
;
496 EFI_EVENT EndOfDxeEvent
;
498 Status
= VariableCommonInitialize ();
499 ASSERT_EFI_ERROR (Status
);
501 Status
= gBS
->InstallMultipleProtocolInterfaces (
503 &gEdkiiVariableLockProtocolGuid
,
507 ASSERT_EFI_ERROR (Status
);
509 Status
= gBS
->InstallMultipleProtocolInterfaces (
511 &gEdkiiVarCheckProtocolGuid
,
515 ASSERT_EFI_ERROR (Status
);
517 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
518 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
519 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
520 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
523 // Now install the Variable Runtime Architectural protocol on a new handle.
525 Status
= gBS
->InstallProtocolInterface (
527 &gEfiVariableArchProtocolGuid
,
528 EFI_NATIVE_INTERFACE
,
531 ASSERT_EFI_ERROR (Status
);
533 if (!PcdGetBool (PcdEmuVariableNvModeEnable
)) {
535 // Register FtwNotificationEvent () notify function.
537 EfiCreateProtocolNotifyEvent (
538 &gEfiFaultTolerantWriteProtocolGuid
,
540 FtwNotificationEvent
,
546 // Emulated non-volatile variable mode does not depend on FVB and FTW.
548 VariableWriteServiceInitializeDxe ();
551 Status
= gBS
->CreateEventEx (
554 VariableClassAddressChangeEvent
,
556 &gEfiEventVirtualAddressChangeGuid
,
557 &mVirtualAddressChangeEvent
559 ASSERT_EFI_ERROR (Status
);
562 // Register the event handling function to reclaim variable for OS usage.
564 Status
= EfiCreateEventReadyToBootEx (
570 ASSERT_EFI_ERROR (Status
);
573 // Register the event handling function to set the End Of DXE flag.
575 Status
= gBS
->CreateEventEx (
580 &gEfiEndOfDxeEventGroupGuid
,
583 ASSERT_EFI_ERROR (Status
);