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