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