]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c
MdeModulePkg: Variable: Fix typo in variable measure
[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 - 2017, 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 //
295 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.
296 //
297 mEndOfDxe = TRUE;
298 mVarCheckAddressPointer = VarCheckLibInitializeAtEndOfDxe (&mVarCheckAddressPointerCount);
299 //
300 // The initialization for variable quota.
301 //
302 InitializeVariableQuota ();
303 }
304 ReclaimForOS ();
305 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
306 if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
307 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);
308 } else {
309 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);
310 }
311 }
312
313 gBS->CloseEvent (Event);
314 }
315
316 /**
317 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
318
319 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.
320
321 @param Event Event whose notification function is being invoked.
322 @param Context Pointer to the notification function's context.
323
324 **/
325 VOID
326 EFIAPI
327 OnEndOfDxe (
328 EFI_EVENT Event,
329 VOID *Context
330 )
331 {
332 DEBUG ((EFI_D_INFO, "[Variable]END_OF_DXE is signaled\n"));
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 Fault Tolerant Write protocol notification event handler.
348
349 Non-Volatile variable write may needs FTW protocol to reclaim when
350 writting variable.
351
352 @param[in] Event Event whose notification function is being invoked.
353 @param[in] Context Pointer to the notification function's context.
354
355 **/
356 VOID
357 EFIAPI
358 FtwNotificationEvent (
359 IN EFI_EVENT Event,
360 IN VOID *Context
361 )
362 {
363 EFI_STATUS Status;
364 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
365 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
366 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
367 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
368 EFI_PHYSICAL_ADDRESS BaseAddress;
369 UINT64 Length;
370 EFI_PHYSICAL_ADDRESS VariableStoreBase;
371 UINT64 VariableStoreLength;
372 UINTN FtwMaxBlockSize;
373
374 //
375 // Ensure FTW protocol is installed.
376 //
377 Status = GetFtwProtocol ((VOID**) &FtwProtocol);
378 if (EFI_ERROR (Status)) {
379 return ;
380 }
381
382 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);
383 if (!EFI_ERROR (Status)) {
384 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);
385 }
386
387 //
388 // Find the proper FVB protocol for variable.
389 //
390 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
391 if (NvStorageVariableBase == 0) {
392 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
393 }
394 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
395 if (EFI_ERROR (Status)) {
396 return ;
397 }
398 mVariableModuleGlobal->FvbInstance = FvbProtocol;
399
400 //
401 // Mark the variable storage region of the FLASH as RUNTIME.
402 //
403 VariableStoreBase = NvStorageVariableBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageVariableBase))->HeaderLength);
404 VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;
405 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);
406 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);
407 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
408
409 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
410 if (EFI_ERROR (Status)) {
411 DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));
412 } else {
413 Status = gDS->SetMemorySpaceAttributes (
414 BaseAddress,
415 Length,
416 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
417 );
418 if (EFI_ERROR (Status)) {
419 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));
420 }
421 }
422
423 Status = VariableWriteServiceInitialize ();
424 if (EFI_ERROR (Status)) {
425 DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));
426 }
427
428 //
429 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
430 // Secure Boot Policy Variable change. Record their initial value.
431 //
432 RecordSecureBootPolicyVarData();
433
434 //
435 // Install the Variable Write Architectural protocol.
436 //
437 Status = gBS->InstallProtocolInterface (
438 &mHandle,
439 &gEfiVariableWriteArchProtocolGuid,
440 EFI_NATIVE_INTERFACE,
441 NULL
442 );
443 ASSERT_EFI_ERROR (Status);
444
445 //
446 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.
447 //
448 gBS->CloseEvent (Event);
449
450 }
451
452
453 /**
454 Variable Driver main entry point. The Variable driver places the 4 EFI
455 runtime services in the EFI System Table and installs arch protocols
456 for variable read and write services being available. It also registers
457 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
458
459 @param[in] ImageHandle The firmware allocated handle for the EFI image.
460 @param[in] SystemTable A pointer to the EFI System Table.
461
462 @retval EFI_SUCCESS Variable service successfully initialized.
463
464 **/
465 EFI_STATUS
466 EFIAPI
467 VariableServiceInitialize (
468 IN EFI_HANDLE ImageHandle,
469 IN EFI_SYSTEM_TABLE *SystemTable
470 )
471 {
472 EFI_STATUS Status;
473 EFI_EVENT ReadyToBootEvent;
474 EFI_EVENT EndOfDxeEvent;
475
476 Status = VariableCommonInitialize ();
477 ASSERT_EFI_ERROR (Status);
478
479 Status = gBS->InstallMultipleProtocolInterfaces (
480 &mHandle,
481 &gEdkiiVariableLockProtocolGuid,
482 &mVariableLock,
483 NULL
484 );
485 ASSERT_EFI_ERROR (Status);
486
487 Status = gBS->InstallMultipleProtocolInterfaces (
488 &mHandle,
489 &gEdkiiVarCheckProtocolGuid,
490 &mVarCheck,
491 NULL
492 );
493 ASSERT_EFI_ERROR (Status);
494
495 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;
496 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;
497 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;
498 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;
499
500 //
501 // Now install the Variable Runtime Architectural protocol on a new handle.
502 //
503 Status = gBS->InstallProtocolInterface (
504 &mHandle,
505 &gEfiVariableArchProtocolGuid,
506 EFI_NATIVE_INTERFACE,
507 NULL
508 );
509 ASSERT_EFI_ERROR (Status);
510
511 //
512 // Register FtwNotificationEvent () notify function.
513 //
514 EfiCreateProtocolNotifyEvent (
515 &gEfiFaultTolerantWriteProtocolGuid,
516 TPL_CALLBACK,
517 FtwNotificationEvent,
518 (VOID *)SystemTable,
519 &mFtwRegistration
520 );
521
522 Status = gBS->CreateEventEx (
523 EVT_NOTIFY_SIGNAL,
524 TPL_NOTIFY,
525 VariableClassAddressChangeEvent,
526 NULL,
527 &gEfiEventVirtualAddressChangeGuid,
528 &mVirtualAddressChangeEvent
529 );
530 ASSERT_EFI_ERROR (Status);
531
532 //
533 // Register the event handling function to reclaim variable for OS usage.
534 //
535 Status = EfiCreateEventReadyToBootEx (
536 TPL_NOTIFY,
537 OnReadyToBoot,
538 NULL,
539 &ReadyToBootEvent
540 );
541 ASSERT_EFI_ERROR (Status);
542
543 //
544 // Register the event handling function to set the End Of DXE flag.
545 //
546 Status = gBS->CreateEventEx (
547 EVT_NOTIFY_SIGNAL,
548 TPL_NOTIFY,
549 OnEndOfDxe,
550 NULL,
551 &gEfiEndOfDxeEventGroupGuid,
552 &EndOfDxeEvent
553 );
554 ASSERT_EFI_ERROR (Status);
555
556 return EFI_SUCCESS;
557 }
558