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