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