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 - 2014, 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
;
54 SecureBoot Hook for SetVariable.
56 @param[in] VariableName Name of Variable to be found.
57 @param[in] VendorGuid Variable vendor GUID.
63 IN CHAR16
*VariableName
,
64 IN EFI_GUID
*VendorGuid
72 This code sets variable in storage blocks (Volatile or Non-Volatile).
74 @param VariableName Name of Variable to be found.
75 @param VendorGuid Variable vendor GUID.
76 @param Attributes Attribute value of the variable found
77 @param DataSize Size of Data found. If size is less than the
78 data, this value contains the required size.
79 @param Data Data pointer.
81 @return EFI_INVALID_PARAMETER Invalid parameter.
82 @return EFI_SUCCESS Set successfully.
83 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
84 @return EFI_NOT_FOUND Not found.
85 @return EFI_WRITE_PROTECTED Variable is read-only.
90 SmmVariableSetVariable (
91 IN CHAR16
*VariableName
,
92 IN EFI_GUID
*VendorGuid
,
101 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.
103 mEnableLocking
= FALSE
;
104 Status
= VariableServiceSetVariable (
111 mEnableLocking
= TRUE
;
115 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable
= {
116 VariableServiceGetVariable
,
117 VariableServiceGetNextVariableName
,
118 SmmVariableSetVariable
,
119 VariableServiceQueryVariableInfo
123 Return TRUE if ExitBootServices () has been called.
125 @retval TRUE If ExitBootServices () has been called.
136 This function check if the address is in SMRAM.
138 @param Buffer the buffer address to be checked.
139 @param Length the buffer length to be checked.
141 @retval TRUE this address is in SMRAM.
142 @retval FALSE this address is NOT in SMRAM.
145 InternalIsAddressInSmram (
146 IN EFI_PHYSICAL_ADDRESS Buffer
,
152 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
153 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
154 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
163 This function check if the address refered by Buffer and Length is valid.
165 @param Buffer the buffer address to be checked.
166 @param Length the buffer length to be checked.
168 @retval TRUE this address is valid.
169 @retval FALSE this address is NOT valid.
172 InternalIsAddressValid (
177 if (Buffer
> (MAX_ADDRESS
- Length
)) {
183 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)Buffer
, (UINT64
)Length
)) {
190 Initializes a basic mutual exclusion lock.
192 This function initializes a basic mutual exclusion lock to the released state
193 and returns the lock. Each lock provides mutual exclusion access at its task
194 priority level. Since there is no preemption or multiprocessor support in EFI,
195 acquiring the lock only consists of raising to the locks TPL.
196 If Lock is NULL, then ASSERT().
197 If Priority is not a valid TPL value, then ASSERT().
199 @param Lock A pointer to the lock data structure to initialize.
200 @param Priority EFI TPL is associated with the lock.
207 IN OUT EFI_LOCK
*Lock
,
215 Acquires lock only at boot time. Simply returns at runtime.
217 This is a temperary function that will be removed when
218 EfiAcquireLock() in UefiLib can handle the call in UEFI
219 Runtimer driver in RT phase.
220 It calls EfiAcquireLock() at boot time, and simply returns
223 @param Lock A pointer to the lock to acquire.
227 AcquireLockOnlyAtBootTime (
236 Releases lock only at boot time. Simply returns at runtime.
238 This is a temperary function which will be removed when
239 EfiReleaseLock() in UefiLib can handle the call in UEFI
240 Runtimer driver in RT phase.
241 It calls EfiReleaseLock() at boot time and simply returns
244 @param Lock A pointer to the lock to release.
248 ReleaseLockOnlyAtBootTime (
256 Retrive the SMM Fault Tolerent Write protocol interface.
258 @param[out] FtwProtocol The interface of SMM Ftw protocol
260 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
261 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
262 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
267 OUT VOID
**FtwProtocol
273 // Locate Smm Fault Tolerent Write protocol
275 Status
= gSmst
->SmmLocateProtocol (
276 &gEfiSmmFaultTolerantWriteProtocolGuid
,
285 Retrive the SMM FVB protocol interface by HANDLE.
287 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
288 reading, writing, and erasing the target block.
289 @param[out] FvBlock The interface of SMM FVB protocol
291 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
292 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
293 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
298 IN EFI_HANDLE FvBlockHandle
,
299 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
303 // To get the SMM FVB protocol interface on the handle
305 return gSmst
->SmmHandleProtocol (
307 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
314 Function returns an array of handles that support the SMM FVB protocol
315 in a buffer allocated from pool.
317 @param[out] NumberHandles The number of handles returned in Buffer.
318 @param[out] Buffer A pointer to the buffer to return the requested
319 array of handles that support SMM FVB protocol.
321 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
322 handles in Buffer was returned in NumberHandles.
323 @retval EFI_NOT_FOUND No SMM FVB handle was found.
324 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
325 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
329 GetFvbCountAndBuffer (
330 OUT UINTN
*NumberHandles
,
331 OUT EFI_HANDLE
**Buffer
337 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
338 return EFI_INVALID_PARAMETER
;
344 Status
= gSmst
->SmmLocateHandle (
346 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
351 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
352 return EFI_NOT_FOUND
;
355 *Buffer
= AllocatePool (BufferSize
);
356 if (*Buffer
== NULL
) {
357 return EFI_OUT_OF_RESOURCES
;
360 Status
= gSmst
->SmmLocateHandle (
362 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
368 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
369 if (EFI_ERROR(Status
)) {
380 Get the variable statistics information from the information buffer pointed by gVariableInfo.
382 Caution: This function may be invoked at SMM runtime.
383 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
385 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
386 On input, point to the variable information returned last time. if
387 InfoEntry->VendorGuid is zero, return the first information.
388 On output, point to the next variable information.
389 @param[in, out] InfoSize On input, the size of the variable information buffer.
390 On output, the returned variable information size.
392 @retval EFI_SUCCESS The variable information is found and returned successfully.
393 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
394 PcdVariableCollectStatistics should be set TRUE to support it.
395 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
396 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
400 SmmVariableGetStatistics (
401 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
402 IN OUT UINTN
*InfoSize
405 VARIABLE_INFO_ENTRY
*VariableInfo
;
407 UINTN StatisticsInfoSize
;
411 if (InfoEntry
== NULL
) {
412 return EFI_INVALID_PARAMETER
;
415 VariableInfo
= gVariableInfo
;
416 if (VariableInfo
== NULL
) {
417 return EFI_UNSUPPORTED
;
420 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
421 if (*InfoSize
< StatisticsInfoSize
) {
422 *InfoSize
= StatisticsInfoSize
;
423 return EFI_BUFFER_TOO_SMALL
;
425 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
427 CopyGuid (&VendorGuid
, &InfoEntry
->VendorGuid
);
429 if (CompareGuid (&VendorGuid
, &mZeroGuid
)) {
431 // Return the first variable info
433 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
434 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
435 *InfoSize
= StatisticsInfoSize
;
440 // Get the next variable info
442 while (VariableInfo
!= NULL
) {
443 if (CompareGuid (&VariableInfo
->VendorGuid
, &VendorGuid
)) {
444 NameLength
= StrSize (VariableInfo
->Name
);
445 if (NameLength
== StrSize (InfoName
)) {
446 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
448 // Find the match one
450 VariableInfo
= VariableInfo
->Next
;
455 VariableInfo
= VariableInfo
->Next
;
458 if (VariableInfo
== NULL
) {
464 // Output the new variable info
466 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
467 if (*InfoSize
< StatisticsInfoSize
) {
468 *InfoSize
= StatisticsInfoSize
;
469 return EFI_BUFFER_TOO_SMALL
;
472 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
473 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
474 *InfoSize
= StatisticsInfoSize
;
481 Communication service SMI Handler entry.
483 This SMI handler provides services for the variable wrapper driver.
485 Caution: This function may receive untrusted input.
486 This variable data and communicate buffer are external input, so this function will do basic validation.
487 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
488 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
489 SmmVariableGetStatistics() should also do validation based on its own knowledge.
491 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
492 @param[in] RegisterContext Points to an optional handler context which was specified when the
493 handler was registered.
494 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
495 be conveyed from a non-SMM environment into an SMM environment.
496 @param[in, out] CommBufferSize The size of the CommBuffer.
498 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
499 should still be called.
500 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
502 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
504 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
510 IN EFI_HANDLE DispatchHandle
,
511 IN CONST VOID
*RegisterContext
,
512 IN OUT VOID
*CommBuffer
,
513 IN OUT UINTN
*CommBufferSize
517 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
518 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
519 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
520 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
521 VARIABLE_INFO_ENTRY
*VariableInfo
;
522 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
524 UINTN NameBufferSize
;
525 UINTN CommBufferPayloadSize
;
526 UINTN TempCommBufferSize
;
529 // If input is invalid, stop processing this SMI
531 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
535 TempCommBufferSize
= *CommBufferSize
;
537 if (TempCommBufferSize
< SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
538 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
541 CommBufferPayloadSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
542 if (CommBufferPayloadSize
> mVariableBufferPayloadSize
) {
543 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
547 if (!InternalIsAddressValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
548 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
552 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
554 switch (SmmVariableFunctionHeader
->Function
) {
555 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
556 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
557 DEBUG ((EFI_D_ERROR
, "GetVariable: SMM communication buffer size invalid!\n"));
561 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
563 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
564 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
565 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
566 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
568 // Prevent InfoSize overflow happen
570 Status
= EFI_ACCESS_DENIED
;
573 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
574 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
577 // SMRAM range check already covered before
579 if (InfoSize
> CommBufferPayloadSize
) {
580 DEBUG ((EFI_D_ERROR
, "GetVariable: Data size exceed communication buffer size limit!\n"));
581 Status
= EFI_ACCESS_DENIED
;
585 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
587 // Make sure VariableName is A Null-terminated string.
589 Status
= EFI_ACCESS_DENIED
;
593 Status
= VariableServiceGetVariable (
594 SmmVariableHeader
->Name
,
595 &SmmVariableHeader
->Guid
,
596 &SmmVariableHeader
->Attributes
,
597 &SmmVariableHeader
->DataSize
,
598 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
600 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
603 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
604 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
605 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: SMM communication buffer size invalid!\n"));
609 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
611 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
612 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) mVariableBufferPayload
;
613 if ((UINTN
)(~0) - GetNextVariableName
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
615 // Prevent InfoSize overflow happen
617 Status
= EFI_ACCESS_DENIED
;
620 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + GetNextVariableName
->NameSize
;
623 // SMRAM range check already covered before
625 if (InfoSize
> CommBufferPayloadSize
) {
626 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
627 Status
= EFI_ACCESS_DENIED
;
631 NameBufferSize
= CommBufferPayloadSize
- OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
632 if (NameBufferSize
< sizeof (CHAR16
) || GetNextVariableName
->Name
[NameBufferSize
/sizeof (CHAR16
) - 1] != L
'\0') {
634 // Make sure input VariableName is A Null-terminated string.
636 Status
= EFI_ACCESS_DENIED
;
640 Status
= VariableServiceGetNextVariableName (
641 &GetNextVariableName
->NameSize
,
642 GetNextVariableName
->Name
,
643 &GetNextVariableName
->Guid
645 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
648 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
649 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
650 DEBUG ((EFI_D_ERROR
, "SetVariable: SMM communication buffer size invalid!\n"));
654 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
656 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
657 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
658 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
659 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
661 // Prevent InfoSize overflow happen
663 Status
= EFI_ACCESS_DENIED
;
666 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
667 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
670 // SMRAM range check already covered before
671 // Data buffer should not contain SMM range
673 if (InfoSize
> CommBufferPayloadSize
) {
674 DEBUG ((EFI_D_ERROR
, "SetVariable: Data size exceed communication buffer size limit!\n"));
675 Status
= EFI_ACCESS_DENIED
;
679 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
681 // Make sure VariableName is A Null-terminated string.
683 Status
= EFI_ACCESS_DENIED
;
687 Status
= VariableServiceSetVariable (
688 SmmVariableHeader
->Name
,
689 &SmmVariableHeader
->Guid
,
690 SmmVariableHeader
->Attributes
,
691 SmmVariableHeader
->DataSize
,
692 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
696 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
697 if (CommBufferPayloadSize
< sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
)) {
698 DEBUG ((EFI_D_ERROR
, "QueryVariableInfo: SMM communication buffer size invalid!\n"));
701 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
703 Status
= VariableServiceQueryVariableInfo (
704 QueryVariableInfo
->Attributes
,
705 &QueryVariableInfo
->MaximumVariableStorageSize
,
706 &QueryVariableInfo
->RemainingVariableStorageSize
,
707 &QueryVariableInfo
->MaximumVariableSize
711 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
714 Status
= EFI_UNSUPPORTED
;
718 Status
= EFI_SUCCESS
;
721 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
723 Status
= EFI_SUCCESS
;
726 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
727 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
728 InfoSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
731 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
732 // It is covered by previous CommBuffer check
735 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CommBufferSize
, sizeof(UINTN
))) {
736 DEBUG ((EFI_D_ERROR
, "GetStatistics: SMM communication buffer in SMRAM!\n"));
737 Status
= EFI_ACCESS_DENIED
;
741 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
742 *CommBufferSize
= InfoSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
745 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
:
747 Status
= EFI_ACCESS_DENIED
;
749 VariableToLock
= (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
750 Status
= VariableLockRequestToLock (
752 VariableToLock
->Name
,
753 &VariableToLock
->Guid
759 Status
= EFI_UNSUPPORTED
;
764 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
769 SMM END_OF_DXE protocol notification event handler.
771 @param Protocol Points to the protocol's unique identifier
772 @param Interface Points to the interface instance
773 @param Handle The handle on which the interface was installed
775 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
780 SmmEndOfDxeCallback (
781 IN CONST EFI_GUID
*Protocol
,
786 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
792 SMM Fault Tolerant Write protocol notification event handler.
794 Non-Volatile variable write may needs FTW protocol to reclaim when
797 @param Protocol Points to the protocol's unique identifier
798 @param Interface Points to the interface instance
799 @param Handle The handle on which the interface was installed
801 @retval EFI_SUCCESS SmmEventCallback runs successfully
802 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
807 SmmFtwNotificationEvent (
808 IN CONST EFI_GUID
*Protocol
,
814 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
815 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
816 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
817 UINTN FtwMaxBlockSize
;
819 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
824 // Ensure SMM FTW protocol is installed.
826 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
827 if (EFI_ERROR (Status
)) {
831 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
832 if (!EFI_ERROR (Status
)) {
833 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
837 // Find the proper FVB protocol for variable.
839 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
840 if (NvStorageVariableBase
== 0) {
841 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
843 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
844 if (EFI_ERROR (Status
)) {
845 return EFI_NOT_FOUND
;
848 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
850 Status
= VariableWriteServiceInitialize ();
851 if (EFI_ERROR (Status
)) {
852 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
856 // Notify the variable wrapper driver the variable write service is ready
858 Status
= gBS
->InstallProtocolInterface (
860 &gSmmVariableWriteGuid
,
861 EFI_NATIVE_INTERFACE
,
864 ASSERT_EFI_ERROR (Status
);
871 Variable Driver main entry point. The Variable driver places the 4 EFI
872 runtime services in the EFI System Table and installs arch protocols
873 for variable read and write services being available. It also registers
874 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
876 @param[in] ImageHandle The firmware allocated handle for the EFI image.
877 @param[in] SystemTable A pointer to the EFI System Table.
879 @retval EFI_SUCCESS Variable service successfully initialized.
884 VariableServiceInitialize (
885 IN EFI_HANDLE ImageHandle
,
886 IN EFI_SYSTEM_TABLE
*SystemTable
890 EFI_HANDLE VariableHandle
;
891 VOID
*SmmFtwRegistration
;
892 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
894 VOID
*SmmEndOfDxeRegistration
;
897 // Variable initialize.
899 Status
= VariableCommonInitialize ();
900 ASSERT_EFI_ERROR (Status
);
903 // Install the Smm Variable Protocol on a new handle.
905 VariableHandle
= NULL
;
906 Status
= gSmst
->SmmInstallProtocolInterface (
908 &gEfiSmmVariableProtocolGuid
,
909 EFI_NATIVE_INTERFACE
,
912 ASSERT_EFI_ERROR (Status
);
915 // Get SMRAM information
917 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
918 ASSERT_EFI_ERROR (Status
);
921 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
922 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
924 Status
= gSmst
->SmmAllocatePool (
925 EfiRuntimeServicesData
,
927 (VOID
**)&mSmramRanges
929 ASSERT_EFI_ERROR (Status
);
931 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
932 ASSERT_EFI_ERROR (Status
);
934 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
936 mVariableBufferPayloadSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) +
937 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - sizeof (VARIABLE_HEADER
);
939 Status
= gSmst
->SmmAllocatePool (
940 EfiRuntimeServicesData
,
941 mVariableBufferPayloadSize
,
942 (VOID
**)&mVariableBufferPayload
944 ASSERT_EFI_ERROR (Status
);
947 /// Register SMM variable SMI handler
949 VariableHandle
= NULL
;
950 Status
= gSmst
->SmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
951 ASSERT_EFI_ERROR (Status
);
954 // Notify the variable wrapper driver the variable service is ready
956 Status
= SystemTable
->BootServices
->InstallProtocolInterface (
958 &gEfiSmmVariableProtocolGuid
,
959 EFI_NATIVE_INTERFACE
,
962 ASSERT_EFI_ERROR (Status
);
965 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
967 Status
= gSmst
->SmmRegisterProtocolNotify (
968 &gEfiSmmEndOfDxeProtocolGuid
,
970 &SmmEndOfDxeRegistration
972 ASSERT_EFI_ERROR (Status
);
975 // Register FtwNotificationEvent () notify function.
977 Status
= gSmst
->SmmRegisterProtocolNotify (
978 &gEfiSmmFaultTolerantWriteProtocolGuid
,
979 SmmFtwNotificationEvent
,
982 ASSERT_EFI_ERROR (Status
);
984 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);