3 The sample implementation for SMM variable protocol. And this driver
4 implements an SMI handler to communicate with the DXE runtime driver
5 to provide variable services.
7 Copyright (c) 2010 - 2011, 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.
17 #include <Protocol/SmmVariable.h>
18 #include <Protocol/SmmFirmwareVolumeBlock.h>
19 #include <Protocol/SmmFaultTolerantWrite.h>
20 #include <Library/SmmServicesTableLib.h>
22 #include <Guid/VariableFormat.h>
23 #include <Guid/SmmVariableCommon.h>
26 extern VARIABLE_INFO_ENTRY
*gVariableInfo
;
27 EFI_HANDLE mSmmVariableHandle
= NULL
;
28 EFI_HANDLE mVariableHandle
= NULL
;
29 BOOLEAN mAtRuntime
= FALSE
;
30 EFI_GUID mZeroGuid
= {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
32 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable
= {
33 VariableServiceGetVariable
,
34 VariableServiceGetNextVariableName
,
35 VariableServiceSetVariable
,
36 VariableServiceQueryVariableInfo
41 Return TRUE if ExitBootServices () has been called.
43 @retval TRUE If ExitBootServices () has been called.
54 Initializes a basic mutual exclusion lock.
56 This function initializes a basic mutual exclusion lock to the released state
57 and returns the lock. Each lock provides mutual exclusion access at its task
58 priority level. Since there is no preemption or multiprocessor support in EFI,
59 acquiring the lock only consists of raising to the locks TPL.
60 If Lock is NULL, then ASSERT().
61 If Priority is not a valid TPL value, then ASSERT().
63 @param Lock A pointer to the lock data structure to initialize.
64 @param Priority EFI TPL is associated with the lock.
71 IN OUT EFI_LOCK
*Lock
,
79 Acquires lock only at boot time. Simply returns at runtime.
81 This is a temperary function that will be removed when
82 EfiAcquireLock() in UefiLib can handle the call in UEFI
83 Runtimer driver in RT phase.
84 It calls EfiAcquireLock() at boot time, and simply returns
87 @param Lock A pointer to the lock to acquire.
91 AcquireLockOnlyAtBootTime (
100 Releases lock only at boot time. Simply returns at runtime.
102 This is a temperary function which will be removed when
103 EfiReleaseLock() in UefiLib can handle the call in UEFI
104 Runtimer driver in RT phase.
105 It calls EfiReleaseLock() at boot time and simply returns
108 @param Lock A pointer to the lock to release.
112 ReleaseLockOnlyAtBootTime (
120 Retrive the SMM Fault Tolerent Write protocol interface.
122 @param[out] FtwProtocol The interface of SMM Ftw protocol
124 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
125 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
126 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
131 OUT VOID
**FtwProtocol
137 // Locate Smm Fault Tolerent Write protocol
139 Status
= gSmst
->SmmLocateProtocol (
140 &gEfiSmmFaultTolerantWriteProtocolGuid
,
149 Retrive the SMM FVB protocol interface by HANDLE.
151 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
152 reading, writing, and erasing the target block.
153 @param[out] FvBlock The interface of SMM FVB protocol
155 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
156 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
157 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
162 IN EFI_HANDLE FvBlockHandle
,
163 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
**FvBlock
167 // To get the SMM FVB protocol interface on the handle
169 return gSmst
->SmmHandleProtocol (
171 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
178 Function returns an array of handles that support the SMM FVB protocol
179 in a buffer allocated from pool.
181 @param[out] NumberHandles The number of handles returned in Buffer.
182 @param[out] Buffer A pointer to the buffer to return the requested
183 array of handles that support SMM FVB protocol.
185 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
186 handles in Buffer was returned in NumberHandles.
187 @retval EFI_NOT_FOUND No SMM FVB handle was found.
188 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
189 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
193 GetFvbCountAndBuffer (
194 OUT UINTN
*NumberHandles
,
195 OUT EFI_HANDLE
**Buffer
201 if ((NumberHandles
== NULL
) || (Buffer
== NULL
)) {
202 return EFI_INVALID_PARAMETER
;
208 Status
= gSmst
->SmmLocateHandle (
210 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
215 if (EFI_ERROR(Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
216 return EFI_NOT_FOUND
;
219 *Buffer
= AllocatePool (BufferSize
);
220 if (*Buffer
== NULL
) {
221 return EFI_OUT_OF_RESOURCES
;
224 Status
= gSmst
->SmmLocateHandle (
226 &gEfiSmmFirmwareVolumeBlockProtocolGuid
,
232 *NumberHandles
= BufferSize
/ sizeof(EFI_HANDLE
);
233 if (EFI_ERROR(Status
)) {
242 Get the variable statistics information from the information buffer pointed by gVariableInfo.
244 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
245 On input, point to the variable information returned last time. if
246 InfoEntry->VendorGuid is zero, return the first information.
247 On output, point to the next variable information.
248 @param[in, out] InfoSize On input, the size of the variable information buffer.
249 On output, the returned variable information size.
251 @retval EFI_SUCCESS The variable information is found and returned successfully.
252 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
253 PcdVariableCollectStatistics should be set TRUE to support it.
254 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
258 SmmVariableGetStatistics (
259 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
260 IN OUT UINTN
*InfoSize
263 VARIABLE_INFO_ENTRY
*VariableInfo
;
265 UINTN StatisticsInfoSize
;
268 ASSERT (InfoEntry
!= NULL
);
269 VariableInfo
= gVariableInfo
;
270 if (VariableInfo
== NULL
) {
271 return EFI_UNSUPPORTED
;
274 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
275 if (*InfoSize
< sizeof (VARIABLE_INFO_ENTRY
)) {
276 *InfoSize
= StatisticsInfoSize
;
277 return EFI_BUFFER_TOO_SMALL
;
279 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
281 if (CompareGuid (&InfoEntry
->VendorGuid
, &mZeroGuid
)) {
283 // Return the first variable info
285 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
286 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
287 *InfoSize
= StatisticsInfoSize
;
292 // Get the next variable info
294 while (VariableInfo
!= NULL
) {
295 if (CompareGuid (&VariableInfo
->VendorGuid
, &InfoEntry
->VendorGuid
)) {
296 NameLength
= StrSize (VariableInfo
->Name
);
297 if (NameLength
== StrSize (InfoName
)) {
298 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
300 // Find the match one
302 VariableInfo
= VariableInfo
->Next
;
307 VariableInfo
= VariableInfo
->Next
;
310 if (VariableInfo
== NULL
) {
316 // Output the new variable info
318 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
319 if (*InfoSize
< StatisticsInfoSize
) {
320 *InfoSize
= StatisticsInfoSize
;
321 return EFI_BUFFER_TOO_SMALL
;
324 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
325 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
326 *InfoSize
= StatisticsInfoSize
;
333 Communication service SMI Handler entry.
335 This SMI handler provides services for the variable wrapper driver.
337 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
338 @param[in] RegisterContext Points to an optional handler context which was specified when the
339 handler was registered.
340 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
341 be conveyed from a non-SMM environment into an SMM environment.
342 @param[in, out] CommBufferSize The size of the CommBuffer.
344 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
345 should still be called.
346 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
348 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
350 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
355 IN EFI_HANDLE DispatchHandle
,
356 IN CONST VOID
*RegisterContext
,
357 IN OUT VOID
*CommBuffer
,
358 IN OUT UINTN
*CommBufferSize
362 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
363 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
364 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
365 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
366 VARIABLE_INFO_ENTRY
*VariableInfo
;
369 ASSERT (CommBuffer
!= NULL
);
371 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
372 switch (SmmVariableFunctionHeader
->Function
) {
373 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
374 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
375 Status
= VariableServiceGetVariable (
376 SmmVariableHeader
->Name
,
377 &SmmVariableHeader
->Guid
,
378 &SmmVariableHeader
->Attributes
,
379 &SmmVariableHeader
->DataSize
,
380 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
384 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
385 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) SmmVariableFunctionHeader
->Data
;
386 Status
= VariableServiceGetNextVariableName (
387 &GetNextVariableName
->NameSize
,
388 GetNextVariableName
->Name
,
389 &GetNextVariableName
->Guid
393 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
394 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
395 Status
= VariableServiceSetVariable (
396 SmmVariableHeader
->Name
,
397 &SmmVariableHeader
->Guid
,
398 SmmVariableHeader
->Attributes
,
399 SmmVariableHeader
->DataSize
,
400 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
404 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
405 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
406 Status
= VariableServiceQueryVariableInfo (
407 QueryVariableInfo
->Attributes
,
408 &QueryVariableInfo
->MaximumVariableStorageSize
,
409 &QueryVariableInfo
->RemainingVariableStorageSize
,
410 &QueryVariableInfo
->MaximumVariableSize
414 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
416 Status
= EFI_SUCCESS
;
419 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
421 Status
= EFI_SUCCESS
;
424 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
425 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
426 InfoSize
= *CommBufferSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER
, Data
);
427 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
428 *CommBufferSize
= InfoSize
+ OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER
, Data
);
433 Status
= EFI_UNSUPPORTED
;
436 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
443 SMM Fault Tolerant Write protocol notification event handler.
445 Non-Volatile variable write may needs FTW protocol to reclaim when
448 @param Protocol Points to the protocol's unique identifier
449 @param Interface Points to the interface instance
450 @param Handle The handle on which the interface was installed
452 @retval EFI_SUCCESS SmmEventCallback runs successfully
453 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
458 SmmFtwNotificationEvent (
459 IN CONST EFI_GUID
*Protocol
,
465 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
466 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
467 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
469 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
474 // Ensure SMM FTW protocol is installed.
476 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
477 if (EFI_ERROR (Status
)) {
482 // Find the proper FVB protocol for variable.
484 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
485 if (NvStorageVariableBase
== 0) {
486 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
488 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
489 if (EFI_ERROR (Status
)) {
490 return EFI_NOT_FOUND
;
493 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
495 Status
= VariableWriteServiceInitialize ();
496 ASSERT_EFI_ERROR (Status
);
499 // Notify the variable wrapper driver the variable write service is ready
501 Status
= gBS
->InstallProtocolInterface (
503 &gSmmVariableWriteGuid
,
504 EFI_NATIVE_INTERFACE
,
507 ASSERT_EFI_ERROR (Status
);
514 Variable Driver main entry point. The Variable driver places the 4 EFI
515 runtime services in the EFI System Table and installs arch protocols
516 for variable read and write services being available. It also registers
517 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
519 @param[in] ImageHandle The firmware allocated handle for the EFI image.
520 @param[in] SystemTable A pointer to the EFI System Table.
522 @retval EFI_SUCCESS Variable service successfully initialized.
527 VariableServiceInitialize (
528 IN EFI_HANDLE ImageHandle
,
529 IN EFI_SYSTEM_TABLE
*SystemTable
533 EFI_HANDLE VariableHandle
;
534 VOID
*SmmFtwRegistration
;
537 // Variable initialize.
539 Status
= VariableCommonInitialize ();
540 ASSERT_EFI_ERROR (Status
);
543 // Install the Smm Variable Protocol on a new handle.
545 VariableHandle
= NULL
;
546 Status
= gSmst
->SmmInstallProtocolInterface (
548 &gEfiSmmVariableProtocolGuid
,
549 EFI_NATIVE_INTERFACE
,
552 ASSERT_EFI_ERROR (Status
);
555 /// Register SMM variable SMI handler
557 VariableHandle
= NULL
;
558 Status
= gSmst
->SmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
559 ASSERT_EFI_ERROR (Status
);
562 // Notify the variable wrapper driver the variable service is ready
564 Status
= SystemTable
->BootServices
->InstallProtocolInterface (
566 &gEfiSmmVariableProtocolGuid
,
567 EFI_NATIVE_INTERFACE
,
570 ASSERT_EFI_ERROR (Status
);
573 // Register FtwNotificationEvent () notify function.
575 Status
= gSmst
->SmmRegisterProtocolNotify (
576 &gEfiSmmFaultTolerantWriteProtocolGuid
,
577 SmmFtwNotificationEvent
,
580 ASSERT_EFI_ERROR (Status
);
582 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);