2 The sample implementation for SMM variable protocol. And this driver
3 implements an SMI handler to communicate with the DXE runtime driver
4 to provide variable services.
6 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 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/AuthenticatedVariableFormat.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.
255 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
259 SmmVariableGetStatistics (
260 IN OUT VARIABLE_INFO_ENTRY
*InfoEntry
,
261 IN OUT UINTN
*InfoSize
264 VARIABLE_INFO_ENTRY
*VariableInfo
;
266 UINTN StatisticsInfoSize
;
269 if (InfoEntry
== NULL
) {
270 return EFI_INVALID_PARAMETER
;
273 VariableInfo
= gVariableInfo
;
274 if (VariableInfo
== NULL
) {
275 return EFI_UNSUPPORTED
;
278 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
279 if (*InfoSize
< sizeof (VARIABLE_INFO_ENTRY
)) {
280 *InfoSize
= StatisticsInfoSize
;
281 return EFI_BUFFER_TOO_SMALL
;
283 InfoName
= (CHAR16
*)(InfoEntry
+ 1);
285 if (CompareGuid (&InfoEntry
->VendorGuid
, &mZeroGuid
)) {
287 // Return the first variable info
289 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
290 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
291 *InfoSize
= StatisticsInfoSize
;
296 // Get the next variable info
298 while (VariableInfo
!= NULL
) {
299 if (CompareGuid (&VariableInfo
->VendorGuid
, &InfoEntry
->VendorGuid
)) {
300 NameLength
= StrSize (VariableInfo
->Name
);
301 if (NameLength
== StrSize (InfoName
)) {
302 if (CompareMem (VariableInfo
->Name
, InfoName
, NameLength
) == 0) {
304 // Find the match one
306 VariableInfo
= VariableInfo
->Next
;
311 VariableInfo
= VariableInfo
->Next
;
314 if (VariableInfo
== NULL
) {
320 // Output the new variable info
322 StatisticsInfoSize
= sizeof (VARIABLE_INFO_ENTRY
) + StrSize (VariableInfo
->Name
);
323 if (*InfoSize
< StatisticsInfoSize
) {
324 *InfoSize
= StatisticsInfoSize
;
325 return EFI_BUFFER_TOO_SMALL
;
328 CopyMem (InfoEntry
, VariableInfo
, sizeof (VARIABLE_INFO_ENTRY
));
329 CopyMem (InfoName
, VariableInfo
->Name
, StrSize (VariableInfo
->Name
));
330 *InfoSize
= StatisticsInfoSize
;
337 Communication service SMI Handler entry.
339 This SMI handler provides services for the variable wrapper driver.
341 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
342 @param[in] RegisterContext Points to an optional handler context which was specified when the
343 handler was registered.
344 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
345 be conveyed from a non-SMM environment into an SMM environment.
346 @param[in, out] CommBufferSize The size of the CommBuffer.
348 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
349 should still be called.
350 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
352 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
354 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
355 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
361 IN EFI_HANDLE DispatchHandle
,
362 IN CONST VOID
*RegisterContext
,
363 IN OUT VOID
*CommBuffer
,
364 IN OUT UINTN
*CommBufferSize
368 SMM_VARIABLE_COMMUNICATE_HEADER
*SmmVariableFunctionHeader
;
369 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*SmmVariableHeader
;
370 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*GetNextVariableName
;
371 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*QueryVariableInfo
;
372 VARIABLE_INFO_ENTRY
*VariableInfo
;
375 if (CommBuffer
== NULL
) {
376 return EFI_INVALID_PARAMETER
;
379 SmmVariableFunctionHeader
= (SMM_VARIABLE_COMMUNICATE_HEADER
*)CommBuffer
;
380 switch (SmmVariableFunctionHeader
->Function
) {
381 case SMM_VARIABLE_FUNCTION_GET_VARIABLE
:
382 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
383 Status
= VariableServiceGetVariable (
384 SmmVariableHeader
->Name
,
385 &SmmVariableHeader
->Guid
,
386 &SmmVariableHeader
->Attributes
,
387 &SmmVariableHeader
->DataSize
,
388 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
392 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME
:
393 GetNextVariableName
= (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME
*) SmmVariableFunctionHeader
->Data
;
394 Status
= VariableServiceGetNextVariableName (
395 &GetNextVariableName
->NameSize
,
396 GetNextVariableName
->Name
,
397 &GetNextVariableName
->Guid
401 case SMM_VARIABLE_FUNCTION_SET_VARIABLE
:
402 SmmVariableHeader
= (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE
*) SmmVariableFunctionHeader
->Data
;
403 Status
= VariableServiceSetVariable (
404 SmmVariableHeader
->Name
,
405 &SmmVariableHeader
->Guid
,
406 SmmVariableHeader
->Attributes
,
407 SmmVariableHeader
->DataSize
,
408 (UINT8
*)SmmVariableHeader
->Name
+ SmmVariableHeader
->NameSize
412 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO
:
413 QueryVariableInfo
= (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO
*) SmmVariableFunctionHeader
->Data
;
414 Status
= VariableServiceQueryVariableInfo (
415 QueryVariableInfo
->Attributes
,
416 &QueryVariableInfo
->MaximumVariableStorageSize
,
417 &QueryVariableInfo
->RemainingVariableStorageSize
,
418 &QueryVariableInfo
->MaximumVariableSize
422 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT
:
424 Status
= EFI_SUCCESS
;
427 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE
:
429 Status
= EFI_SUCCESS
;
432 case SMM_VARIABLE_FUNCTION_GET_STATISTICS
:
433 VariableInfo
= (VARIABLE_INFO_ENTRY
*) SmmVariableFunctionHeader
->Data
;
434 InfoSize
= *CommBufferSize
- OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER
, Data
);
435 Status
= SmmVariableGetStatistics (VariableInfo
, &InfoSize
);
436 *CommBufferSize
= InfoSize
+ OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER
, Data
);
441 Status
= EFI_UNSUPPORTED
;
444 SmmVariableFunctionHeader
->ReturnStatus
= Status
;
451 SMM Fault Tolerant Write protocol notification event handler.
453 Non-Volatile variable write may needs FTW protocol to reclaim when
456 @param Protocol Points to the protocol's unique identifier
457 @param Interface Points to the interface instance
458 @param Handle The handle on which the interface was installed
460 @retval EFI_SUCCESS SmmEventCallback runs successfully
461 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
466 SmmFtwNotificationEvent (
467 IN CONST EFI_GUID
*Protocol
,
473 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL
*FvbProtocol
;
474 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL
*FtwProtocol
;
475 EFI_PHYSICAL_ADDRESS NvStorageVariableBase
;
477 if (mVariableModuleGlobal
->FvbInstance
!= NULL
) {
482 // Ensure SMM FTW protocol is installed.
484 Status
= GetFtwProtocol ((VOID
**)&FtwProtocol
);
485 if (EFI_ERROR (Status
)) {
490 // Find the proper FVB protocol for variable.
492 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet64 (PcdFlashNvStorageVariableBase64
);
493 if (NvStorageVariableBase
== 0) {
494 NvStorageVariableBase
= (EFI_PHYSICAL_ADDRESS
) PcdGet32 (PcdFlashNvStorageVariableBase
);
496 Status
= GetFvbInfoByAddress (NvStorageVariableBase
, NULL
, &FvbProtocol
);
497 if (EFI_ERROR (Status
)) {
498 return EFI_NOT_FOUND
;
501 mVariableModuleGlobal
->FvbInstance
= FvbProtocol
;
503 Status
= VariableWriteServiceInitialize ();
504 ASSERT_EFI_ERROR (Status
);
507 // Notify the variable wrapper driver the variable write service is ready
509 Status
= gBS
->InstallProtocolInterface (
511 &gSmmVariableWriteGuid
,
512 EFI_NATIVE_INTERFACE
,
515 ASSERT_EFI_ERROR (Status
);
522 Variable Driver main entry point. The Variable driver places the 4 EFI
523 runtime services in the EFI System Table and installs arch protocols
524 for variable read and write services being available. It also registers
525 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
527 @param[in] ImageHandle The firmware allocated handle for the EFI image.
528 @param[in] SystemTable A pointer to the EFI System Table.
530 @retval EFI_SUCCESS Variable service successfully initialized.
535 VariableServiceInitialize (
536 IN EFI_HANDLE ImageHandle
,
537 IN EFI_SYSTEM_TABLE
*SystemTable
541 EFI_HANDLE VariableHandle
;
542 VOID
*SmmFtwRegistration
;
545 // Variable initialize.
547 Status
= VariableCommonInitialize ();
548 ASSERT_EFI_ERROR (Status
);
551 // Install the Smm Variable Protocol on a new handle.
553 VariableHandle
= NULL
;
554 Status
= gSmst
->SmmInstallProtocolInterface (
556 &gEfiSmmVariableProtocolGuid
,
557 EFI_NATIVE_INTERFACE
,
560 ASSERT_EFI_ERROR (Status
);
563 /// Register SMM variable SMI handler
565 VariableHandle
= NULL
;
566 Status
= gSmst
->SmiHandlerRegister (SmmVariableHandler
, &gEfiSmmVariableProtocolGuid
, &VariableHandle
);
567 ASSERT_EFI_ERROR (Status
);
570 // Notify the variable wrapper driver the variable service is ready
572 Status
= SystemTable
->BootServices
->InstallProtocolInterface (
574 &gEfiSmmVariableProtocolGuid
,
575 EFI_NATIVE_INTERFACE
,
578 ASSERT_EFI_ERROR (Status
);
581 // Register FtwNotificationEvent () notify function.
583 Status
= gSmst
->SmmRegisterProtocolNotify (
584 &gEfiSmmFaultTolerantWriteProtocolGuid
,
585 SmmFtwNotificationEvent
,
588 ASSERT_EFI_ERROR (Status
);
590 SmmFtwNotificationEvent (NULL
, NULL
, NULL
);