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