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 - 2014, Intel Corporation. All rights reserved.<BR>
17 This program and the accompanying materials
18 are licensed and made available under the terms and conditions of the BSD License
19 which accompanies this distribution. The full text of the license may be found at
20 http://opensource.org/licenses/bsd-license.php
22 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
23 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 #include <Protocol/VariableWrite.h>
29 #include <Protocol/Variable.h>
30 #include <Protocol/SmmCommunication.h>
31 #include <Protocol/SmmVariable.h>
32 #include <Protocol/VariableLock.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/UefiRuntimeServicesTableLib.h>
36 #include <Library/MemoryAllocationLib.h>
37 #include <Library/UefiDriverEntryPoint.h>
38 #include <Library/UefiRuntimeLib.h>
39 #include <Library/BaseMemoryLib.h>
40 #include <Library/DebugLib.h>
41 #include <Library/PcdLib.h>
42 #include <Library/UefiLib.h>
43 #include <Library/BaseLib.h>
45 #include <Guid/EventGroup.h>
46 #include <Guid/AuthenticatedVariableFormat.h>
47 #include <Guid/SmmVariableCommon.h>
49 EFI_HANDLE mHandle
= NULL
;
50 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
= NULL
;
51 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
52 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
53 UINT8
*mVariableBuffer
= NULL
;
54 UINT8
*mVariableBufferPhysical
= NULL
;
55 UINTN mVariableBufferSize
;
56 UINTN mVariableBufferPayloadSize
;
57 EFI_LOCK mVariableServicesLock
;
58 EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock
;
61 SecureBoot Hook for SetVariable.
63 @param[in] VariableName Name of Variable to be found.
64 @param[in] VendorGuid Variable vendor GUID.
70 IN CHAR16
*VariableName
,
71 IN EFI_GUID
*VendorGuid
75 Acquires lock only at boot time. Simply returns at runtime.
77 This is a temperary function that will be removed when
78 EfiAcquireLock() in UefiLib can handle the call in UEFI
79 Runtimer driver in RT phase.
80 It calls EfiAcquireLock() at boot time, and simply returns
83 @param Lock A pointer to the lock to acquire.
87 AcquireLockOnlyAtBootTime (
91 if (!EfiAtRuntime ()) {
92 EfiAcquireLock (Lock
);
97 Releases lock only at boot time. Simply returns at runtime.
99 This is a temperary function which will be removed when
100 EfiReleaseLock() in UefiLib can handle the call in UEFI
101 Runtimer driver in RT phase.
102 It calls EfiReleaseLock() at boot time and simply returns
105 @param Lock A pointer to the lock to release.
109 ReleaseLockOnlyAtBootTime (
113 if (!EfiAtRuntime ()) {
114 EfiReleaseLock (Lock
);
119 Initialize the communicate buffer using DataSize and Function.
121 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
124 Caution: This function may receive untrusted input.
125 The data size external input, so this function will validate it carefully to avoid buffer overflow.
127 @param[out] DataPtr Points to the data in the communicate buffer.
128 @param[in] DataSize The data size to send to SMM.
129 @param[in] Function The function number to initialize the communicate header.
131 @retval EFI_INVALID_PARAMETER The data size is too big.
132 @retval EFI_SUCCESS Find the specified variable.
136 InitCommunicateBuffer (
137 OUT VOID
**DataPtr OPTIONAL
,
142 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
143 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
146 if (DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
> mVariableBufferSize
) {
147 return EFI_INVALID_PARAMETER
;
150 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
151 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
152 SmmCommunicateHeader
->MessageLength
= DataSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
154 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
155 SmmVariableFunctionHeader
->Function
= Function
;
156 if (DataPtr
!= NULL
) {
157 *DataPtr
= SmmVariableFunctionHeader
->Data
;
165 Send the data in communicate buffer to SMM.
167 @param[in] DataSize This size of the function header and the data.
169 @retval EFI_SUCCESS Success is returned from the functin in SMM.
170 @retval Others Failure is returned from the function in SMM.
174 SendCommunicateBuffer (
180 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
181 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
183 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
184 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mVariableBufferPhysical
, &CommSize
);
185 ASSERT_EFI_ERROR (Status
);
187 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
188 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)SmmCommunicateHeader
->Data
;
189 return SmmVariableFunctionHeader
->ReturnStatus
;
193 Mark a variable that will become read-only after leaving the DXE phase of execution.
195 @param[in] This The VARIABLE_LOCK_PROTOCOL instance.
196 @param[in] VariableName A pointer to the variable name that will be made read-only subsequently.
197 @param[in] VendorGuid A pointer to the vendor GUID that will be made read-only subsequently.
199 @retval EFI_SUCCESS The variable specified by the VariableName and the VendorGuid was marked
200 as pending to be read-only.
201 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL.
202 Or VariableName is an empty string.
203 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
204 already been signaled.
205 @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the lock request.
209 VariableLockRequestToLock (
210 IN CONST EDKII_VARIABLE_LOCK_PROTOCOL
*This
,
211 IN CHAR16
*VariableName
,
212 IN EFI_GUID
*VendorGuid
216 UINTN VariableNameSize
;
218 SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
*VariableToLock
;
220 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
221 return EFI_INVALID_PARAMETER
;
224 VariableNameSize
= StrSize (VariableName
);
225 VariableToLock
= NULL
;
228 // If VariableName exceeds SMM payload limit. Return failure
230 if (VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
, Name
)) {
231 return EFI_INVALID_PARAMETER
;
234 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
237 // Init the communicate buffer. The buffer data size is:
238 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
240 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE
, Name
) + VariableNameSize
;
241 Status
= InitCommunicateBuffer ((VOID
**) &VariableToLock
, PayloadSize
, SMM_VARIABLE_FUNCTION_LOCK_VARIABLE
);
242 if (EFI_ERROR (Status
)) {
245 ASSERT (VariableToLock
!= NULL
);
247 CopyGuid (&VariableToLock
->Guid
, VendorGuid
);
248 VariableToLock
->NameSize
= VariableNameSize
;
249 CopyMem (VariableToLock
->Name
, VariableName
, VariableToLock
->NameSize
);
254 Status
= SendCommunicateBuffer (PayloadSize
);
257 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
262 This code finds variable in storage blocks (Volatile or Non-Volatile).
264 Caution: This function may receive untrusted input.
265 The data size is external input, so this function will validate it carefully to avoid buffer overflow.
267 @param[in] VariableName Name of Variable to be found.
268 @param[in] VendorGuid Variable vendor GUID.
269 @param[out] Attributes Attribute value of the variable found.
270 @param[in, out] DataSize Size of Data found. If size is less than the
271 data, this value contains the required size.
272 @param[out] Data Data pointer.
274 @retval EFI_INVALID_PARAMETER Invalid parameter.
275 @retval EFI_SUCCESS Find the specified variable.
276 @retval EFI_NOT_FOUND Not found.
277 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
282 RuntimeServiceGetVariable (
283 IN CHAR16
*VariableName
,
284 IN EFI_GUID
*VendorGuid
,
285 OUT UINT32
*Attributes OPTIONAL
,
286 IN OUT UINTN
*DataSize
,
292 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
294 UINTN VariableNameSize
;
296 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
297 return EFI_INVALID_PARAMETER
;
300 if ((*DataSize
!= 0) && (Data
== NULL
)) {
301 return EFI_INVALID_PARAMETER
;
304 TempDataSize
= *DataSize
;
305 VariableNameSize
= StrSize (VariableName
);
306 SmmVariableHeader
= NULL
;
309 // If VariableName exceeds SMM payload limit. Return failure
311 if (VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) {
312 return EFI_INVALID_PARAMETER
;
315 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
318 // Init the communicate buffer. The buffer data size is:
319 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
321 if (TempDataSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
) {
323 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
325 TempDataSize
= mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
;
327 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + VariableNameSize
+ TempDataSize
;
329 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_VARIABLE
);
330 if (EFI_ERROR (Status
)) {
333 ASSERT (SmmVariableHeader
!= NULL
);
335 CopyGuid (&SmmVariableHeader
->Guid
, VendorGuid
);
336 SmmVariableHeader
->DataSize
= TempDataSize
;
337 SmmVariableHeader
->NameSize
= VariableNameSize
;
338 if (Attributes
== NULL
) {
339 SmmVariableHeader
->Attributes
= 0;
341 SmmVariableHeader
->Attributes
= *Attributes
;
343 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
348 Status
= SendCommunicateBuffer (PayloadSize
);
351 // Get data from SMM.
353 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
355 // SMM CommBuffer DataSize can be a trimed value
356 // Only update DataSize when needed
358 *DataSize
= SmmVariableHeader
->DataSize
;
360 if (Attributes
!= NULL
) {
361 *Attributes
= SmmVariableHeader
->Attributes
;
364 if (EFI_ERROR (Status
)) {
368 CopyMem (Data
, (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, SmmVariableHeader
->DataSize
);
371 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
377 This code Finds the Next available variable.
379 @param[in, out] VariableNameSize Size of the variable name.
380 @param[in, out] VariableName Pointer to variable name.
381 @param[in, out] VendorGuid Variable Vendor Guid.
383 @retval EFI_INVALID_PARAMETER Invalid parameter.
384 @retval EFI_SUCCESS Find the specified variable.
385 @retval EFI_NOT_FOUND Not found.
386 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
391 RuntimeServiceGetNextVariableName (
392 IN OUT UINTN
*VariableNameSize
,
393 IN OUT CHAR16
*VariableName
,
394 IN OUT EFI_GUID
*VendorGuid
399 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*SmmGetNextVariableName
;
400 UINTN OutVariableNameSize
;
401 UINTN InVariableNameSize
;
403 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
404 return EFI_INVALID_PARAMETER
;
407 OutVariableNameSize
= *VariableNameSize
;
408 InVariableNameSize
= StrSize (VariableName
);
409 SmmGetNextVariableName
= NULL
;
412 // If input string exceeds SMM payload limit. Return failure
414 if (InVariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
415 return EFI_INVALID_PARAMETER
;
418 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
421 // Init the communicate buffer. The buffer data size is:
422 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
424 if (OutVariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
426 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
428 OutVariableNameSize
= mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
431 // Payload should be Guid + NameSize + MAX of Input & Output buffer
433 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + MAX (OutVariableNameSize
, InVariableNameSize
);
435 Status
= InitCommunicateBuffer ((VOID
**)&SmmGetNextVariableName
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
);
436 if (EFI_ERROR (Status
)) {
439 ASSERT (SmmGetNextVariableName
!= NULL
);
442 // SMM comm buffer->NameSize is buffer size for return string
444 SmmGetNextVariableName
->NameSize
= OutVariableNameSize
;
446 CopyGuid (&SmmGetNextVariableName
->Guid
, VendorGuid
);
450 CopyMem (SmmGetNextVariableName
->Name
, VariableName
, InVariableNameSize
);
451 if (OutVariableNameSize
> InVariableNameSize
) {
452 ZeroMem ((UINT8
*) SmmGetNextVariableName
->Name
+ InVariableNameSize
, OutVariableNameSize
- InVariableNameSize
);
458 Status
= SendCommunicateBuffer (PayloadSize
);
461 // Get data from SMM.
463 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
465 // SMM CommBuffer NameSize can be a trimed value
466 // Only update VariableNameSize when needed
468 *VariableNameSize
= SmmGetNextVariableName
->NameSize
;
470 if (EFI_ERROR (Status
)) {
474 CopyGuid (VendorGuid
, &SmmGetNextVariableName
->Guid
);
475 CopyMem (VariableName
, SmmGetNextVariableName
->Name
, SmmGetNextVariableName
->NameSize
);
478 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
483 This code sets variable in storage blocks (Volatile or Non-Volatile).
485 Caution: This function may receive untrusted input.
486 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.
488 @param[in] VariableName Name of Variable to be found.
489 @param[in] VendorGuid Variable vendor GUID.
490 @param[in] Attributes Attribute value of the variable found
491 @param[in] DataSize Size of Data found. If size is less than the
492 data, this value contains the required size.
493 @param[in] Data Data pointer.
495 @retval EFI_INVALID_PARAMETER Invalid parameter.
496 @retval EFI_SUCCESS Set successfully.
497 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
498 @retval EFI_NOT_FOUND Not found.
499 @retval EFI_WRITE_PROTECTED Variable is read-only.
504 RuntimeServiceSetVariable (
505 IN CHAR16
*VariableName
,
506 IN EFI_GUID
*VendorGuid
,
507 IN UINT32 Attributes
,
514 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
515 UINTN VariableNameSize
;
518 // Check input parameters.
520 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
521 return EFI_INVALID_PARAMETER
;
524 if (DataSize
!= 0 && Data
== NULL
) {
525 return EFI_INVALID_PARAMETER
;
528 VariableNameSize
= StrSize (VariableName
);
529 SmmVariableHeader
= NULL
;
532 // If VariableName or DataSize exceeds SMM payload limit. Return failure
534 if ((VariableNameSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
)) ||
535 (DataSize
> mVariableBufferPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
)){
536 return EFI_INVALID_PARAMETER
;
539 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
542 // Init the communicate buffer. The buffer data size is:
543 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
545 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + VariableNameSize
+ DataSize
;
546 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_SET_VARIABLE
);
547 if (EFI_ERROR (Status
)) {
550 ASSERT (SmmVariableHeader
!= NULL
);
552 CopyGuid ((EFI_GUID
*) &SmmVariableHeader
->Guid
, VendorGuid
);
553 SmmVariableHeader
->DataSize
= DataSize
;
554 SmmVariableHeader
->NameSize
= VariableNameSize
;
555 SmmVariableHeader
->Attributes
= Attributes
;
556 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
557 CopyMem ((UINT8
*) SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, Data
, DataSize
);
562 Status
= SendCommunicateBuffer (PayloadSize
);
565 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
567 if (!EfiAtRuntime ()) {
568 if (!EFI_ERROR (Status
)) {
580 This code returns information about the EFI variables.
582 @param[in] Attributes Attributes bitmask to specify the type of variables
583 on which to return information.
584 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
585 for the EFI variables associated with the attributes specified.
586 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
587 for EFI variables associated with the attributes specified.
588 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
589 associated with the attributes specified.
591 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
592 @retval EFI_SUCCESS Query successfully.
593 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
598 RuntimeServiceQueryVariableInfo (
599 IN UINT32 Attributes
,
600 OUT UINT64
*MaximumVariableStorageSize
,
601 OUT UINT64
*RemainingVariableStorageSize
,
602 OUT UINT64
*MaximumVariableSize
607 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*SmmQueryVariableInfo
;
609 SmmQueryVariableInfo
= NULL
;
611 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
612 return EFI_INVALID_PARAMETER
;
615 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
618 // Init the communicate buffer. The buffer data size is:
619 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
621 PayloadSize
= sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
);
622 Status
= InitCommunicateBuffer ((VOID
**)&SmmQueryVariableInfo
, PayloadSize
, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
);
623 if (EFI_ERROR (Status
)) {
626 ASSERT (SmmQueryVariableInfo
!= NULL
);
628 SmmQueryVariableInfo
->Attributes
= Attributes
;
633 Status
= SendCommunicateBuffer (PayloadSize
);
634 if (EFI_ERROR (Status
)) {
639 // Get data from SMM.
641 *MaximumVariableSize
= SmmQueryVariableInfo
->MaximumVariableSize
;
642 *MaximumVariableStorageSize
= SmmQueryVariableInfo
->MaximumVariableStorageSize
;
643 *RemainingVariableStorageSize
= SmmQueryVariableInfo
->RemainingVariableStorageSize
;
646 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
652 Exit Boot Services Event notification handler.
654 Notify SMM variable driver about the event.
656 @param[in] Event Event whose notification function is being invoked.
657 @param[in] Context Pointer to the notification function's context.
668 // Init the communicate buffer. The buffer data size is:
669 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
671 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
);
676 SendCommunicateBuffer (0);
681 On Ready To Boot Services Event notification handler.
683 Notify SMM variable driver about the event.
685 @param[in] Event Event whose notification function is being invoked
686 @param[in] Context Pointer to the notification function's context
697 // Init the communicate buffer. The buffer data size is:
698 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
700 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT
);
705 SendCommunicateBuffer (0);
710 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
712 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
713 It convers pointer to new virtual address.
715 @param[in] Event Event whose notification function is being invoked.
716 @param[in] Context Pointer to the notification function's context.
721 VariableAddressChangeEvent (
726 EfiConvertPointer (0x0, (VOID
**) &mVariableBuffer
);
727 EfiConvertPointer (0x0, (VOID
**) &mSmmCommunication
);
732 Initialize variable service and install Variable Architectural protocol.
734 @param[in] Event Event whose notification function is being invoked.
735 @param[in] Context Pointer to the notification function's context.
747 Status
= gBS
->LocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**)&mSmmVariable
);
748 if (EFI_ERROR (Status
)) {
752 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &mSmmCommunication
);
753 ASSERT_EFI_ERROR (Status
);
756 // Allocate memory for variable communicate buffer.
758 mVariableBufferPayloadSize
= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
)) +
759 OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - sizeof (VARIABLE_HEADER
);
760 mVariableBufferSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
+ mVariableBufferPayloadSize
;
761 mVariableBuffer
= AllocateRuntimePool (mVariableBufferSize
);
762 ASSERT (mVariableBuffer
!= NULL
);
765 // Save the buffer physical address used for SMM conmunication.
767 mVariableBufferPhysical
= mVariableBuffer
;
769 gRT
->GetVariable
= RuntimeServiceGetVariable
;
770 gRT
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
771 gRT
->SetVariable
= RuntimeServiceSetVariable
;
772 gRT
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
775 // Install the Variable Architectural Protocol on a new handle.
777 Status
= gBS
->InstallProtocolInterface (
779 &gEfiVariableArchProtocolGuid
,
780 EFI_NATIVE_INTERFACE
,
783 ASSERT_EFI_ERROR (Status
);
788 SMM Non-Volatile variable write service is ready notify event handler.
790 @param[in] Event Event whose notification function is being invoked.
791 @param[in] Context Pointer to the notification function's context.
796 SmmVariableWriteReady (
805 // Check whether the protocol is installed or not.
807 Status
= gBS
->LocateProtocol (&gSmmVariableWriteGuid
, NULL
, (VOID
**) &ProtocolOps
);
808 if (EFI_ERROR (Status
)) {
812 Status
= gBS
->InstallProtocolInterface (
814 &gEfiVariableWriteArchProtocolGuid
,
815 EFI_NATIVE_INTERFACE
,
818 ASSERT_EFI_ERROR (Status
);
823 Variable Driver main entry point. The Variable driver places the 4 EFI
824 runtime services in the EFI System Table and installs arch protocols
825 for variable read and write services being available. It also registers
826 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
828 @param[in] ImageHandle The firmware allocated handle for the EFI image.
829 @param[in] SystemTable A pointer to the EFI System Table.
831 @retval EFI_SUCCESS Variable service successfully initialized.
836 VariableSmmRuntimeInitialize (
837 IN EFI_HANDLE ImageHandle
,
838 IN EFI_SYSTEM_TABLE
*SystemTable
842 VOID
*SmmVariableRegistration
;
843 VOID
*SmmVariableWriteRegistration
;
844 EFI_EVENT OnReadyToBootEvent
;
845 EFI_EVENT ExitBootServiceEvent
;
847 EfiInitializeLock (&mVariableServicesLock
, TPL_NOTIFY
);
849 mVariableLock
.RequestToLock
= VariableLockRequestToLock
;
850 Status
= gBS
->InstallMultipleProtocolInterfaces (
852 &gEdkiiVariableLockProtocolGuid
,
856 ASSERT_EFI_ERROR (Status
);
859 // Smm variable service is ready
861 EfiCreateProtocolNotifyEvent (
862 &gEfiSmmVariableProtocolGuid
,
866 &SmmVariableRegistration
870 // Smm Non-Volatile variable write service is ready
872 EfiCreateProtocolNotifyEvent (
873 &gSmmVariableWriteGuid
,
875 SmmVariableWriteReady
,
877 &SmmVariableWriteRegistration
881 // Register the event to reclaim variable for OS usage.
883 EfiCreateEventReadyToBootEx (
891 // Register the event to inform SMM variable that it is at runtime.
898 &gEfiEventExitBootServicesGuid
,
899 &ExitBootServiceEvent
903 // Register the event to convert the pointer for runtime.
908 VariableAddressChangeEvent
,
910 &gEfiEventVirtualAddressChangeGuid
,
911 &mVirtualAddressChangeEvent