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 - 2018, 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.
294 MorLockInitAtEndOfDxe ();
296 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
299 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
301 // The initialization for variable quota.
303 InitializeVariableQuota ();
306 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
307 if (mVariableModuleGlobal
->VariableGlobal
.AuthFormat
) {
308 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
310 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, gVariableInfo
);
314 gBS
->CloseEvent (Event
);
318 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
320 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
322 @param Event Event whose notification function is being invoked.
323 @param Context Pointer to the notification function's context.
333 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
334 MorLockInitAtEndOfDxe ();
336 mVarCheckAddressPointer
= VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount
);
338 // The initialization for variable quota.
340 InitializeVariableQuota ();
341 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
345 gBS
->CloseEvent (Event
);
349 Fault Tolerant Write protocol notification event handler.
351 Non-Volatile variable write may needs FTW protocol to reclaim when
354 @param[in] Event Event whose notification function is being invoked.
355 @param[in] Context Pointer to the notification function's context.
360 FtwNotificationEvent (
366 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
367 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
368 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
369 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
370 EFI_PHYSICAL_ADDRESS BaseAddress
;
372 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
373 UINT64 VariableStoreLength
;
374 UINTN FtwMaxBlockSize
;
377 // Ensure FTW protocol is installed.
379 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
380 if (EFI_ERROR (Status
)) {
384 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
385 if (!EFI_ERROR (Status
)) {
386 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
390 // Find the proper FVB protocol for variable.
392 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
393 if (NvStorageVariableBase
== 0) {
394 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
396 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
397 if (EFI_ERROR (Status
)) {
400 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
403 // Mark the variable storage region of the FLASH as RUNTIME.
405 VariableStoreBase
= NvStorageVariableBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageVariableBase
))->HeaderLength
);
406 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
407 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
408 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
409 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
411 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
412 if (EFI_ERROR (Status
)) {
413 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
415 Status
= gDS
->SetMemorySpaceAttributes (
418 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
420 if (EFI_ERROR (Status
)) {
421 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
425 Status
= VariableWriteServiceInitialize ();
426 if (EFI_ERROR (Status
)) {
427 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
431 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
432 // Secure Boot Policy Variable change. Record their initial value.
434 RecordSecureBootPolicyVarData();
437 // Install the Variable Write Architectural protocol.
439 Status
= gBS
->InstallProtocolInterface (
441 &gEfiVariableWriteArchProtocolGuid
,
442 EFI_NATIVE_INTERFACE
,
445 ASSERT_EFI_ERROR (Status
);
448 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
450 gBS
->CloseEvent (Event
);
456 Variable Driver main entry point. The Variable driver places the 4 EFI
457 runtime services in the EFI System Table and installs arch protocols
458 for variable read and write services being available. It also registers
459 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
461 @param[in] ImageHandle The firmware allocated handle for the EFI image.
462 @param[in] SystemTable A pointer to the EFI System Table.
464 @retval EFI_SUCCESS Variable service successfully initialized.
469 VariableServiceInitialize (
470 IN EFI_HANDLE ImageHandle
,
471 IN EFI_SYSTEM_TABLE
*SystemTable
475 EFI_EVENT ReadyToBootEvent
;
476 EFI_EVENT EndOfDxeEvent
;
478 Status
= VariableCommonInitialize ();
479 ASSERT_EFI_ERROR (Status
);
481 Status
= gBS
->InstallMultipleProtocolInterfaces (
483 &gEdkiiVariableLockProtocolGuid
,
487 ASSERT_EFI_ERROR (Status
);
489 Status
= gBS
->InstallMultipleProtocolInterfaces (
491 &gEdkiiVarCheckProtocolGuid
,
495 ASSERT_EFI_ERROR (Status
);
497 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
498 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
499 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
500 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
503 // Now install the Variable Runtime Architectural protocol on a new handle.
505 Status
= gBS
->InstallProtocolInterface (
507 &gEfiVariableArchProtocolGuid
,
508 EFI_NATIVE_INTERFACE
,
511 ASSERT_EFI_ERROR (Status
);
514 // Register FtwNotificationEvent () notify function.
516 EfiCreateProtocolNotifyEvent (
517 &gEfiFaultTolerantWriteProtocolGuid
,
519 FtwNotificationEvent
,
524 Status
= gBS
->CreateEventEx (
527 VariableClassAddressChangeEvent
,
529 &gEfiEventVirtualAddressChangeGuid
,
530 &mVirtualAddressChangeEvent
532 ASSERT_EFI_ERROR (Status
);
535 // Register the event handling function to reclaim variable for OS usage.
537 Status
= EfiCreateEventReadyToBootEx (
543 ASSERT_EFI_ERROR (Status
);
546 // Register the event handling function to set the End Of DXE flag.
548 Status
= gBS
->CreateEventEx (
553 &gEfiEndOfDxeEventGroupGuid
,
556 ASSERT_EFI_ERROR (Status
);