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