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