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