3 Implement all four UEFI Runtime Variable services for the nonvolatile
4 and volatile storage space and install variable architecture protocol
5 based on SMM variable module.
7 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Protocol/VariableWrite.h>
19 #include <Protocol/Variable.h>
20 #include <Protocol/SmmCommunication.h>
21 #include <Protocol/SmmVariable.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiRuntimeServicesTableLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiRuntimeLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/BaseLib.h>
34 #include <Guid/EventGroup.h>
35 #include <Guid/VariableFormat.h>
36 #include <Guid/SmmVariableCommon.h>
38 EFI_HANDLE mHandle
= NULL
;
39 EFI_SMM_VARIABLE_PROTOCOL
*mSmmVariable
= NULL
;
40 EFI_EVENT mVirtualAddressChangeEvent
= NULL
;
41 EFI_SMM_COMMUNICATION_PROTOCOL
*mSmmCommunication
= NULL
;
42 UINT8
*mVariableBuffer
= NULL
;
43 UINT8
*mVariableBufferPhysical
= NULL
;
44 UINTN mVariableBufferSize
;
45 EFI_LOCK mVariableServicesLock
;
48 Acquires lock only at boot time. Simply returns at runtime.
50 This is a temperary function that will be removed when
51 EfiAcquireLock() in UefiLib can handle the call in UEFI
52 Runtimer driver in RT phase.
53 It calls EfiAcquireLock() at boot time, and simply returns
56 @param Lock A pointer to the lock to acquire.
60 AcquireLockOnlyAtBootTime (
64 if (!EfiAtRuntime ()) {
65 EfiAcquireLock (Lock
);
70 Releases lock only at boot time. Simply returns at runtime.
72 This is a temperary function which will be removed when
73 EfiReleaseLock() in UefiLib can handle the call in UEFI
74 Runtimer driver in RT phase.
75 It calls EfiReleaseLock() at boot time and simply returns
78 @param Lock A pointer to the lock to release.
82 ReleaseLockOnlyAtBootTime (
86 if (!EfiAtRuntime ()) {
87 EfiReleaseLock (Lock
);
92 Initialize the communicate buffer using DataSize and Function.
94 The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE +
97 @param[out] DataPtr Points to the data in the communicate buffer.
98 @param[in] DataSize The data size to send to SMM.
99 @param[in] Function The function number to initialize the communicate header.
101 @retval EFI_INVALID_PARAMETER The data size is too big.
102 @retval EFI_SUCCESS Find the specified variable.
106 InitCommunicateBuffer (
107 OUT VOID
**DataPtr OPTIONAL
,
112 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
113 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
116 if (DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
> mVariableBufferSize
) {
117 return EFI_INVALID_PARAMETER
;
120 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
121 CopyGuid (&SmmCommunicateHeader
->HeaderGuid
, &gEfiSmmVariableProtocolGuid
);
122 SmmCommunicateHeader
->MessageLength
= DataSize
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
124 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*) SmmCommunicateHeader
->Data
;
125 SmmVariableFunctionHeader
->Function
= Function
;
126 if (DataPtr
!= NULL
) {
127 *DataPtr
= SmmVariableFunctionHeader
->Data
;
135 Send the data in communicate buffer to SMM.
137 @param[in] DataSize This size of the function header and the data.
139 @retval EFI_SUCCESS Success is returned from the functin in SMM.
140 @retval Others Failure is returned from the function in SMM.
144 SendCommunicateBuffer (
150 EFI_SMM_COMMUNICATE_HEADER
*SmmCommunicateHeader
;
151 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
153 CommSize
= DataSize
+ SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
154 Status
= mSmmCommunication
->Communicate (mSmmCommunication
, mVariableBufferPhysical
, &CommSize
);
155 ASSERT_EFI_ERROR (Status
);
157 SmmCommunicateHeader
= (EFI_SMM_COMMUNICATE_HEADER
*) mVariableBuffer
;
158 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)SmmCommunicateHeader
->Data
;
159 return SmmVariableFunctionHeader
->ReturnStatus
;
164 This code finds variable in storage blocks (Volatile or Non-Volatile).
166 @param[in] VariableName Name of Variable to be found.
167 @param[in] VendorGuid Variable vendor GUID.
168 @param[out] Attributes Attribute value of the variable found.
169 @param[in, out] DataSize Size of Data found. If size is less than the
170 data, this value contains the required size.
171 @param[out] Data Data pointer.
173 @retval EFI_INVALID_PARAMETER Invalid parameter.
174 @retval EFI_SUCCESS Find the specified variable.
175 @retval EFI_NOT_FOUND Not found.
176 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
181 RuntimeServiceGetVariable (
182 IN CHAR16
*VariableName
,
183 IN EFI_GUID
*VendorGuid
,
184 OUT UINT32
*Attributes OPTIONAL
,
185 IN OUT UINTN
*DataSize
,
191 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
193 if (VariableName
== NULL
|| VendorGuid
== NULL
|| DataSize
== NULL
) {
194 return EFI_INVALID_PARAMETER
;
197 if ((*DataSize
!= 0) && (Data
== NULL
)) {
198 return EFI_INVALID_PARAMETER
;
201 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
204 // Init the communicate buffer. The buffer data size is:
205 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
207 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + StrSize (VariableName
) + *DataSize
;
208 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_VARIABLE
);
209 if (EFI_ERROR (Status
)) {
212 ASSERT (SmmVariableHeader
!= NULL
);
214 CopyGuid (&SmmVariableHeader
->Guid
, VendorGuid
);
215 SmmVariableHeader
->DataSize
= *DataSize
;
216 SmmVariableHeader
->NameSize
= StrSize (VariableName
);
217 if (Attributes
== NULL
) {
218 SmmVariableHeader
->Attributes
= 0;
220 SmmVariableHeader
->Attributes
= *Attributes
;
222 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
227 Status
= SendCommunicateBuffer (PayloadSize
);
230 // Get data from SMM.
232 *DataSize
= SmmVariableHeader
->DataSize
;
233 if (Attributes
!= NULL
) {
234 *Attributes
= SmmVariableHeader
->Attributes
;
237 if (EFI_ERROR (Status
)) {
241 CopyMem (Data
, (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, SmmVariableHeader
->DataSize
);
244 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
250 This code Finds the Next available variable.
252 @param[in, out] VariableNameSize Size of the variable name.
253 @param[in, out] VariableName Pointer to variable name.
254 @param[in, out] VendorGuid Variable Vendor Guid.
256 @retval EFI_INVALID_PARAMETER Invalid parameter.
257 @retval EFI_SUCCESS Find the specified variable.
258 @retval EFI_NOT_FOUND Not found.
259 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
264 RuntimeServiceGetNextVariableName (
265 IN OUT UINTN
*VariableNameSize
,
266 IN OUT CHAR16
*VariableName
,
267 IN OUT EFI_GUID
*VendorGuid
272 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*SmmGetNextVariableName
;
274 if (VariableNameSize
== NULL
|| VariableName
== NULL
|| VendorGuid
== NULL
) {
275 return EFI_INVALID_PARAMETER
;
278 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
281 // Init the communicate buffer. The buffer data size is:
282 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
284 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
, Name
) + *VariableNameSize
;
285 Status
= InitCommunicateBuffer ((VOID
**)&SmmGetNextVariableName
, PayloadSize
, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
);
286 if (EFI_ERROR (Status
)) {
289 ASSERT (SmmGetNextVariableName
!= NULL
);
291 SmmGetNextVariableName
->NameSize
= *VariableNameSize
;
292 CopyGuid (&SmmGetNextVariableName
->Guid
, VendorGuid
);
293 CopyMem (SmmGetNextVariableName
->Name
, VariableName
, *VariableNameSize
);
298 Status
= SendCommunicateBuffer (PayloadSize
);
301 // Get data from SMM.
303 *VariableNameSize
= SmmGetNextVariableName
->NameSize
;
304 if (EFI_ERROR (Status
)) {
308 CopyGuid (VendorGuid
, &SmmGetNextVariableName
->Guid
);
309 CopyMem (VariableName
, SmmGetNextVariableName
->Name
, SmmGetNextVariableName
->NameSize
);
312 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
317 This code sets variable in storage blocks (Volatile or Non-Volatile).
319 @param[in] VariableName Name of Variable to be found.
320 @param[in] VendorGuid Variable vendor GUID.
321 @param[in] Attributes Attribute value of the variable found
322 @param[in] DataSize Size of Data found. If size is less than the
323 data, this value contains the required size.
324 @param[in] Data Data pointer.
326 @retval EFI_INVALID_PARAMETER Invalid parameter.
327 @retval EFI_SUCCESS Set successfully.
328 @retval EFI_OUT_OF_RESOURCES Resource not enough to set variable.
329 @retval EFI_NOT_FOUND Not found.
330 @retval EFI_WRITE_PROTECTED Variable is read-only.
335 RuntimeServiceSetVariable (
336 IN CHAR16
*VariableName
,
337 IN EFI_GUID
*VendorGuid
,
338 IN UINT32 Attributes
,
345 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
348 // Check input parameters.
350 if (VariableName
== NULL
|| VariableName
[0] == 0 || VendorGuid
== NULL
) {
351 return EFI_INVALID_PARAMETER
;
354 if (DataSize
!= 0 && Data
== NULL
) {
355 return EFI_INVALID_PARAMETER
;
358 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
361 // Init the communicate buffer. The buffer data size is:
362 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize.
364 PayloadSize
= OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
, Name
) + StrSize (VariableName
) + DataSize
;
365 Status
= InitCommunicateBuffer ((VOID
**)&SmmVariableHeader
, PayloadSize
, SMM_VARIABLE_FUNCTION_SET_VARIABLE
);
366 if (EFI_ERROR (Status
)) {
369 ASSERT (SmmVariableHeader
!= NULL
);
371 CopyGuid ((EFI_GUID
*) &SmmVariableHeader
->Guid
, VendorGuid
);
372 SmmVariableHeader
->DataSize
= DataSize
;
373 SmmVariableHeader
->NameSize
= StrSize (VariableName
);
374 SmmVariableHeader
->Attributes
= Attributes
;
375 CopyMem (SmmVariableHeader
->Name
, VariableName
, SmmVariableHeader
->NameSize
);
376 CopyMem ((UINT8
*) SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
, Data
, DataSize
);
381 Status
= SendCommunicateBuffer (PayloadSize
);
384 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
390 This code returns information about the EFI variables.
392 @param[in] Attributes Attributes bitmask to specify the type of variables
393 on which to return information.
394 @param[out] MaximumVariableStorageSize Pointer to the maximum size of the storage space available
395 for the EFI variables associated with the attributes specified.
396 @param[out] RemainingVariableStorageSize Pointer to the remaining size of the storage space available
397 for EFI variables associated with the attributes specified.
398 @param[out] MaximumVariableSize Pointer to the maximum size of an individual EFI variables
399 associated with the attributes specified.
401 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
402 @retval EFI_SUCCESS Query successfully.
403 @retval EFI_UNSUPPORTED The attribute is not supported on this platform.
408 RuntimeServiceQueryVariableInfo (
409 IN UINT32 Attributes
,
410 OUT UINT64
*MaximumVariableStorageSize
,
411 OUT UINT64
*RemainingVariableStorageSize
,
412 OUT UINT64
*MaximumVariableSize
417 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*SmmQueryVariableInfo
;
419 if(MaximumVariableStorageSize
== NULL
|| RemainingVariableStorageSize
== NULL
|| MaximumVariableSize
== NULL
|| Attributes
== 0) {
420 return EFI_INVALID_PARAMETER
;
423 AcquireLockOnlyAtBootTime(&mVariableServicesLock
);
426 // Init the communicate buffer. The buffer data size is:
427 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize;
429 PayloadSize
= sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
);
430 Status
= InitCommunicateBuffer ((VOID
**)&SmmQueryVariableInfo
, PayloadSize
, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
);
431 if (EFI_ERROR (Status
)) {
434 ASSERT (SmmQueryVariableInfo
!= NULL
);
436 SmmQueryVariableInfo
->Attributes
= Attributes
;
441 Status
= SendCommunicateBuffer (PayloadSize
);
442 if (EFI_ERROR (Status
)) {
447 // Get data from SMM.
449 *MaximumVariableSize
= SmmQueryVariableInfo
->MaximumVariableSize
;
450 *MaximumVariableStorageSize
= SmmQueryVariableInfo
->MaximumVariableStorageSize
;
451 *RemainingVariableStorageSize
= SmmQueryVariableInfo
->RemainingVariableStorageSize
;
454 ReleaseLockOnlyAtBootTime (&mVariableServicesLock
);
460 Exit Boot Services Event notification handler.
462 Notify SMM variable driver about the event.
464 @param[in] Event Event whose notification function is being invoked.
465 @param[in] Context Pointer to the notification function's context.
476 // Init the communicate buffer. The buffer data size is:
477 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
479 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
);
484 SendCommunicateBuffer (0);
489 On Ready To Boot Services Event notification handler.
491 Notify SMM variable driver about the event.
493 @param[in] Event Event whose notification function is being invoked
494 @param[in] Context Pointer to the notification function's context
505 // Init the communicate buffer. The buffer data size is:
506 // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE.
508 InitCommunicateBuffer (NULL
, 0, SMM_VARIABLE_FUNCTION_READY_TO_BOOT
);
513 SendCommunicateBuffer (0);
518 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.
520 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
521 It convers pointer to new virtual address.
523 @param[in] Event Event whose notification function is being invoked.
524 @param[in] Context Pointer to the notification function's context.
529 VariableAddressChangeEvent (
534 EfiConvertPointer (0x0, (VOID
**) &mVariableBuffer
);
535 EfiConvertPointer (0x0, (VOID
**) &mSmmCommunication
);
540 Initialize variable service and install Variable Architectural protocol.
542 @param[in] Event Event whose notification function is being invoked.
543 @param[in] Context Pointer to the notification function's context.
555 Status
= gBS
->LocateProtocol (&gEfiSmmVariableProtocolGuid
, NULL
, (VOID
**)&mSmmVariable
);
556 if (EFI_ERROR (Status
)) {
560 Status
= gBS
->LocateProtocol (&gEfiSmmCommunicationProtocolGuid
, NULL
, (VOID
**) &mSmmCommunication
);
561 ASSERT_EFI_ERROR (Status
);
564 // Allocate memory for variable store.
566 mVariableBufferSize
= SMM_COMMUNICATE_HEADER_SIZE
+ SMM_VARIABLE_COMMUNICATE_HEADER_SIZE
;
567 mVariableBufferSize
+= MAX (PcdGet32 (PcdMaxVariableSize
), PcdGet32 (PcdMaxHardwareErrorVariableSize
));
568 mVariableBuffer
= AllocateRuntimePool (mVariableBufferSize
);
569 ASSERT (mVariableBuffer
!= NULL
);
572 // Save the buffer physical address used for SMM conmunication.
574 mVariableBufferPhysical
= mVariableBuffer
;
576 gRT
->GetVariable
= RuntimeServiceGetVariable
;
577 gRT
->GetNextVariableName
= RuntimeServiceGetNextVariableName
;
578 gRT
->SetVariable
= RuntimeServiceSetVariable
;
579 gRT
->QueryVariableInfo
= RuntimeServiceQueryVariableInfo
;
582 // Install the Variable Architectural Protocol on a new handle.
584 Status
= gBS
->InstallProtocolInterface (
586 &gEfiVariableArchProtocolGuid
,
587 EFI_NATIVE_INTERFACE
,
590 ASSERT_EFI_ERROR (Status
);
595 SMM Non-Volatile variable write service is ready notify event handler.
597 @param[in] Event Event whose notification function is being invoked.
598 @param[in] Context Pointer to the notification function's context.
603 SmmVariableWriteReady (
612 // Check whether the protocol is installed or not.
614 Status
= gBS
->LocateProtocol (&gSmmVariableWriteGuid
, NULL
, (VOID
**) &ProtocolOps
);
615 if (EFI_ERROR (Status
)) {
619 Status
= gBS
->InstallProtocolInterface (
621 &gEfiVariableWriteArchProtocolGuid
,
622 EFI_NATIVE_INTERFACE
,
625 ASSERT_EFI_ERROR (Status
);
630 Variable Driver main entry point. The Variable driver places the 4 EFI
631 runtime services in the EFI System Table and installs arch protocols
632 for variable read and write services being available. It also registers
633 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
635 @param[in] ImageHandle The firmware allocated handle for the EFI image.
636 @param[in] SystemTable A pointer to the EFI System Table.
638 @retval EFI_SUCCESS Variable service successfully initialized.
643 VariableSmmRuntimeInitialize (
644 IN EFI_HANDLE ImageHandle
,
645 IN EFI_SYSTEM_TABLE
*SystemTable
648 VOID
*SmmVariableRegistration
;
649 VOID
*SmmVariableWriteRegistration
;
650 EFI_EVENT OnReadyToBootEvent
;
651 EFI_EVENT ExitBootServiceEvent
;
653 EfiInitializeLock (&mVariableServicesLock
, TPL_NOTIFY
);
656 // Smm variable service is ready
658 EfiCreateProtocolNotifyEvent (
659 &gEfiSmmVariableProtocolGuid
,
663 &SmmVariableRegistration
667 // Smm Non-Volatile variable write service is ready
669 EfiCreateProtocolNotifyEvent (
670 &gSmmVariableWriteGuid
,
672 SmmVariableWriteReady
,
674 &SmmVariableWriteRegistration
678 // Register the event to reclaim variable for OS usage.
680 EfiCreateEventReadyToBootEx (
688 // Register the event to inform SMM variable that it is at runtime.
695 &gEfiEventExitBootServicesGuid
,
696 &ExitBootServiceEvent
700 // Register the event to convert the pointer for runtime.
705 VariableAddressChangeEvent
,
707 &gEfiEventVirtualAddressChangeGuid
,
708 &mVirtualAddressChangeEvent