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