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