]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
MdeModulePkg Variable: Add some missing changes for 9b18845
[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 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 + mNvFvHeaderCache->HeaderLength;
406 VariableStoreLength = mNvVariableCache->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 if ((GcdDescriptor.Attributes & EFI_MEMORY_RUNTIME) == 0) {
416 Status = gDS->SetMemorySpaceAttributes (
417 BaseAddress,
418 Length,
419 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
420 );
421 if (EFI_ERROR (Status)) {
422 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
423 }
424 }
425 }
426
427 Status = VariableWriteServiceInitialize ();
428 if (EFI_ERROR (Status)) {
429 DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));
430 }
431
432 //
433 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
434 // Secure Boot Policy Variable change. Record their initial value.
435 //
436 RecordSecureBootPolicyVarData();
437
438 //
439 // Install the Variable Write Architectural protocol.
440 //
441 Status = gBS->InstallProtocolInterface (
442 &mHandle,
443 &gEfiVariableWriteArchProtocolGuid,
444 EFI_NATIVE_INTERFACE,
445 NULL
446 );
447 ASSERT_EFI_ERROR (Status);
448
449 //
450 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
451 //
452 gBS->CloseEvent (Event);
453
454 }
455
456
457 /**
458 Variable Driver main entry point. The Variable driver places the 4 EFI
459 runtime services in the EFI System Table and installs arch protocols
460 for variable read and write services being available. It also registers
461 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
462
463 @param[in] ImageHandle The firmware allocated handle for the EFI image.
464 @param[in] SystemTable A pointer to the EFI System Table.
465
466 @retval EFI_SUCCESS Variable service successfully initialized.
467
468 **/
469 EFI_STATUS
470 EFIAPI
471 VariableServiceInitialize (
472 IN EFI_HANDLE ImageHandle,
473 IN EFI_SYSTEM_TABLE *SystemTable
474 )
475 {
476 EFI_STATUS Status;
477 EFI_EVENT ReadyToBootEvent;
478 EFI_EVENT EndOfDxeEvent;
479
480 Status = VariableCommonInitialize ();
481 ASSERT_EFI_ERROR (Status);
482
483 Status = gBS->InstallMultipleProtocolInterfaces (
484 &mHandle,
485 &gEdkiiVariableLockProtocolGuid,
486 &mVariableLock,
487 NULL
488 );
489 ASSERT_EFI_ERROR (Status);
490
491 Status = gBS->InstallMultipleProtocolInterfaces (
492 &mHandle,
493 &gEdkiiVarCheckProtocolGuid,
494 &mVarCheck,
495 NULL
496 );
497 ASSERT_EFI_ERROR (Status);
498
499 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;
500 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;
501 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;
502 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;
503
504 //
505 // Now install the Variable Runtime Architectural protocol on a new handle.
506 //
507 Status = gBS->InstallProtocolInterface (
508 &mHandle,
509 &gEfiVariableArchProtocolGuid,
510 EFI_NATIVE_INTERFACE,
511 NULL
512 );
513 ASSERT_EFI_ERROR (Status);
514
515 //
516 // Register FtwNotificationEvent () notify function.
517 //
518 EfiCreateProtocolNotifyEvent (
519 &gEfiFaultTolerantWriteProtocolGuid,
520 TPL_CALLBACK,
521 FtwNotificationEvent,
522 (VOID *)SystemTable,
523 &mFtwRegistration
524 );
525
526 Status = gBS->CreateEventEx (
527 EVT_NOTIFY_SIGNAL,
528 TPL_NOTIFY,
529 VariableClassAddressChangeEvent,
530 NULL,
531 &gEfiEventVirtualAddressChangeGuid,
532 &mVirtualAddressChangeEvent
533 );
534 ASSERT_EFI_ERROR (Status);
535
536 //
537 // Register the event handling function to reclaim variable for OS usage.
538 //
539 Status = EfiCreateEventReadyToBootEx (
540 TPL_NOTIFY,
541 OnReadyToBoot,
542 NULL,
543 &ReadyToBootEvent
544 );
545 ASSERT_EFI_ERROR (Status);
546
547 //
548 // Register the event handling function to set the End Of DXE flag.
549 //
550 Status = gBS->CreateEventEx (
551 EVT_NOTIFY_SIGNAL,
552 TPL_CALLBACK,
553 OnEndOfDxe,
554 NULL,
555 &gEfiEndOfDxeEventGroupGuid,
556 &EndOfDxeEvent
557 );
558 ASSERT_EFI_ERROR (Status);
559
560 return EFI_SUCCESS;
561 }
562