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