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 - 2013, 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>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/UefiRuntimeServicesTableLib.h>
35 #include <Library/MemoryAllocationLib.h>
36 #include <Library/UefiDriverEntryPoint.h>
37 #include <Library/UefiRuntimeLib.h>
38 #include <Library/BaseMemoryLib.h>
39 #include <Library/DebugLib.h>
40 #include <Library/PcdLib.h>
41 #include <Library/UefiLib.h>
42 #include <Library/BaseLib.h>
44 #include <Guid/EventGroup.h>
45 #include <Guid/AuthenticatedVariableFormat.h>
46 #include <Guid/SmmVariableCommon.h>
48 EFI_HANDLE mHandle
= NULL
;
49 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
= NULL
;
50 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
51 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
52 UINT8
*mVariableBuffer
= NULL
;
53 UINT8
*mVariableBufferPhysical
= NULL
;
54 UINTN mVariableBufferSize
;
55 EFI_LOCK mVariableServicesLock
;
58 Acquires lock only at boot time. Simply returns at runtime.
60 This is a temperary function that will be removed when
61 EfiAcquireLock() in UefiLib can handle the call in UEFI
62 Runtimer driver in RT phase.
63 It calls EfiAcquireLock() at boot time, and simply returns
66 @param Lock A pointer to the lock to acquire.
70 AcquireLockOnlyAtBootTime (
74 if (!EfiAtRuntime ()) {
75 EfiAcquireLock (Lock
);
80 Releases lock only at boot time. Simply returns at runtime.
82 This is a temperary function which will be removed when
83 EfiReleaseLock() in UefiLib can handle the call in UEFI
84 Runtimer driver in RT phase.
85 It calls EfiReleaseLock() at boot time and simply returns
88 @param Lock A pointer to the lock to release.
92 ReleaseLockOnlyAtBootTime (
96 if (!EfiAtRuntime ()) {
97 EfiReleaseLock (Lock
);
102 Initialize the communicate buffer using DataSize and Function.
104 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
107 Caution: This function may receive untrusted input.
108 The data size external input, so this function will validate it carefully to avoid buffer overflow.
110 @param[out] DataPtr Points to the data in the communicate buffer.
111 @param[in] DataSize The data size to send to SMM.
112 @param[in] Function The function number to initialize the communicate header.
114 @retval EFI_INVALID_PARAMETER The data size is too big.
115 @retval EFI_SUCCESS Find the specified variable.
119 InitCommunicateBuffer (
120 OUT VOID
**DataPtr OPTIONAL
,
125 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
126 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
129 if (DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
> mVariableBufferSize
) {
130 return EFI_INVALID_PARAMETER
;
133 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
134 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
135 SmmCommunicateHeader
->MessageLength
= DataSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
137 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
138 SmmVariableFunctionHeader
->Function
= Function
;
139 if (DataPtr
!= NULL
) {
140 *DataPtr
= SmmVariableFunctionHeader
->Data
;
148 Send the data in communicate buffer to SMM.
150 @param[in] DataSize This size of the function header and the data.
152 @retval EFI_SUCCESS Success is returned from the functin in SMM.
153 @retval Others Failure is returned from the function in SMM.
157 SendCommunicateBuffer (
163 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
164 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
166 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
167 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mVariableBufferPhysical
, &CommSize
);
168 ASSERT_EFI_ERROR (Status
);
170 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
171 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)SmmCommunicateHeader
->Data
;
172 return SmmVariableFunctionHeader
->ReturnStatus
;
177 This code finds variable in storage blocks (Volatile or Non-Volatile).
179 Caution: This function may receive untrusted input.
180 The data size is external input, so this function will validate it carefully to avoid buffer overflow.
182 @param[in] VariableName Name of Variable to be found.
183 @param[in] VendorGuid Variable vendor GUID.
184 @param[out] Attributes Attribute value of the variable found.
185 @param[in, out] DataSize Size of Data found. If size is less than the
186 data, this value contains the required size.
187 @param[out] Data Data pointer.
189 @retval EFI_INVALID_PARAMETER Invalid parameter.
190 @retval EFI_SUCCESS Find the specified variable.
191 @retval EFI_NOT_FOUND Not found.
192 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
197 RuntimeServiceGetVariable (
198 IN CHAR16
*VariableName
,
199 IN EFI_GUID
*VendorGuid
,
200 OUT UINT32
*Attributes OPTIONAL
,
201 IN OUT UINTN
*DataSize
,
207 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
208 UINTN SmmCommBufPayloadSize
;
210 UINTN VariableNameSize
;
212 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
213 return EFI_INVALID_PARAMETER
;
216 if ((*DataSize
!= 0) && (Data
== NULL
)) {
217 return EFI_INVALID_PARAMETER
;
221 // SMM Communication Buffer max payload size
223 SmmCommBufPayloadSize
= mVariableBufferSize
- (SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
);
224 TempDataSize
= *DataSize
;
225 VariableNameSize
= StrSize (VariableName
);
228 // If VariableName exceeds SMM payload limit. Return failure
230 if (VariableNameSize
> SmmCommBufPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_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 if (TempDataSize
> SmmCommBufPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
) {
242 // If output data buffer exceed SMM payload limit. Trim output buffer to SMM payload size
244 TempDataSize
= SmmCommBufPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) - VariableNameSize
;
246 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + VariableNameSize
+ TempDataSize
;
248 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_VARIABLE
);
249 if (EFI_ERROR (Status
)) {
252 ASSERT (SmmVariableHeader
!= NULL
);
254 CopyGuid (&SmmVariableHeader
->Guid
, VendorGuid
);
255 SmmVariableHeader
->DataSize
= TempDataSize
;
256 SmmVariableHeader
->NameSize
= VariableNameSize
;
257 if (Attributes
== NULL
) {
258 SmmVariableHeader
->Attributes
= 0;
260 SmmVariableHeader
->Attributes
= *Attributes
;
262 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
267 Status
= SendCommunicateBuffer (PayloadSize
);
270 // Get data from SMM.
272 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
274 // SMM CommBuffer DataSize can be a trimed value
275 // Only update DataSize when needed
277 *DataSize
= SmmVariableHeader
->DataSize
;
279 if (Attributes
!= NULL
) {
280 *Attributes
= SmmVariableHeader
->Attributes
;
283 if (EFI_ERROR (Status
)) {
287 CopyMem (Data
, (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, SmmVariableHeader
->DataSize
);
290 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
296 This code Finds the Next available variable.
298 @param[in, out] VariableNameSize Size of the variable name.
299 @param[in, out] VariableName Pointer to variable name.
300 @param[in, out] VendorGuid Variable Vendor Guid.
302 @retval EFI_INVALID_PARAMETER Invalid parameter.
303 @retval EFI_SUCCESS Find the specified variable.
304 @retval EFI_NOT_FOUND Not found.
305 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
310 RuntimeServiceGetNextVariableName (
311 IN OUT UINTN
*VariableNameSize
,
312 IN OUT CHAR16
*VariableName
,
313 IN OUT EFI_GUID
*VendorGuid
318 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*SmmGetNextVariableName
;
319 UINTN SmmCommBufPayloadSize
;
320 UINTN OutVariableNameSize
;
321 UINTN InVariableNameSize
;
323 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
324 return EFI_INVALID_PARAMETER
;
328 // SMM Communication Buffer max payload size
330 SmmCommBufPayloadSize
= mVariableBufferSize
- (SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
);
331 OutVariableNameSize
= *VariableNameSize
;
332 InVariableNameSize
= StrSize (VariableName
);
335 // If input string exceeds SMM payload limit. Return failure
337 if (InVariableNameSize
> SmmCommBufPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
338 return EFI_INVALID_PARAMETER
;
341 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
344 // Init the communicate buffer. The buffer data size is:
345 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
347 if (OutVariableNameSize
> SmmCommBufPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
)) {
349 // If output buffer exceed SMM payload limit. Trim output buffer to SMM payload size
351 OutVariableNameSize
= SmmCommBufPayloadSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
);
354 // Payload should be Guid + NameSize + MAX of Input & Output buffer
356 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + MAX (OutVariableNameSize
, InVariableNameSize
);
358 Status
= InitCommunicateBuffer ((VOID
**)&SmmGetNextVariableName
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
);
359 if (EFI_ERROR (Status
)) {
362 ASSERT (SmmGetNextVariableName
!= NULL
);
365 // SMM comm buffer->NameSize is buffer size for return string
367 SmmGetNextVariableName
->NameSize
= OutVariableNameSize
;
369 CopyGuid (&SmmGetNextVariableName
->Guid
, VendorGuid
);
373 CopyMem (SmmGetNextVariableName
->Name
, VariableName
, InVariableNameSize
);
374 if (OutVariableNameSize
> InVariableNameSize
) {
375 ZeroMem ((UINT8
*) SmmGetNextVariableName
->Name
+ InVariableNameSize
, OutVariableNameSize
- InVariableNameSize
);
381 Status
= SendCommunicateBuffer (PayloadSize
);
384 // Get data from SMM.
386 if (Status
== EFI_SUCCESS
|| Status
== EFI_BUFFER_TOO_SMALL
) {
388 // SMM CommBuffer NameSize can be a trimed value
389 // Only update VariableNameSize when needed
391 *VariableNameSize
= SmmGetNextVariableName
->NameSize
;
393 if (EFI_ERROR (Status
)) {
397 CopyGuid (VendorGuid
, &SmmGetNextVariableName
->Guid
);
398 CopyMem (VariableName
, SmmGetNextVariableName
->Name
, SmmGetNextVariableName
->NameSize
);
401 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
406 This code sets variable in storage blocks (Volatile or Non-Volatile).
408 Caution: This function may receive untrusted input.
409 The data size and data are external input, so this function will validate it carefully to avoid buffer overflow.
411 @param[in] VariableName Name of Variable to be found.
412 @param[in] VendorGuid Variable vendor GUID.
413 @param[in] Attributes Attribute value of the variable found
414 @param[in] DataSize Size of Data found. If size is less than the
415 data, this value contains the required size.
416 @param[in] Data Data pointer.
418 @retval EFI_INVALID_PARAMETER Invalid parameter.
419 @retval EFI_SUCCESS Set successfully.
420 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
421 @retval EFI_NOT_FOUND Not found.
422 @retval EFI_WRITE_PROTECTED Variable is read-only.
427 RuntimeServiceSetVariable (
428 IN CHAR16
*VariableName
,
429 IN EFI_GUID
*VendorGuid
,
430 IN UINT32 Attributes
,
437 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
438 UINTN VariableNameSize
;
441 // Check input parameters.
443 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
444 return EFI_INVALID_PARAMETER
;
447 if (DataSize
!= 0 && Data
== NULL
) {
448 return EFI_INVALID_PARAMETER
;
451 if (DataSize
>= mVariableBufferSize
) {
453 // DataSize may be near MAX_ADDRESS incorrectly, this can cause the computed PayLoadSize to
454 // overflow to a small value and pass the check in InitCommunicateBuffer().
455 // To protect against this vulnerability, return EFI_INVALID_PARAMETER if DataSize is >= mVariableBufferSize.
456 // And there will be further check to ensure the total size is also not > mVariableBufferSize.
458 return EFI_INVALID_PARAMETER
;
460 VariableNameSize
= StrSize (VariableName
);
462 if ((UINTN
)(~0) - VariableNameSize
< OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + DataSize
) {
464 // Prevent PayloadSize overflow
466 return EFI_INVALID_PARAMETER
;
469 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
472 // Init the communicate buffer. The buffer data size is:
473 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
475 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + VariableNameSize
+ DataSize
;
476 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_SET_VARIABLE
);
477 if (EFI_ERROR (Status
)) {
480 ASSERT (SmmVariableHeader
!= NULL
);
482 CopyGuid ((EFI_GUID
*) &SmmVariableHeader
->Guid
, VendorGuid
);
483 SmmVariableHeader
->DataSize
= DataSize
;
484 SmmVariableHeader
->NameSize
= VariableNameSize
;
485 SmmVariableHeader
->Attributes
= Attributes
;
486 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
487 CopyMem ((UINT8
*) SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, Data
, DataSize
);
492 Status
= SendCommunicateBuffer (PayloadSize
);
495 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
501 This code returns information about the EFI variables.
503 @param[in] Attributes Attributes bitmask to specify the type of variables
504 on which to return information.
505 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
506 for the EFI variables associated with the attributes specified.
507 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
508 for EFI variables associated with the attributes specified.
509 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
510 associated with the attributes specified.
512 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
513 @retval EFI_SUCCESS Query successfully.
514 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
519 RuntimeServiceQueryVariableInfo (
520 IN UINT32 Attributes
,
521 OUT UINT64
*MaximumVariableStorageSize
,
522 OUT UINT64
*RemainingVariableStorageSize
,
523 OUT UINT64
*MaximumVariableSize
528 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*SmmQueryVariableInfo
;
530 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
531 return EFI_INVALID_PARAMETER
;
534 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
537 // Init the communicate buffer. The buffer data size is:
538 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
540 PayloadSize
= sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
);
541 Status
= InitCommunicateBuffer ((VOID
**)&SmmQueryVariableInfo
, PayloadSize
, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
);
542 if (EFI_ERROR (Status
)) {
545 ASSERT (SmmQueryVariableInfo
!= NULL
);
547 SmmQueryVariableInfo
->Attributes
= Attributes
;
552 Status
= SendCommunicateBuffer (PayloadSize
);
553 if (EFI_ERROR (Status
)) {
558 // Get data from SMM.
560 *MaximumVariableSize
= SmmQueryVariableInfo
->MaximumVariableSize
;
561 *MaximumVariableStorageSize
= SmmQueryVariableInfo
->MaximumVariableStorageSize
;
562 *RemainingVariableStorageSize
= SmmQueryVariableInfo
->RemainingVariableStorageSize
;
565 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
571 Exit Boot Services Event notification handler.
573 Notify SMM variable driver about the event.
575 @param[in] Event Event whose notification function is being invoked.
576 @param[in] Context Pointer to the notification function's context.
587 // Init the communicate buffer. The buffer data size is:
588 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
590 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
);
595 SendCommunicateBuffer (0);
600 On Ready To Boot Services Event notification handler.
602 Notify SMM variable driver about the event.
604 @param[in] Event Event whose notification function is being invoked
605 @param[in] Context Pointer to the notification function's context
616 // Init the communicate buffer. The buffer data size is:
617 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
619 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT
);
624 SendCommunicateBuffer (0);
629 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
631 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
632 It convers pointer to new virtual address.
634 @param[in] Event Event whose notification function is being invoked.
635 @param[in] Context Pointer to the notification function's context.
640 VariableAddressChangeEvent (
645 EfiConvertPointer (0x0, (VOID
**) &mVariableBuffer
);
646 EfiConvertPointer (0x0, (VOID
**) &mSmmCommunication
);
651 Initialize variable service and install Variable Architectural protocol.
653 @param[in] Event Event whose notification function is being invoked.
654 @param[in] Context Pointer to the notification function's context.
666 Status
= gBS
->LocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**)&mSmmVariable
);
667 if (EFI_ERROR (Status
)) {
671 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &mSmmCommunication
);
672 ASSERT_EFI_ERROR (Status
);
675 // Allocate memory for variable store.
677 mVariableBufferSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
678 mVariableBufferSize
+= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
679 mVariableBuffer
= AllocateRuntimePool (mVariableBufferSize
);
680 ASSERT (mVariableBuffer
!= NULL
);
683 // Save the buffer physical address used for SMM conmunication.
685 mVariableBufferPhysical
= mVariableBuffer
;
687 gRT
->GetVariable
= RuntimeServiceGetVariable
;
688 gRT
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
689 gRT
->SetVariable
= RuntimeServiceSetVariable
;
690 gRT
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
693 // Install the Variable Architectural Protocol on a new handle.
695 Status
= gBS
->InstallProtocolInterface (
697 &gEfiVariableArchProtocolGuid
,
698 EFI_NATIVE_INTERFACE
,
701 ASSERT_EFI_ERROR (Status
);
706 SMM Non-Volatile variable write service is ready notify event handler.
708 @param[in] Event Event whose notification function is being invoked.
709 @param[in] Context Pointer to the notification function's context.
714 SmmVariableWriteReady (
723 // Check whether the protocol is installed or not.
725 Status
= gBS
->LocateProtocol (&gSmmVariableWriteGuid
, NULL
, (VOID
**) &ProtocolOps
);
726 if (EFI_ERROR (Status
)) {
730 Status
= gBS
->InstallProtocolInterface (
732 &gEfiVariableWriteArchProtocolGuid
,
733 EFI_NATIVE_INTERFACE
,
736 ASSERT_EFI_ERROR (Status
);
741 Variable Driver main entry point. The Variable driver places the 4 EFI
742 runtime services in the EFI System Table and installs arch protocols
743 for variable read and write services being available. It also registers
744 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
746 @param[in] ImageHandle The firmware allocated handle for the EFI image.
747 @param[in] SystemTable A pointer to the EFI System Table.
749 @retval EFI_SUCCESS Variable service successfully initialized.
754 VariableSmmRuntimeInitialize (
755 IN EFI_HANDLE ImageHandle
,
756 IN EFI_SYSTEM_TABLE
*SystemTable
759 VOID
*SmmVariableRegistration
;
760 VOID
*SmmVariableWriteRegistration
;
761 EFI_EVENT OnReadyToBootEvent
;
762 EFI_EVENT ExitBootServiceEvent
;
764 EfiInitializeLock (&mVariableServicesLock
, TPL_NOTIFY
);
767 // Smm variable service is ready
769 EfiCreateProtocolNotifyEvent (
770 &gEfiSmmVariableProtocolGuid
,
774 &SmmVariableRegistration
778 // Smm Non-Volatile variable write service is ready
780 EfiCreateProtocolNotifyEvent (
781 &gSmmVariableWriteGuid
,
783 SmmVariableWriteReady
,
785 &SmmVariableWriteRegistration
789 // Register the event to reclaim variable for OS usage.
791 EfiCreateEventReadyToBootEx (
799 // Register the event to inform SMM variable that it is at runtime.
806 &gEfiEventExitBootServicesGuid
,
807 &ExitBootServiceEvent
811 // Register the event to convert the pointer for runtime.
816 VariableAddressChangeEvent
,
818 &gEfiEventVirtualAddressChangeGuid
,
819 &mVirtualAddressChangeEvent