]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c
Add security package to repository.
[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
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
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
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
27 \r
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
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
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
134 ); \r
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
148 \r
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
169 in a buffer allocated from pool. \r
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
180 \r
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
236 EfiConvertPointer (0x0, (VOID **) &mStorageArea); \r
237 EfiConvertPointer (0x0, (VOID **) &mNvVariableCache); \r
238}\r
239\r
240\r
241/**\r
242 Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
243\r
244 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
245 When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
246 storage if free size is below the threshold.\r
247\r
248 @param Event Event whose notification function is being invoked.\r
249 @param Context Pointer to the notification function's context.\r
250\r
251**/\r
252VOID\r
253EFIAPI\r
254OnReadyToBoot (\r
255 EFI_EVENT Event,\r
256 VOID *Context\r
257 )\r
258{\r
259 ReclaimForOS ();\r
260 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
261 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
262 }\r
263}\r
264\r
265\r
266/**\r
267 Fault Tolerant Write protocol notification event handler.\r
268\r
269 Non-Volatile variable write may needs FTW protocol to reclaim when \r
270 writting variable.\r
271\r
272 @param[in] Event Event whose notification function is being invoked.\r
273 @param[in] Context Pointer to the notification function's context.\r
274 \r
275**/\r
276VOID\r
277EFIAPI\r
278FtwNotificationEvent (\r
279 IN EFI_EVENT Event,\r
280 IN VOID *Context\r
281 )\r
282{\r
283 EFI_STATUS Status;\r
284 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
285 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
286 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
287 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
288 EFI_PHYSICAL_ADDRESS BaseAddress;\r
289 UINT64 Length;\r
290 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
291 UINT64 VariableStoreLength;\r
292\r
293 //\r
294 // Ensure FTW protocol is installed.\r
295 //\r
296 Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r
297 if (EFI_ERROR (Status)) {\r
298 return ;\r
299 }\r
300 \r
301 //\r
302 // Find the proper FVB protocol for variable.\r
303 //\r
304 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
305 if (NvStorageVariableBase == 0) {\r
306 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
307 }\r
308 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
309 if (EFI_ERROR (Status)) {\r
310 return ;\r
311 }\r
312 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
313\r
314 //\r
315 // Mark the variable storage region of the FLASH as RUNTIME.\r
316 //\r
317 VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r
318 VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r
319 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
320 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
321 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
322\r
323 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
324 if (EFI_ERROR (Status)) {\r
325 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
326 } else {\r
327 Status = gDS->SetMemorySpaceAttributes (\r
328 BaseAddress,\r
329 Length,\r
330 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
331 );\r
332 if (EFI_ERROR (Status)) {\r
333 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
334 }\r
335 }\r
336 \r
337 Status = VariableWriteServiceInitialize ();\r
338 ASSERT_EFI_ERROR (Status);\r
339 \r
340 //\r
341 // Install the Variable Write Architectural protocol.\r
342 //\r
343 Status = gBS->InstallProtocolInterface (\r
344 &mHandle,\r
345 &gEfiVariableWriteArchProtocolGuid, \r
346 EFI_NATIVE_INTERFACE,\r
347 NULL\r
348 );\r
349 ASSERT_EFI_ERROR (Status);\r
350 \r
351 //\r
352 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
353 //\r
354 gBS->CloseEvent (Event);\r
355\r
356}\r
357\r
358\r
359/**\r
360 Variable Driver main entry point. The Variable driver places the 4 EFI\r
361 runtime services in the EFI System Table and installs arch protocols \r
362 for variable read and write services being available. It also registers\r
363 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
364\r
365 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
366 @param[in] SystemTable A pointer to the EFI System Table.\r
367 \r
368 @retval EFI_SUCCESS Variable service successfully initialized.\r
369\r
370**/\r
371EFI_STATUS\r
372EFIAPI\r
373VariableServiceInitialize (\r
374 IN EFI_HANDLE ImageHandle,\r
375 IN EFI_SYSTEM_TABLE *SystemTable\r
376 )\r
377{\r
378 EFI_STATUS Status;\r
379 EFI_EVENT ReadyToBootEvent; \r
380\r
381 Status = VariableCommonInitialize ();\r
382 ASSERT_EFI_ERROR (Status);\r
383\r
384 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r
385 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
386 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r
387 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r
388 \r
389 //\r
390 // Now install the Variable Runtime Architectural protocol on a new handle.\r
391 //\r
392 Status = gBS->InstallProtocolInterface (\r
393 &mHandle,\r
394 &gEfiVariableArchProtocolGuid, \r
395 EFI_NATIVE_INTERFACE,\r
396 NULL\r
397 );\r
398 ASSERT_EFI_ERROR (Status);\r
399\r
400 //\r
401 // Register FtwNotificationEvent () notify function.\r
402 // \r
403 EfiCreateProtocolNotifyEvent (\r
404 &gEfiFaultTolerantWriteProtocolGuid,\r
405 TPL_CALLBACK,\r
406 FtwNotificationEvent,\r
407 (VOID *)SystemTable,\r
408 &mFtwRegistration\r
409 );\r
410\r
411 Status = gBS->CreateEventEx (\r
412 EVT_NOTIFY_SIGNAL,\r
413 TPL_NOTIFY,\r
414 VariableClassAddressChangeEvent,\r
415 NULL,\r
416 &gEfiEventVirtualAddressChangeGuid,\r
417 &mVirtualAddressChangeEvent\r
418 );\r
419 ASSERT_EFI_ERROR (Status);\r
420\r
421 //\r
422 // Register the event handling function to reclaim variable for OS usage.\r
423 //\r
424 Status = EfiCreateEventReadyToBootEx (\r
425 TPL_NOTIFY, \r
426 OnReadyToBoot, \r
427 NULL, \r
428 &ReadyToBootEvent\r
429 );\r
430\r
431 return EFI_SUCCESS;\r
432}\r
433\r