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