3 The sample implementation for SMM variable protocol. And this driver
4 implements an SMI handler to communicate with the DXE runtime driver
5 to provide variable services.
7 Caution: This module requires additional review when modified.
8 This driver will have external input - variable data and communicate buffer in SMM mode.
9 This external input must be validated carefully to avoid security issue like
10 buffer overflow, integer overflow.
12 SmmVariableHandler() will receive untrusted input and do basic validation.
14 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
15 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
16 SmmVariableGetStatistics() should also do validation based on its own knowledge.
18 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
19 This program and the accompanying materials
20 are licensed and made available under the terms and conditions of the BSD License
21 which accompanies this distribution. The full text of the license may be found at
22 http://opensource.org/licenses/bsd-license.php
24 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
25 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/VariableFormat.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.
382 SmmVariableGetStatistics (
383 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
384 IN OUT UINTN
*InfoSize
387 VARIABLE_INFO_ENTRY
*VariableInfo
;
389 UINTN StatisticsInfoSize
;
393 ASSERT (InfoEntry
!= NULL
);
394 VariableInfo
= gVariableInfo
;
395 if (VariableInfo
== NULL
) {
396 return EFI_UNSUPPORTED
;
399 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
400 if (*InfoSize
< StatisticsInfoSize
) {
401 *InfoSize
= StatisticsInfoSize
;
402 return EFI_BUFFER_TOO_SMALL
;
404 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
406 CopyGuid (&VendorGuid
, &InfoEntry
->VendorGuid
);
408 if (CompareGuid (&VendorGuid
, &mZeroGuid
)) {
410 // Return the first variable info
412 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
413 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
414 *InfoSize
= StatisticsInfoSize
;
419 // Get the next variable info
421 while (VariableInfo
!= NULL
) {
422 if (CompareGuid (&VariableInfo
->VendorGuid
, &VendorGuid
)) {
423 NameLength
= StrSize (VariableInfo
->Name
);
424 if (NameLength
== StrSize (InfoName
)) {
425 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
427 // Find the match one
429 VariableInfo
= VariableInfo
->Next
;
434 VariableInfo
= VariableInfo
->Next
;
437 if (VariableInfo
== NULL
) {
443 // Output the new variable info
445 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
446 if (*InfoSize
< StatisticsInfoSize
) {
447 *InfoSize
= StatisticsInfoSize
;
448 return EFI_BUFFER_TOO_SMALL
;
451 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
452 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
453 *InfoSize
= StatisticsInfoSize
;
460 Communication service SMI Handler entry.
462 This SMI handler provides services for the variable wrapper driver.
464 Caution: This function may receive untrusted input.
465 This variable data and communicate buffer are external input, so this function will do basic validation.
466 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
467 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
468 SmmVariableGetStatistics() should also do validation based on its own knowledge.
470 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
471 @param[in] RegisterContext Points to an optional handler context which was specified when the
472 handler was registered.
473 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
474 be conveyed from a non-SMM environment into an SMM environment.
475 @param[in, out] CommBufferSize The size of the CommBuffer.
477 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
478 should still be called.
479 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
481 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
483 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
488 IN EFI_HANDLE DispatchHandle
,
489 IN CONST VOID
*RegisterContext
,
490 IN OUT VOID
*CommBuffer
,
491 IN OUT UINTN
*CommBufferSize
495 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
496 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
497 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
498 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
499 VARIABLE_INFO_ENTRY
*VariableInfo
;
500 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
502 UINTN NameBufferSize
;
503 UINTN CommBufferPayloadSize
;
504 UINTN TempCommBufferSize
;
507 // If input is invalid, stop processing this SMI
509 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
513 TempCommBufferSize
= *CommBufferSize
;
515 if (TempCommBufferSize
< SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
516 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
519 CommBufferPayloadSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
520 if (CommBufferPayloadSize
> mVariableBufferPayloadSize
) {
521 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
525 if (!InternalIsAddressValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
526 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
530 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
531 switch (SmmVariableFunctionHeader
->Function
) {
532 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
533 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
534 DEBUG ((EFI_D_ERROR
, "GetVariable: SMM communication buffer size invalid!\n"));
538 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
540 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
541 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
542 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
543 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
545 // Prevent InfoSize overflow happen
547 Status
= EFI_ACCESS_DENIED
;
550 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
551 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
554 // SMRAM range check already covered before
556 if (InfoSize
> CommBufferPayloadSize
) {
557 DEBUG ((EFI_D_ERROR
, "GetVariable: Data size exceed communication buffer size limit!\n"));
558 Status
= EFI_ACCESS_DENIED
;
562 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
564 // Make sure VariableName is A Null-terminated string.
566 Status
= EFI_ACCESS_DENIED
;
570 Status
= VariableServiceGetVariable (
571 SmmVariableHeader
->Name
,
572 &SmmVariableHeader
->Guid
,
573 &SmmVariableHeader
->Attributes
,
574 &SmmVariableHeader
->DataSize
,
575 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
577 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
580 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
581 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
582 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: SMM communication buffer size invalid!\n"));
586 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
588 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
589 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) mVariableBufferPayload
;
590 if ((UINTN
)(~0) - GetNextVariableName
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
592 // Prevent InfoSize overflow happen
594 Status
= EFI_ACCESS_DENIED
;
597 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + GetNextVariableName
->NameSize
;
600 // SMRAM range check already covered before
602 if (InfoSize
> CommBufferPayloadSize
) {
603 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
604 Status
= EFI_ACCESS_DENIED
;
608 NameBufferSize
= CommBufferPayloadSize
- OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
609 if (NameBufferSize
< sizeof (CHAR16
) || GetNextVariableName
->Name
[NameBufferSize
/sizeof (CHAR16
) - 1] != L
'\0') {
611 // Make sure input VariableName is A Null-terminated string.
613 Status
= EFI_ACCESS_DENIED
;
617 Status
= VariableServiceGetNextVariableName (
618 &GetNextVariableName
->NameSize
,
619 GetNextVariableName
->Name
,
620 &GetNextVariableName
->Guid
622 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
625 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
626 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
627 DEBUG ((EFI_D_ERROR
, "SetVariable: SMM communication buffer size invalid!\n"));
631 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
633 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
634 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
635 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
636 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
638 // Prevent InfoSize overflow happen
640 Status
= EFI_ACCESS_DENIED
;
643 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
644 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
647 // SMRAM range check already covered before
648 // Data buffer should not contain SMM range
650 if (InfoSize
> CommBufferPayloadSize
) {
651 DEBUG ((EFI_D_ERROR
, "SetVariable: Data size exceed communication buffer size limit!\n"));
652 Status
= EFI_ACCESS_DENIED
;
656 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
658 // Make sure VariableName is A Null-terminated string.
660 Status
= EFI_ACCESS_DENIED
;
664 Status
= VariableServiceSetVariable (
665 SmmVariableHeader
->Name
,
666 &SmmVariableHeader
->Guid
,
667 SmmVariableHeader
->Attributes
,
668 SmmVariableHeader
->DataSize
,
669 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
673 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
674 if (CommBufferPayloadSize
< sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
)) {
675 DEBUG ((EFI_D_ERROR
, "QueryVariableInfo: SMM communication buffer size invalid!\n"));
678 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
680 Status
= VariableServiceQueryVariableInfo (
681 QueryVariableInfo
->Attributes
,
682 &QueryVariableInfo
->MaximumVariableStorageSize
,
683 &QueryVariableInfo
->RemainingVariableStorageSize
,
684 &QueryVariableInfo
->MaximumVariableSize
688 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
691 Status
= EFI_UNSUPPORTED
;
695 Status
= EFI_SUCCESS
;
698 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
700 Status
= EFI_SUCCESS
;
703 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
704 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
705 InfoSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
708 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
709 // It is covered by previous CommBuffer check
712 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CommBufferSize
, sizeof(UINTN
))) {
713 DEBUG ((EFI_D_ERROR
, "GetStatistics: SMM communication buffer in SMRAM!\n"));
714 Status
= EFI_ACCESS_DENIED
;
718 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
719 *CommBufferSize
= InfoSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
722 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
:
724 Status
= EFI_ACCESS_DENIED
;
726 VariableToLock
= (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
727 Status
= VariableLockRequestToLock (
729 VariableToLock
->Name
,
730 &VariableToLock
->Guid
736 Status
= EFI_UNSUPPORTED
;
741 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
747 SMM END_OF_DXE protocol notification event handler.
749 @param Protocol Points to the protocol's unique identifier
750 @param Interface Points to the interface instance
751 @param Handle The handle on which the interface was installed
753 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
758 SmmEndOfDxeCallback (
759 IN CONST EFI_GUID
*Protocol
,
764 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
770 SMM Fault Tolerant Write protocol notification event handler.
772 Non-Volatile variable write may needs FTW protocol to reclaim when
775 @param Protocol Points to the protocol's unique identifier
776 @param Interface Points to the interface instance
777 @param Handle The handle on which the interface was installed
779 @retval EFI_SUCCESS SmmEventCallback runs successfully
780 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
785 SmmFtwNotificationEvent (
786 IN CONST EFI_GUID
*Protocol
,
792 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
793 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
794 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
795 UINTN FtwMaxBlockSize
;
797 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
802 // Ensure SMM FTW protocol is installed.
804 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
805 if (EFI_ERROR (Status
)) {
809 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
810 if (!EFI_ERROR (Status
)) {
811 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
815 // Find the proper FVB protocol for variable.
817 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
818 if (NvStorageVariableBase
== 0) {
819 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
821 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
822 if (EFI_ERROR (Status
)) {
823 return EFI_NOT_FOUND
;
826 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
828 Status
= VariableWriteServiceInitialize ();
829 ASSERT_EFI_ERROR (Status
);
832 // Notify the variable wrapper driver the variable write service is ready
834 Status
= gBS
->InstallProtocolInterface (
836 &gSmmVariableWriteGuid
,
837 EFI_NATIVE_INTERFACE
,
840 ASSERT_EFI_ERROR (Status
);
847 Variable Driver main entry point. The Variable driver places the 4 EFI
848 runtime services in the EFI System Table and installs arch protocols
849 for variable read and write services being available. It also registers
850 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
852 @param[in] ImageHandle The firmware allocated handle for the EFI image.
853 @param[in] SystemTable A pointer to the EFI System Table.
855 @retval EFI_SUCCESS Variable service successfully initialized.
860 VariableServiceInitialize (
861 IN EFI_HANDLE ImageHandle
,
862 IN EFI_SYSTEM_TABLE
*SystemTable
866 EFI_HANDLE VariableHandle
;
867 VOID
*SmmFtwRegistration
;
868 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
870 VOID
*SmmEndOfDxeRegistration
;
873 // Variable initialize.
875 Status
= VariableCommonInitialize ();
876 ASSERT_EFI_ERROR (Status
);
879 // Install the Smm Variable Protocol on a new handle.
881 VariableHandle
= NULL
;
882 Status
= gSmst
->SmmInstallProtocolInterface (
884 &gEfiSmmVariableProtocolGuid
,
885 EFI_NATIVE_INTERFACE
,
888 ASSERT_EFI_ERROR (Status
);
891 // Get SMRAM information
893 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
894 ASSERT_EFI_ERROR (Status
);
897 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
898 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
900 Status
= gSmst
->SmmAllocatePool (
901 EfiRuntimeServicesData
,
903 (VOID
**)&mSmramRanges
905 ASSERT_EFI_ERROR (Status
);
907 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
908 ASSERT_EFI_ERROR (Status
);
910 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
912 mVariableBufferPayloadSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) +
913 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - sizeof (VARIABLE_HEADER
);
915 Status
= gSmst
->SmmAllocatePool (
916 EfiRuntimeServicesData
,
917 mVariableBufferPayloadSize
,
918 (VOID
**)&mVariableBufferPayload
920 ASSERT_EFI_ERROR (Status
);
923 /// Register SMM variable SMI handler
925 VariableHandle
= NULL
;
926 Status
= gSmst
->SmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
927 ASSERT_EFI_ERROR (Status
);
930 // Notify the variable wrapper driver the variable service is ready
932 Status
= SystemTable
->BootServices
->InstallProtocolInterface (
934 &gEfiSmmVariableProtocolGuid
,
935 EFI_NATIVE_INTERFACE
,
938 ASSERT_EFI_ERROR (Status
);
941 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
943 Status
= gSmst
->SmmRegisterProtocolNotify (
944 &gEfiSmmEndOfDxeProtocolGuid
,
946 &SmmEndOfDxeRegistration
948 ASSERT_EFI_ERROR (Status
);
951 // Register FtwNotificationEvent () notify function.
953 Status
= gSmst
->SmmRegisterProtocolNotify (
954 &gEfiSmmFaultTolerantWriteProtocolGuid
,
955 SmmFtwNotificationEvent
,
958 ASSERT_EFI_ERROR (Status
);
960 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);