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