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