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) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "AuthService.h"
20 extern VARIABLE_STORE_HEADER
*mNvVariableCache
;
21 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
22 EFI_HANDLE mHandle
= NULL
;
23 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
24 EFI_EVENT mFtwRegistration
= NULL
;
25 extern LIST_ENTRY mLockedVariableList
;
26 extern BOOLEAN mEndOfDxe
;
27 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
= { VariableLockRequestToLock
};
30 Return TRUE if ExitBootServices () has been called.
32 @retval TRUE If ExitBootServices () has been called.
39 return EfiAtRuntime ();
44 Initializes a basic mutual exclusion lock.
46 This function initializes a basic mutual exclusion lock to the released state
47 and returns the lock. Each lock provides mutual exclusion access at its task
48 priority level. Since there is no preemption or multiprocessor support in EFI,
49 acquiring the lock only consists of raising to the locks TPL.
50 If Lock is NULL, then ASSERT().
51 If Priority is not a valid TPL value, then ASSERT().
53 @param Lock A pointer to the lock data structure to initialize.
54 @param Priority EFI TPL is associated with the lock.
61 IN OUT EFI_LOCK
*Lock
,
65 return EfiInitializeLock (Lock
, Priority
);
70 Acquires lock only at boot time. Simply returns at runtime.
72 This is a temperary function that will be removed when
73 EfiAcquireLock() in UefiLib can handle the call in UEFI
74 Runtimer driver in RT phase.
75 It calls EfiAcquireLock() at boot time, and simply returns
78 @param Lock A pointer to the lock to acquire.
82 AcquireLockOnlyAtBootTime (
87 EfiAcquireLock (Lock
);
93 Releases lock only at boot time. Simply returns at runtime.
95 This is a temperary function which will be removed when
96 EfiReleaseLock() in UefiLib can handle the call in UEFI
97 Runtimer driver in RT phase.
98 It calls EfiReleaseLock() at boot time and simply returns
101 @param Lock A pointer to the lock to release.
105 ReleaseLockOnlyAtBootTime (
110 EfiReleaseLock (Lock
);
115 Retrive the Fault Tolerent Write protocol interface.
117 @param[out] FtwProtocol The interface of Ftw protocol
119 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.
120 @retval EFI_NOT_FOUND The FTW protocol instance was not found.
121 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
126 OUT VOID
**FtwProtocol
132 // Locate Fault Tolerent Write protocol
134 Status
= gBS
->LocateProtocol (
135 &gEfiFaultTolerantWriteProtocolGuid
,
143 Retrive the FVB protocol interface by HANDLE.
145 @param[in] FvBlockHandle The handle of FVB protocol that provides services for
146 reading, writing, and erasing the target block.
147 @param[out] FvBlock The interface of FVB protocol
149 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
150 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.
151 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
156 IN EFI_HANDLE FvBlockHandle
,
157 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
161 // To get the FVB protocol interface on the handle
163 return gBS
->HandleProtocol (
165 &gEfiFirmwareVolumeBlockProtocolGuid
,
172 Function returns an array of handles that support the FVB protocol
173 in a buffer allocated from pool.
175 @param[out] NumberHandles The number of handles returned in Buffer.
176 @param[out] Buffer A pointer to the buffer to return the requested
177 array of handles that support FVB protocol.
179 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
180 handles in Buffer was returned in NumberHandles.
181 @retval EFI_NOT_FOUND No FVB handle was found.
182 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
183 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
187 GetFvbCountAndBuffer (
188 OUT UINTN
*NumberHandles
,
189 OUT EFI_HANDLE
**Buffer
195 // Locate all handles of Fvb protocol
197 Status
= gBS
->LocateHandleBuffer (
199 &gEfiFirmwareVolumeBlockProtocolGuid
,
209 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
211 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
212 It convers pointer to new virtual address.
214 @param Event Event whose notification function is being invoked.
215 @param Context Pointer to the notification function's context.
220 VariableClassAddressChangeEvent (
226 VARIABLE_ENTRY
*Entry
;
229 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetBlockSize
);
230 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetPhysicalAddress
);
231 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->GetAttributes
);
232 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->SetAttributes
);
233 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Read
);
234 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->Write
);
235 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
->EraseBlocks
);
236 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->FvbInstance
);
237 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLangCodes
);
238 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->LangCodes
);
239 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->PlatformLang
);
240 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
);
241 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
->VariableGlobal
.VolatileVariableBase
);
242 EfiConvertPointer (0x0, (VOID
**) &mVariableModuleGlobal
);
243 EfiConvertPointer (0x0, (VOID
**) &mHashCtx
);
244 EfiConvertPointer (0x0, (VOID
**) &mSerializationRuntimeBuffer
);
245 EfiConvertPointer (0x0, (VOID
**) &mNvVariableCache
);
246 EfiConvertPointer (0x0, (VOID
**) &mPubKeyStore
);
247 EfiConvertPointer (0x0, (VOID
**) &mCertDbStore
);
250 // in the list of locked variables, convert the name pointers first
252 for ( Link
= GetFirstNode (&mLockedVariableList
)
253 ; !IsNull (&mLockedVariableList
, Link
)
254 ; Link
= GetNextNode (&mLockedVariableList
, Link
)
256 Entry
= BASE_CR (Link
, VARIABLE_ENTRY
, Link
);
257 Status
= EfiConvertPointer (0x0, (VOID
**) &Entry
->Name
);
258 ASSERT_EFI_ERROR (Status
);
261 // second, convert the list itself using UefiRuntimeLib
263 Status
= EfiConvertList (0x0, &mLockedVariableList
);
264 ASSERT_EFI_ERROR (Status
);
269 Notification function of EVT_GROUP_READY_TO_BOOT event group.
271 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
272 When the Boot Manager is about to load and execute a boot option, it reclaims variable
273 storage if free size is below the threshold.
275 @param Event Event whose notification function is being invoked.
276 @param Context Pointer to the notification function's context.
287 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
291 if (FeaturePcdGet (PcdVariableCollectStatistics
)) {
292 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, gVariableInfo
);
297 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
299 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
301 @param Event Event whose notification function is being invoked.
302 @param Context Pointer to the notification function's context.
316 Fault Tolerant Write protocol notification event handler.
318 Non-Volatile variable write may needs FTW protocol to reclaim when
321 @param[in] Event Event whose notification function is being invoked.
322 @param[in] Context Pointer to the notification function's context.
327 FtwNotificationEvent (
333 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
334 EFI_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
335 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
336 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
337 EFI_PHYSICAL_ADDRESS BaseAddress
;
339 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
340 UINT64 VariableStoreLength
;
341 UINTN FtwMaxBlockSize
;
344 // Ensure FTW protocol is installed.
346 Status
= GetFtwProtocol ((VOID
**) &FtwProtocol
);
347 if (EFI_ERROR (Status
)) {
351 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
352 if (!EFI_ERROR (Status
)) {
353 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
357 // Find the proper FVB protocol for variable.
359 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
360 if (NvStorageVariableBase
== 0) {
361 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
363 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
364 if (EFI_ERROR (Status
)) {
367 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
370 // Mark the variable storage region of the FLASH as RUNTIME.
372 VariableStoreBase
= NvStorageVariableBase
+ (((EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)(NvStorageVariableBase
))->HeaderLength
);
373 VariableStoreLength
= ((VARIABLE_STORE_HEADER
*)(UINTN
)VariableStoreBase
)->Size
;
374 BaseAddress
= VariableStoreBase
& (~EFI_PAGE_MASK
);
375 Length
= VariableStoreLength
+ (VariableStoreBase
- BaseAddress
);
376 Length
= (Length
+ EFI_PAGE_SIZE
- 1) & (~EFI_PAGE_MASK
);
378 Status
= gDS
->GetMemorySpaceDescriptor (BaseAddress
, &GcdDescriptor
);
379 if (EFI_ERROR (Status
)) {
380 DEBUG ((DEBUG_WARN
, "Variable driver failed to get flash memory attribute.\n"));
382 Status
= gDS
->SetMemorySpaceAttributes (
385 GcdDescriptor
.Attributes
| EFI_MEMORY_RUNTIME
387 if (EFI_ERROR (Status
)) {
388 DEBUG ((DEBUG_WARN
, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
392 Status
= VariableWriteServiceInitialize ();
393 if (EFI_ERROR (Status
)) {
394 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
398 // Install the Variable Write Architectural protocol.
400 Status
= gBS
->InstallProtocolInterface (
402 &gEfiVariableWriteArchProtocolGuid
,
403 EFI_NATIVE_INTERFACE
,
406 ASSERT_EFI_ERROR (Status
);
409 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
411 gBS
->CloseEvent (Event
);
417 Variable Driver main entry point. The Variable driver places the 4 EFI
418 runtime services in the EFI System Table and installs arch protocols
419 for variable read and write services being available. It also registers
420 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
422 @param[in] ImageHandle The firmware allocated handle for the EFI image.
423 @param[in] SystemTable A pointer to the EFI System Table.
425 @retval EFI_SUCCESS Variable service successfully initialized.
430 VariableServiceInitialize (
431 IN EFI_HANDLE ImageHandle
,
432 IN EFI_SYSTEM_TABLE
*SystemTable
436 EFI_EVENT ReadyToBootEvent
;
437 EFI_EVENT EndOfDxeEvent
;
439 Status
= VariableCommonInitialize ();
440 ASSERT_EFI_ERROR (Status
);
442 Status
= gBS
->InstallMultipleProtocolInterfaces (
444 &gEdkiiVariableLockProtocolGuid
,
448 ASSERT_EFI_ERROR (Status
);
450 SystemTable
->RuntimeServices
->GetVariable
= VariableServiceGetVariable
;
451 SystemTable
->RuntimeServices
->GetNextVariableName
= VariableServiceGetNextVariableName
;
452 SystemTable
->RuntimeServices
->SetVariable
= VariableServiceSetVariable
;
453 SystemTable
->RuntimeServices
->QueryVariableInfo
= VariableServiceQueryVariableInfo
;
456 // Now install the Variable Runtime Architectural protocol on a new handle.
458 Status
= gBS
->InstallProtocolInterface (
460 &gEfiVariableArchProtocolGuid
,
461 EFI_NATIVE_INTERFACE
,
464 ASSERT_EFI_ERROR (Status
);
467 // Register FtwNotificationEvent () notify function.
469 EfiCreateProtocolNotifyEvent (
470 &gEfiFaultTolerantWriteProtocolGuid
,
472 FtwNotificationEvent
,
477 Status
= gBS
->CreateEventEx (
480 VariableClassAddressChangeEvent
,
482 &gEfiEventVirtualAddressChangeGuid
,
483 &mVirtualAddressChangeEvent
485 ASSERT_EFI_ERROR (Status
);
488 // Register the event handling function to reclaim variable for OS usage.
490 Status
= EfiCreateEventReadyToBootEx (
496 ASSERT_EFI_ERROR (Status
);
499 // Register the event handling function to set the End Of DXE flag.
501 Status
= gBS
->CreateEventEx (
506 &gEfiEndOfDxeEventGroupGuid
,
509 ASSERT_EFI_ERROR (Status
);