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 - 2015, 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>
33 #include <Protocol/SmmVarCheck.h>
35 #include <Library/SmmServicesTableLib.h>
36 #include <Library/SmmMemLib.h>
38 #include <Guid/VariableFormat.h>
39 #include <Guid/SmmVariableCommon.h>
42 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
43 EFI_HANDLE mSmmVariableHandle
= NULL
;
44 EFI_HANDLE mVariableHandle
= NULL
;
45 BOOLEAN mAtRuntime
= FALSE
;
46 EFI_GUID mZeroGuid
= {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
47 UINT8
*mVariableBufferPayload
= NULL
;
48 UINTN mVariableBufferPayloadSize
;
49 extern BOOLEAN mEndOfDxe
;
50 extern BOOLEAN mEnableLocking
;
54 This code sets variable in storage blocks (Volatile or Non-Volatile).
56 @param VariableName Name of Variable to be found.
57 @param VendorGuid Variable vendor GUID.
58 @param Attributes Attribute value of the variable found
59 @param DataSize Size of Data found. If size is less than the
60 data, this value contains the required size.
61 @param Data Data pointer.
63 @return EFI_INVALID_PARAMETER Invalid parameter.
64 @return EFI_SUCCESS Set successfully.
65 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
66 @return EFI_NOT_FOUND Not found.
67 @return EFI_WRITE_PROTECTED Variable is read-only.
72 SmmVariableSetVariable (
73 IN CHAR16
*VariableName
,
74 IN EFI_GUID
*VendorGuid
,
83 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.
85 mEnableLocking
= FALSE
;
86 Status
= VariableServiceSetVariable (
93 mEnableLocking
= TRUE
;
97 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable
= {
98 VariableServiceGetVariable
,
99 VariableServiceGetNextVariableName
,
100 SmmVariableSetVariable
,
101 VariableServiceQueryVariableInfo
104 EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck
= { VarCheckRegisterSetVariableCheckHandler
,
105 VarCheckVariablePropertySet
,
106 VarCheckVariablePropertyGet
};
109 Return TRUE if ExitBootServices () has been called.
111 @retval TRUE If ExitBootServices () has been called.
122 Initializes a basic mutual exclusion lock.
124 This function initializes a basic mutual exclusion lock to the released state
125 and returns the lock. Each lock provides mutual exclusion access at its task
126 priority level. Since there is no preemption or multiprocessor support in EFI,
127 acquiring the lock only consists of raising to the locks TPL.
128 If Lock is NULL, then ASSERT().
129 If Priority is not a valid TPL value, then ASSERT().
131 @param Lock A pointer to the lock data structure to initialize.
132 @param Priority EFI TPL is associated with the lock.
139 IN OUT EFI_LOCK
*Lock
,
147 Acquires lock only at boot time. Simply returns at runtime.
149 This is a temperary function that will be removed when
150 EfiAcquireLock() in UefiLib can handle the call in UEFI
151 Runtimer driver in RT phase.
152 It calls EfiAcquireLock() at boot time, and simply returns
155 @param Lock A pointer to the lock to acquire.
159 AcquireLockOnlyAtBootTime (
168 Releases lock only at boot time. Simply returns at runtime.
170 This is a temperary function which will be removed when
171 EfiReleaseLock() in UefiLib can handle the call in UEFI
172 Runtimer driver in RT phase.
173 It calls EfiReleaseLock() at boot time and simply returns
176 @param Lock A pointer to the lock to release.
180 ReleaseLockOnlyAtBootTime (
188 Retrive the SMM Fault Tolerent Write protocol interface.
190 @param[out] FtwProtocol The interface of SMM Ftw protocol
192 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
193 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
194 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
199 OUT VOID
**FtwProtocol
205 // Locate Smm Fault Tolerent Write protocol
207 Status
= gSmst
->SmmLocateProtocol (
208 &gEfiSmmFaultTolerantWriteProtocolGuid
,
217 Retrive the SMM FVB protocol interface by HANDLE.
219 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
220 reading, writing, and erasing the target block.
221 @param[out] FvBlock The interface of SMM FVB protocol
223 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
224 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
225 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
230 IN EFI_HANDLE FvBlockHandle
,
231 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
235 // To get the SMM FVB protocol interface on the handle
237 return gSmst
->SmmHandleProtocol (
239 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
246 Function returns an array of handles that support the SMM FVB protocol
247 in a buffer allocated from pool.
249 @param[out] NumberHandles The number of handles returned in Buffer.
250 @param[out] Buffer A pointer to the buffer to return the requested
251 array of handles that support SMM FVB protocol.
253 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
254 handles in Buffer was returned in NumberHandles.
255 @retval EFI_NOT_FOUND No SMM FVB handle was found.
256 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
257 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
261 GetFvbCountAndBuffer (
262 OUT UINTN
*NumberHandles
,
263 OUT EFI_HANDLE
**Buffer
269 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
270 return EFI_INVALID_PARAMETER
;
276 Status
= gSmst
->SmmLocateHandle (
278 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
283 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
284 return EFI_NOT_FOUND
;
287 *Buffer
= AllocatePool (BufferSize
);
288 if (*Buffer
== NULL
) {
289 return EFI_OUT_OF_RESOURCES
;
292 Status
= gSmst
->SmmLocateHandle (
294 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
300 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
301 if (EFI_ERROR(Status
)) {
312 Get the variable statistics information from the information buffer pointed by gVariableInfo.
314 Caution: This function may be invoked at SMM runtime.
315 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
317 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
318 On input, point to the variable information returned last time. if
319 InfoEntry->VendorGuid is zero, return the first information.
320 On output, point to the next variable information.
321 @param[in, out] InfoSize On input, the size of the variable information buffer.
322 On output, the returned variable information size.
324 @retval EFI_SUCCESS The variable information is found and returned successfully.
325 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
326 PcdVariableCollectStatistics should be set TRUE to support it.
327 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
331 SmmVariableGetStatistics (
332 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
333 IN OUT UINTN
*InfoSize
336 VARIABLE_INFO_ENTRY
*VariableInfo
;
338 UINTN StatisticsInfoSize
;
342 ASSERT (InfoEntry
!= NULL
);
343 VariableInfo
= gVariableInfo
;
344 if (VariableInfo
== NULL
) {
345 return EFI_UNSUPPORTED
;
348 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
349 if (*InfoSize
< StatisticsInfoSize
) {
350 *InfoSize
= StatisticsInfoSize
;
351 return EFI_BUFFER_TOO_SMALL
;
353 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
355 CopyGuid (&VendorGuid
, &InfoEntry
->VendorGuid
);
357 if (CompareGuid (&VendorGuid
, &mZeroGuid
)) {
359 // Return the first variable info
361 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
362 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
363 *InfoSize
= StatisticsInfoSize
;
368 // Get the next variable info
370 while (VariableInfo
!= NULL
) {
371 if (CompareGuid (&VariableInfo
->VendorGuid
, &VendorGuid
)) {
372 NameLength
= StrSize (VariableInfo
->Name
);
373 if (NameLength
== StrSize (InfoName
)) {
374 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
376 // Find the match one
378 VariableInfo
= VariableInfo
->Next
;
383 VariableInfo
= VariableInfo
->Next
;
386 if (VariableInfo
== NULL
) {
392 // Output the new variable info
394 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
395 if (*InfoSize
< StatisticsInfoSize
) {
396 *InfoSize
= StatisticsInfoSize
;
397 return EFI_BUFFER_TOO_SMALL
;
400 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
401 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
402 *InfoSize
= StatisticsInfoSize
;
409 Communication service SMI Handler entry.
411 This SMI handler provides services for the variable wrapper driver.
413 Caution: This function may receive untrusted input.
414 This variable data and communicate buffer are external input, so this function will do basic validation.
415 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
416 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
417 SmmVariableGetStatistics() should also do validation based on its own knowledge.
419 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
420 @param[in] RegisterContext Points to an optional handler context which was specified when the
421 handler was registered.
422 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
423 be conveyed from a non-SMM environment into an SMM environment.
424 @param[in, out] CommBufferSize The size of the CommBuffer.
426 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
427 should still be called.
428 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
430 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
432 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
437 IN EFI_HANDLE DispatchHandle
,
438 IN CONST VOID
*RegisterContext
,
439 IN OUT VOID
*CommBuffer
,
440 IN OUT UINTN
*CommBufferSize
444 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
445 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
446 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
447 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
448 VARIABLE_INFO_ENTRY
*VariableInfo
;
449 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
450 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*CommVariableProperty
;
452 UINTN NameBufferSize
;
453 UINTN CommBufferPayloadSize
;
454 UINTN TempCommBufferSize
;
457 // If input is invalid, stop processing this SMI
459 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
463 TempCommBufferSize
= *CommBufferSize
;
465 if (TempCommBufferSize
< SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
466 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
469 CommBufferPayloadSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
470 if (CommBufferPayloadSize
> mVariableBufferPayloadSize
) {
471 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
475 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
476 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
480 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
481 switch (SmmVariableFunctionHeader
->Function
) {
482 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
483 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
484 DEBUG ((EFI_D_ERROR
, "GetVariable: SMM communication buffer size invalid!\n"));
488 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
490 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
491 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
492 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
493 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
495 // Prevent InfoSize overflow happen
497 Status
= EFI_ACCESS_DENIED
;
500 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
501 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
504 // SMRAM range check already covered before
506 if (InfoSize
> CommBufferPayloadSize
) {
507 DEBUG ((EFI_D_ERROR
, "GetVariable: Data size exceed communication buffer size limit!\n"));
508 Status
= EFI_ACCESS_DENIED
;
512 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
514 // Make sure VariableName is A Null-terminated string.
516 Status
= EFI_ACCESS_DENIED
;
520 Status
= VariableServiceGetVariable (
521 SmmVariableHeader
->Name
,
522 &SmmVariableHeader
->Guid
,
523 &SmmVariableHeader
->Attributes
,
524 &SmmVariableHeader
->DataSize
,
525 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
527 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
530 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
531 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
532 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: SMM communication buffer size invalid!\n"));
536 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
538 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
539 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) mVariableBufferPayload
;
540 if ((UINTN
)(~0) - GetNextVariableName
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
542 // Prevent InfoSize overflow happen
544 Status
= EFI_ACCESS_DENIED
;
547 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + GetNextVariableName
->NameSize
;
550 // SMRAM range check already covered before
552 if (InfoSize
> CommBufferPayloadSize
) {
553 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
554 Status
= EFI_ACCESS_DENIED
;
558 NameBufferSize
= CommBufferPayloadSize
- OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
559 if (NameBufferSize
< sizeof (CHAR16
) || GetNextVariableName
->Name
[NameBufferSize
/sizeof (CHAR16
) - 1] != L
'\0') {
561 // Make sure input VariableName is A Null-terminated string.
563 Status
= EFI_ACCESS_DENIED
;
567 Status
= VariableServiceGetNextVariableName (
568 &GetNextVariableName
->NameSize
,
569 GetNextVariableName
->Name
,
570 &GetNextVariableName
->Guid
572 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
575 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
576 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
577 DEBUG ((EFI_D_ERROR
, "SetVariable: SMM communication buffer size invalid!\n"));
581 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
583 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
584 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
585 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
586 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
588 // Prevent InfoSize overflow happen
590 Status
= EFI_ACCESS_DENIED
;
593 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
594 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
597 // SMRAM range check already covered before
598 // Data buffer should not contain SMM range
600 if (InfoSize
> CommBufferPayloadSize
) {
601 DEBUG ((EFI_D_ERROR
, "SetVariable: Data size exceed communication buffer size limit!\n"));
602 Status
= EFI_ACCESS_DENIED
;
606 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
608 // Make sure VariableName is A Null-terminated string.
610 Status
= EFI_ACCESS_DENIED
;
614 Status
= VariableServiceSetVariable (
615 SmmVariableHeader
->Name
,
616 &SmmVariableHeader
->Guid
,
617 SmmVariableHeader
->Attributes
,
618 SmmVariableHeader
->DataSize
,
619 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
623 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
624 if (CommBufferPayloadSize
< sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
)) {
625 DEBUG ((EFI_D_ERROR
, "QueryVariableInfo: SMM communication buffer size invalid!\n"));
628 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
630 Status
= VariableServiceQueryVariableInfo (
631 QueryVariableInfo
->Attributes
,
632 &QueryVariableInfo
->MaximumVariableStorageSize
,
633 &QueryVariableInfo
->RemainingVariableStorageSize
,
634 &QueryVariableInfo
->MaximumVariableSize
638 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
641 // The initialization for variable quota.
643 InitializeVariableQuota ();
645 Status
= EFI_UNSUPPORTED
;
649 Status
= EFI_SUCCESS
;
652 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
654 Status
= EFI_SUCCESS
;
657 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
658 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
659 InfoSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
662 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
663 // It is covered by previous CommBuffer check
666 if (!SmmIsBufferOutsideSmmValid ((EFI_PHYSICAL_ADDRESS
)(UINTN
)CommBufferSize
, sizeof(UINTN
))) {
667 DEBUG ((EFI_D_ERROR
, "GetStatistics: SMM communication buffer in SMRAM!\n"));
668 Status
= EFI_ACCESS_DENIED
;
672 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
673 *CommBufferSize
= InfoSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
676 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
:
678 Status
= EFI_ACCESS_DENIED
;
680 VariableToLock
= (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
681 Status
= VariableLockRequestToLock (
683 VariableToLock
->Name
,
684 &VariableToLock
->Guid
688 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET
:
690 Status
= EFI_ACCESS_DENIED
;
692 CommVariableProperty
= (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*) SmmVariableFunctionHeader
->Data
;
693 Status
= VarCheckVariablePropertySet (
694 CommVariableProperty
->Name
,
695 &CommVariableProperty
->Guid
,
696 &CommVariableProperty
->VariableProperty
700 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
:
701 if (CommBufferPayloadSize
< OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
)) {
702 DEBUG ((EFI_D_ERROR
, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n"));
706 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
708 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
709 CommVariableProperty
= (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*) mVariableBufferPayload
;
710 if ((UINTN
) (~0) - CommVariableProperty
->NameSize
< OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
)) {
712 // Prevent InfoSize overflow happen
714 Status
= EFI_ACCESS_DENIED
;
717 InfoSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
) + CommVariableProperty
->NameSize
;
720 // SMRAM range check already covered before
722 if (InfoSize
> CommBufferPayloadSize
) {
723 DEBUG ((EFI_D_ERROR
, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n"));
724 Status
= EFI_ACCESS_DENIED
;
728 if (CommVariableProperty
->NameSize
< sizeof (CHAR16
) || CommVariableProperty
->Name
[CommVariableProperty
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
730 // Make sure VariableName is A Null-terminated string.
732 Status
= EFI_ACCESS_DENIED
;
736 Status
= VarCheckVariablePropertyGet (
737 CommVariableProperty
->Name
,
738 &CommVariableProperty
->Guid
,
739 &CommVariableProperty
->VariableProperty
741 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
745 Status
= EFI_UNSUPPORTED
;
750 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
756 SMM END_OF_DXE protocol notification event handler.
758 @param Protocol Points to the protocol's unique identifier
759 @param Interface Points to the interface instance
760 @param Handle The handle on which the interface was installed
762 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
767 SmmEndOfDxeCallback (
768 IN CONST EFI_GUID
*Protocol
,
773 DEBUG ((EFI_D_INFO
, "[Variable]END_OF_DXE is signaled\n"));
776 // The initialization for variable quota.
778 InitializeVariableQuota ();
779 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
786 SMM Fault Tolerant Write protocol notification event handler.
788 Non-Volatile variable write may needs FTW protocol to reclaim when
791 @param Protocol Points to the protocol's unique identifier
792 @param Interface Points to the interface instance
793 @param Handle The handle on which the interface was installed
795 @retval EFI_SUCCESS SmmEventCallback runs successfully
796 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
801 SmmFtwNotificationEvent (
802 IN CONST EFI_GUID
*Protocol
,
808 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
809 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
810 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
811 UINTN FtwMaxBlockSize
;
813 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
818 // Ensure SMM FTW protocol is installed.
820 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
821 if (EFI_ERROR (Status
)) {
825 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
826 if (!EFI_ERROR (Status
)) {
827 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
831 // Find the proper FVB protocol for variable.
833 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
834 if (NvStorageVariableBase
== 0) {
835 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
837 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
838 if (EFI_ERROR (Status
)) {
839 return EFI_NOT_FOUND
;
842 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
844 Status
= VariableWriteServiceInitialize ();
845 ASSERT_EFI_ERROR (Status
);
848 // Notify the variable wrapper driver the variable write service is ready
850 Status
= gBS
->InstallProtocolInterface (
852 &gSmmVariableWriteGuid
,
853 EFI_NATIVE_INTERFACE
,
856 ASSERT_EFI_ERROR (Status
);
863 Variable Driver main entry point. The Variable driver places the 4 EFI
864 runtime services in the EFI System Table and installs arch protocols
865 for variable read and write services being available. It also registers
866 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
868 @param[in] ImageHandle The firmware allocated handle for the EFI image.
869 @param[in] SystemTable A pointer to the EFI System Table.
871 @retval EFI_SUCCESS Variable service successfully initialized.
876 VariableServiceInitialize (
877 IN EFI_HANDLE ImageHandle
,
878 IN EFI_SYSTEM_TABLE
*SystemTable
882 EFI_HANDLE VariableHandle
;
883 VOID
*SmmFtwRegistration
;
884 VOID
*SmmEndOfDxeRegistration
;
887 // Variable initialize.
889 Status
= VariableCommonInitialize ();
890 ASSERT_EFI_ERROR (Status
);
893 // Install the Smm Variable Protocol on a new handle.
895 VariableHandle
= NULL
;
896 Status
= gSmst
->SmmInstallProtocolInterface (
898 &gEfiSmmVariableProtocolGuid
,
899 EFI_NATIVE_INTERFACE
,
902 ASSERT_EFI_ERROR (Status
);
904 Status
= gSmst
->SmmInstallProtocolInterface (
906 &gEdkiiSmmVarCheckProtocolGuid
,
907 EFI_NATIVE_INTERFACE
,
910 ASSERT_EFI_ERROR (Status
);
912 mVariableBufferPayloadSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) +
913 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, 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
);