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 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
240 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
241 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
242 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
243 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
244 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
245 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
246 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
247 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
248 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
249 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
250 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
251 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
252 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.HobVariableBase
);
253 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
254 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
255 EfiConvertPointer (0x0, (VOID
**) &mNvFvHeaderCache
);
257 if (mAuthContextOut
.AddressPointer
!= NULL
) {
258 for (Index
= 0; Index
< mAuthContextOut
.AddressPointerCount
; Index
++) {
259 EfiConvertPointer (0x0, (VOID
**) mAuthContextOut
.AddressPointer
[Index
]);
263 if (mVarCheckAddressPointer
!= NULL
) {
264 for (Index
= 0; Index
< mVarCheckAddressPointerCount
; Index
++) {
265 EfiConvertPointer (0x0, (VOID
**) mVarCheckAddressPointer
[Index
]);
272 Notification function of EVT_GROUP_READY_TO_BOOT event group.
274 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
275 When the Boot Manager is about to load and execute a boot option, it reclaims variable
276 storage if free size is below the threshold.
278 @param Event Event whose notification function is being invoked.
279 @param Context Pointer to the notification function's context.
290 MorLockInitAtEndOfDxe ();
292 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
295 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
297 // The initialization for variable quota.
299 InitializeVariableQuota ();
302 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
303 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
304 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
306 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
310 gBS
->CloseEvent (Event
);
314 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
316 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
318 @param Event Event whose notification function is being invoked.
319 @param Context Pointer to the notification function's context.
329 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
330 MorLockInitAtEndOfDxe ();
332 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
334 // The initialization for variable quota.
336 InitializeVariableQuota ();
337 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
341 gBS
->CloseEvent (Event
);
345 Initializes variable write service for DXE.
349 VariableWriteServiceInitializeDxe (
355 Status
= VariableWriteServiceInitialize ();
356 if (EFI_ERROR (Status
)) {
357 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
361 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
362 // Secure Boot Policy Variable change. Record their initial value.
364 RecordSecureBootPolicyVarData();
367 // Install the Variable Write Architectural protocol.
369 Status
= gBS
->InstallProtocolInterface (
371 &gEfiVariableWriteArchProtocolGuid
,
372 EFI_NATIVE_INTERFACE
,
375 ASSERT_EFI_ERROR (Status
);
379 Fault Tolerant Write protocol notification event handler.
381 Non-Volatile variable write may needs FTW protocol to reclaim when
384 @param[in] Event Event whose notification function is being invoked.
385 @param[in] Context Pointer to the notification function's context.
390 FtwNotificationEvent (
396 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
397 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
398 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
399 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
400 EFI_PHYSICAL_ADDRESS BaseAddress
;
402 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
403 UINT64 VariableStoreLength
;
404 UINTN FtwMaxBlockSize
;
407 // Ensure FTW protocol is installed.
409 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
410 if (EFI_ERROR (Status
)) {
414 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
415 if (!EFI_ERROR (Status
)) {
416 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
419 NvStorageVariableBase
= NV_STORAGE_VARIABLE_BASE
;
420 VariableStoreBase
= NvStorageVariableBase
+ mNvFvHeaderCache
->HeaderLength
;
423 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
425 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
428 // Find the proper FVB protocol for variable.
430 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
431 if (EFI_ERROR (Status
)) {
434 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
437 // Mark the variable storage region of the FLASH as RUNTIME.
439 VariableStoreLength
= mNvVariableCache
->Size
;
440 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
441 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
442 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
444 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
445 if (EFI_ERROR (Status
)) {
446 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
448 if ((GcdDescriptor
.Attributes
& EFI_MEMORY_RUNTIME
) == 0) {
449 Status
= gDS
->SetMemorySpaceAttributes (
452 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
454 if (EFI_ERROR (Status
)) {
455 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
461 // Initializes variable write service after FTW was ready.
463 VariableWriteServiceInitializeDxe ();
466 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
468 gBS
->CloseEvent (Event
);
474 Variable Driver main entry point. The Variable driver places the 4 EFI
475 runtime services in the EFI System Table and installs arch protocols
476 for variable read and write services being available. It also registers
477 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
479 @param[in] ImageHandle The firmware allocated handle for the EFI image.
480 @param[in] SystemTable A pointer to the EFI System Table.
482 @retval EFI_SUCCESS Variable service successfully initialized.
487 VariableServiceInitialize (
488 IN EFI_HANDLE ImageHandle
,
489 IN EFI_SYSTEM_TABLE
*SystemTable
493 EFI_EVENT ReadyToBootEvent
;
494 EFI_EVENT EndOfDxeEvent
;
496 Status
= VariableCommonInitialize ();
497 ASSERT_EFI_ERROR (Status
);
499 Status
= gBS
->InstallMultipleProtocolInterfaces (
501 &gEdkiiVariableLockProtocolGuid
,
505 ASSERT_EFI_ERROR (Status
);
507 Status
= gBS
->InstallMultipleProtocolInterfaces (
509 &gEdkiiVarCheckProtocolGuid
,
513 ASSERT_EFI_ERROR (Status
);
515 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
516 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
517 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
518 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
521 // Now install the Variable Runtime Architectural protocol on a new handle.
523 Status
= gBS
->InstallProtocolInterface (
525 &gEfiVariableArchProtocolGuid
,
526 EFI_NATIVE_INTERFACE
,
529 ASSERT_EFI_ERROR (Status
);
532 // Register FtwNotificationEvent () notify function.
534 EfiCreateProtocolNotifyEvent (
535 &gEfiFaultTolerantWriteProtocolGuid
,
537 FtwNotificationEvent
,
542 Status
= gBS
->CreateEventEx (
545 VariableClassAddressChangeEvent
,
547 &gEfiEventVirtualAddressChangeGuid
,
548 &mVirtualAddressChangeEvent
550 ASSERT_EFI_ERROR (Status
);
553 // Register the event handling function to reclaim variable for OS usage.
555 Status
= EfiCreateEventReadyToBootEx (
561 ASSERT_EFI_ERROR (Status
);
564 // Register the event handling function to set the End Of DXE flag.
566 Status
= gBS
->CreateEventEx (
571 &gEfiEndOfDxeEventGroupGuid
,
574 ASSERT_EFI_ERROR (Status
);