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