2 The sample implementation for SMM variable protocol. And this driver
3 implements an SMI handler to communicate with the DXE runtime driver
4 to provide variable services.
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable data and communicate buffer in SMM mode.
8 This external input must be validated carefully to avoid security issue like
9 buffer overflow, integer overflow.
11 SmmVariableHandler() will receive untrusted input and do basic validation.
13 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
14 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
15 SmmVariableGetStatistics() should also do validation based on its own knowledge.
17 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 #include <Protocol/SmmVariable.h>
29 #include <Protocol/SmmFirmwareVolumeBlock.h>
30 #include <Protocol/SmmFaultTolerantWrite.h>
31 #include <Library/SmmServicesTableLib.h>
33 #include <Guid/AuthenticatedVariableFormat.h>
34 #include <Guid/SmmVariableCommon.h>
37 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
38 EFI_HANDLE mSmmVariableHandle
= NULL
;
39 EFI_HANDLE mVariableHandle
= NULL
;
40 BOOLEAN mAtRuntime
= FALSE
;
41 EFI_GUID mZeroGuid
= {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
43 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable
= {
44 VariableServiceGetVariable
,
45 VariableServiceGetNextVariableName
,
46 VariableServiceSetVariable
,
47 VariableServiceQueryVariableInfo
52 Return TRUE if ExitBootServices () has been called.
54 @retval TRUE If ExitBootServices () has been called.
65 Initializes a basic mutual exclusion lock.
67 This function initializes a basic mutual exclusion lock to the released state
68 and returns the lock. Each lock provides mutual exclusion access at its task
69 priority level. Since there is no preemption or multiprocessor support in EFI,
70 acquiring the lock only consists of raising to the locks TPL.
71 If Lock is NULL, then ASSERT().
72 If Priority is not a valid TPL value, then ASSERT().
74 @param Lock A pointer to the lock data structure to initialize.
75 @param Priority EFI TPL is associated with the lock.
82 IN OUT EFI_LOCK
*Lock
,
90 Acquires lock only at boot time. Simply returns at runtime.
92 This is a temperary function that will be removed when
93 EfiAcquireLock() in UefiLib can handle the call in UEFI
94 Runtimer driver in RT phase.
95 It calls EfiAcquireLock() at boot time, and simply returns
98 @param Lock A pointer to the lock to acquire.
102 AcquireLockOnlyAtBootTime (
111 Releases lock only at boot time. Simply returns at runtime.
113 This is a temperary function which will be removed when
114 EfiReleaseLock() in UefiLib can handle the call in UEFI
115 Runtimer driver in RT phase.
116 It calls EfiReleaseLock() at boot time and simply returns
119 @param Lock A pointer to the lock to release.
123 ReleaseLockOnlyAtBootTime (
131 Retrive the SMM Fault Tolerent Write protocol interface.
133 @param[out] FtwProtocol The interface of SMM Ftw protocol
135 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
136 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
137 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
142 OUT VOID
**FtwProtocol
148 // Locate Smm Fault Tolerent Write protocol
150 Status
= gSmst
->SmmLocateProtocol (
151 &gEfiSmmFaultTolerantWriteProtocolGuid
,
160 Retrive the SMM FVB protocol interface by HANDLE.
162 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
163 reading, writing, and erasing the target block.
164 @param[out] FvBlock The interface of SMM FVB protocol
166 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
167 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
168 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
173 IN EFI_HANDLE FvBlockHandle
,
174 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
178 // To get the SMM FVB protocol interface on the handle
180 return gSmst
->SmmHandleProtocol (
182 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
189 Function returns an array of handles that support the SMM FVB protocol
190 in a buffer allocated from pool.
192 @param[out] NumberHandles The number of handles returned in Buffer.
193 @param[out] Buffer A pointer to the buffer to return the requested
194 array of handles that support SMM FVB protocol.
196 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
197 handles in Buffer was returned in NumberHandles.
198 @retval EFI_NOT_FOUND No SMM FVB handle was found.
199 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
200 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
204 GetFvbCountAndBuffer (
205 OUT UINTN
*NumberHandles
,
206 OUT EFI_HANDLE
**Buffer
212 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
213 return EFI_INVALID_PARAMETER
;
219 Status
= gSmst
->SmmLocateHandle (
221 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
226 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
227 return EFI_NOT_FOUND
;
230 *Buffer
= AllocatePool (BufferSize
);
231 if (*Buffer
== NULL
) {
232 return EFI_OUT_OF_RESOURCES
;
235 Status
= gSmst
->SmmLocateHandle (
237 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
243 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
244 if (EFI_ERROR(Status
)) {
253 Get the variable statistics information from the information buffer pointed by gVariableInfo.
255 Caution: This function may be invoked at SMM runtime.
256 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
258 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
259 On input, point to the variable information returned last time. if
260 InfoEntry->VendorGuid is zero, return the first information.
261 On output, point to the next variable information.
262 @param[in, out] InfoSize On input, the size of the variable information buffer.
263 On output, the returned variable information size.
265 @retval EFI_SUCCESS The variable information is found and returned successfully.
266 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
267 PcdVariableCollectStatistics should be set TRUE to support it.
268 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
269 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
273 SmmVariableGetStatistics (
274 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
275 IN OUT UINTN
*InfoSize
278 VARIABLE_INFO_ENTRY
*VariableInfo
;
280 UINTN StatisticsInfoSize
;
283 if (InfoEntry
== NULL
) {
284 return EFI_INVALID_PARAMETER
;
287 VariableInfo
= gVariableInfo
;
288 if (VariableInfo
== NULL
) {
289 return EFI_UNSUPPORTED
;
292 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
293 if (*InfoSize
< sizeof (VARIABLE_INFO_ENTRY
)) {
294 *InfoSize
= StatisticsInfoSize
;
295 return EFI_BUFFER_TOO_SMALL
;
297 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
299 if (CompareGuid (&InfoEntry
->VendorGuid
, &mZeroGuid
)) {
301 // Return the first variable info
303 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
304 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
305 *InfoSize
= StatisticsInfoSize
;
310 // Get the next variable info
312 while (VariableInfo
!= NULL
) {
313 if (CompareGuid (&VariableInfo
->VendorGuid
, &InfoEntry
->VendorGuid
)) {
314 NameLength
= StrSize (VariableInfo
->Name
);
315 if (NameLength
== StrSize (InfoName
)) {
316 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
318 // Find the match one
320 VariableInfo
= VariableInfo
->Next
;
325 VariableInfo
= VariableInfo
->Next
;
328 if (VariableInfo
== NULL
) {
334 // Output the new variable info
336 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
337 if (*InfoSize
< StatisticsInfoSize
) {
338 *InfoSize
= StatisticsInfoSize
;
339 return EFI_BUFFER_TOO_SMALL
;
342 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
343 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
344 *InfoSize
= StatisticsInfoSize
;
351 Communication service SMI Handler entry.
353 This SMI handler provides services for the variable wrapper driver.
355 Caution: This function may receive untrusted input.
356 This variable data and communicate buffer are external input, so this function will do basic validation.
357 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
358 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
359 SmmVariableGetStatistics() should also do validation based on its own knowledge.
361 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
362 @param[in] RegisterContext Points to an optional handler context which was specified when the
363 handler was registered.
364 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
365 be conveyed from a non-SMM environment into an SMM environment.
366 @param[in, out] CommBufferSize The size of the CommBuffer.
368 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
369 should still be called.
370 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
372 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
374 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
375 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
381 IN EFI_HANDLE DispatchHandle
,
382 IN CONST VOID
*RegisterContext
,
383 IN OUT VOID
*CommBuffer
,
384 IN OUT UINTN
*CommBufferSize
388 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
389 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
390 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
391 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
392 VARIABLE_INFO_ENTRY
*VariableInfo
;
395 if (CommBuffer
== NULL
) {
396 return EFI_INVALID_PARAMETER
;
399 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
400 switch (SmmVariableFunctionHeader
->Function
) {
401 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
402 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
403 Status
= VariableServiceGetVariable (
404 SmmVariableHeader
->Name
,
405 &SmmVariableHeader
->Guid
,
406 &SmmVariableHeader
->Attributes
,
407 &SmmVariableHeader
->DataSize
,
408 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
412 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
413 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) SmmVariableFunctionHeader
->Data
;
414 Status
= VariableServiceGetNextVariableName (
415 &GetNextVariableName
->NameSize
,
416 GetNextVariableName
->Name
,
417 &GetNextVariableName
->Guid
421 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
422 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
423 Status
= VariableServiceSetVariable (
424 SmmVariableHeader
->Name
,
425 &SmmVariableHeader
->Guid
,
426 SmmVariableHeader
->Attributes
,
427 SmmVariableHeader
->DataSize
,
428 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
432 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
433 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
434 Status
= VariableServiceQueryVariableInfo (
435 QueryVariableInfo
->Attributes
,
436 &QueryVariableInfo
->MaximumVariableStorageSize
,
437 &QueryVariableInfo
->RemainingVariableStorageSize
,
438 &QueryVariableInfo
->MaximumVariableSize
442 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
444 Status
= EFI_SUCCESS
;
447 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
449 Status
= EFI_SUCCESS
;
452 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
453 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
454 InfoSize
= *CommBufferSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER
, Data
);
455 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
456 *CommBufferSize
= InfoSize
+ OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER
, Data
);
461 Status
= EFI_UNSUPPORTED
;
464 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
471 SMM Fault Tolerant Write protocol notification event handler.
473 Non-Volatile variable write may needs FTW protocol to reclaim when
476 @param Protocol Points to the protocol's unique identifier
477 @param Interface Points to the interface instance
478 @param Handle The handle on which the interface was installed
480 @retval EFI_SUCCESS SmmEventCallback runs successfully
481 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
486 SmmFtwNotificationEvent (
487 IN CONST EFI_GUID
*Protocol
,
493 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
494 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
495 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
497 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
502 // Ensure SMM FTW protocol is installed.
504 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
505 if (EFI_ERROR (Status
)) {
510 // Find the proper FVB protocol for variable.
512 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
513 if (NvStorageVariableBase
== 0) {
514 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
516 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
517 if (EFI_ERROR (Status
)) {
518 return EFI_NOT_FOUND
;
521 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
523 Status
= VariableWriteServiceInitialize ();
524 ASSERT_EFI_ERROR (Status
);
527 // Notify the variable wrapper driver the variable write service is ready
529 Status
= gBS
->InstallProtocolInterface (
531 &gSmmVariableWriteGuid
,
532 EFI_NATIVE_INTERFACE
,
535 ASSERT_EFI_ERROR (Status
);
542 Variable Driver main entry point. The Variable driver places the 4 EFI
543 runtime services in the EFI System Table and installs arch protocols
544 for variable read and write services being available. It also registers
545 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
547 @param[in] ImageHandle The firmware allocated handle for the EFI image.
548 @param[in] SystemTable A pointer to the EFI System Table.
550 @retval EFI_SUCCESS Variable service successfully initialized.
555 VariableServiceInitialize (
556 IN EFI_HANDLE ImageHandle
,
557 IN EFI_SYSTEM_TABLE
*SystemTable
561 EFI_HANDLE VariableHandle
;
562 VOID
*SmmFtwRegistration
;
565 // Variable initialize.
567 Status
= VariableCommonInitialize ();
568 ASSERT_EFI_ERROR (Status
);
571 // Install the Smm Variable Protocol on a new handle.
573 VariableHandle
= NULL
;
574 Status
= gSmst
->SmmInstallProtocolInterface (
576 &gEfiSmmVariableProtocolGuid
,
577 EFI_NATIVE_INTERFACE
,
580 ASSERT_EFI_ERROR (Status
);
583 /// Register SMM variable SMI handler
585 VariableHandle
= NULL
;
586 Status
= gSmst
->SmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
587 ASSERT_EFI_ERROR (Status
);
590 // Notify the variable wrapper driver the variable service is ready
592 Status
= SystemTable
->BootServices
->InstallProtocolInterface (
594 &gEfiSmmVariableProtocolGuid
,
595 EFI_NATIVE_INTERFACE
,
598 ASSERT_EFI_ERROR (Status
);
601 // Register FtwNotificationEvent () notify function.
603 Status
= gSmst
->SmmRegisterProtocolNotify (
604 &gEfiSmmFaultTolerantWriteProtocolGuid
,
605 SmmFtwNotificationEvent
,
608 ASSERT_EFI_ERROR (Status
);
610 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);