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 SPDX-License-Identifier: BSD-2-Clause-Patent
14 EFI_HANDLE mHandle
= NULL
;
15 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
16 EFI_EVENT mFtwRegistration
= NULL
;
17 VOID
***mVarCheckAddressPointer
= NULL
;
18 UINTN mVarCheckAddressPointerCount
= 0;
19 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
20 EDKII_VAR_CHECK_PROTOCOL mVarCheck
= { VarCheckRegisterSetVariableCheckHandler
,
21 VarCheckVariablePropertySet
,
22 VarCheckVariablePropertyGet
};
25 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).
26 Record their initial State when variable write service is ready.
31 RecordSecureBootPolicyVarData(
36 Return TRUE if ExitBootServices () has been called.
38 @retval TRUE If ExitBootServices () has been called.
45 return EfiAtRuntime ();
50 Initializes a basic mutual exclusion lock.
52 This function initializes a basic mutual exclusion lock to the released state
53 and returns the lock. Each lock provides mutual exclusion access at its task
54 priority level. Since there is no preemption or multiprocessor support in EFI,
55 acquiring the lock only consists of raising to the locks TPL.
56 If Lock is NULL, then ASSERT().
57 If Priority is not a valid TPL value, then ASSERT().
59 @param Lock A pointer to the lock data structure to initialize.
60 @param Priority EFI TPL is associated with the lock.
67 IN OUT EFI_LOCK
*Lock
,
71 return EfiInitializeLock (Lock
, Priority
);
76 Acquires lock only at boot time. Simply returns at runtime.
78 This is a temperary function that will be removed when
79 EfiAcquireLock() in UefiLib can handle the call in UEFI
80 Runtimer driver in RT phase.
81 It calls EfiAcquireLock() at boot time, and simply returns
84 @param Lock A pointer to the lock to acquire.
88 AcquireLockOnlyAtBootTime (
93 EfiAcquireLock (Lock
);
99 Releases lock only at boot time. Simply returns at runtime.
101 This is a temperary function which will be removed when
102 EfiReleaseLock() in UefiLib can handle the call in UEFI
103 Runtimer driver in RT phase.
104 It calls EfiReleaseLock() at boot time and simply returns
107 @param Lock A pointer to the lock to release.
111 ReleaseLockOnlyAtBootTime (
116 EfiReleaseLock (Lock
);
121 Retrieve the Fault Tolerent Write protocol interface.
123 @param[out] FtwProtocol The interface of Ftw protocol
125 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
126 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
127 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
132 OUT VOID
**FtwProtocol
138 // Locate Fault Tolerent Write protocol
140 Status
= gBS
->LocateProtocol (
141 &gEfiFaultTolerantWriteProtocolGuid
,
149 Retrieve the FVB protocol interface by HANDLE.
151 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
152 reading, writing, and erasing the target block.
153 @param[out] FvBlock The interface of FVB protocol
155 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
156 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
157 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
162 IN EFI_HANDLE FvBlockHandle
,
163 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
167 // To get the FVB protocol interface on the handle
169 return gBS
->HandleProtocol (
171 &gEfiFirmwareVolumeBlockProtocolGuid
,
178 Function returns an array of handles that support the FVB protocol
179 in a buffer allocated from pool.
181 @param[out] NumberHandles The number of handles returned in Buffer.
182 @param[out] Buffer A pointer to the buffer to return the requested
183 array of handles that support FVB protocol.
185 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
186 handles in Buffer was returned in NumberHandles.
187 @retval EFI_NOT_FOUND No FVB handle was found.
188 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
189 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
193 GetFvbCountAndBuffer (
194 OUT UINTN
*NumberHandles
,
195 OUT EFI_HANDLE
**Buffer
201 // Locate all handles of Fvb protocol
203 Status
= gBS
->LocateHandleBuffer (
205 &gEfiFirmwareVolumeBlockProtocolGuid
,
215 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
217 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
218 It convers pointer to new virtual address.
220 @param Event Event whose notification function is being invoked.
221 @param Context Pointer to the notification function's context.
226 VariableClassAddressChangeEvent (
233 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
235 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
236 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
237 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
238 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
239 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
240 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
241 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
243 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
244 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
245 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
246 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
247 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
248 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
249 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
250 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
251 EfiConvertPointer (0x0, (VOID
**) &mNvFvHeaderCache
);
253 if (mAuthContextOut
.AddressPointer
!= NULL
) {
254 for (Index
= 0; Index
< mAuthContextOut
.AddressPointerCount
; Index
++) {
255 EfiConvertPointer (0x0, (VOID
**) mAuthContextOut
.AddressPointer
[Index
]);
259 if (mVarCheckAddressPointer
!= NULL
) {
260 for (Index
= 0; Index
< mVarCheckAddressPointerCount
; Index
++) {
261 EfiConvertPointer (0x0, (VOID
**) mVarCheckAddressPointer
[Index
]);
268 Notification function of EVT_GROUP_READY_TO_BOOT event group.
270 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
271 When the Boot Manager is about to load and execute a boot option, it reclaims variable
272 storage if free size is below the threshold.
274 @param Event Event whose notification function is being invoked.
275 @param Context Pointer to the notification function's context.
286 MorLockInitAtEndOfDxe ();
288 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
291 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
293 // The initialization for variable quota.
295 InitializeVariableQuota ();
298 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
299 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
300 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
302 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
306 gBS
->CloseEvent (Event
);
310 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
312 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
314 @param Event Event whose notification function is being invoked.
315 @param Context Pointer to the notification function's context.
325 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
326 MorLockInitAtEndOfDxe ();
328 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
330 // The initialization for variable quota.
332 InitializeVariableQuota ();
333 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
337 gBS
->CloseEvent (Event
);
341 Initializes variable write service for DXE.
345 VariableWriteServiceInitializeDxe (
351 Status
= VariableWriteServiceInitialize ();
352 if (EFI_ERROR (Status
)) {
353 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
357 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
358 // Secure Boot Policy Variable change. Record their initial value.
360 RecordSecureBootPolicyVarData();
363 // Install the Variable Write Architectural protocol.
365 Status
= gBS
->InstallProtocolInterface (
367 &gEfiVariableWriteArchProtocolGuid
,
368 EFI_NATIVE_INTERFACE
,
371 ASSERT_EFI_ERROR (Status
);
375 Fault Tolerant Write protocol notification event handler.
377 Non-Volatile variable write may needs FTW protocol to reclaim when
380 @param[in] Event Event whose notification function is being invoked.
381 @param[in] Context Pointer to the notification function's context.
386 FtwNotificationEvent (
392 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
393 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
394 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
395 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
396 EFI_PHYSICAL_ADDRESS BaseAddress
;
398 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
399 UINT64 VariableStoreLength
;
400 UINTN FtwMaxBlockSize
;
403 // Ensure FTW protocol is installed.
405 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
406 if (EFI_ERROR (Status
)) {
410 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
411 if (!EFI_ERROR (Status
)) {
412 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
415 NvStorageVariableBase
= NV_STORAGE_VARIABLE_BASE
;
416 VariableStoreBase
= NvStorageVariableBase
+ mNvFvHeaderCache
->HeaderLength
;
419 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
421 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
424 // Find the proper FVB protocol for variable.
426 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
427 if (EFI_ERROR (Status
)) {
430 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
433 // Mark the variable storage region of the FLASH as RUNTIME.
435 VariableStoreLength
= mNvVariableCache
->Size
;
436 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
437 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
438 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
440 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
441 if (EFI_ERROR (Status
)) {
442 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
444 if ((GcdDescriptor
.Attributes
& EFI_MEMORY_RUNTIME
) == 0) {
445 Status
= gDS
->SetMemorySpaceAttributes (
448 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
450 if (EFI_ERROR (Status
)) {
451 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
457 // Initializes variable write service after FTW was ready.
459 VariableWriteServiceInitializeDxe ();
462 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
464 gBS
->CloseEvent (Event
);
470 Variable Driver main entry point. The Variable driver places the 4 EFI
471 runtime services in the EFI System Table and installs arch protocols
472 for variable read and write services being available. It also registers
473 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
475 @param[in] ImageHandle The firmware allocated handle for the EFI image.
476 @param[in] SystemTable A pointer to the EFI System Table.
478 @retval EFI_SUCCESS Variable service successfully initialized.
483 VariableServiceInitialize (
484 IN EFI_HANDLE ImageHandle
,
485 IN EFI_SYSTEM_TABLE
*SystemTable
489 EFI_EVENT ReadyToBootEvent
;
490 EFI_EVENT EndOfDxeEvent
;
492 Status
= VariableCommonInitialize ();
493 ASSERT_EFI_ERROR (Status
);
495 Status
= gBS
->InstallMultipleProtocolInterfaces (
497 &gEdkiiVariableLockProtocolGuid
,
501 ASSERT_EFI_ERROR (Status
);
503 Status
= gBS
->InstallMultipleProtocolInterfaces (
505 &gEdkiiVarCheckProtocolGuid
,
509 ASSERT_EFI_ERROR (Status
);
511 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
512 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
513 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
514 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
517 // Now install the Variable Runtime Architectural protocol on a new handle.
519 Status
= gBS
->InstallProtocolInterface (
521 &gEfiVariableArchProtocolGuid
,
522 EFI_NATIVE_INTERFACE
,
525 ASSERT_EFI_ERROR (Status
);
527 if (!PcdGetBool (PcdEmuVariableNvModeEnable
)) {
529 // Register FtwNotificationEvent () notify function.
531 EfiCreateProtocolNotifyEvent (
532 &gEfiFaultTolerantWriteProtocolGuid
,
534 FtwNotificationEvent
,
540 // Emulated non-volatile variable mode does not depend on FVB and FTW.
542 VariableWriteServiceInitializeDxe ();
545 Status
= gBS
->CreateEventEx (
548 VariableClassAddressChangeEvent
,
550 &gEfiEventVirtualAddressChangeGuid
,
551 &mVirtualAddressChangeEvent
553 ASSERT_EFI_ERROR (Status
);
556 // Register the event handling function to reclaim variable for OS usage.
558 Status
= EfiCreateEventReadyToBootEx (
564 ASSERT_EFI_ERROR (Status
);
567 // Register the event handling function to set the End Of DXE flag.
569 Status
= gBS
->CreateEventEx (
574 &gEfiEndOfDxeEventGroupGuid
,
577 ASSERT_EFI_ERROR (Status
);