]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
MdeModulePkg: Apply uncrustify changes
[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
e4b7e2c9
RN
426\r
427 //\r
428 // Ensure FTW protocol is installed.\r
429 //\r
1436aea4 430 Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r
e4b7e2c9 431 if (EFI_ERROR (Status)) {\r
1436aea4 432 return;\r
e4b7e2c9 433 }\r
2c4b18e0
SZ
434\r
435 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
436 if (!EFI_ERROR (Status)) {\r
437 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
438 }\r
439\r
904e0ca9 440 NvStorageVariableBase = NV_STORAGE_VARIABLE_BASE;\r
1436aea4 441 VariableStoreBase = NvStorageVariableBase + mNvFvHeaderCache->HeaderLength;\r
904e0ca9
SZ
442\r
443 //\r
444 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r
445 //\r
446 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r
447\r
e4b7e2c9
RN
448 //\r
449 // Find the proper FVB protocol for variable.\r
450 //\r
e4b7e2c9
RN
451 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
452 if (EFI_ERROR (Status)) {\r
1436aea4 453 return;\r
e4b7e2c9 454 }\r
1436aea4 455\r
e4b7e2c9
RN
456 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
457\r
458 //\r
459 // Mark the variable storage region of the FLASH as RUNTIME.\r
460 //\r
874c8434 461 VariableStoreLength = mNvVariableCache->Size;\r
1436aea4
MK
462 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
463 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
464 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
e4b7e2c9 465\r
1436aea4 466 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
e4b7e2c9 467 if (EFI_ERROR (Status)) {\r
e7bafeb9 468 DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
e4b7e2c9 469 } else {\r
f8829096
BS
470 if ((GcdDescriptor.Attributes & EFI_MEMORY_RUNTIME) == 0) {\r
471 Status = gDS->SetMemorySpaceAttributes (\r
472 BaseAddress,\r
473 Length,\r
474 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
475 );\r
476 if (EFI_ERROR (Status)) {\r
477 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
478 }\r
e4b7e2c9
RN
479 }\r
480 }\r
fa0737a8 481\r
dc9bd6ed 482 //\r
b59fd889 483 // Initializes variable write service after FTW was ready.\r
dc9bd6ed 484 //\r
b59fd889 485 VariableWriteServiceInitializeDxe ();\r
fa0737a8 486\r
e4b7e2c9
RN
487 //\r
488 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
489 //\r
490 gBS->CloseEvent (Event);\r
e4b7e2c9
RN
491}\r
492\r
b6490426
BB
493/**\r
494 This API function returns whether or not the policy engine is\r
495 currently being enforced.\r
496\r
497 @param[out] State Pointer to a return value for whether the policy enforcement\r
498 is currently enabled.\r
499\r
500 @retval EFI_SUCCESS\r
501 @retval Others An error has prevented this command from completing.\r
502\r
503**/\r
504EFI_STATUS\r
505EFIAPI\r
506ProtocolIsVariablePolicyEnabled (\r
1436aea4 507 OUT BOOLEAN *State\r
b6490426
BB
508 )\r
509{\r
510 *State = IsVariablePolicyEnabled ();\r
511 return EFI_SUCCESS;\r
512}\r
513\r
e4b7e2c9
RN
514/**\r
515 Variable Driver main entry point. The Variable driver places the 4 EFI\r
fa0737a8
SZ
516 runtime services in the EFI System Table and installs arch protocols\r
517 for variable read and write services being available. It also registers\r
e4b7e2c9
RN
518 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
519\r
fa0737a8 520 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
e4b7e2c9 521 @param[in] SystemTable A pointer to the EFI System Table.\r
fa0737a8 522\r
e4b7e2c9
RN
523 @retval EFI_SUCCESS Variable service successfully initialized.\r
524\r
525**/\r
526EFI_STATUS\r
527EFIAPI\r
528VariableServiceInitialize (\r
1436aea4
MK
529 IN EFI_HANDLE ImageHandle,\r
530 IN EFI_SYSTEM_TABLE *SystemTable\r
e4b7e2c9
RN
531 )\r
532{\r
1436aea4
MK
533 EFI_STATUS Status;\r
534 EFI_EVENT ReadyToBootEvent;\r
535 EFI_EVENT EndOfDxeEvent;\r
e4b7e2c9
RN
536\r
537 Status = VariableCommonInitialize ();\r
538 ASSERT_EFI_ERROR (Status);\r
539\r
540 Status = gBS->InstallMultipleProtocolInterfaces (\r
541 &mHandle,\r
542 &gEdkiiVariableLockProtocolGuid,\r
543 &mVariableLock,\r
efb01a10
SZ
544 NULL\r
545 );\r
546 ASSERT_EFI_ERROR (Status);\r
547\r
548 Status = gBS->InstallMultipleProtocolInterfaces (\r
549 &mHandle,\r
550 &gEdkiiVarCheckProtocolGuid,\r
551 &mVarCheck,\r
e4b7e2c9
RN
552 NULL\r
553 );\r
554 ASSERT_EFI_ERROR (Status);\r
555\r
556 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r
557 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
558 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r
559 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r
fa0737a8 560\r
e4b7e2c9
RN
561 //\r
562 // Now install the Variable Runtime Architectural protocol on a new handle.\r
563 //\r
564 Status = gBS->InstallProtocolInterface (\r
565 &mHandle,\r
fa0737a8 566 &gEfiVariableArchProtocolGuid,\r
e4b7e2c9
RN
567 EFI_NATIVE_INTERFACE,\r
568 NULL\r
569 );\r
570 ASSERT_EFI_ERROR (Status);\r
571\r
7cd69959
SZ
572 if (!PcdGetBool (PcdEmuVariableNvModeEnable)) {\r
573 //\r
574 // Register FtwNotificationEvent () notify function.\r
575 //\r
576 EfiCreateProtocolNotifyEvent (\r
577 &gEfiFaultTolerantWriteProtocolGuid,\r
578 TPL_CALLBACK,\r
579 FtwNotificationEvent,\r
580 (VOID *)SystemTable,\r
581 &mFtwRegistration\r
582 );\r
583 } else {\r
584 //\r
585 // Emulated non-volatile variable mode does not depend on FVB and FTW.\r
586 //\r
587 VariableWriteServiceInitializeDxe ();\r
588 }\r
e4b7e2c9
RN
589\r
590 Status = gBS->CreateEventEx (\r
591 EVT_NOTIFY_SIGNAL,\r
592 TPL_NOTIFY,\r
593 VariableClassAddressChangeEvent,\r
594 NULL,\r
595 &gEfiEventVirtualAddressChangeGuid,\r
596 &mVirtualAddressChangeEvent\r
597 );\r
598 ASSERT_EFI_ERROR (Status);\r
599\r
600 //\r
601 // Register the event handling function to reclaim variable for OS usage.\r
602 //\r
603 Status = EfiCreateEventReadyToBootEx (\r
fa0737a8
SZ
604 TPL_NOTIFY,\r
605 OnReadyToBoot,\r
606 NULL,\r
e4b7e2c9
RN
607 &ReadyToBootEvent\r
608 );\r
609 ASSERT_EFI_ERROR (Status);\r
610\r
611 //\r
612 // Register the event handling function to set the End Of DXE flag.\r
613 //\r
614 Status = gBS->CreateEventEx (\r
615 EVT_NOTIFY_SIGNAL,\r
3d7ebd64 616 TPL_CALLBACK,\r
e4b7e2c9
RN
617 OnEndOfDxe,\r
618 NULL,\r
619 &gEfiEndOfDxeEventGroupGuid,\r
620 &EndOfDxeEvent\r
621 );\r
622 ASSERT_EFI_ERROR (Status);\r
623\r
b6490426
BB
624 // Register and initialize the VariablePolicy engine.\r
625 Status = InitVariablePolicyLib (VariableServiceGetVariable);\r
626 ASSERT_EFI_ERROR (Status);\r
627 Status = VarCheckRegisterSetVariableCheckHandler (ValidateSetVariable);\r
628 ASSERT_EFI_ERROR (Status);\r
629 Status = gBS->InstallMultipleProtocolInterfaces (\r
1436aea4
MK
630 &mHandle,\r
631 &gEdkiiVariablePolicyProtocolGuid,\r
632 &mVariablePolicyProtocol,\r
633 NULL\r
634 );\r
b6490426
BB
635 ASSERT_EFI_ERROR (Status);\r
636\r
e4b7e2c9
RN
637 return EFI_SUCCESS;\r
638}\r