]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
Fixed system hang issue with accessing to Boot Maintenance Manager
[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
285 ReclaimForOS ();\r
286 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
287 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
288 }\r
289}\r
290\r
291/**\r
292 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
293\r
294 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
295\r
296 @param Event Event whose notification function is being invoked.\r
297 @param Context Pointer to the notification function's context.\r
298\r
299**/\r
300VOID\r
301EFIAPI\r
302OnEndOfDxe (\r
303 EFI_EVENT Event,\r
304 VOID *Context\r
305 )\r
306{\r
307 mEndOfDxe = TRUE;\r
308}\r
309\r
310/**\r
311 Fault Tolerant Write protocol notification event handler.\r
312\r
313 Non-Volatile variable write may needs FTW protocol to reclaim when \r
314 writting variable.\r
315\r
316 @param[in] Event Event whose notification function is being invoked.\r
317 @param[in] Context Pointer to the notification function's context.\r
318 \r
319**/\r
320VOID\r
321EFIAPI\r
322FtwNotificationEvent (\r
323 IN EFI_EVENT Event,\r
324 IN VOID *Context\r
325 )\r
326{\r
327 EFI_STATUS Status;\r
328 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
329 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
330 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
331 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
332 EFI_PHYSICAL_ADDRESS BaseAddress;\r
333 UINT64 Length;\r
334 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
335 UINT64 VariableStoreLength;\r
2c4b18e0 336 UINTN FtwMaxBlockSize;\r
e4b7e2c9
RN
337\r
338 //\r
339 // Ensure FTW protocol is installed.\r
340 //\r
341 Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r
342 if (EFI_ERROR (Status)) {\r
343 return ;\r
344 }\r
2c4b18e0
SZ
345\r
346 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
347 if (!EFI_ERROR (Status)) {\r
348 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
349 }\r
350\r
e4b7e2c9
RN
351 //\r
352 // Find the proper FVB protocol for variable.\r
353 //\r
354 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
355 if (NvStorageVariableBase == 0) {\r
356 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
357 }\r
358 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
359 if (EFI_ERROR (Status)) {\r
360 return ;\r
361 }\r
362 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
363\r
364 //\r
365 // Mark the variable storage region of the FLASH as RUNTIME.\r
366 //\r
e7bafeb9 367 VariableStoreBase = NvStorageVariableBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageVariableBase))->HeaderLength);\r
e4b7e2c9
RN
368 VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r
369 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
370 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
371 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
372\r
373 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
374 if (EFI_ERROR (Status)) {\r
e7bafeb9 375 DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
e4b7e2c9
RN
376 } else {\r
377 Status = gDS->SetMemorySpaceAttributes (\r
378 BaseAddress,\r
379 Length,\r
380 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
381 );\r
382 if (EFI_ERROR (Status)) {\r
383 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
384 }\r
385 }\r
386 \r
387 Status = VariableWriteServiceInitialize ();\r
388 ASSERT_EFI_ERROR (Status);\r
389 \r
390 //\r
391 // Install the Variable Write Architectural protocol.\r
392 //\r
393 Status = gBS->InstallProtocolInterface (\r
394 &mHandle,\r
395 &gEfiVariableWriteArchProtocolGuid, \r
396 EFI_NATIVE_INTERFACE,\r
397 NULL\r
398 );\r
399 ASSERT_EFI_ERROR (Status);\r
400 \r
401 //\r
402 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
403 //\r
404 gBS->CloseEvent (Event);\r
405\r
406}\r
407\r
408\r
409/**\r
410 Variable Driver main entry point. The Variable driver places the 4 EFI\r
411 runtime services in the EFI System Table and installs arch protocols \r
412 for variable read and write services being availible. It also registers\r
413 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
414\r
415 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
416 @param[in] SystemTable A pointer to the EFI System Table.\r
417 \r
418 @retval EFI_SUCCESS Variable service successfully initialized.\r
419\r
420**/\r
421EFI_STATUS\r
422EFIAPI\r
423VariableServiceInitialize (\r
424 IN EFI_HANDLE ImageHandle,\r
425 IN EFI_SYSTEM_TABLE *SystemTable\r
426 )\r
427{\r
428 EFI_STATUS Status;\r
429 EFI_EVENT ReadyToBootEvent;\r
430 EFI_EVENT EndOfDxeEvent;\r
431\r
432 Status = VariableCommonInitialize ();\r
433 ASSERT_EFI_ERROR (Status);\r
434\r
435 Status = gBS->InstallMultipleProtocolInterfaces (\r
436 &mHandle,\r
437 &gEdkiiVariableLockProtocolGuid,\r
438 &mVariableLock,\r
efb01a10
SZ
439 NULL\r
440 );\r
441 ASSERT_EFI_ERROR (Status);\r
442\r
443 Status = gBS->InstallMultipleProtocolInterfaces (\r
444 &mHandle,\r
445 &gEdkiiVarCheckProtocolGuid,\r
446 &mVarCheck,\r
e4b7e2c9
RN
447 NULL\r
448 );\r
449 ASSERT_EFI_ERROR (Status);\r
450\r
451 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r
452 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
453 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r
454 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r
455 \r
456 //\r
457 // Now install the Variable Runtime Architectural protocol on a new handle.\r
458 //\r
459 Status = gBS->InstallProtocolInterface (\r
460 &mHandle,\r
461 &gEfiVariableArchProtocolGuid, \r
462 EFI_NATIVE_INTERFACE,\r
463 NULL\r
464 );\r
465 ASSERT_EFI_ERROR (Status);\r
466\r
467 //\r
468 // Register FtwNotificationEvent () notify function.\r
469 // \r
470 EfiCreateProtocolNotifyEvent (\r
471 &gEfiFaultTolerantWriteProtocolGuid,\r
472 TPL_CALLBACK,\r
473 FtwNotificationEvent,\r
474 (VOID *)SystemTable,\r
475 &mFtwRegistration\r
476 );\r
477\r
478 Status = gBS->CreateEventEx (\r
479 EVT_NOTIFY_SIGNAL,\r
480 TPL_NOTIFY,\r
481 VariableClassAddressChangeEvent,\r
482 NULL,\r
483 &gEfiEventVirtualAddressChangeGuid,\r
484 &mVirtualAddressChangeEvent\r
485 );\r
486 ASSERT_EFI_ERROR (Status);\r
487\r
488 //\r
489 // Register the event handling function to reclaim variable for OS usage.\r
490 //\r
491 Status = EfiCreateEventReadyToBootEx (\r
492 TPL_NOTIFY, \r
493 OnReadyToBoot, \r
494 NULL, \r
495 &ReadyToBootEvent\r
496 );\r
497 ASSERT_EFI_ERROR (Status);\r
498\r
499 //\r
500 // Register the event handling function to set the End Of DXE flag.\r
501 //\r
502 Status = gBS->CreateEventEx (\r
503 EVT_NOTIFY_SIGNAL,\r
504 TPL_NOTIFY,\r
505 OnEndOfDxe,\r
506 NULL,\r
507 &gEfiEndOfDxeEventGroupGuid,\r
508 &EndOfDxeEvent\r
509 );\r
510 ASSERT_EFI_ERROR (Status);\r
511\r
512 return EFI_SUCCESS;\r
513}\r
514\r