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