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 - 2019, Intel Corporation. All rights reserved.<BR>
18 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
19 SPDX-License-Identifier: BSD-2-Clause-Patent
23 #include <Protocol/SmmVariable.h>
24 #include <Protocol/SmmFirmwareVolumeBlock.h>
25 #include <Protocol/SmmFaultTolerantWrite.h>
26 #include <Protocol/MmEndOfDxe.h>
27 #include <Protocol/SmmVarCheck.h>
29 #include <Library/MmServicesTableLib.h>
31 #include <Guid/SmmVariableCommon.h>
34 BOOLEAN mAtRuntime
= FALSE
;
35 UINT8
*mVariableBufferPayload
= NULL
;
36 UINTN mVariableBufferPayloadSize
;
39 SecureBoot Hook for SetVariable.
41 @param[in] VariableName Name of Variable to be found.
42 @param[in] VendorGuid Variable vendor GUID.
48 IN CHAR16
*VariableName
,
49 IN EFI_GUID
*VendorGuid
57 This code sets variable in storage blocks (Volatile or Non-Volatile).
59 @param VariableName Name of Variable to be found.
60 @param VendorGuid Variable vendor GUID.
61 @param Attributes Attribute value of the variable found
62 @param DataSize Size of Data found. If size is less than the
63 data, this value contains the required size.
64 @param Data Data pointer.
66 @return EFI_INVALID_PARAMETER Invalid parameter.
67 @return EFI_SUCCESS Set successfully.
68 @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.
69 @return EFI_NOT_FOUND Not found.
70 @return EFI_WRITE_PROTECTED Variable is read-only.
75 SmmVariableSetVariable (
76 IN CHAR16
*VariableName
,
77 IN EFI_GUID
*VendorGuid
,
86 // Disable write protection when the calling SetVariable() through EFI_SMM_VARIABLE_PROTOCOL.
88 mRequestSource
= VarCheckFromTrusted
;
89 Status
= VariableServiceSetVariable (
96 mRequestSource
= VarCheckFromUntrusted
;
100 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable
= {
101 VariableServiceGetVariable
,
102 VariableServiceGetNextVariableName
,
103 SmmVariableSetVariable
,
104 VariableServiceQueryVariableInfo
107 EDKII_SMM_VAR_CHECK_PROTOCOL mSmmVarCheck
= { VarCheckRegisterSetVariableCheckHandler
,
108 VarCheckVariablePropertySet
,
109 VarCheckVariablePropertyGet
};
112 Return TRUE if ExitBootServices () has been called.
114 @retval TRUE If ExitBootServices () has been called.
125 Initializes a basic mutual exclusion lock.
127 This function initializes a basic mutual exclusion lock to the released state
128 and returns the lock. Each lock provides mutual exclusion access at its task
129 priority level. Since there is no preemption or multiprocessor support in EFI,
130 acquiring the lock only consists of raising to the locks TPL.
131 If Lock is NULL, then ASSERT().
132 If Priority is not a valid TPL value, then ASSERT().
134 @param Lock A pointer to the lock data structure to initialize.
135 @param Priority EFI TPL is associated with the lock.
142 IN OUT EFI_LOCK
*Lock
,
150 Acquires lock only at boot time. Simply returns at runtime.
152 This is a temperary function that will be removed when
153 EfiAcquireLock() in UefiLib can handle the call in UEFI
154 Runtimer driver in RT phase.
155 It calls EfiAcquireLock() at boot time, and simply returns
158 @param Lock A pointer to the lock to acquire.
162 AcquireLockOnlyAtBootTime (
171 Releases lock only at boot time. Simply returns at runtime.
173 This is a temperary function which will be removed when
174 EfiReleaseLock() in UefiLib can handle the call in UEFI
175 Runtimer driver in RT phase.
176 It calls EfiReleaseLock() at boot time and simply returns
179 @param Lock A pointer to the lock to release.
183 ReleaseLockOnlyAtBootTime (
191 Retrieve the SMM Fault Tolerent Write protocol interface.
193 @param[out] FtwProtocol The interface of SMM Ftw protocol
195 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
196 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
197 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
202 OUT VOID
**FtwProtocol
208 // Locate Smm Fault Tolerent Write protocol
210 Status
= gMmst
->MmLocateProtocol (
211 &gEfiSmmFaultTolerantWriteProtocolGuid
,
220 Retrieve the SMM FVB protocol interface by HANDLE.
222 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
223 reading, writing, and erasing the target block.
224 @param[out] FvBlock The interface of SMM FVB protocol
226 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
227 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
228 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
233 IN EFI_HANDLE FvBlockHandle
,
234 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
238 // To get the SMM FVB protocol interface on the handle
240 return gMmst
->MmHandleProtocol (
242 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
249 Function returns an array of handles that support the SMM FVB protocol
250 in a buffer allocated from pool.
252 @param[out] NumberHandles The number of handles returned in Buffer.
253 @param[out] Buffer A pointer to the buffer to return the requested
254 array of handles that support SMM FVB protocol.
256 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
257 handles in Buffer was returned in NumberHandles.
258 @retval EFI_NOT_FOUND No SMM FVB handle was found.
259 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
260 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
264 GetFvbCountAndBuffer (
265 OUT UINTN
*NumberHandles
,
266 OUT EFI_HANDLE
**Buffer
272 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
273 return EFI_INVALID_PARAMETER
;
279 Status
= gMmst
->MmLocateHandle (
281 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
286 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
287 return EFI_NOT_FOUND
;
290 *Buffer
= AllocatePool (BufferSize
);
291 if (*Buffer
== NULL
) {
292 return EFI_OUT_OF_RESOURCES
;
295 Status
= gMmst
->MmLocateHandle (
297 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
303 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
304 if (EFI_ERROR(Status
)) {
315 Get the variable statistics information from the information buffer pointed by gVariableInfo.
317 Caution: This function may be invoked at SMM runtime.
318 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
320 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
321 On input, point to the variable information returned last time. if
322 InfoEntry->VendorGuid is zero, return the first information.
323 On output, point to the next variable information.
324 @param[in, out] InfoSize On input, the size of the variable information buffer.
325 On output, the returned variable information size.
327 @retval EFI_SUCCESS The variable information is found and returned successfully.
328 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
329 PcdVariableCollectStatistics should be set TRUE to support it.
330 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
331 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
335 SmmVariableGetStatistics (
336 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
337 IN OUT UINTN
*InfoSize
340 VARIABLE_INFO_ENTRY
*VariableInfo
;
342 UINTN StatisticsInfoSize
;
344 UINTN InfoNameMaxSize
;
347 if (InfoEntry
== NULL
) {
348 return EFI_INVALID_PARAMETER
;
351 VariableInfo
= gVariableInfo
;
352 if (VariableInfo
== NULL
) {
353 return EFI_UNSUPPORTED
;
356 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
);
357 if (*InfoSize
< StatisticsInfoSize
) {
358 *InfoSize
= StatisticsInfoSize
;
359 return EFI_BUFFER_TOO_SMALL
;
361 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
362 InfoNameMaxSize
= (*InfoSize
- sizeof (VARIABLE_INFO_ENTRY
));
364 CopyGuid (&VendorGuid
, &InfoEntry
->VendorGuid
);
366 if (IsZeroGuid (&VendorGuid
)) {
368 // Return the first variable info
370 NameSize
= StrSize (VariableInfo
->Name
);
371 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + NameSize
;
372 if (*InfoSize
< StatisticsInfoSize
) {
373 *InfoSize
= StatisticsInfoSize
;
374 return EFI_BUFFER_TOO_SMALL
;
376 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
377 CopyMem (InfoName
, VariableInfo
->Name
, NameSize
);
378 *InfoSize
= StatisticsInfoSize
;
383 // Get the next variable info
385 while (VariableInfo
!= NULL
) {
386 if (CompareGuid (&VariableInfo
->VendorGuid
, &VendorGuid
)) {
387 NameSize
= StrSize (VariableInfo
->Name
);
388 if (NameSize
<= InfoNameMaxSize
) {
389 if (CompareMem (VariableInfo
->Name
, InfoName
, NameSize
) == 0) {
391 // Find the match one
393 VariableInfo
= VariableInfo
->Next
;
398 VariableInfo
= VariableInfo
->Next
;
401 if (VariableInfo
== NULL
) {
407 // Output the new variable info
409 NameSize
= StrSize (VariableInfo
->Name
);
410 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + NameSize
;
411 if (*InfoSize
< StatisticsInfoSize
) {
412 *InfoSize
= StatisticsInfoSize
;
413 return EFI_BUFFER_TOO_SMALL
;
416 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
417 CopyMem (InfoName
, VariableInfo
->Name
, NameSize
);
418 *InfoSize
= StatisticsInfoSize
;
425 Communication service SMI Handler entry.
427 This SMI handler provides services for the variable wrapper driver.
429 Caution: This function may receive untrusted input.
430 This variable data and communicate buffer are external input, so this function will do basic validation.
431 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
432 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
433 SmmVariableGetStatistics() should also do validation based on its own knowledge.
435 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
436 @param[in] RegisterContext Points to an optional handler context which was specified when the
437 handler was registered.
438 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
439 be conveyed from a non-SMM environment into an SMM environment.
440 @param[in, out] CommBufferSize The size of the CommBuffer.
442 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
443 should still be called.
444 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
446 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
448 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
453 IN EFI_HANDLE DispatchHandle
,
454 IN CONST VOID
*RegisterContext
,
455 IN OUT VOID
*CommBuffer
,
456 IN OUT UINTN
*CommBufferSize
460 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
461 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
462 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
463 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
464 SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
*GetPayloadSize
;
465 VARIABLE_INFO_ENTRY
*VariableInfo
;
466 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
467 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*CommVariableProperty
;
469 UINTN NameBufferSize
;
470 UINTN CommBufferPayloadSize
;
471 UINTN TempCommBufferSize
;
474 // If input is invalid, stop processing this SMI
476 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
480 TempCommBufferSize
= *CommBufferSize
;
482 if (TempCommBufferSize
< SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
483 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer size invalid!\n"));
486 CommBufferPayloadSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
487 if (CommBufferPayloadSize
> mVariableBufferPayloadSize
) {
488 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer payload size invalid!\n"));
492 if (!VariableSmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
493 DEBUG ((EFI_D_ERROR
, "SmmVariableHandler: SMM communication buffer in SMRAM or overflow!\n"));
497 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
498 switch (SmmVariableFunctionHeader
->Function
) {
499 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
500 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
501 DEBUG ((EFI_D_ERROR
, "GetVariable: SMM communication buffer size invalid!\n"));
505 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
507 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
508 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
509 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
510 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
512 // Prevent InfoSize overflow happen
514 Status
= EFI_ACCESS_DENIED
;
517 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
518 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
521 // SMRAM range check already covered before
523 if (InfoSize
> CommBufferPayloadSize
) {
524 DEBUG ((EFI_D_ERROR
, "GetVariable: Data size exceed communication buffer size limit!\n"));
525 Status
= EFI_ACCESS_DENIED
;
530 // The VariableSpeculationBarrier() call here is to ensure the previous
531 // range/content checks for the CommBuffer have been completed before the
532 // subsequent consumption of the CommBuffer content.
534 VariableSpeculationBarrier ();
535 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
537 // Make sure VariableName is A Null-terminated string.
539 Status
= EFI_ACCESS_DENIED
;
543 Status
= VariableServiceGetVariable (
544 SmmVariableHeader
->Name
,
545 &SmmVariableHeader
->Guid
,
546 &SmmVariableHeader
->Attributes
,
547 &SmmVariableHeader
->DataSize
,
548 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
550 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
553 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
554 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
555 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: SMM communication buffer size invalid!\n"));
559 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
561 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
562 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) mVariableBufferPayload
;
563 if ((UINTN
)(~0) - GetNextVariableName
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
565 // Prevent InfoSize overflow happen
567 Status
= EFI_ACCESS_DENIED
;
570 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + GetNextVariableName
->NameSize
;
573 // SMRAM range check already covered before
575 if (InfoSize
> CommBufferPayloadSize
) {
576 DEBUG ((EFI_D_ERROR
, "GetNextVariableName: Data size exceed communication buffer size limit!\n"));
577 Status
= EFI_ACCESS_DENIED
;
581 NameBufferSize
= CommBufferPayloadSize
- OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
582 if (NameBufferSize
< sizeof (CHAR16
) || GetNextVariableName
->Name
[NameBufferSize
/sizeof (CHAR16
) - 1] != L
'\0') {
584 // Make sure input VariableName is A Null-terminated string.
586 Status
= EFI_ACCESS_DENIED
;
590 Status
= VariableServiceGetNextVariableName (
591 &GetNextVariableName
->NameSize
,
592 GetNextVariableName
->Name
,
593 &GetNextVariableName
->Guid
595 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
598 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
599 if (CommBufferPayloadSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
600 DEBUG ((EFI_D_ERROR
, "SetVariable: SMM communication buffer size invalid!\n"));
604 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
606 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
607 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) mVariableBufferPayload
;
608 if (((UINTN
)(~0) - SmmVariableHeader
->DataSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
609 ((UINTN
)(~0) - SmmVariableHeader
->NameSize
< OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + SmmVariableHeader
->DataSize
)) {
611 // Prevent InfoSize overflow happen
613 Status
= EFI_ACCESS_DENIED
;
616 InfoSize
= OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)
617 + SmmVariableHeader
->DataSize
+ SmmVariableHeader
->NameSize
;
620 // SMRAM range check already covered before
621 // Data buffer should not contain SMM range
623 if (InfoSize
> CommBufferPayloadSize
) {
624 DEBUG ((EFI_D_ERROR
, "SetVariable: Data size exceed communication buffer size limit!\n"));
625 Status
= EFI_ACCESS_DENIED
;
630 // The VariableSpeculationBarrier() call here is to ensure the previous
631 // range/content checks for the CommBuffer have been completed before the
632 // subsequent consumption of the CommBuffer content.
634 VariableSpeculationBarrier ();
635 if (SmmVariableHeader
->NameSize
< sizeof (CHAR16
) || SmmVariableHeader
->Name
[SmmVariableHeader
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
637 // Make sure VariableName is A Null-terminated string.
639 Status
= EFI_ACCESS_DENIED
;
643 Status
= VariableServiceSetVariable (
644 SmmVariableHeader
->Name
,
645 &SmmVariableHeader
->Guid
,
646 SmmVariableHeader
->Attributes
,
647 SmmVariableHeader
->DataSize
,
648 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
652 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
653 if (CommBufferPayloadSize
< sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
)) {
654 DEBUG ((EFI_D_ERROR
, "QueryVariableInfo: SMM communication buffer size invalid!\n"));
657 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
659 Status
= VariableServiceQueryVariableInfo (
660 QueryVariableInfo
->Attributes
,
661 &QueryVariableInfo
->MaximumVariableStorageSize
,
662 &QueryVariableInfo
->RemainingVariableStorageSize
,
663 &QueryVariableInfo
->MaximumVariableSize
667 case SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE
:
668 if (CommBufferPayloadSize
< sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
)) {
669 DEBUG ((EFI_D_ERROR
, "GetPayloadSize: SMM communication buffer size invalid!\n"));
672 GetPayloadSize
= (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
*) SmmVariableFunctionHeader
->Data
;
673 GetPayloadSize
->VariablePayloadSize
= mVariableBufferPayloadSize
;
674 Status
= EFI_SUCCESS
;
677 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
679 Status
= EFI_UNSUPPORTED
;
683 MorLockInitAtEndOfDxe ();
685 VarCheckLibInitializeAtEndOfDxe (NULL
);
687 // The initialization for variable quota.
689 InitializeVariableQuota ();
692 Status
= EFI_SUCCESS
;
695 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
697 Status
= EFI_SUCCESS
;
700 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
701 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
702 InfoSize
= TempCommBufferSize
- SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
705 // Do not need to check SmmVariableFunctionHeader->Data in SMRAM here.
706 // It is covered by previous CommBuffer check
710 // Do not need to check CommBufferSize buffer as it should point to SMRAM
711 // that was used by SMM core to cache CommSize from SmmCommunication protocol.
714 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
715 *CommBufferSize
= InfoSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
718 case SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
:
720 Status
= EFI_ACCESS_DENIED
;
722 VariableToLock
= (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
723 Status
= VariableLockRequestToLock (
725 VariableToLock
->Name
,
726 &VariableToLock
->Guid
730 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET
:
732 Status
= EFI_ACCESS_DENIED
;
734 CommVariableProperty
= (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*) SmmVariableFunctionHeader
->Data
;
735 Status
= VarCheckVariablePropertySet (
736 CommVariableProperty
->Name
,
737 &CommVariableProperty
->Guid
,
738 &CommVariableProperty
->VariableProperty
742 case SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
:
743 if (CommBufferPayloadSize
< OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
)) {
744 DEBUG ((EFI_D_ERROR
, "VarCheckVariablePropertyGet: SMM communication buffer size invalid!\n"));
748 // Copy the input communicate buffer payload to pre-allocated SMM variable buffer payload.
750 CopyMem (mVariableBufferPayload
, SmmVariableFunctionHeader
->Data
, CommBufferPayloadSize
);
751 CommVariableProperty
= (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*) mVariableBufferPayload
;
752 if ((UINTN
) (~0) - CommVariableProperty
->NameSize
< OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
)) {
754 // Prevent InfoSize overflow happen
756 Status
= EFI_ACCESS_DENIED
;
759 InfoSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
) + CommVariableProperty
->NameSize
;
762 // SMRAM range check already covered before
764 if (InfoSize
> CommBufferPayloadSize
) {
765 DEBUG ((EFI_D_ERROR
, "VarCheckVariablePropertyGet: Data size exceed communication buffer size limit!\n"));
766 Status
= EFI_ACCESS_DENIED
;
771 // The VariableSpeculationBarrier() call here is to ensure the previous
772 // range/content checks for the CommBuffer have been completed before the
773 // subsequent consumption of the CommBuffer content.
775 VariableSpeculationBarrier ();
776 if (CommVariableProperty
->NameSize
< sizeof (CHAR16
) || CommVariableProperty
->Name
[CommVariableProperty
->NameSize
/sizeof (CHAR16
) - 1] != L
'\0') {
778 // Make sure VariableName is A Null-terminated string.
780 Status
= EFI_ACCESS_DENIED
;
784 Status
= VarCheckVariablePropertyGet (
785 CommVariableProperty
->Name
,
786 &CommVariableProperty
->Guid
,
787 &CommVariableProperty
->VariableProperty
789 CopyMem (SmmVariableFunctionHeader
->Data
, mVariableBufferPayload
, CommBufferPayloadSize
);
793 Status
= EFI_UNSUPPORTED
;
798 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
804 SMM END_OF_DXE protocol notification event handler.
806 @param Protocol Points to the protocol's unique identifier
807 @param Interface Points to the interface instance
808 @param Handle The handle on which the interface was installed
810 @retval EFI_SUCCESS SmmEndOfDxeCallback runs successfully
815 SmmEndOfDxeCallback (
816 IN CONST EFI_GUID
*Protocol
,
821 DEBUG ((EFI_D_INFO
, "[Variable]SMM_END_OF_DXE is signaled\n"));
822 MorLockInitAtEndOfDxe ();
824 VarCheckLibInitializeAtEndOfDxe (NULL
);
826 // The initialization for variable quota.
828 InitializeVariableQuota ();
829 if (PcdGetBool (PcdReclaimVariableSpaceAtEndOfDxe
)) {
837 Initializes variable write service for SMM.
841 VariableWriteServiceInitializeSmm (
847 Status
= VariableWriteServiceInitialize ();
848 if (EFI_ERROR (Status
)) {
849 DEBUG ((DEBUG_ERROR
, "Variable write service initialization failed. Status = %r\n", Status
));
853 // Notify the variable wrapper driver the variable write service is ready
855 VariableNotifySmmWriteReady ();
859 SMM Fault Tolerant Write protocol notification event handler.
861 Non-Volatile variable write may needs FTW protocol to reclaim when
864 @param Protocol Points to the protocol's unique identifier
865 @param Interface Points to the interface instance
866 @param Handle The handle on which the interface was installed
868 @retval EFI_SUCCESS SmmEventCallback runs successfully
869 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
874 SmmFtwNotificationEvent (
875 IN CONST EFI_GUID
*Protocol
,
881 EFI_PHYSICAL_ADDRESS VariableStoreBase
;
882 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
883 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
884 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
885 UINTN FtwMaxBlockSize
;
887 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
892 // Ensure SMM FTW protocol is installed.
894 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
895 if (EFI_ERROR (Status
)) {
899 Status
= FtwProtocol
->GetMaxBlockSize (FtwProtocol
, &FtwMaxBlockSize
);
900 if (!EFI_ERROR (Status
)) {
901 ASSERT (PcdGet32 (PcdFlashNvStorageVariableSize
) <= FtwMaxBlockSize
);
904 NvStorageVariableBase
= NV_STORAGE_VARIABLE_BASE
;
905 VariableStoreBase
= NvStorageVariableBase
+ mNvFvHeaderCache
->HeaderLength
;
908 // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.
910 mVariableModuleGlobal
->VariableGlobal
.NonVolatileVariableBase
= VariableStoreBase
;
913 // Find the proper FVB protocol for variable.
915 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
916 if (EFI_ERROR (Status
)) {
917 return EFI_NOT_FOUND
;
920 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
923 // Initializes variable write service after FTW was ready.
925 VariableWriteServiceInitializeSmm ();
932 Variable Driver main entry point. The Variable driver places the 4 EFI
933 runtime services in the EFI System Table and installs arch protocols
934 for variable read and write services being available. It also registers
935 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
937 @retval EFI_SUCCESS Variable service successfully initialized.
942 MmVariableServiceInitialize (
947 EFI_HANDLE VariableHandle
;
948 VOID
*SmmFtwRegistration
;
949 VOID
*SmmEndOfDxeRegistration
;
952 // Variable initialize.
954 Status
= VariableCommonInitialize ();
955 ASSERT_EFI_ERROR (Status
);
958 // Install the Smm Variable Protocol on a new handle.
960 VariableHandle
= NULL
;
961 Status
= gMmst
->MmInstallProtocolInterface (
963 &gEfiSmmVariableProtocolGuid
,
964 EFI_NATIVE_INTERFACE
,
967 ASSERT_EFI_ERROR (Status
);
969 Status
= gMmst
->MmInstallProtocolInterface (
971 &gEdkiiSmmVarCheckProtocolGuid
,
972 EFI_NATIVE_INTERFACE
,
975 ASSERT_EFI_ERROR (Status
);
977 mVariableBufferPayloadSize
= GetMaxVariableSize () +
978 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
) - GetVariableHeaderSize ();
980 Status
= gMmst
->MmAllocatePool (
981 EfiRuntimeServicesData
,
982 mVariableBufferPayloadSize
,
983 (VOID
**)&mVariableBufferPayload
985 ASSERT_EFI_ERROR (Status
);
988 /// Register SMM variable SMI handler
990 VariableHandle
= NULL
;
991 Status
= gMmst
->MmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
992 ASSERT_EFI_ERROR (Status
);
995 // Notify the variable wrapper driver the variable service is ready
997 VariableNotifySmmReady ();
1000 // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
1002 Status
= gMmst
->MmRegisterProtocolNotify (
1003 &gEfiMmEndOfDxeProtocolGuid
,
1004 SmmEndOfDxeCallback
,
1005 &SmmEndOfDxeRegistration
1007 ASSERT_EFI_ERROR (Status
);
1009 if (!PcdGetBool (PcdEmuVariableNvModeEnable
)) {
1011 // Register FtwNotificationEvent () notify function.
1013 Status
= gMmst
->MmRegisterProtocolNotify (
1014 &gEfiSmmFaultTolerantWriteProtocolGuid
,
1015 SmmFtwNotificationEvent
,
1018 ASSERT_EFI_ERROR (Status
);
1020 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);
1023 // Emulated non-volatile variable mode does not depend on FVB and FTW.
1025 VariableWriteServiceInitializeSmm ();