]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
MdeModulePkg/Variable: Consume Variable Flash Info
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / VariableDxe.c
CommitLineData
e4b7e2c9 1/** @file\r
e4b7e2c9
RN
2 Implement all four UEFI Runtime Variable services for the nonvolatile\r
3 and volatile storage space and install variable architecture protocol.\r
fa0737a8 4\r
328e5d8c 5Copyright (C) 2013, Red Hat, Inc.\r
874c8434 6Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9b18845a 7(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
b6490426 8Copyright (c) Microsoft Corporation.\r
9d510e61 9SPDX-License-Identifier: BSD-2-Clause-Patent\r
e4b7e2c9
RN
10\r
11**/\r
12\r
13#include "Variable.h"\r
14\r
b6490426
BB
15#include <Protocol/VariablePolicy.h>\r
16#include <Library/VariablePolicyLib.h>\r
17\r
18EFI_STATUS\r
19EFIAPI\r
20ProtocolIsVariablePolicyEnabled (\r
1436aea4 21 OUT BOOLEAN *State\r
b6490426
BB
22 );\r
23\r
1436aea4
MK
24EFI_HANDLE mHandle = NULL;\r
25EFI_EVENT mVirtualAddressChangeEvent = NULL;\r
26VOID *mFtwRegistration = NULL;\r
27VOID ***mVarCheckAddressPointer = NULL;\r
28UINTN mVarCheckAddressPointerCount = 0;\r
29EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock = { VariableLockRequestToLock };\r
30EDKII_VARIABLE_POLICY_PROTOCOL mVariablePolicyProtocol = {\r
31 EDKII_VARIABLE_POLICY_PROTOCOL_REVISION,\r
32 DisableVariablePolicy,\r
33 ProtocolIsVariablePolicyEnabled,\r
34 RegisterVariablePolicy,\r
35 DumpVariablePolicy,\r
36 LockVariablePolicy\r
37};\r
38EDKII_VAR_CHECK_PROTOCOL mVarCheck = {\r
39 VarCheckRegisterSetVariableCheckHandler,\r
40 VarCheckVariablePropertySet,\r
41 VarCheckVariablePropertyGet\r
42};\r
e4b7e2c9 43\r
dc9bd6ed
ZC
44/**\r
45 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).\r
46 Record their initial State when variable write service is ready.\r
47\r
48**/\r
49VOID\r
50EFIAPI\r
1436aea4 51RecordSecureBootPolicyVarData (\r
dc9bd6ed
ZC
52 VOID\r
53 );\r
54\r
e4b7e2c9
RN
55/**\r
56 Return TRUE if ExitBootServices () has been called.\r
fa0737a8 57\r
e4b7e2c9
RN
58 @retval TRUE If ExitBootServices () has been called.\r
59**/\r
60BOOLEAN\r
61AtRuntime (\r
62 VOID\r
63 )\r
64{\r
65 return EfiAtRuntime ();\r
66}\r
67\r
e4b7e2c9
RN
68/**\r
69 Initializes a basic mutual exclusion lock.\r
70\r
fa0737a8
SZ
71 This function initializes a basic mutual exclusion lock to the released state\r
72 and returns the lock. Each lock provides mutual exclusion access at its task\r
e4b7e2c9
RN
73 priority level. Since there is no preemption or multiprocessor support in EFI,\r
74 acquiring the lock only consists of raising to the locks TPL.\r
75 If Lock is NULL, then ASSERT().\r
76 If Priority is not a valid TPL value, then ASSERT().\r
77\r
78 @param Lock A pointer to the lock data structure to initialize.\r
79 @param Priority EFI TPL is associated with the lock.\r
80\r
81 @return The lock.\r
82\r
83**/\r
84EFI_LOCK *\r
85InitializeLock (\r
1436aea4
MK
86 IN OUT EFI_LOCK *Lock,\r
87 IN EFI_TPL Priority\r
e4b7e2c9
RN
88 )\r
89{\r
90 return EfiInitializeLock (Lock, Priority);\r
91}\r
92\r
e4b7e2c9
RN
93/**\r
94 Acquires lock only at boot time. Simply returns at runtime.\r
95\r
96 This is a temperary function that will be removed when\r
97 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
98 Runtimer driver in RT phase.\r
99 It calls EfiAcquireLock() at boot time, and simply returns\r
100 at runtime.\r
101\r
102 @param Lock A pointer to the lock to acquire.\r
103\r
104**/\r
105VOID\r
106AcquireLockOnlyAtBootTime (\r
1436aea4 107 IN EFI_LOCK *Lock\r
e4b7e2c9
RN
108 )\r
109{\r
110 if (!AtRuntime ()) {\r
111 EfiAcquireLock (Lock);\r
112 }\r
113}\r
114\r
e4b7e2c9
RN
115/**\r
116 Releases lock only at boot time. Simply returns at runtime.\r
117\r
118 This is a temperary function which will be removed when\r
119 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
120 Runtimer driver in RT phase.\r
121 It calls EfiReleaseLock() at boot time and simply returns\r
122 at runtime.\r
123\r
124 @param Lock A pointer to the lock to release.\r
125\r
126**/\r
127VOID\r
128ReleaseLockOnlyAtBootTime (\r
1436aea4 129 IN EFI_LOCK *Lock\r
e4b7e2c9
RN
130 )\r
131{\r
132 if (!AtRuntime ()) {\r
133 EfiReleaseLock (Lock);\r
134 }\r
135}\r
136\r
137/**\r
0a18956d 138 Retrieve the Fault Tolerent Write protocol interface.\r
e4b7e2c9
RN
139\r
140 @param[out] FtwProtocol The interface of Ftw protocol\r
141\r
142 @retval EFI_SUCCESS The FTW protocol instance was found and returned in FtwProtocol.\r
143 @retval EFI_NOT_FOUND The FTW protocol instance was not found.\r
144 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.\r
145\r
146**/\r
147EFI_STATUS\r
148GetFtwProtocol (\r
1436aea4 149 OUT VOID **FtwProtocol\r
e4b7e2c9
RN
150 )\r
151{\r
1436aea4 152 EFI_STATUS Status;\r
e4b7e2c9
RN
153\r
154 //\r
155 // Locate Fault Tolerent Write protocol\r
156 //\r
157 Status = gBS->LocateProtocol (\r
158 &gEfiFaultTolerantWriteProtocolGuid,\r
159 NULL,\r
160 FtwProtocol\r
fa0737a8 161 );\r
e4b7e2c9
RN
162 return Status;\r
163}\r
164\r
165/**\r
0a18956d 166 Retrieve the FVB protocol interface by HANDLE.\r
e4b7e2c9
RN
167\r
168 @param[in] FvBlockHandle The handle of FVB protocol that provides services for\r
169 reading, writing, and erasing the target block.\r
170 @param[out] FvBlock The interface of FVB protocol\r
171\r
172 @retval EFI_SUCCESS The interface information for the specified protocol was returned.\r
173 @retval EFI_UNSUPPORTED The device does not support the FVB protocol.\r
174 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.\r
fa0737a8 175\r
e4b7e2c9
RN
176**/\r
177EFI_STATUS\r
178GetFvbByHandle (\r
179 IN EFI_HANDLE FvBlockHandle,\r
180 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock\r
181 )\r
182{\r
183 //\r
184 // To get the FVB protocol interface on the handle\r
185 //\r
186 return gBS->HandleProtocol (\r
187 FvBlockHandle,\r
188 &gEfiFirmwareVolumeBlockProtocolGuid,\r
1436aea4 189 (VOID **)FvBlock\r
e4b7e2c9
RN
190 );\r
191}\r
192\r
e4b7e2c9
RN
193/**\r
194 Function returns an array of handles that support the FVB protocol\r
fa0737a8 195 in a buffer allocated from pool.\r
e4b7e2c9
RN
196\r
197 @param[out] NumberHandles The number of handles returned in Buffer.\r
198 @param[out] Buffer A pointer to the buffer to return the requested\r
199 array of handles that support FVB protocol.\r
200\r
201 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of\r
202 handles in Buffer was returned in NumberHandles.\r
203 @retval EFI_NOT_FOUND No FVB handle was found.\r
204 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.\r
205 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.\r
fa0737a8 206\r
e4b7e2c9
RN
207**/\r
208EFI_STATUS\r
209GetFvbCountAndBuffer (\r
1436aea4
MK
210 OUT UINTN *NumberHandles,\r
211 OUT EFI_HANDLE **Buffer\r
e4b7e2c9
RN
212 )\r
213{\r
1436aea4 214 EFI_STATUS Status;\r
e4b7e2c9
RN
215\r
216 //\r
217 // Locate all handles of Fvb protocol\r
218 //\r
219 Status = gBS->LocateHandleBuffer (\r
220 ByProtocol,\r
221 &gEfiFirmwareVolumeBlockProtocolGuid,\r
222 NULL,\r
223 NumberHandles,\r
224 Buffer\r
225 );\r
226 return Status;\r
227}\r
228\r
e4b7e2c9
RN
229/**\r
230 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
231\r
232 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
233 It convers pointer to new virtual address.\r
234\r
235 @param Event Event whose notification function is being invoked.\r
236 @param Context Pointer to the notification function's context.\r
237\r
238**/\r
239VOID\r
240EFIAPI\r
241VariableClassAddressChangeEvent (\r
1436aea4
MK
242 IN EFI_EVENT Event,\r
243 IN VOID *Context\r
e4b7e2c9
RN
244 )\r
245{\r
1436aea4 246 UINTN Index;\r
328e5d8c 247\r
7cd69959 248 if (mVariableModuleGlobal->FvbInstance != NULL) {\r
1436aea4
MK
249 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->GetBlockSize);\r
250 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);\r
251 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->GetAttributes);\r
252 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->SetAttributes);\r
253 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->Read);\r
254 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->Write);\r
255 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance->EraseBlocks);\r
256 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->FvbInstance);\r
7cd69959 257 }\r
1436aea4
MK
258\r
259 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->PlatformLangCodes);\r
260 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->LangCodes);\r
261 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->PlatformLang);\r
262 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r
263 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r
264 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r
265 EfiConvertPointer (0x0, (VOID **)&mVariableModuleGlobal);\r
266 EfiConvertPointer (0x0, (VOID **)&mNvVariableCache);\r
267 EfiConvertPointer (0x0, (VOID **)&mNvFvHeaderCache);\r
efb01a10 268\r
8021f4c7
SZ
269 if (mAuthContextOut.AddressPointer != NULL) {\r
270 for (Index = 0; Index < mAuthContextOut.AddressPointerCount; Index++) {\r
1436aea4 271 EfiConvertPointer (0x0, (VOID **)mAuthContextOut.AddressPointer[Index]);\r
8021f4c7
SZ
272 }\r
273 }\r
fa0737a8 274\r
8021f4c7
SZ
275 if (mVarCheckAddressPointer != NULL) {\r
276 for (Index = 0; Index < mVarCheckAddressPointerCount; Index++) {\r
1436aea4 277 EfiConvertPointer (0x0, (VOID **)mVarCheckAddressPointer[Index]);\r
fa0737a8
SZ
278 }\r
279 }\r
e4b7e2c9
RN
280}\r
281\r
e4b7e2c9
RN
282/**\r
283 Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
284\r
285 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
286 When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
287 storage if free size is below the threshold.\r
288\r
289 @param Event Event whose notification function is being invoked.\r
290 @param Context Pointer to the notification function's context.\r
291\r
292**/\r
293VOID\r
294EFIAPI\r
295OnReadyToBoot (\r
1436aea4
MK
296 EFI_EVENT Event,\r
297 VOID *Context\r
e4b7e2c9
RN
298 )\r
299{\r
1436aea4 300 EFI_STATUS Status;\r
b6490426 301\r
8021f4c7 302 if (!mEndOfDxe) {\r
f1304280 303 MorLockInitAtEndOfDxe ();\r
b6490426
BB
304\r
305 Status = LockVariablePolicy ();\r
306 ASSERT_EFI_ERROR (Status);\r
8021f4c7
SZ
307 //\r
308 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.\r
309 //\r
1436aea4 310 mEndOfDxe = TRUE;\r
8021f4c7
SZ
311 mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);\r
312 //\r
313 // The initialization for variable quota.\r
314 //\r
315 InitializeVariableQuota ();\r
316 }\r
1436aea4 317\r
e4b7e2c9
RN
318 ReclaimForOS ();\r
319 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
fa0737a8
SZ
320 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r
321 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
322 } else {\r
323 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
324 }\r
e4b7e2c9 325 }\r
fa0737a8
SZ
326\r
327 gBS->CloseEvent (Event);\r
e4b7e2c9
RN
328}\r
329\r
330/**\r
331 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
332\r
333 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
334\r
335 @param Event Event whose notification function is being invoked.\r
336 @param Context Pointer to the notification function's context.\r
337\r
338**/\r
339VOID\r
340EFIAPI\r
341OnEndOfDxe (\r
1436aea4
MK
342 EFI_EVENT Event,\r
343 VOID *Context\r
e4b7e2c9
RN
344 )\r
345{\r
1436aea4 346 EFI_STATUS Status;\r
b6490426 347\r
87000d77 348 DEBUG ((DEBUG_INFO, "[Variable]END_OF_DXE is signaled\n"));\r
f1304280 349 MorLockInitAtEndOfDxe ();\r
b6490426
BB
350 Status = LockVariablePolicy ();\r
351 ASSERT_EFI_ERROR (Status);\r
1436aea4 352 mEndOfDxe = TRUE;\r
8021f4c7 353 mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);\r
4edb1866
SZ
354 //\r
355 // The initialization for variable quota.\r
356 //\r
357 InitializeVariableQuota ();\r
0fb5e515
SZ
358 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {\r
359 ReclaimForOS ();\r
360 }\r
fa0737a8
SZ
361\r
362 gBS->CloseEvent (Event);\r
e4b7e2c9
RN
363}\r
364\r
b59fd889
SZ
365/**\r
366 Initializes variable write service for DXE.\r
367\r
368**/\r
369VOID\r
370VariableWriteServiceInitializeDxe (\r
371 VOID\r
372 )\r
373{\r
1436aea4 374 EFI_STATUS Status;\r
b59fd889
SZ
375\r
376 Status = VariableWriteServiceInitialize ();\r
377 if (EFI_ERROR (Status)) {\r
378 DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
379 }\r
380\r
381 //\r
382 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other\r
383 // Secure Boot Policy Variable change. Record their initial value.\r
384 //\r
1436aea4 385 RecordSecureBootPolicyVarData ();\r
b59fd889
SZ
386\r
387 //\r
388 // Install the Variable Write Architectural protocol.\r
389 //\r
390 Status = gBS->InstallProtocolInterface (\r
391 &mHandle,\r
392 &gEfiVariableWriteArchProtocolGuid,\r
393 EFI_NATIVE_INTERFACE,\r
394 NULL\r
395 );\r
396 ASSERT_EFI_ERROR (Status);\r
397}\r
398\r
e4b7e2c9
RN
399/**\r
400 Fault Tolerant Write protocol notification event handler.\r
401\r
fa0737a8 402 Non-Volatile variable write may needs FTW protocol to reclaim when\r
e4b7e2c9
RN
403 writting variable.\r
404\r
405 @param[in] Event Event whose notification function is being invoked.\r
406 @param[in] Context Pointer to the notification function's context.\r
fa0737a8 407\r
e4b7e2c9
RN
408**/\r
409VOID\r
410EFIAPI\r
411FtwNotificationEvent (\r
1436aea4
MK
412 IN EFI_EVENT Event,\r
413 IN VOID *Context\r
e4b7e2c9
RN
414 )\r
415{\r
1436aea4
MK
416 EFI_STATUS Status;\r
417 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
418 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
419 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
420 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
421 EFI_PHYSICAL_ADDRESS BaseAddress;\r
422 UINT64 Length;\r
423 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
424 UINT64 VariableStoreLength;\r
425 UINTN FtwMaxBlockSize;\r
4dbebc2d
MK
426 UINT32 NvStorageVariableSize;\r
427 UINT64 NvStorageVariableSize64;\r
e4b7e2c9
RN
428\r
429 //\r
430 // Ensure FTW protocol is installed.\r
431 //\r
1436aea4 432 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
e4b7e2c9 433 if (EFI_ERROR (Status)) {\r
1436aea4 434 return;\r
e4b7e2c9 435 }\r
2c4b18e0 436\r
4dbebc2d
MK
437 Status = GetVariableFlashNvStorageInfo (&NvStorageVariableBase, &NvStorageVariableSize64);\r
438 ASSERT_EFI_ERROR (Status);\r
439\r
440 Status = SafeUint64ToUint32 (NvStorageVariableSize64, &NvStorageVariableSize);\r
441 // This driver currently assumes the size will be UINT32 so assert the value is safe for now.\r
442 ASSERT_EFI_ERROR (Status);\r
443\r
444 VariableStoreBase = NvStorageVariableBase + mNvFvHeaderCache->HeaderLength;\r
445\r
2c4b18e0
SZ
446 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
447 if (!EFI_ERROR (Status)) {\r
4dbebc2d 448 ASSERT (NvStorageVariableSize <= FtwMaxBlockSize);\r
2c4b18e0
SZ
449 }\r
450\r
904e0ca9
SZ
451 //\r
452 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
453 //\r
454 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
455\r
e4b7e2c9
RN
456 //\r
457 // Find the proper FVB protocol for variable.\r
458 //\r
e4b7e2c9
RN
459 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
460 if (EFI_ERROR (Status)) {\r
1436aea4 461 return;\r
e4b7e2c9 462 }\r
1436aea4 463\r
e4b7e2c9
RN
464 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
465\r
466 //\r
467 // Mark the variable storage region of the FLASH as RUNTIME.\r
468 //\r
874c8434 469 VariableStoreLength = mNvVariableCache->Size;\r
1436aea4
MK
470 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
471 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
472 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
e4b7e2c9 473\r
1436aea4 474 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
e4b7e2c9 475 if (EFI_ERROR (Status)) {\r
e7bafeb9 476 DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
e4b7e2c9 477 } else {\r
f8829096
BS
478 if ((GcdDescriptor.Attributes & EFI_MEMORY_RUNTIME) == 0) {\r
479 Status = gDS->SetMemorySpaceAttributes (\r
480 BaseAddress,\r
481 Length,\r
482 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
483 );\r
484 if (EFI_ERROR (Status)) {\r
485 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
486 }\r
e4b7e2c9
RN
487 }\r
488 }\r
fa0737a8 489\r
dc9bd6ed 490 //\r
b59fd889 491 // Initializes variable write service after FTW was ready.\r
dc9bd6ed 492 //\r
b59fd889 493 VariableWriteServiceInitializeDxe ();\r
fa0737a8 494\r
e4b7e2c9
RN
495 //\r
496 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
497 //\r
498 gBS->CloseEvent (Event);\r
e4b7e2c9
RN
499}\r
500\r
b6490426
BB
501/**\r
502 This API function returns whether or not the policy engine is\r
503 currently being enforced.\r
504\r
505 @param[out] State Pointer to a return value for whether the policy enforcement\r
506 is currently enabled.\r
507\r
508 @retval EFI_SUCCESS\r
509 @retval Others An error has prevented this command from completing.\r
510\r
511**/\r
512EFI_STATUS\r
513EFIAPI\r
514ProtocolIsVariablePolicyEnabled (\r
1436aea4 515 OUT BOOLEAN *State\r
b6490426
BB
516 )\r
517{\r
518 *State = IsVariablePolicyEnabled ();\r
519 return EFI_SUCCESS;\r
520}\r
521\r
e4b7e2c9
RN
522/**\r
523 Variable Driver main entry point. The Variable driver places the 4 EFI\r
fa0737a8
SZ
524 runtime services in the EFI System Table and installs arch protocols\r
525 for variable read and write services being available. It also registers\r
e4b7e2c9
RN
526 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
527\r
fa0737a8 528 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
e4b7e2c9 529 @param[in] SystemTable A pointer to the EFI System Table.\r
fa0737a8 530\r
e4b7e2c9
RN
531 @retval EFI_SUCCESS Variable service successfully initialized.\r
532\r
533**/\r
534EFI_STATUS\r
535EFIAPI\r
536VariableServiceInitialize (\r
1436aea4
MK
537 IN EFI_HANDLE ImageHandle,\r
538 IN EFI_SYSTEM_TABLE *SystemTable\r
e4b7e2c9
RN
539 )\r
540{\r
1436aea4
MK
541 EFI_STATUS Status;\r
542 EFI_EVENT ReadyToBootEvent;\r
543 EFI_EVENT EndOfDxeEvent;\r
e4b7e2c9
RN
544\r
545 Status = VariableCommonInitialize ();\r
546 ASSERT_EFI_ERROR (Status);\r
547\r
548 Status = gBS->InstallMultipleProtocolInterfaces (\r
549 &mHandle,\r
550 &gEdkiiVariableLockProtocolGuid,\r
551 &mVariableLock,\r
efb01a10
SZ
552 NULL\r
553 );\r
554 ASSERT_EFI_ERROR (Status);\r
555\r
556 Status = gBS->InstallMultipleProtocolInterfaces (\r
557 &mHandle,\r
558 &gEdkiiVarCheckProtocolGuid,\r
559 &mVarCheck,\r
e4b7e2c9
RN
560 NULL\r
561 );\r
562 ASSERT_EFI_ERROR (Status);\r
563\r
564 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r
565 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
566 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r
567 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r
fa0737a8 568\r
e4b7e2c9
RN
569 //\r
570 // Now install the Variable Runtime Architectural protocol on a new handle.\r
571 //\r
572 Status = gBS->InstallProtocolInterface (\r
573 &mHandle,\r
fa0737a8 574 &gEfiVariableArchProtocolGuid,\r
e4b7e2c9
RN
575 EFI_NATIVE_INTERFACE,\r
576 NULL\r
577 );\r
578 ASSERT_EFI_ERROR (Status);\r
579\r
7cd69959
SZ
580 if (!PcdGetBool (PcdEmuVariableNvModeEnable)) {\r
581 //\r
582 // Register FtwNotificationEvent () notify function.\r
583 //\r
584 EfiCreateProtocolNotifyEvent (\r
585 &gEfiFaultTolerantWriteProtocolGuid,\r
586 TPL_CALLBACK,\r
587 FtwNotificationEvent,\r
588 (VOID *)SystemTable,\r
589 &mFtwRegistration\r
590 );\r
591 } else {\r
592 //\r
593 // Emulated non-volatile variable mode does not depend on FVB and FTW.\r
594 //\r
595 VariableWriteServiceInitializeDxe ();\r
596 }\r
e4b7e2c9
RN
597\r
598 Status = gBS->CreateEventEx (\r
599 EVT_NOTIFY_SIGNAL,\r
600 TPL_NOTIFY,\r
601 VariableClassAddressChangeEvent,\r
602 NULL,\r
603 &gEfiEventVirtualAddressChangeGuid,\r
604 &mVirtualAddressChangeEvent\r
605 );\r
606 ASSERT_EFI_ERROR (Status);\r
607\r
608 //\r
609 // Register the event handling function to reclaim variable for OS usage.\r
610 //\r
611 Status = EfiCreateEventReadyToBootEx (\r
fa0737a8
SZ
612 TPL_NOTIFY,\r
613 OnReadyToBoot,\r
614 NULL,\r
e4b7e2c9
RN
615 &ReadyToBootEvent\r
616 );\r
617 ASSERT_EFI_ERROR (Status);\r
618\r
619 //\r
620 // Register the event handling function to set the End Of DXE flag.\r
621 //\r
622 Status = gBS->CreateEventEx (\r
623 EVT_NOTIFY_SIGNAL,\r
3d7ebd64 624 TPL_CALLBACK,\r
e4b7e2c9
RN
625 OnEndOfDxe,\r
626 NULL,\r
627 &gEfiEndOfDxeEventGroupGuid,\r
628 &EndOfDxeEvent\r
629 );\r
630 ASSERT_EFI_ERROR (Status);\r
631\r
b6490426
BB
632 // Register and initialize the VariablePolicy engine.\r
633 Status = InitVariablePolicyLib (VariableServiceGetVariable);\r
634 ASSERT_EFI_ERROR (Status);\r
635 Status = VarCheckRegisterSetVariableCheckHandler (ValidateSetVariable);\r
636 ASSERT_EFI_ERROR (Status);\r
637 Status = gBS->InstallMultipleProtocolInterfaces (\r
1436aea4
MK
638 &mHandle,\r
639 &gEdkiiVariablePolicyProtocolGuid,\r
640 &mVariablePolicyProtocol,\r
641 NULL\r
642 );\r
b6490426
BB
643 ASSERT_EFI_ERROR (Status);\r
644\r
e4b7e2c9
RN
645 return EFI_SUCCESS;\r
646}\r