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