2 Implement all four UEFI Runtime Variable services for the nonvolatile
3 and volatile storage space and install variable architecture protocol
4 based on SMM variable module.
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable data.
8 This external input must be validated carefully to avoid security issue like
9 buffer overflow, integer overflow.
11 RuntimeServiceGetVariable() and RuntimeServiceSetVariable() are external API
12 to receive data buffer. The size should be checked carefully.
14 InitCommunicateBuffer() is really function to check the variable data size.
16 Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
17 Copyright (c) Microsoft Corporation.<BR>
18 SPDX-License-Identifier: BSD-2-Clause-Patent
22 #include <Protocol/VariableWrite.h>
23 #include <Protocol/Variable.h>
24 #include <Protocol/SmmCommunication.h>
25 #include <Protocol/SmmVariable.h>
26 #include <Protocol/VariableLock.h>
27 #include <Protocol/VarCheck.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/UefiRuntimeServicesTableLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/UefiDriverEntryPoint.h>
33 #include <Library/UefiRuntimeLib.h>
34 #include <Library/BaseMemoryLib.h>
35 #include <Library/DebugLib.h>
36 #include <Library/UefiLib.h>
37 #include <Library/BaseLib.h>
39 #include <Guid/EventGroup.h>
40 #include <Guid/SmmVariableCommon.h>
42 #include "PrivilegePolymorphic.h"
43 #include "VariableParsing.h"
45 EFI_HANDLE mHandle
= NULL
;
46 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
= NULL
;
47 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
48 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
49 UINT8
*mVariableBuffer
= NULL
;
50 UINT8
*mVariableBufferPhysical
= NULL
;
51 VARIABLE_INFO_ENTRY
*mVariableInfo
= NULL
;
52 VARIABLE_STORE_HEADER
*mVariableRuntimeHobCacheBuffer
= NULL
;
53 VARIABLE_STORE_HEADER
*mVariableRuntimeNvCacheBuffer
= NULL
;
54 VARIABLE_STORE_HEADER
*mVariableRuntimeVolatileCacheBuffer
= NULL
;
55 UINTN mVariableBufferSize
;
56 UINTN mVariableRuntimeHobCacheBufferSize
;
57 UINTN mVariableRuntimeNvCacheBufferSize
;
58 UINTN mVariableRuntimeVolatileCacheBufferSize
;
59 UINTN mVariableBufferPayloadSize
;
60 BOOLEAN mVariableRuntimeCachePendingUpdate
;
61 BOOLEAN mVariableRuntimeCacheReadLock
;
62 BOOLEAN mVariableAuthFormat
;
63 BOOLEAN mHobFlushComplete
;
64 EFI_LOCK mVariableServicesLock
;
65 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
;
66 EDKII_VAR_CHECK_PROTOCOL mVarCheck
;
69 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).
70 Record their initial State when variable write service is ready.
75 RecordSecureBootPolicyVarData(
80 Acquires lock only at boot time. Simply returns at runtime.
82 This is a temperary function that will be removed when
83 EfiAcquireLock() in UefiLib can handle the call in UEFI
84 Runtimer driver in RT phase.
85 It calls EfiAcquireLock() at boot time, and simply returns
88 @param Lock A pointer to the lock to acquire.
92 AcquireLockOnlyAtBootTime (
96 if (!EfiAtRuntime ()) {
97 EfiAcquireLock (Lock
);
102 Releases lock only at boot time. Simply returns at runtime.
104 This is a temperary function which will be removed when
105 EfiReleaseLock() in UefiLib can handle the call in UEFI
106 Runtimer driver in RT phase.
107 It calls EfiReleaseLock() at boot time and simply returns
110 @param Lock A pointer to the lock to release.
114 ReleaseLockOnlyAtBootTime (
118 if (!EfiAtRuntime ()) {
119 EfiReleaseLock (Lock
);
124 Return TRUE if ExitBootServices () has been called.
126 @retval TRUE If ExitBootServices () has been called. FALSE if ExitBootServices () has not been called.
133 return EfiAtRuntime ();
137 Initialize the variable cache buffer as an empty variable store.
139 @param[out] VariableCacheBuffer A pointer to pointer of a cache variable store.
140 @param[in,out] TotalVariableCacheSize On input, the minimum size needed for the UEFI variable store cache
141 buffer that is allocated. On output, the actual size of the buffer allocated.
142 If TotalVariableCacheSize is zero, a buffer will not be allocated and the
143 function will return with EFI_SUCCESS.
145 @retval EFI_SUCCESS The variable cache was allocated and initialized successfully.
146 @retval EFI_INVALID_PARAMETER A given pointer is NULL or an invalid variable store size was specified.
147 @retval EFI_OUT_OF_RESOURCES Insufficient resources are available to allocate the variable store cache buffer.
152 OUT VARIABLE_STORE_HEADER
**VariableCacheBuffer
,
153 IN OUT UINTN
*TotalVariableCacheSize
156 VARIABLE_STORE_HEADER
*VariableCacheStorePtr
;
158 if (TotalVariableCacheSize
== NULL
) {
159 return EFI_INVALID_PARAMETER
;
161 if (*TotalVariableCacheSize
== 0) {
164 if (VariableCacheBuffer
== NULL
|| *TotalVariableCacheSize
< sizeof (VARIABLE_STORE_HEADER
)) {
165 return EFI_INVALID_PARAMETER
;
167 *TotalVariableCacheSize
= ALIGN_VALUE (*TotalVariableCacheSize
, sizeof (UINT32
));
170 // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)
172 *VariableCacheBuffer
= (VARIABLE_STORE_HEADER
*) AllocateRuntimePages (
173 EFI_SIZE_TO_PAGES (*TotalVariableCacheSize
)
175 if (*VariableCacheBuffer
== NULL
) {
176 return EFI_OUT_OF_RESOURCES
;
178 VariableCacheStorePtr
= *VariableCacheBuffer
;
179 SetMem32 ((VOID
*) VariableCacheStorePtr
, *TotalVariableCacheSize
, (UINT32
) 0xFFFFFFFF);
181 ZeroMem ((VOID
*) VariableCacheStorePtr
, sizeof (VARIABLE_STORE_HEADER
));
182 VariableCacheStorePtr
->Size
= (UINT32
) *TotalVariableCacheSize
;
183 VariableCacheStorePtr
->Format
= VARIABLE_STORE_FORMATTED
;
184 VariableCacheStorePtr
->State
= VARIABLE_STORE_HEALTHY
;
190 Initialize the communicate buffer using DataSize and Function.
192 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
195 Caution: This function may receive untrusted input.
196 The data size external input, so this function will validate it carefully to avoid buffer overflow.
198 @param[out] DataPtr Points to the data in the communicate buffer.
199 @param[in] DataSize The data size to send to SMM.
200 @param[in] Function The function number to initialize the communicate header.
202 @retval EFI_INVALID_PARAMETER The data size is too big.
203 @retval EFI_SUCCESS Find the specified variable.
207 InitCommunicateBuffer (
208 OUT VOID
**DataPtr OPTIONAL
,
213 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
214 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
217 if (DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
> mVariableBufferSize
) {
218 return EFI_INVALID_PARAMETER
;
221 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
222 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
223 SmmCommunicateHeader
->MessageLength
= DataSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
225 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
226 SmmVariableFunctionHeader
->Function
= Function
;
227 if (DataPtr
!= NULL
) {
228 *DataPtr
= SmmVariableFunctionHeader
->Data
;
236 Send the data in communicate buffer to SMM.
238 @param[in] DataSize This size of the function header and the data.
240 @retval EFI_SUCCESS Success is returned from the functin in SMM.
241 @retval Others Failure is returned from the function in SMM.
245 SendCommunicateBuffer (
251 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
252 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
254 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
255 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mVariableBufferPhysical
, &CommSize
);
256 ASSERT_EFI_ERROR (Status
);
258 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
259 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)SmmCommunicateHeader
->Data
;
260 return SmmVariableFunctionHeader
->ReturnStatus
;
264 Mark a variable that will become read-only after leaving the DXE phase of execution.
266 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
267 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
268 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
270 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
271 as pending to be read-only.
272 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
273 Or VariableName is an empty string.
274 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
275 already been signaled.
276 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
280 VariableLockRequestToLock (
281 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
282 IN CHAR16
*VariableName
,
283 IN EFI_GUID
*VendorGuid
287 UINTN VariableNameSize
;
289 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
291 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
292 return EFI_INVALID_PARAMETER
;
295 VariableNameSize
= StrSize (VariableName
);
296 VariableToLock
= NULL
;
299 // If VariableName exceeds SMM payload limit. Return failure
301 if (VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
, Name
)) {
302 return EFI_INVALID_PARAMETER
;
305 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
308 // Init the communicate buffer. The buffer data size is:
309 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
311 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
, Name
) + VariableNameSize
;
312 Status
= InitCommunicateBuffer ((VOID
**) &VariableToLock
, PayloadSize
, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
);
313 if (EFI_ERROR (Status
)) {
316 ASSERT (VariableToLock
!= NULL
);
318 CopyGuid (&VariableToLock
->Guid
, VendorGuid
);
319 VariableToLock
->NameSize
= VariableNameSize
;
320 CopyMem (VariableToLock
->Name
, VariableName
, VariableToLock
->NameSize
);
325 Status
= SendCommunicateBuffer (PayloadSize
);
328 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
333 Register SetVariable check handler.
335 @param[in] Handler Pointer to check handler.
337 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.
338 @retval EFI_INVALID_PARAMETER Handler is NULL.
339 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
340 already been signaled.
341 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.
342 @retval EFI_UNSUPPORTED This interface is not implemented.
343 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.
348 VarCheckRegisterSetVariableCheckHandler (
349 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler
352 return EFI_UNSUPPORTED
;
356 Variable property set.
358 @param[in] Name Pointer to the variable name.
359 @param[in] Guid Pointer to the vendor GUID.
360 @param[in] VariableProperty Pointer to the input variable property.
362 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
363 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,
364 or the fields of VariableProperty are not valid.
365 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
366 already been signaled.
367 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
372 VarCheckVariablePropertySet (
375 IN VAR_CHECK_VARIABLE_PROPERTY
*VariableProperty
379 UINTN VariableNameSize
;
381 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*CommVariableProperty
;
383 if (Name
== NULL
|| Name
[0] == 0 || Guid
== NULL
) {
384 return EFI_INVALID_PARAMETER
;
387 if (VariableProperty
== NULL
) {
388 return EFI_INVALID_PARAMETER
;
391 if (VariableProperty
->Revision
!= VAR_CHECK_VARIABLE_PROPERTY_REVISION
) {
392 return EFI_INVALID_PARAMETER
;
395 VariableNameSize
= StrSize (Name
);
396 CommVariableProperty
= NULL
;
399 // If VariableName exceeds SMM payload limit. Return failure
401 if (VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
)) {
402 return EFI_INVALID_PARAMETER
;
405 AcquireLockOnlyAtBootTime (&mVariableServicesLock
);
408 // Init the communicate buffer. The buffer data size is:
409 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
411 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
) + VariableNameSize
;
412 Status
= InitCommunicateBuffer ((VOID
**) &CommVariableProperty
, PayloadSize
, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET
);
413 if (EFI_ERROR (Status
)) {
416 ASSERT (CommVariableProperty
!= NULL
);
418 CopyGuid (&CommVariableProperty
->Guid
, Guid
);
419 CopyMem (&CommVariableProperty
->VariableProperty
, VariableProperty
, sizeof (*VariableProperty
));
420 CommVariableProperty
->NameSize
= VariableNameSize
;
421 CopyMem (CommVariableProperty
->Name
, Name
, CommVariableProperty
->NameSize
);
426 Status
= SendCommunicateBuffer (PayloadSize
);
429 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
434 Variable property get.
436 @param[in] Name Pointer to the variable name.
437 @param[in] Guid Pointer to the vendor GUID.
438 @param[out] VariableProperty Pointer to the output variable property.
440 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
441 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.
442 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
447 VarCheckVariablePropertyGet (
450 OUT VAR_CHECK_VARIABLE_PROPERTY
*VariableProperty
454 UINTN VariableNameSize
;
456 SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
*CommVariableProperty
;
458 if (Name
== NULL
|| Name
[0] == 0 || Guid
== NULL
) {
459 return EFI_INVALID_PARAMETER
;
462 if (VariableProperty
== NULL
) {
463 return EFI_INVALID_PARAMETER
;
466 VariableNameSize
= StrSize (Name
);
467 CommVariableProperty
= NULL
;
470 // If VariableName exceeds SMM payload limit. Return failure
472 if (VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
)) {
473 return EFI_INVALID_PARAMETER
;
476 AcquireLockOnlyAtBootTime (&mVariableServicesLock
);
479 // Init the communicate buffer. The buffer data size is:
480 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
482 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY
, Name
) + VariableNameSize
;
483 Status
= InitCommunicateBuffer ((VOID
**) &CommVariableProperty
, PayloadSize
, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET
);
484 if (EFI_ERROR (Status
)) {
487 ASSERT (CommVariableProperty
!= NULL
);
489 CopyGuid (&CommVariableProperty
->Guid
, Guid
);
490 CommVariableProperty
->NameSize
= VariableNameSize
;
491 CopyMem (CommVariableProperty
->Name
, Name
, CommVariableProperty
->NameSize
);
496 Status
= SendCommunicateBuffer (PayloadSize
);
497 if (Status
== EFI_SUCCESS
) {
498 CopyMem (VariableProperty
, &CommVariableProperty
->VariableProperty
, sizeof (*VariableProperty
));
502 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
507 Signals SMM to synchronize any pending variable updates with the runtime cache(s).
516 // Init the communicate buffer. The buffer data size is:
517 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
519 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE
);
524 SendCommunicateBuffer (0);
528 Check whether a SMI must be triggered to retrieve pending cache updates.
530 If the variable HOB was finished being flushed since the last check for a runtime cache update, this function
531 will prevent the HOB cache from being used for future runtime cache hits.
535 CheckForRuntimeCacheSync (
539 if (mVariableRuntimeCachePendingUpdate
) {
542 ASSERT (!mVariableRuntimeCachePendingUpdate
);
545 // The HOB variable data may have finished being flushed in the runtime cache sync update
547 if (mHobFlushComplete
&& mVariableRuntimeHobCacheBuffer
!= NULL
) {
548 if (!EfiAtRuntime ()) {
549 FreePages (mVariableRuntimeHobCacheBuffer
, EFI_SIZE_TO_PAGES (mVariableRuntimeHobCacheBufferSize
));
551 mVariableRuntimeHobCacheBuffer
= NULL
;
556 Finds the given variable in a runtime cache variable store.
558 Caution: This function may receive untrusted input.
559 The data size is external input, so this function will validate it carefully to avoid buffer overflow.
561 @param[in] VariableName Name of Variable to be found.
562 @param[in] VendorGuid Variable vendor GUID.
563 @param[out] Attributes Attribute value of the variable found.
564 @param[in, out] DataSize Size of Data found. If size is less than the
565 data, this value contains the required size.
566 @param[out] Data Data pointer.
568 @retval EFI_SUCCESS Found the specified variable.
569 @retval EFI_INVALID_PARAMETER Invalid parameter.
570 @retval EFI_NOT_FOUND The specified variable could not be found.
574 FindVariableInRuntimeCache (
575 IN CHAR16
*VariableName
,
576 IN EFI_GUID
*VendorGuid
,
577 OUT UINT32
*Attributes OPTIONAL
,
578 IN OUT UINTN
*DataSize
,
579 OUT VOID
*Data OPTIONAL
584 VARIABLE_POINTER_TRACK RtPtrTrack
;
585 VARIABLE_STORE_TYPE StoreType
;
586 VARIABLE_STORE_HEADER
*VariableStoreList
[VariableStoreTypeMax
];
588 Status
= EFI_NOT_FOUND
;
590 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
591 return EFI_INVALID_PARAMETER
;
594 ZeroMem (&RtPtrTrack
, sizeof (RtPtrTrack
));
597 // The UEFI specification restricts Runtime Services callers from invoking the same or certain other Runtime Service
598 // functions prior to completion and return from a previous Runtime Service call. These restrictions prevent
599 // a GetVariable () or GetNextVariable () call from being issued until a prior call has returned. The runtime
600 // cache read lock should always be free when entering this function.
602 ASSERT (!mVariableRuntimeCacheReadLock
);
604 mVariableRuntimeCacheReadLock
= TRUE
;
605 CheckForRuntimeCacheSync ();
607 if (!mVariableRuntimeCachePendingUpdate
) {
609 // 0: Volatile, 1: HOB, 2: Non-Volatile.
610 // The index and attributes mapping must be kept in this order as FindVariable
611 // makes use of this mapping to implement search algorithm.
613 VariableStoreList
[VariableStoreTypeVolatile
] = mVariableRuntimeVolatileCacheBuffer
;
614 VariableStoreList
[VariableStoreTypeHob
] = mVariableRuntimeHobCacheBuffer
;
615 VariableStoreList
[VariableStoreTypeNv
] = mVariableRuntimeNvCacheBuffer
;
617 for (StoreType
= (VARIABLE_STORE_TYPE
) 0; StoreType
< VariableStoreTypeMax
; StoreType
++) {
618 if (VariableStoreList
[StoreType
] == NULL
) {
622 RtPtrTrack
.StartPtr
= GetStartPointer (VariableStoreList
[StoreType
]);
623 RtPtrTrack
.EndPtr
= GetEndPointer (VariableStoreList
[StoreType
]);
624 RtPtrTrack
.Volatile
= (BOOLEAN
) (StoreType
== VariableStoreTypeVolatile
);
626 Status
= FindVariableEx (VariableName
, VendorGuid
, FALSE
, &RtPtrTrack
, mVariableAuthFormat
);
627 if (!EFI_ERROR (Status
)) {
632 if (!EFI_ERROR (Status
)) {
636 TempDataSize
= DataSizeOfVariable (RtPtrTrack
.CurrPtr
, mVariableAuthFormat
);
637 ASSERT (TempDataSize
!= 0);
639 if (*DataSize
>= TempDataSize
) {
641 Status
= EFI_INVALID_PARAMETER
;
645 CopyMem (Data
, GetVariableDataPtr (RtPtrTrack
.CurrPtr
, mVariableAuthFormat
), TempDataSize
);
646 *DataSize
= TempDataSize
;
648 UpdateVariableInfo (VariableName
, VendorGuid
, RtPtrTrack
.Volatile
, TRUE
, FALSE
, FALSE
, TRUE
, &mVariableInfo
);
650 Status
= EFI_SUCCESS
;
653 *DataSize
= TempDataSize
;
654 Status
= EFI_BUFFER_TOO_SMALL
;
661 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
662 if (Attributes
!= NULL
&& RtPtrTrack
.CurrPtr
!= NULL
) {
663 *Attributes
= RtPtrTrack
.CurrPtr
->Attributes
;
666 mVariableRuntimeCacheReadLock
= FALSE
;
672 Finds the given variable in a variable store in SMM.
674 Caution: This function may receive untrusted input.
675 The data size is external input, so this function will validate it carefully to avoid buffer overflow.
677 @param[in] VariableName Name of Variable to be found.
678 @param[in] VendorGuid Variable vendor GUID.
679 @param[out] Attributes Attribute value of the variable found.
680 @param[in, out] DataSize Size of Data found. If size is less than the
681 data, this value contains the required size.
682 @param[out] Data Data pointer.
684 @retval EFI_SUCCESS Found the specified variable.
685 @retval EFI_INVALID_PARAMETER Invalid parameter.
686 @retval EFI_NOT_FOUND The specified variable could not be found.
691 IN CHAR16
*VariableName
,
692 IN EFI_GUID
*VendorGuid
,
693 OUT UINT32
*Attributes OPTIONAL
,
694 IN OUT UINTN
*DataSize
,
695 OUT VOID
*Data OPTIONAL
700 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
702 UINTN VariableNameSize
;
704 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
705 return EFI_INVALID_PARAMETER
;
708 TempDataSize
= *DataSize
;
709 VariableNameSize
= StrSize (VariableName
);
710 SmmVariableHeader
= NULL
;
713 // If VariableName exceeds SMM payload limit. Return failure
715 if (VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
716 return EFI_INVALID_PARAMETER
;
720 // Init the communicate buffer. The buffer data size is:
721 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
723 if (TempDataSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
) {
725 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
727 TempDataSize
= mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
;
729 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + VariableNameSize
+ TempDataSize
;
731 Status
= InitCommunicateBuffer ((VOID
**) &SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_VARIABLE
);
732 if (EFI_ERROR (Status
)) {
735 ASSERT (SmmVariableHeader
!= NULL
);
737 CopyGuid (&SmmVariableHeader
->Guid
, VendorGuid
);
738 SmmVariableHeader
->DataSize
= TempDataSize
;
739 SmmVariableHeader
->NameSize
= VariableNameSize
;
740 if (Attributes
== NULL
) {
741 SmmVariableHeader
->Attributes
= 0;
743 SmmVariableHeader
->Attributes
= *Attributes
;
745 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
750 Status
= SendCommunicateBuffer (PayloadSize
);
753 // Get data from SMM.
755 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
757 // SMM CommBuffer DataSize can be a trimed value
758 // Only update DataSize when needed
760 *DataSize
= SmmVariableHeader
->DataSize
;
762 if (Attributes
!= NULL
) {
763 *Attributes
= SmmVariableHeader
->Attributes
;
766 if (EFI_ERROR (Status
)) {
771 CopyMem (Data
, (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, SmmVariableHeader
->DataSize
);
773 Status
= EFI_INVALID_PARAMETER
;
781 This code finds variable in storage blocks (Volatile or Non-Volatile).
783 Caution: This function may receive untrusted input.
784 The data size is external input, so this function will validate it carefully to avoid buffer overflow.
786 @param[in] VariableName Name of Variable to be found.
787 @param[in] VendorGuid Variable vendor GUID.
788 @param[out] Attributes Attribute value of the variable found.
789 @param[in, out] DataSize Size of Data found. If size is less than the
790 data, this value contains the required size.
791 @param[out] Data Data pointer.
793 @retval EFI_INVALID_PARAMETER Invalid parameter.
794 @retval EFI_SUCCESS Find the specified variable.
795 @retval EFI_NOT_FOUND Not found.
796 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
801 RuntimeServiceGetVariable (
802 IN CHAR16
*VariableName
,
803 IN EFI_GUID
*VendorGuid
,
804 OUT UINT32
*Attributes OPTIONAL
,
805 IN OUT UINTN
*DataSize
,
811 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
812 return EFI_INVALID_PARAMETER
;
814 if (VariableName
[0] == 0) {
815 return EFI_NOT_FOUND
;
818 AcquireLockOnlyAtBootTime (&mVariableServicesLock
);
819 if (FeaturePcdGet (PcdEnableVariableRuntimeCache
)) {
820 Status
= FindVariableInRuntimeCache (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
822 Status
= FindVariableInSmm (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
824 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
830 Finds the next available variable in a runtime cache variable store.
832 @param[in, out] VariableNameSize Size of the variable name.
833 @param[in, out] VariableName Pointer to variable name.
834 @param[in, out] VendorGuid Variable Vendor Guid.
836 @retval EFI_INVALID_PARAMETER Invalid parameter.
837 @retval EFI_SUCCESS Find the specified variable.
838 @retval EFI_NOT_FOUND Not found.
839 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
843 GetNextVariableNameInRuntimeCache (
844 IN OUT UINTN
*VariableNameSize
,
845 IN OUT CHAR16
*VariableName
,
846 IN OUT EFI_GUID
*VendorGuid
851 VARIABLE_HEADER
*VariablePtr
;
852 VARIABLE_STORE_HEADER
*VariableStoreHeader
[VariableStoreTypeMax
];
854 Status
= EFI_NOT_FOUND
;
857 // The UEFI specification restricts Runtime Services callers from invoking the same or certain other Runtime Service
858 // functions prior to completion and return from a previous Runtime Service call. These restrictions prevent
859 // a GetVariable () or GetNextVariable () call from being issued until a prior call has returned. The runtime
860 // cache read lock should always be free when entering this function.
862 ASSERT (!mVariableRuntimeCacheReadLock
);
864 CheckForRuntimeCacheSync ();
866 mVariableRuntimeCacheReadLock
= TRUE
;
867 if (!mVariableRuntimeCachePendingUpdate
) {
869 // 0: Volatile, 1: HOB, 2: Non-Volatile.
870 // The index and attributes mapping must be kept in this order as FindVariable
871 // makes use of this mapping to implement search algorithm.
873 VariableStoreHeader
[VariableStoreTypeVolatile
] = mVariableRuntimeVolatileCacheBuffer
;
874 VariableStoreHeader
[VariableStoreTypeHob
] = mVariableRuntimeHobCacheBuffer
;
875 VariableStoreHeader
[VariableStoreTypeNv
] = mVariableRuntimeNvCacheBuffer
;
877 Status
= VariableServiceGetNextVariableInternal (
884 if (!EFI_ERROR (Status
)) {
885 VarNameSize
= NameSizeOfVariable (VariablePtr
, mVariableAuthFormat
);
886 ASSERT (VarNameSize
!= 0);
887 if (VarNameSize
<= *VariableNameSize
) {
888 CopyMem (VariableName
, GetVariableNamePtr (VariablePtr
, mVariableAuthFormat
), VarNameSize
);
889 CopyMem (VendorGuid
, GetVendorGuidPtr (VariablePtr
, mVariableAuthFormat
), sizeof (EFI_GUID
));
890 Status
= EFI_SUCCESS
;
892 Status
= EFI_BUFFER_TOO_SMALL
;
895 *VariableNameSize
= VarNameSize
;
898 mVariableRuntimeCacheReadLock
= FALSE
;
904 Finds the next available variable in a SMM variable store.
906 @param[in, out] VariableNameSize Size of the variable name.
907 @param[in, out] VariableName Pointer to variable name.
908 @param[in, out] VendorGuid Variable Vendor Guid.
910 @retval EFI_INVALID_PARAMETER Invalid parameter.
911 @retval EFI_SUCCESS Find the specified variable.
912 @retval EFI_NOT_FOUND Not found.
913 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
917 GetNextVariableNameInSmm (
918 IN OUT UINTN
*VariableNameSize
,
919 IN OUT CHAR16
*VariableName
,
920 IN OUT EFI_GUID
*VendorGuid
925 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*SmmGetNextVariableName
;
926 UINTN OutVariableNameSize
;
927 UINTN InVariableNameSize
;
929 OutVariableNameSize
= *VariableNameSize
;
930 InVariableNameSize
= StrSize (VariableName
);
931 SmmGetNextVariableName
= NULL
;
934 // If input string exceeds SMM payload limit. Return failure
936 if (InVariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
937 return EFI_INVALID_PARAMETER
;
941 // Init the communicate buffer. The buffer data size is:
942 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
944 if (OutVariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
946 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
948 OutVariableNameSize
= mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
951 // Payload should be Guid + NameSize + MAX of Input & Output buffer
953 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + MAX (OutVariableNameSize
, InVariableNameSize
);
955 Status
= InitCommunicateBuffer ((VOID
**)&SmmGetNextVariableName
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
);
956 if (EFI_ERROR (Status
)) {
959 ASSERT (SmmGetNextVariableName
!= NULL
);
962 // SMM comm buffer->NameSize is buffer size for return string
964 SmmGetNextVariableName
->NameSize
= OutVariableNameSize
;
966 CopyGuid (&SmmGetNextVariableName
->Guid
, VendorGuid
);
970 CopyMem (SmmGetNextVariableName
->Name
, VariableName
, InVariableNameSize
);
971 if (OutVariableNameSize
> InVariableNameSize
) {
972 ZeroMem ((UINT8
*) SmmGetNextVariableName
->Name
+ InVariableNameSize
, OutVariableNameSize
- InVariableNameSize
);
978 Status
= SendCommunicateBuffer (PayloadSize
);
981 // Get data from SMM.
983 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
985 // SMM CommBuffer NameSize can be a trimed value
986 // Only update VariableNameSize when needed
988 *VariableNameSize
= SmmGetNextVariableName
->NameSize
;
990 if (EFI_ERROR (Status
)) {
994 CopyGuid (VendorGuid
, &SmmGetNextVariableName
->Guid
);
995 CopyMem (VariableName
, SmmGetNextVariableName
->Name
, SmmGetNextVariableName
->NameSize
);
1002 This code Finds the Next available variable.
1004 @param[in, out] VariableNameSize Size of the variable name.
1005 @param[in, out] VariableName Pointer to variable name.
1006 @param[in, out] VendorGuid Variable Vendor Guid.
1008 @retval EFI_INVALID_PARAMETER Invalid parameter.
1009 @retval EFI_SUCCESS Find the specified variable.
1010 @retval EFI_NOT_FOUND Not found.
1011 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
1016 RuntimeServiceGetNextVariableName (
1017 IN OUT UINTN
*VariableNameSize
,
1018 IN OUT CHAR16
*VariableName
,
1019 IN OUT EFI_GUID
*VendorGuid
1025 Status
= EFI_NOT_FOUND
;
1027 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
1028 return EFI_INVALID_PARAMETER
;
1032 // Calculate the possible maximum length of name string, including the Null terminator.
1034 MaxLen
= *VariableNameSize
/ sizeof (CHAR16
);
1035 if ((MaxLen
== 0) || (StrnLenS (VariableName
, MaxLen
) == MaxLen
)) {
1037 // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,
1038 // follow spec to return EFI_INVALID_PARAMETER.
1040 return EFI_INVALID_PARAMETER
;
1043 AcquireLockOnlyAtBootTime (&mVariableServicesLock
);
1044 if (FeaturePcdGet (PcdEnableVariableRuntimeCache
)) {
1045 Status
= GetNextVariableNameInRuntimeCache (VariableNameSize
, VariableName
, VendorGuid
);
1047 Status
= GetNextVariableNameInSmm (VariableNameSize
, VariableName
, VendorGuid
);
1049 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
1055 This code sets variable in storage blocks (Volatile or Non-Volatile).
1057 Caution: This function may receive untrusted input.
1058 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.
1060 @param[in] VariableName Name of Variable to be found.
1061 @param[in] VendorGuid Variable vendor GUID.
1062 @param[in] Attributes Attribute value of the variable found
1063 @param[in] DataSize Size of Data found. If size is less than the
1064 data, this value contains the required size.
1065 @param[in] Data Data pointer.
1067 @retval EFI_INVALID_PARAMETER Invalid parameter.
1068 @retval EFI_SUCCESS Set successfully.
1069 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
1070 @retval EFI_NOT_FOUND Not found.
1071 @retval EFI_WRITE_PROTECTED Variable is read-only.
1076 RuntimeServiceSetVariable (
1077 IN CHAR16
*VariableName
,
1078 IN EFI_GUID
*VendorGuid
,
1079 IN UINT32 Attributes
,
1086 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
1087 UINTN VariableNameSize
;
1090 // Check input parameters.
1092 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
1093 return EFI_INVALID_PARAMETER
;
1096 if (DataSize
!= 0 && Data
== NULL
) {
1097 return EFI_INVALID_PARAMETER
;
1100 VariableNameSize
= StrSize (VariableName
);
1101 SmmVariableHeader
= NULL
;
1104 // If VariableName or DataSize exceeds SMM payload limit. Return failure
1106 if ((VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
1107 (DataSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
)){
1108 return EFI_INVALID_PARAMETER
;
1111 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
1114 // Init the communicate buffer. The buffer data size is:
1115 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
1117 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + VariableNameSize
+ DataSize
;
1118 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_SET_VARIABLE
);
1119 if (EFI_ERROR (Status
)) {
1122 ASSERT (SmmVariableHeader
!= NULL
);
1124 CopyGuid ((EFI_GUID
*) &SmmVariableHeader
->Guid
, VendorGuid
);
1125 SmmVariableHeader
->DataSize
= DataSize
;
1126 SmmVariableHeader
->NameSize
= VariableNameSize
;
1127 SmmVariableHeader
->Attributes
= Attributes
;
1128 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
1129 CopyMem ((UINT8
*) SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, Data
, DataSize
);
1132 // Send data to SMM.
1134 Status
= SendCommunicateBuffer (PayloadSize
);
1137 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
1139 if (!EfiAtRuntime ()) {
1140 if (!EFI_ERROR (Status
)) {
1152 This code returns information about the EFI variables.
1154 @param[in] Attributes Attributes bitmask to specify the type of variables
1155 on which to return information.
1156 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
1157 for the EFI variables associated with the attributes specified.
1158 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
1159 for EFI variables associated with the attributes specified.
1160 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
1161 associated with the attributes specified.
1163 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
1164 @retval EFI_SUCCESS Query successfully.
1165 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
1170 RuntimeServiceQueryVariableInfo (
1171 IN UINT32 Attributes
,
1172 OUT UINT64
*MaximumVariableStorageSize
,
1173 OUT UINT64
*RemainingVariableStorageSize
,
1174 OUT UINT64
*MaximumVariableSize
1179 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*SmmQueryVariableInfo
;
1181 SmmQueryVariableInfo
= NULL
;
1183 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
1184 return EFI_INVALID_PARAMETER
;
1187 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
1190 // Init the communicate buffer. The buffer data size is:
1191 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
1193 PayloadSize
= sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
);
1194 Status
= InitCommunicateBuffer ((VOID
**)&SmmQueryVariableInfo
, PayloadSize
, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
);
1195 if (EFI_ERROR (Status
)) {
1198 ASSERT (SmmQueryVariableInfo
!= NULL
);
1200 SmmQueryVariableInfo
->Attributes
= Attributes
;
1203 // Send data to SMM.
1205 Status
= SendCommunicateBuffer (PayloadSize
);
1206 if (EFI_ERROR (Status
)) {
1211 // Get data from SMM.
1213 *MaximumVariableSize
= SmmQueryVariableInfo
->MaximumVariableSize
;
1214 *MaximumVariableStorageSize
= SmmQueryVariableInfo
->MaximumVariableStorageSize
;
1215 *RemainingVariableStorageSize
= SmmQueryVariableInfo
->RemainingVariableStorageSize
;
1218 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
1224 Exit Boot Services Event notification handler.
1226 Notify SMM variable driver about the event.
1228 @param[in] Event Event whose notification function is being invoked.
1229 @param[in] Context Pointer to the notification function's context.
1234 OnExitBootServices (
1240 // Init the communicate buffer. The buffer data size is:
1241 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
1243 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
);
1246 // Send data to SMM.
1248 SendCommunicateBuffer (0);
1253 On Ready To Boot Services Event notification handler.
1255 Notify SMM variable driver about the event.
1257 @param[in] Event Event whose notification function is being invoked
1258 @param[in] Context Pointer to the notification function's context
1269 // Init the communicate buffer. The buffer data size is:
1270 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
1272 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT
);
1275 // Send data to SMM.
1277 SendCommunicateBuffer (0);
1280 // Install the system configuration table for variable info data captured
1282 if (FeaturePcdGet (PcdEnableVariableRuntimeCache
) && FeaturePcdGet (PcdVariableCollectStatistics
)) {
1283 if (mVariableAuthFormat
) {
1284 gBS
->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid
, mVariableInfo
);
1286 gBS
->InstallConfigurationTable (&gEfiVariableGuid
, mVariableInfo
);
1290 gBS
->CloseEvent (Event
);
1295 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
1297 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
1298 It convers pointer to new virtual address.
1300 @param[in] Event Event whose notification function is being invoked.
1301 @param[in] Context Pointer to the notification function's context.
1306 VariableAddressChangeEvent (
1311 EfiConvertPointer (0x0, (VOID
**) &mVariableBuffer
);
1312 EfiConvertPointer (0x0, (VOID
**) &mSmmCommunication
);
1313 EfiConvertPointer (EFI_OPTIONAL_PTR
, (VOID
**) &mVariableRuntimeHobCacheBuffer
);
1314 EfiConvertPointer (EFI_OPTIONAL_PTR
, (VOID
**) &mVariableRuntimeNvCacheBuffer
);
1315 EfiConvertPointer (EFI_OPTIONAL_PTR
, (VOID
**) &mVariableRuntimeVolatileCacheBuffer
);
1319 This code gets variable payload size.
1321 @param[out] VariablePayloadSize Output pointer to variable payload size.
1323 @retval EFI_SUCCESS Get successfully.
1324 @retval Others Get unsuccessfully.
1329 GetVariablePayloadSize (
1330 OUT UINTN
*VariablePayloadSize
1334 SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
*SmmGetPayloadSize
;
1335 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
1336 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
1340 SmmGetPayloadSize
= NULL
;
1343 if(VariablePayloadSize
== NULL
) {
1344 return EFI_INVALID_PARAMETER
;
1347 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
1350 // Init the communicate buffer. The buffer data size is:
1351 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE);
1353 CommSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
);
1354 CommBuffer
= AllocateZeroPool (CommSize
);
1355 if (CommBuffer
== NULL
) {
1356 Status
= EFI_OUT_OF_RESOURCES
;
1360 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) CommBuffer
;
1361 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
1362 SmmCommunicateHeader
->MessageLength
= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ sizeof (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
);
1364 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
1365 SmmVariableFunctionHeader
->Function
= SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE
;
1366 SmmGetPayloadSize
= (SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE
*) SmmVariableFunctionHeader
->Data
;
1369 // Send data to SMM.
1371 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, CommBuffer
, &CommSize
);
1372 ASSERT_EFI_ERROR (Status
);
1374 Status
= SmmVariableFunctionHeader
->ReturnStatus
;
1375 if (EFI_ERROR (Status
)) {
1380 // Get data from SMM.
1382 *VariablePayloadSize
= SmmGetPayloadSize
->VariablePayloadSize
;
1385 if (CommBuffer
!= NULL
) {
1386 FreePool (CommBuffer
);
1388 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
1393 This code gets information needed from SMM for runtime cache initialization.
1395 @param[out] TotalHobStorageSize Output pointer for the total HOB storage size in bytes.
1396 @param[out] TotalNvStorageSize Output pointer for the total non-volatile storage size in bytes.
1397 @param[out] TotalVolatileStorageSize Output pointer for the total volatile storage size in bytes.
1398 @param[out] AuthenticatedVariableUsage Output pointer that indicates if authenticated variables are to be used.
1400 @retval EFI_SUCCESS Retrieved the size successfully.
1401 @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is NULL.
1402 @retval EFI_OUT_OF_RESOURCES The memory resources needed for a CommBuffer are not available.
1403 @retval Others Could not retrieve the size successfully.
1407 GetRuntimeCacheInfo (
1408 OUT UINTN
*TotalHobStorageSize
,
1409 OUT UINTN
*TotalNvStorageSize
,
1410 OUT UINTN
*TotalVolatileStorageSize
,
1411 OUT BOOLEAN
*AuthenticatedVariableUsage
1415 SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
*SmmGetRuntimeCacheInfo
;
1416 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
1417 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
1421 SmmGetRuntimeCacheInfo
= NULL
;
1422 CommBuffer
= mVariableBuffer
;
1424 if (TotalHobStorageSize
== NULL
|| TotalNvStorageSize
== NULL
|| TotalVolatileStorageSize
== NULL
|| AuthenticatedVariableUsage
== NULL
) {
1425 return EFI_INVALID_PARAMETER
;
1428 if (CommBuffer
== NULL
) {
1429 return EFI_OUT_OF_RESOURCES
;
1432 AcquireLockOnlyAtBootTime (&mVariableServicesLock
);
1434 CommSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
);
1435 ZeroMem (CommBuffer
, CommSize
);
1437 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) CommBuffer
;
1438 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
1439 SmmCommunicateHeader
->MessageLength
= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
);
1441 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
1442 SmmVariableFunctionHeader
->Function
= SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO
;
1443 SmmGetRuntimeCacheInfo
= (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO
*) SmmVariableFunctionHeader
->Data
;
1446 // Send data to SMM.
1448 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, CommBuffer
, &CommSize
);
1449 ASSERT_EFI_ERROR (Status
);
1450 if (CommSize
<= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
1451 Status
= EFI_BAD_BUFFER_SIZE
;
1455 Status
= SmmVariableFunctionHeader
->ReturnStatus
;
1456 if (EFI_ERROR (Status
)) {
1461 // Get data from SMM.
1463 *TotalHobStorageSize
= SmmGetRuntimeCacheInfo
->TotalHobStorageSize
;
1464 *TotalNvStorageSize
= SmmGetRuntimeCacheInfo
->TotalNvStorageSize
;
1465 *TotalVolatileStorageSize
= SmmGetRuntimeCacheInfo
->TotalVolatileStorageSize
;
1466 *AuthenticatedVariableUsage
= SmmGetRuntimeCacheInfo
->AuthenticatedVariableUsage
;
1469 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
1474 Sends the runtime variable cache context information to SMM.
1476 @retval EFI_SUCCESS Retrieved the size successfully.
1477 @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is NULL.
1478 @retval EFI_OUT_OF_RESOURCES The memory resources needed for a CommBuffer are not available.
1479 @retval Others Could not retrieve the size successfully.;
1483 SendRuntimeVariableCacheContextToSmm (
1488 SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
*SmmRuntimeVarCacheContext
;
1489 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
1490 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
1494 SmmRuntimeVarCacheContext
= NULL
;
1495 CommBuffer
= mVariableBuffer
;
1497 if (CommBuffer
== NULL
) {
1498 return EFI_OUT_OF_RESOURCES
;
1501 AcquireLockOnlyAtBootTime (&mVariableServicesLock
);
1504 // Init the communicate buffer. The buffer data size is:
1505 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT);
1507 CommSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
);
1508 ZeroMem (CommBuffer
, CommSize
);
1510 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) CommBuffer
;
1511 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
1512 SmmCommunicateHeader
->MessageLength
= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
);
1514 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
1515 SmmVariableFunctionHeader
->Function
= SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT
;
1516 SmmRuntimeVarCacheContext
= (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT
*) SmmVariableFunctionHeader
->Data
;
1518 SmmRuntimeVarCacheContext
->RuntimeHobCache
= mVariableRuntimeHobCacheBuffer
;
1519 SmmRuntimeVarCacheContext
->RuntimeVolatileCache
= mVariableRuntimeVolatileCacheBuffer
;
1520 SmmRuntimeVarCacheContext
->RuntimeNvCache
= mVariableRuntimeNvCacheBuffer
;
1521 SmmRuntimeVarCacheContext
->PendingUpdate
= &mVariableRuntimeCachePendingUpdate
;
1522 SmmRuntimeVarCacheContext
->ReadLock
= &mVariableRuntimeCacheReadLock
;
1523 SmmRuntimeVarCacheContext
->HobFlushComplete
= &mHobFlushComplete
;
1526 // Send data to SMM.
1528 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, CommBuffer
, &CommSize
);
1529 ASSERT_EFI_ERROR (Status
);
1530 if (CommSize
<= SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
) {
1531 Status
= EFI_BAD_BUFFER_SIZE
;
1535 Status
= SmmVariableFunctionHeader
->ReturnStatus
;
1536 if (EFI_ERROR (Status
)) {
1541 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
1546 Initialize variable service and install Variable Architectural protocol.
1548 @param[in] Event Event whose notification function is being invoked.
1549 @param[in] Context Pointer to the notification function's context.
1561 Status
= gBS
->LocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**) &mSmmVariable
);
1562 if (EFI_ERROR (Status
)) {
1566 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &mSmmCommunication
);
1567 ASSERT_EFI_ERROR (Status
);
1570 // Allocate memory for variable communicate buffer.
1572 Status
= GetVariablePayloadSize (&mVariableBufferPayloadSize
);
1573 ASSERT_EFI_ERROR (Status
);
1574 mVariableBufferSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ mVariableBufferPayloadSize
;
1575 mVariableBuffer
= AllocateRuntimePool (mVariableBufferSize
);
1576 ASSERT (mVariableBuffer
!= NULL
);
1579 // Save the buffer physical address used for SMM conmunication.
1581 mVariableBufferPhysical
= mVariableBuffer
;
1583 if (FeaturePcdGet (PcdEnableVariableRuntimeCache
)) {
1584 DEBUG ((DEBUG_INFO
, "Variable driver runtime cache is enabled.\n"));
1586 // Allocate runtime variable cache memory buffers.
1588 Status
= GetRuntimeCacheInfo (
1589 &mVariableRuntimeHobCacheBufferSize
,
1590 &mVariableRuntimeNvCacheBufferSize
,
1591 &mVariableRuntimeVolatileCacheBufferSize
,
1592 &mVariableAuthFormat
1594 if (!EFI_ERROR (Status
)) {
1595 Status
= InitVariableCache (&mVariableRuntimeHobCacheBuffer
, &mVariableRuntimeHobCacheBufferSize
);
1596 if (!EFI_ERROR (Status
)) {
1597 Status
= InitVariableCache (&mVariableRuntimeNvCacheBuffer
, &mVariableRuntimeNvCacheBufferSize
);
1598 if (!EFI_ERROR (Status
)) {
1599 Status
= InitVariableCache (&mVariableRuntimeVolatileCacheBuffer
, &mVariableRuntimeVolatileCacheBufferSize
);
1600 if (!EFI_ERROR (Status
)) {
1601 Status
= SendRuntimeVariableCacheContextToSmm ();
1602 if (!EFI_ERROR (Status
)) {
1603 SyncRuntimeCache ();
1608 if (EFI_ERROR (Status
)) {
1609 mVariableRuntimeHobCacheBuffer
= NULL
;
1610 mVariableRuntimeNvCacheBuffer
= NULL
;
1611 mVariableRuntimeVolatileCacheBuffer
= NULL
;
1614 ASSERT_EFI_ERROR (Status
);
1616 DEBUG ((DEBUG_INFO
, "Variable driver runtime cache is disabled.\n"));
1619 gRT
->GetVariable
= RuntimeServiceGetVariable
;
1620 gRT
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
1621 gRT
->SetVariable
= RuntimeServiceSetVariable
;
1622 gRT
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
1625 // Install the Variable Architectural Protocol on a new handle.
1627 Status
= gBS
->InstallProtocolInterface (
1629 &gEfiVariableArchProtocolGuid
,
1630 EFI_NATIVE_INTERFACE
,
1633 ASSERT_EFI_ERROR (Status
);
1635 mVariableLock
.RequestToLock
= VariableLockRequestToLock
;
1636 Status
= gBS
->InstallMultipleProtocolInterfaces (
1638 &gEdkiiVariableLockProtocolGuid
,
1642 ASSERT_EFI_ERROR (Status
);
1644 mVarCheck
.RegisterSetVariableCheckHandler
= VarCheckRegisterSetVariableCheckHandler
;
1645 mVarCheck
.VariablePropertySet
= VarCheckVariablePropertySet
;
1646 mVarCheck
.VariablePropertyGet
= VarCheckVariablePropertyGet
;
1647 Status
= gBS
->InstallMultipleProtocolInterfaces (
1649 &gEdkiiVarCheckProtocolGuid
,
1653 ASSERT_EFI_ERROR (Status
);
1655 gBS
->CloseEvent (Event
);
1660 SMM Non-Volatile variable write service is ready notify event handler.
1662 @param[in] Event Event whose notification function is being invoked.
1663 @param[in] Context Pointer to the notification function's context.
1668 SmmVariableWriteReady (
1677 // Check whether the protocol is installed or not.
1679 Status
= gBS
->LocateProtocol (&gSmmVariableWriteGuid
, NULL
, (VOID
**) &ProtocolOps
);
1680 if (EFI_ERROR (Status
)) {
1685 // Some Secure Boot Policy Var (SecureBoot, etc) updates following other
1686 // Secure Boot Policy Variable change. Record their initial value.
1688 RecordSecureBootPolicyVarData();
1690 Status
= gBS
->InstallProtocolInterface (
1692 &gEfiVariableWriteArchProtocolGuid
,
1693 EFI_NATIVE_INTERFACE
,
1696 ASSERT_EFI_ERROR (Status
);
1698 gBS
->CloseEvent (Event
);
1703 Variable Driver main entry point. The Variable driver places the 4 EFI
1704 runtime services in the EFI System Table and installs arch protocols
1705 for variable read and write services being available. It also registers
1706 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
1708 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1709 @param[in] SystemTable A pointer to the EFI System Table.
1711 @retval EFI_SUCCESS Variable service successfully initialized.
1716 VariableSmmRuntimeInitialize (
1717 IN EFI_HANDLE ImageHandle
,
1718 IN EFI_SYSTEM_TABLE
*SystemTable
1721 VOID
*SmmVariableRegistration
;
1722 VOID
*SmmVariableWriteRegistration
;
1723 EFI_EVENT OnReadyToBootEvent
;
1724 EFI_EVENT ExitBootServiceEvent
;
1725 EFI_EVENT LegacyBootEvent
;
1727 EfiInitializeLock (&mVariableServicesLock
, TPL_NOTIFY
);
1730 // Smm variable service is ready
1732 EfiCreateProtocolNotifyEvent (
1733 &gEfiSmmVariableProtocolGuid
,
1737 &SmmVariableRegistration
1741 // Smm Non-Volatile variable write service is ready
1743 EfiCreateProtocolNotifyEvent (
1744 &gSmmVariableWriteGuid
,
1746 SmmVariableWriteReady
,
1748 &SmmVariableWriteRegistration
1752 // Register the event to reclaim variable for OS usage.
1754 EfiCreateEventReadyToBootEx (
1762 // Register the event to inform SMM variable that it is at runtime.
1764 gBS
->CreateEventEx (
1769 &gEfiEventExitBootServicesGuid
,
1770 &ExitBootServiceEvent
1774 // Register the event to inform SMM variable that it is at runtime for legacy boot.
1775 // Reuse OnExitBootServices() here.
1777 EfiCreateEventLegacyBootEx(
1785 // Register the event to convert the pointer for runtime.
1787 gBS
->CreateEventEx (
1790 VariableAddressChangeEvent
,
1792 &gEfiEventVirtualAddressChangeGuid
,
1793 &mVirtualAddressChangeEvent