]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableDxe.c
SecurityPkg Variable: Implement variable quota management.
[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
RN
250 EfiConvertPointer (0x0, (VOID **) &mSerializationRuntimeBuffer);\r
251 EfiConvertPointer (0x0, (VOID **) &mNvVariableCache);\r
4ccef561
DG
252 EfiConvertPointer (0x0, (VOID **) &mPubKeyStore);\r
253 EfiConvertPointer (0x0, (VOID **) &mCertDbStore);\r
17409b7a
SZ
254 EfiConvertPointer (0x0, (VOID **) &mHandlerTable);\r
255 for (Index = 0; Index < mNumberOfHandler; Index++) {\r
256 EfiConvertPointer (0x0, (VOID **) &mHandlerTable[Index]);\r
328e5d8c 257 }\r
17409b7a 258\r
328e5d8c
LE
259 Status = EfiConvertList (0x0, &mLockedVariableList);\r
260 ASSERT_EFI_ERROR (Status);\r
17409b7a
SZ
261\r
262 Status = EfiConvertList (0x0, &mVarCheckVariableList);\r
263 ASSERT_EFI_ERROR (Status);\r
e4b7e2c9
RN
264}\r
265\r
266\r
267/**\r
268 Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
269\r
270 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
271 When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
272 storage if free size is below the threshold.\r
273\r
274 @param Event Event whose notification function is being invoked.\r
275 @param Context Pointer to the notification function's context.\r
276\r
277**/\r
278VOID\r
279EFIAPI\r
280OnReadyToBoot (\r
281 EFI_EVENT Event,\r
282 VOID *Context\r
283 )\r
284{\r
285 //\r
286 // Set the End Of DXE bit in case the EFI_END_OF_DXE_EVENT_GROUP_GUID event is not signaled.\r
287 //\r
288 mEndOfDxe = TRUE;\r
952ba83c
SZ
289 //\r
290 // The initialization for variable quota.\r
291 //\r
292 InitializeVariableQuota ();\r
e4b7e2c9
RN
293 ReclaimForOS ();\r
294 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
295 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);\r
296 }\r
297}\r
298\r
299/**\r
300 Notification function of EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
301\r
302 This is a notification function registered on EFI_END_OF_DXE_EVENT_GROUP_GUID event group.\r
303\r
304 @param Event Event whose notification function is being invoked.\r
305 @param Context Pointer to the notification function's context.\r
306\r
307**/\r
308VOID\r
309EFIAPI\r
310OnEndOfDxe (\r
311 EFI_EVENT Event,\r
312 VOID *Context\r
313 )\r
314{\r
315 mEndOfDxe = TRUE;\r
952ba83c
SZ
316 //\r
317 // The initialization for variable quota.\r
318 //\r
319 InitializeVariableQuota ();\r
e4b7e2c9
RN
320}\r
321\r
322/**\r
323 Fault Tolerant Write protocol notification event handler.\r
324\r
325 Non-Volatile variable write may needs FTW protocol to reclaim when\r
326 writting variable.\r
327\r
328 @param[in] Event Event whose notification function is being invoked.\r
329 @param[in] Context Pointer to the notification function's context.\r
330\r
331**/\r
332VOID\r
333EFIAPI\r
334FtwNotificationEvent (\r
335 IN EFI_EVENT Event,\r
336 IN VOID *Context\r
337 )\r
338{\r
339 EFI_STATUS Status;\r
340 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
341 EFI_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;\r
342 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;\r
343 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
344 EFI_PHYSICAL_ADDRESS BaseAddress;\r
345 UINT64 Length;\r
346 EFI_PHYSICAL_ADDRESS VariableStoreBase;\r
347 UINT64 VariableStoreLength;\r
41982ebb 348 UINTN FtwMaxBlockSize;\r
e4b7e2c9
RN
349\r
350 //\r
351 // Ensure FTW protocol is installed.\r
352 //\r
353 Status = GetFtwProtocol ((VOID**) &FtwProtocol);\r
354 if (EFI_ERROR (Status)) {\r
355 return ;\r
356 }\r
357\r
41982ebb
SZ
358 Status = FtwProtocol->GetMaxBlockSize (FtwProtocol, &FtwMaxBlockSize);\r
359 if (!EFI_ERROR (Status)) {\r
360 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize) <= FtwMaxBlockSize);\r
361 }\r
362\r
e4b7e2c9
RN
363 //\r
364 // Find the proper FVB protocol for variable.\r
365 //\r
366 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r
367 if (NvStorageVariableBase == 0) {\r
368 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r
369 }\r
370 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);\r
371 if (EFI_ERROR (Status)) {\r
372 return ;\r
373 }\r
374 mVariableModuleGlobal->FvbInstance = FvbProtocol;\r
375\r
376 //\r
377 // Mark the variable storage region of the FLASH as RUNTIME.\r
378 //\r
7c064c31 379 VariableStoreBase = NvStorageVariableBase + (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(NvStorageVariableBase))->HeaderLength);\r
e4b7e2c9
RN
380 VariableStoreLength = ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase)->Size;\r
381 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r
382 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r
383 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r
384\r
385 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r
386 if (EFI_ERROR (Status)) {\r
7c064c31 387 DEBUG ((DEBUG_WARN, "Variable driver failed to get flash memory attribute.\n"));\r
e4b7e2c9
RN
388 } else {\r
389 Status = gDS->SetMemorySpaceAttributes (\r
390 BaseAddress,\r
391 Length,\r
392 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r
393 );\r
394 if (EFI_ERROR (Status)) {\r
395 DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r
396 }\r
397 }\r
398\r
399 Status = VariableWriteServiceInitialize ();\r
25da08c8
DG
400 if (EFI_ERROR (Status)) {\r
401 DEBUG ((DEBUG_ERROR, "Variable write service initialization failed. Status = %r\n", Status));\r
402 }\r
e4b7e2c9
RN
403\r
404 //\r
405 // Install the Variable Write Architectural protocol.\r
406 //\r
407 Status = gBS->InstallProtocolInterface (\r
408 &mHandle,\r
409 &gEfiVariableWriteArchProtocolGuid,\r
410 EFI_NATIVE_INTERFACE,\r
411 NULL\r
412 );\r
413 ASSERT_EFI_ERROR (Status);\r
414\r
415 //\r
416 // Close the notify event to avoid install gEfiVariableWriteArchProtocolGuid again.\r
417 //\r
418 gBS->CloseEvent (Event);\r
419\r
420}\r
421\r
422\r
423/**\r
424 Variable Driver main entry point. The Variable driver places the 4 EFI\r
425 runtime services in the EFI System Table and installs arch protocols\r
426 for variable read and write services being available. It also registers\r
427 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
428\r
429 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
430 @param[in] SystemTable A pointer to the EFI System Table.\r
431\r
432 @retval EFI_SUCCESS Variable service successfully initialized.\r
433\r
434**/\r
435EFI_STATUS\r
436EFIAPI\r
437VariableServiceInitialize (\r
438 IN EFI_HANDLE ImageHandle,\r
439 IN EFI_SYSTEM_TABLE *SystemTable\r
440 )\r
441{\r
442 EFI_STATUS Status;\r
443 EFI_EVENT ReadyToBootEvent;\r
444 EFI_EVENT EndOfDxeEvent;\r
445\r
446 Status = VariableCommonInitialize ();\r
447 ASSERT_EFI_ERROR (Status);\r
448\r
449 Status = gBS->InstallMultipleProtocolInterfaces (\r
450 &mHandle,\r
451 &gEdkiiVariableLockProtocolGuid,\r
452 &mVariableLock,\r
453 NULL\r
454 );\r
455 ASSERT_EFI_ERROR (Status);\r
456\r
17409b7a
SZ
457 Status = gBS->InstallMultipleProtocolInterfaces (\r
458 &mHandle,\r
459 &gEdkiiVarCheckProtocolGuid,\r
460 &mVarCheck,\r
461 NULL\r
462 );\r
463 ASSERT_EFI_ERROR (Status);\r
464\r
e4b7e2c9
RN
465 SystemTable->RuntimeServices->GetVariable = VariableServiceGetVariable;\r
466 SystemTable->RuntimeServices->GetNextVariableName = VariableServiceGetNextVariableName;\r
467 SystemTable->RuntimeServices->SetVariable = VariableServiceSetVariable;\r
468 SystemTable->RuntimeServices->QueryVariableInfo = VariableServiceQueryVariableInfo;\r
469\r
470 //\r
471 // Now install the Variable Runtime Architectural protocol on a new handle.\r
472 //\r
473 Status = gBS->InstallProtocolInterface (\r
474 &mHandle,\r
475 &gEfiVariableArchProtocolGuid,\r
476 EFI_NATIVE_INTERFACE,\r
477 NULL\r
478 );\r
479 ASSERT_EFI_ERROR (Status);\r
480\r
481 //\r
482 // Register FtwNotificationEvent () notify function.\r
483 //\r
484 EfiCreateProtocolNotifyEvent (\r
485 &gEfiFaultTolerantWriteProtocolGuid,\r
486 TPL_CALLBACK,\r
487 FtwNotificationEvent,\r
488 (VOID *)SystemTable,\r
489 &mFtwRegistration\r
490 );\r
491\r
492 Status = gBS->CreateEventEx (\r
493 EVT_NOTIFY_SIGNAL,\r
494 TPL_NOTIFY,\r
495 VariableClassAddressChangeEvent,\r
496 NULL,\r
497 &gEfiEventVirtualAddressChangeGuid,\r
498 &mVirtualAddressChangeEvent\r
499 );\r
500 ASSERT_EFI_ERROR (Status);\r
501\r
502 //\r
503 // Register the event handling function to reclaim variable for OS usage.\r
504 //\r
505 Status = EfiCreateEventReadyToBootEx (\r
506 TPL_NOTIFY,\r
507 OnReadyToBoot,\r
508 NULL,\r
509 &ReadyToBootEvent\r
510 );\r
511 ASSERT_EFI_ERROR (Status);\r
512\r
513 //\r
514 // Register the event handling function to set the End Of DXE flag.\r
515 //\r
516 Status = gBS->CreateEventEx (\r
517 EVT_NOTIFY_SIGNAL,\r
518 TPL_NOTIFY,\r
519 OnEndOfDxe,\r
520 NULL,\r
521 &gEfiEndOfDxeEventGroupGuid,\r
522 &EndOfDxeEvent\r
523 );\r
524 ASSERT_EFI_ERROR (Status);\r
525\r
526 return EFI_SUCCESS;\r
527}\r
528\r