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