]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
521cb4ed24907c0bc58d64849689223aa5e3e7d8
[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 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetBlockSize);
240 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetPhysicalAddress);
241 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->GetAttributes);
242 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->SetAttributes);
243 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Read);
244 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->Write);
245 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance->EraseBlocks);
246 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->FvbInstance);
247 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLangCodes);
248 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->LangCodes);
249 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->PlatformLang);
250 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);
251 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);
252 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal->VariableGlobal.HobVariableBase);
253 EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);
254 EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);
255 EfiConvertPointer (0x0, (VOID **) &mNvFvHeaderCache);
256
257 if (mAuthContextOut.AddressPointer != NULL) {
258 for (Index = 0; Index < mAuthContextOut.AddressPointerCount; Index++) {
259 EfiConvertPointer (0x0, (VOID **) mAuthContextOut.AddressPointer[Index]);
260 }
261 }
262
263 if (mVarCheckAddressPointer != NULL) {
264 for (Index = 0; Index < mVarCheckAddressPointerCount; Index++) {
265 EfiConvertPointer (0x0, (VOID **) mVarCheckAddressPointer[Index]);
266 }
267 }
268 }
269
270
271 /**
272 Notification function of EVT_GROUP_READY_TO_BOOT event group.
273
274 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
275 When the Boot Manager is about to load and execute a boot option, it reclaims variable
276 storage if free size is below the threshold.
277
278 @param Event Event whose notification function is being invoked.
279 @param Context Pointer to the notification function's context.
280
281 **/
282 VOID
283 EFIAPI
284 OnReadyToBoot (
285 EFI_EVENT Event,
286 VOID *Context
287 )
288 {
289 if (!mEndOfDxe) {
290 MorLockInitAtEndOfDxe ();
291 //
292 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
293 //
294 mEndOfDxe = TRUE;
295 mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);
296 //
297 // The initialization for variable quota.
298 //
299 InitializeVariableQuota ();
300 }
301 ReclaimForOS ();
302 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
303 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
304 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);
305 } else {
306 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);
307 }
308 }
309
310 gBS->CloseEvent (Event);
311 }
312
313 /**
314 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
315
316 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
317
318 @param Event Event whose notification function is being invoked.
319 @param Context Pointer to the notification function's context.
320
321 **/
322 VOID
323 EFIAPI
324 OnEndOfDxe (
325 EFI_EVENT Event,
326 VOID *Context
327 )
328 {
329 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));
330 MorLockInitAtEndOfDxe ();
331 mEndOfDxe = TRUE;
332 mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);
333 //
334 // The initialization for variable quota.
335 //
336 InitializeVariableQuota ();
337 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe)) {
338 ReclaimForOS ();
339 }
340
341 gBS->CloseEvent (Event);
342 }
343
344 /**
345 Initializes variable write service for DXE.
346
347 **/
348 VOID
349 VariableWriteServiceInitializeDxe (
350 VOID
351 )
352 {
353 EFI_STATUS Status;
354
355 Status = VariableWriteServiceInitialize ();
356 if (EFI_ERROR (Status)) {
357 DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));
358 }
359
360 //
361 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
362 // Secure Boot Policy Variable change. Record their initial value.
363 //
364 RecordSecureBootPolicyVarData();
365
366 //
367 // Install the Variable Write Architectural protocol.
368 //
369 Status = gBS->InstallProtocolInterface (
370 &mHandle,
371 &gEfiVariableWriteArchProtocolGuid,
372 EFI_NATIVE_INTERFACE,
373 NULL
374 );
375 ASSERT_EFI_ERROR (Status);
376 }
377
378 /**
379 Fault Tolerant Write protocol notification event handler.
380
381 Non-Volatile variable write may needs FTW protocol to reclaim when
382 writting variable.
383
384 @param[in] Event Event whose notification function is being invoked.
385 @param[in] Context Pointer to the notification function's context.
386
387 **/
388 VOID
389 EFIAPI
390 FtwNotificationEvent (
391 IN EFI_EVENT Event,
392 IN VOID *Context
393 )
394 {
395 EFI_STATUS Status;
396 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
397 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
398 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
399 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
400 EFI_PHYSICAL_ADDRESS BaseAddress;
401 UINT64 Length;
402 EFI_PHYSICAL_ADDRESS VariableStoreBase;
403 UINT64 VariableStoreLength;
404 UINTN FtwMaxBlockSize;
405
406 //
407 // Ensure FTW protocol is installed.
408 //
409 Status = GetFtwProtocol ((VOID**) &FtwProtocol);
410 if (EFI_ERROR (Status)) {
411 return ;
412 }
413
414 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);
415 if (!EFI_ERROR (Status)) {
416 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);
417 }
418
419 NvStorageVariableBase = NV_STORAGE_VARIABLE_BASE;
420 VariableStoreBase = NvStorageVariableBase + mNvFvHeaderCache->HeaderLength;
421
422 //
423 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
424 //
425 mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;
426
427 //
428 // Find the proper FVB protocol for variable.
429 //
430 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
431 if (EFI_ERROR (Status)) {
432 return ;
433 }
434 mVariableModuleGlobal->FvbInstance = FvbProtocol;
435
436 //
437 // Mark the variable storage region of the FLASH as RUNTIME.
438 //
439 VariableStoreLength = mNvVariableCache->Size;
440 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);
441 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);
442 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
443
444 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
445 if (EFI_ERROR (Status)) {
446 DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));
447 } else {
448 if ((GcdDescriptor.Attributes & EFI_MEMORY_RUNTIME) == 0) {
449 Status = gDS->SetMemorySpaceAttributes (
450 BaseAddress,
451 Length,
452 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
453 );
454 if (EFI_ERROR (Status)) {
455 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
456 }
457 }
458 }
459
460 //
461 // Initializes variable write service after FTW was ready.
462 //
463 VariableWriteServiceInitializeDxe ();
464
465 //
466 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
467 //
468 gBS->CloseEvent (Event);
469
470 }
471
472
473 /**
474 Variable Driver main entry point. The Variable driver places the 4 EFI
475 runtime services in the EFI System Table and installs arch protocols
476 for variable read and write services being available. It also registers
477 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
478
479 @param[in] ImageHandle The firmware allocated handle for the EFI image.
480 @param[in] SystemTable A pointer to the EFI System Table.
481
482 @retval EFI_SUCCESS Variable service successfully initialized.
483
484 **/
485 EFI_STATUS
486 EFIAPI
487 VariableServiceInitialize (
488 IN EFI_HANDLE ImageHandle,
489 IN EFI_SYSTEM_TABLE *SystemTable
490 )
491 {
492 EFI_STATUS Status;
493 EFI_EVENT ReadyToBootEvent;
494 EFI_EVENT EndOfDxeEvent;
495
496 Status = VariableCommonInitialize ();
497 ASSERT_EFI_ERROR (Status);
498
499 Status = gBS->InstallMultipleProtocolInterfaces (
500 &mHandle,
501 &gEdkiiVariableLockProtocolGuid,
502 &mVariableLock,
503 NULL
504 );
505 ASSERT_EFI_ERROR (Status);
506
507 Status = gBS->InstallMultipleProtocolInterfaces (
508 &mHandle,
509 &gEdkiiVarCheckProtocolGuid,
510 &mVarCheck,
511 NULL
512 );
513 ASSERT_EFI_ERROR (Status);
514
515 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;
516 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;
517 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;
518 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;
519
520 //
521 // Now install the Variable Runtime Architectural protocol on a new handle.
522 //
523 Status = gBS->InstallProtocolInterface (
524 &mHandle,
525 &gEfiVariableArchProtocolGuid,
526 EFI_NATIVE_INTERFACE,
527 NULL
528 );
529 ASSERT_EFI_ERROR (Status);
530
531 //
532 // Register FtwNotificationEvent () notify function.
533 //
534 EfiCreateProtocolNotifyEvent (
535 &gEfiFaultTolerantWriteProtocolGuid,
536 TPL_CALLBACK,
537 FtwNotificationEvent,
538 (VOID *)SystemTable,
539 &mFtwRegistration
540 );
541
542 Status = gBS->CreateEventEx (
543 EVT_NOTIFY_SIGNAL,
544 TPL_NOTIFY,
545 VariableClassAddressChangeEvent,
546 NULL,
547 &gEfiEventVirtualAddressChangeGuid,
548 &mVirtualAddressChangeEvent
549 );
550 ASSERT_EFI_ERROR (Status);
551
552 //
553 // Register the event handling function to reclaim variable for OS usage.
554 //
555 Status = EfiCreateEventReadyToBootEx (
556 TPL_NOTIFY,
557 OnReadyToBoot,
558 NULL,
559 &ReadyToBootEvent
560 );
561 ASSERT_EFI_ERROR (Status);
562
563 //
564 // Register the event handling function to set the End Of DXE flag.
565 //
566 Status = gBS->CreateEventEx (
567 EVT_NOTIFY_SIGNAL,
568 TPL_CALLBACK,
569 OnEndOfDxe,
570 NULL,
571 &gEfiEndOfDxeEventGroupGuid,
572 &EndOfDxeEvent
573 );
574 ASSERT_EFI_ERROR (Status);
575
576 return EFI_SUCCESS;
577 }
578