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