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 - 2013, 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 <Protocol/SmmAccess2.h>
32 #include <Protocol/SmmEndOfDxe.h>
34 #include <Library/SmmServicesTableLib.h>
36 #include <Guid/AuthenticatedVariableFormat.h>
37 #include <Guid/SmmVariableCommon.h>
40 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
41 UINTN mSmramRangeCount
;
43 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
44 EFI_HANDLE mSmmVariableHandle
= NULL
;
45 EFI_HANDLE mVariableHandle
= NULL
;
46 BOOLEAN mAtRuntime
= FALSE
;
47 EFI_GUID mZeroGuid
= {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
48 UINT8
*mVariableBufferPayload
= NULL
;
49 UINTN mVariableBufferPayloadSize
;
50 extern BOOLEAN mEndOfDxe
;
51 extern BOOLEAN mEnableLocking
;
55 This code sets variable in storage blocks (Volatile or Non-Volatile).
57 @param VariableName Name of Variable to be found.
58 @param VendorGuid Variable vendor GUID.
59 @param Attributes Attribute value of the variable found
60 @param DataSize Size of Data found. If size is less than the
61 data, this value contains the required size.
62 @param Data Data pointer.
64 @return EFI_INVALID_PARAMETER Invalid parameter.
65 @return EFI_SUCCESS Set successfully.
66 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
67 @return EFI_NOT_FOUND Not found.
68 @return EFI_WRITE_PROTECTED Variable is read-only.
73 SmmVariableSetVariable (
74 IN CHAR16
*VariableName
,
75 IN EFI_GUID
*VendorGuid
,
84 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.
86 mEnableLocking
= FALSE
;
87 Status
= VariableServiceSetVariable (
94 mEnableLocking
= TRUE
;
98 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable
= {
99 VariableServiceGetVariable
,
100 VariableServiceGetNextVariableName
,
101 SmmVariableSetVariable
,
102 VariableServiceQueryVariableInfo
106 Return TRUE if ExitBootServices () has been called.
108 @retval TRUE If ExitBootServices () has been called.
119 This function check if the address is in SMRAM.
121 @param Buffer the buffer address to be checked.
122 @param Length the buffer length to be checked.
124 @retval TRUE this address is in SMRAM.
125 @retval FALSE this address is NOT in SMRAM.
128 InternalIsAddressInSmram (
129 IN EFI_PHYSICAL_ADDRESS Buffer
,
135 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
136 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
137 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
146 This function check if the address refered by Buffer and Length is valid.
148 @param Buffer the buffer address to be checked.
149 @param Length the buffer length to be checked.
151 @retval TRUE this address is valid.
152 @retval FALSE this address is NOT valid.
155 InternalIsAddressValid (
160 if (Buffer
> (MAX_ADDRESS
- Length
)) {
166 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)Buffer
, (UINT64
)Length
)) {
173 Initializes a basic mutual exclusion lock.
175 This function initializes a basic mutual exclusion lock to the released state
176 and returns the lock. Each lock provides mutual exclusion access at its task
177 priority level. Since there is no preemption or multiprocessor support in EFI,
178 acquiring the lock only consists of raising to the locks TPL.
179 If Lock is NULL, then ASSERT().
180 If Priority is not a valid TPL value, then ASSERT().
182 @param Lock A pointer to the lock data structure to initialize.
183 @param Priority EFI TPL is associated with the lock.
190 IN OUT EFI_LOCK
*Lock
,
198 Acquires lock only at boot time. Simply returns at runtime.
200 This is a temperary function that will be removed when
201 EfiAcquireLock() in UefiLib can handle the call in UEFI
202 Runtimer driver in RT phase.
203 It calls EfiAcquireLock() at boot time, and simply returns
206 @param Lock A pointer to the lock to acquire.
210 AcquireLockOnlyAtBootTime (
219 Releases lock only at boot time. Simply returns at runtime.
221 This is a temperary function which will be removed when
222 EfiReleaseLock() in UefiLib can handle the call in UEFI
223 Runtimer driver in RT phase.
224 It calls EfiReleaseLock() at boot time and simply returns
227 @param Lock A pointer to the lock to release.
231 ReleaseLockOnlyAtBootTime (
239 Retrive the SMM Fault Tolerent Write protocol interface.
241 @param[out] FtwProtocol The interface of SMM Ftw protocol
243 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
244 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
245 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
250 OUT VOID
**FtwProtocol
256 // Locate Smm Fault Tolerent Write protocol
258 Status
= gSmst
->SmmLocateProtocol (
259 &gEfiSmmFaultTolerantWriteProtocolGuid
,
268 Retrive the SMM FVB protocol interface by HANDLE.
270 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
271 reading, writing, and erasing the target block.
272 @param[out] FvBlock The interface of SMM FVB protocol
274 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
275 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
276 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
281 IN EFI_HANDLE FvBlockHandle
,
282 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
286 // To get the SMM FVB protocol interface on the handle
288 return gSmst
->SmmHandleProtocol (
290 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
297 Function returns an array of handles that support the SMM FVB protocol
298 in a buffer allocated from pool.
300 @param[out] NumberHandles The number of handles returned in Buffer.
301 @param[out] Buffer A pointer to the buffer to return the requested
302 array of handles that support SMM FVB protocol.
304 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
305 handles in Buffer was returned in NumberHandles.
306 @retval EFI_NOT_FOUND No SMM FVB handle was found.
307 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
308 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
312 GetFvbCountAndBuffer (
313 OUT UINTN
*NumberHandles
,
314 OUT EFI_HANDLE
**Buffer
320 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
321 return EFI_INVALID_PARAMETER
;
327 Status
= gSmst
->SmmLocateHandle (
329 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
334 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
335 return EFI_NOT_FOUND
;
338 *Buffer
= AllocatePool (BufferSize
);
339 if (*Buffer
== NULL
) {
340 return EFI_OUT_OF_RESOURCES
;
343 Status
= gSmst
->SmmLocateHandle (
345 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
351 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
352 if (EFI_ERROR(Status
)) {
363 Get the variable statistics information from the information buffer pointed by gVariableInfo.
365 Caution: This function may be invoked at SMM runtime.
366 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
368 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
369 On input, point to the variable information returned last time. if
370 InfoEntry->VendorGuid is zero, return the first information.
371 On output, point to the next variable information.
372 @param[in, out] InfoSize On input, the size of the variable information buffer.
373 On output, the returned variable information size.
375 @retval EFI_SUCCESS The variable information is found and returned successfully.
376 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
377 PcdVariableCollectStatistics should be set TRUE to support it.
378 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
379 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
383 SmmVariableGetStatistics (
384 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
385 IN OUT UINTN
*InfoSize
388 VARIABLE_INFO_ENTRY
*VariableInfo
;
390 UINTN StatisticsInfoSize
;
394 if (InfoEntry
== NULL
) {
395 return EFI_INVALID_PARAMETER
;
398 VariableInfo
= gVariableInfo
;
399 if (VariableInfo
== NULL
) {
400 return EFI_UNSUPPORTED
;
403 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
404 if (*InfoSize
< StatisticsInfoSize
) {
405 *InfoSize
= StatisticsInfoSize
;
406 return EFI_BUFFER_TOO_SMALL
;
408 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
410 CopyGuid (&VendorGuid
, &InfoEntry
->VendorGuid
);
412 if (CompareGuid (&VendorGuid
, &mZeroGuid
)) {
414 // Return the first variable info
416 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
417 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
418 *InfoSize
= StatisticsInfoSize
;
423 // Get the next variable info
425 while (VariableInfo
!= NULL
) {
426 if (CompareGuid (&VariableInfo
->VendorGuid
, &VendorGuid
)) {
427 NameLength
= StrSize (VariableInfo
->Name
);
428 if (NameLength
== StrSize (InfoName
)) {
429 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
431 // Find the match one
433 VariableInfo
= VariableInfo
->Next
;
438 VariableInfo
= VariableInfo
->Next
;
441 if (VariableInfo
== NULL
) {
447 // Output the new variable info
449 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
450 if (*InfoSize
< StatisticsInfoSize
) {
451 *InfoSize
= StatisticsInfoSize
;
452 return EFI_BUFFER_TOO_SMALL
;
455 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
456 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
457 *InfoSize
= StatisticsInfoSize
;
464 Communication service SMI Handler entry.
466 This SMI handler provides services for the variable wrapper driver.
468 Caution: This function may receive untrusted input.
469 This variable data and communicate buffer are external input, so this function will do basic validation.
470 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
471 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
472 SmmVariableGetStatistics() should also do validation based on its own knowledge.
474 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
475 @param[in] RegisterContext Points to an optional handler context which was specified when the
476 handler was registered.
477 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
478 be conveyed from a non-SMM environment into an SMM environment.
479 @param[in, out] CommBufferSize The size of the CommBuffer.
481 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
482 should still be called.
483 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
485 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
487 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
493 IN EFI_HANDLE DispatchHandle
,
494 IN CONST VOID
*RegisterContext
,
495 IN OUT VOID
*CommBuffer
,
496 IN OUT UINTN
*CommBufferSize
500 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
501 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
502 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
503 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
504 VARIABLE_INFO_ENTRY
*VariableInfo
;
505 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
507 UINTN NameBufferSize
;
508 UINTN CommBufferPayloadSize
;
509 UINTN TempCommBufferSize
;
512 // If input is invalid, stop processing this SMI
514 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
518 TempCommBufferSize
= *CommBufferSize
;
520 if (TempCommBufferSize
< SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
521 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
524 CommBufferPayloadSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
525 if (CommBufferPayloadSize
> mVariableBufferPayloadSize
) {
526 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
530 if (!InternalIsAddressValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
531 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
535 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
537 switch (SmmVariableFunctionHeader
->Function
) {
538 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
539 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
540 DEBUG ((EFI_D_ERROR
, "GetVariable: SMM communication buffer size invalid!\n"));
544 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
546 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
547 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
548 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
549 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
551 // Prevent InfoSize overflow happen
553 Status
= EFI_ACCESS_DENIED
;
556 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
557 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
560 // SMRAM range check already covered before
562 if (InfoSize
> CommBufferPayloadSize
) {
563 DEBUG ((EFI_D_ERROR
, "GetVariable: Data size exceed communication buffer size limit!\n"));
564 Status
= EFI_ACCESS_DENIED
;
568 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
570 // Make sure VariableName is A Null-terminated string.
572 Status
= EFI_ACCESS_DENIED
;
576 Status
= VariableServiceGetVariable (
577 SmmVariableHeader
->Name
,
578 &SmmVariableHeader
->Guid
,
579 &SmmVariableHeader
->Attributes
,
580 &SmmVariableHeader
->DataSize
,
581 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
583 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
586 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
587 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
588 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: SMM communication buffer size invalid!\n"));
592 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
594 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
595 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) mVariableBufferPayload
;
596 if ((UINTN
)(~0) - GetNextVariableName
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
598 // Prevent InfoSize overflow happen
600 Status
= EFI_ACCESS_DENIED
;
603 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + GetNextVariableName
->NameSize
;
606 // SMRAM range check already covered before
608 if (InfoSize
> CommBufferPayloadSize
) {
609 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
610 Status
= EFI_ACCESS_DENIED
;
614 NameBufferSize
= CommBufferPayloadSize
- OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
615 if (NameBufferSize
< sizeof (CHAR16
) || GetNextVariableName
->Name
[NameBufferSize
/sizeof (CHAR16
) - 1] != L
'\0') {
617 // Make sure input VariableName is A Null-terminated string.
619 Status
= EFI_ACCESS_DENIED
;
623 Status
= VariableServiceGetNextVariableName (
624 &GetNextVariableName
->NameSize
,
625 GetNextVariableName
->Name
,
626 &GetNextVariableName
->Guid
628 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
631 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
632 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
633 DEBUG ((EFI_D_ERROR
, "SetVariable: SMM communication buffer size invalid!\n"));
637 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
639 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
640 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
641 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
642 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
644 // Prevent InfoSize overflow happen
646 Status
= EFI_ACCESS_DENIED
;
649 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
650 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
653 // SMRAM range check already covered before
654 // Data buffer should not contain SMM range
656 if (InfoSize
> CommBufferPayloadSize
) {
657 DEBUG ((EFI_D_ERROR
, "SetVariable: Data size exceed communication buffer size limit!\n"));
658 Status
= EFI_ACCESS_DENIED
;
662 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
664 // Make sure VariableName is A Null-terminated string.
666 Status
= EFI_ACCESS_DENIED
;
670 Status
= VariableServiceSetVariable (
671 SmmVariableHeader
->Name
,
672 &SmmVariableHeader
->Guid
,
673 SmmVariableHeader
->Attributes
,
674 SmmVariableHeader
->DataSize
,
675 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
679 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
680 if (CommBufferPayloadSize
< sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
)) {
681 DEBUG ((EFI_D_ERROR
, "QueryVariableInfo: SMM communication buffer size invalid!\n"));
684 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
686 Status
= VariableServiceQueryVariableInfo (
687 QueryVariableInfo
->Attributes
,
688 &QueryVariableInfo
->MaximumVariableStorageSize
,
689 &QueryVariableInfo
->RemainingVariableStorageSize
,
690 &QueryVariableInfo
->MaximumVariableSize
694 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
697 Status
= EFI_UNSUPPORTED
;
701 Status
= EFI_SUCCESS
;
704 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
706 Status
= EFI_SUCCESS
;
709 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
710 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
711 InfoSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
714 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
715 // It is covered by previous CommBuffer check
718 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CommBufferSize
, sizeof(UINTN
))) {
719 DEBUG ((EFI_D_ERROR
, "GetStatistics: SMM communication buffer in SMRAM!\n"));
720 Status
= EFI_ACCESS_DENIED
;
724 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
725 *CommBufferSize
= InfoSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
728 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
:
730 Status
= EFI_ACCESS_DENIED
;
732 VariableToLock
= (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
733 Status
= VariableLockRequestToLock (
735 VariableToLock
->Name
,
736 &VariableToLock
->Guid
742 Status
= EFI_UNSUPPORTED
;
747 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
752 SMM END_OF_DXE protocol notification event handler.
754 @param Protocol Points to the protocol's unique identifier
755 @param Interface Points to the interface instance
756 @param Handle The handle on which the interface was installed
758 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
763 SmmEndOfDxeCallback (
764 IN CONST EFI_GUID
*Protocol
,
769 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
775 SMM Fault Tolerant Write protocol notification event handler.
777 Non-Volatile variable write may needs FTW protocol to reclaim when
780 @param Protocol Points to the protocol's unique identifier
781 @param Interface Points to the interface instance
782 @param Handle The handle on which the interface was installed
784 @retval EFI_SUCCESS SmmEventCallback runs successfully
785 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
790 SmmFtwNotificationEvent (
791 IN CONST EFI_GUID
*Protocol
,
797 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
798 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
799 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
800 UINTN FtwMaxBlockSize
;
802 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
807 // Ensure SMM FTW protocol is installed.
809 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
810 if (EFI_ERROR (Status
)) {
814 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
815 if (!EFI_ERROR (Status
)) {
816 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
820 // Find the proper FVB protocol for variable.
822 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
823 if (NvStorageVariableBase
== 0) {
824 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
826 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
827 if (EFI_ERROR (Status
)) {
828 return EFI_NOT_FOUND
;
831 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
833 Status
= VariableWriteServiceInitialize ();
834 ASSERT_EFI_ERROR (Status
);
837 // Notify the variable wrapper driver the variable write service is ready
839 Status
= gBS
->InstallProtocolInterface (
841 &gSmmVariableWriteGuid
,
842 EFI_NATIVE_INTERFACE
,
845 ASSERT_EFI_ERROR (Status
);
852 Variable Driver main entry point. The Variable driver places the 4 EFI
853 runtime services in the EFI System Table and installs arch protocols
854 for variable read and write services being available. It also registers
855 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
857 @param[in] ImageHandle The firmware allocated handle for the EFI image.
858 @param[in] SystemTable A pointer to the EFI System Table.
860 @retval EFI_SUCCESS Variable service successfully initialized.
865 VariableServiceInitialize (
866 IN EFI_HANDLE ImageHandle
,
867 IN EFI_SYSTEM_TABLE
*SystemTable
871 EFI_HANDLE VariableHandle
;
872 VOID
*SmmFtwRegistration
;
873 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
875 VOID
*SmmEndOfDxeRegistration
;
878 // Variable initialize.
880 Status
= VariableCommonInitialize ();
881 ASSERT_EFI_ERROR (Status
);
884 // Install the Smm Variable Protocol on a new handle.
886 VariableHandle
= NULL
;
887 Status
= gSmst
->SmmInstallProtocolInterface (
889 &gEfiSmmVariableProtocolGuid
,
890 EFI_NATIVE_INTERFACE
,
893 ASSERT_EFI_ERROR (Status
);
896 // Get SMRAM information
898 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
899 ASSERT_EFI_ERROR (Status
);
902 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
903 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
905 Status
= gSmst
->SmmAllocatePool (
906 EfiRuntimeServicesData
,
908 (VOID
**)&mSmramRanges
910 ASSERT_EFI_ERROR (Status
);
912 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
913 ASSERT_EFI_ERROR (Status
);
915 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
917 mVariableBufferPayloadSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) +
918 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - sizeof (VARIABLE_HEADER
);
920 Status
= gSmst
->SmmAllocatePool (
921 EfiRuntimeServicesData
,
922 mVariableBufferPayloadSize
,
923 (VOID
**)&mVariableBufferPayload
925 ASSERT_EFI_ERROR (Status
);
928 /// Register SMM variable SMI handler
930 VariableHandle
= NULL
;
931 Status
= gSmst
->SmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
932 ASSERT_EFI_ERROR (Status
);
935 // Notify the variable wrapper driver the variable service is ready
937 Status
= SystemTable
->BootServices
->InstallProtocolInterface (
939 &gEfiSmmVariableProtocolGuid
,
940 EFI_NATIVE_INTERFACE
,
943 ASSERT_EFI_ERROR (Status
);
946 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
948 Status
= gSmst
->SmmRegisterProtocolNotify (
949 &gEfiSmmEndOfDxeProtocolGuid
,
951 &SmmEndOfDxeRegistration
953 ASSERT_EFI_ERROR (Status
);
956 // Register FtwNotificationEvent () notify function.
958 Status
= gSmst
->SmmRegisterProtocolNotify (
959 &gEfiSmmFaultTolerantWriteProtocolGuid
,
960 SmmFtwNotificationEvent
,
963 ASSERT_EFI_ERROR (Status
);
965 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);