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