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