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