3 Copyright (c) 2004 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Light weight lib to support Tiano drivers.
23 #include "EfiRuntimeLib.h"
24 #include EFI_PROTOCOL_DEFINITION (CpuIo)
25 #include EFI_PROTOCOL_DEFINITION (FirmwareVolumeBlock)
26 #include EFI_GUID_DEFINITION (StatusCodeCallerId)
27 #include EFI_ARCH_PROTOCOL_DEFINITION (StatusCode)
30 // Driver Lib Module Globals
32 static EFI_RUNTIME_SERVICES
*mRT
;
33 static EFI_EVENT mRuntimeNotifyEvent
= NULL
;
34 static EFI_EVENT mEfiVirtualNotifyEvent
= NULL
;
35 static BOOLEAN mRuntimeLibInitialized
= FALSE
;
36 static BOOLEAN mEfiGoneVirtual
= FALSE
;
39 // Runtime Global, but you should use the Lib functions
41 EFI_CPU_IO_PROTOCOL
*gCpuIo
;
42 BOOLEAN mEfiAtRuntime
= FALSE
;
45 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
46 static EFI_STATUS_CODE_PROTOCOL
*gStatusCode
= NULL
;
51 IN UINTN DebugDisposition
,
58 Determines the new virtual address that is to be used on subsequent memory accesses.
62 DebugDisposition - Supplies type information for the pointer being converted.
63 Address - A pointer to a pointer that is to be fixed to be the value needed
64 for the new virtual address mappings being applied.
72 return mRT
->ConvertPointer (DebugDisposition
, Address
);
76 EfiConvertInternalPointer (
83 Call EfiConvertPointer() to convert internal pointer.
87 Address - A pointer to a pointer that is to be fixed to be the value needed
88 for the new virtual address mappings being applied.
96 return EfiConvertPointer (EFI_INTERNAL_POINTER
, Address
);
101 EfiRuntimeLibFvbVirtualNotifyEvent (
109 Convert all pointers in mFvbEntry after ExitBootServices.
113 Event - The Event that is being processed
115 Context - Event Context
124 if (mFvbEntry
!= NULL
) {
125 for (Index
= 0; Index
< MAX_FVB_COUNT
; Index
++) {
126 if (NULL
!= mFvbEntry
[Index
].Fvb
) {
127 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->GetBlockSize
);
128 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->GetPhysicalAddress
);
129 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->GetVolumeAttributes
);
130 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->SetVolumeAttributes
);
131 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->Read
);
132 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->Write
);
133 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->EraseBlocks
);
134 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
);
137 if (NULL
!= mFvbEntry
[Index
].FvbExtension
) {
138 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].FvbExtension
->EraseFvbCustomBlock
);
139 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].FvbExtension
);
143 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
);
149 RuntimeDriverExitBootServices (
157 Set AtRuntime flag as TRUE after ExitBootServices
161 Event - The Event that is being processed
163 Context - Event Context
171 mEfiAtRuntime
= TRUE
;
174 extern BOOLEAN gEfiFvbInitialized
;
178 EfiRuntimeLibVirtualNotifyEvent (
186 Fixup internal data so that EFI can be call in virtual mode.
187 Call the passed in Child Notify event and convert any pointers in
192 Event - The Event that is being processed
194 Context - Event Context
202 EFI_EVENT_NOTIFY ChildNotifyEventHandler
;
204 if (Context
!= NULL
) {
205 ChildNotifyEventHandler
= (EFI_EVENT_NOTIFY
) (UINTN
) Context
;
206 ChildNotifyEventHandler (Event
, NULL
);
209 if (gEfiFvbInitialized
) {
210 EfiRuntimeLibFvbVirtualNotifyEvent (Event
, Context
);
213 // Update global for Runtime Services Table and IO
215 EfiConvertInternalPointer ((VOID
**) &gCpuIo
);
216 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
217 if (gStatusCode
!= NULL
) {
218 EfiConvertInternalPointer ((VOID
**) &gStatusCode
->ReportStatusCode
);
219 EfiConvertInternalPointer ((VOID
**) &gStatusCode
);
222 EfiConvertInternalPointer ((VOID
**) &mRT
);
225 // Clear out BootService globals
229 mEfiGoneVirtual
= TRUE
;
233 EfiInitializeRuntimeDriverLib (
234 IN EFI_HANDLE ImageHandle
,
235 IN EFI_SYSTEM_TABLE
*SystemTable
,
236 IN EFI_EVENT_NOTIFY GoVirtualChildEvent
242 Intialize runtime Driver Lib if it has not yet been initialized.
246 ImageHandle - The firmware allocated handle for the EFI image.
248 SystemTable - A pointer to the EFI System Table.
250 GoVirtualChildEvent - Caller can register a virtual notification event.
254 EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
260 if (mRuntimeLibInitialized
) {
261 return EFI_ALREADY_STARTED
;
264 mRuntimeLibInitialized
= TRUE
;
267 ASSERT (gST
!= NULL
);
269 gBS
= SystemTable
->BootServices
;
270 ASSERT (gBS
!= NULL
);
271 mRT
= SystemTable
->RuntimeServices
;
272 ASSERT (mRT
!= NULL
);
274 Status
= EfiLibGetSystemConfigurationTable (&gEfiDxeServicesTableGuid
, (VOID
**) &gDS
);
275 ASSERT_EFI_ERROR (Status
);
277 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
278 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&gStatusCode
);
279 if (EFI_ERROR (Status
)) {
284 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &gCpuIo
);
285 if (EFI_ERROR (Status
)) {
290 // Register our ExitBootServices () notify function
292 Status
= gBS
->CreateEvent (
293 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
295 RuntimeDriverExitBootServices
,
299 ASSERT_EFI_ERROR (Status
);
302 // Register SetVirtualAddressMap () notify function
304 Status
= gBS
->CreateEvent (
305 EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
307 EfiRuntimeLibVirtualNotifyEvent
,
308 (VOID
*) (UINTN
) GoVirtualChildEvent
,
309 &mEfiVirtualNotifyEvent
311 ASSERT_EFI_ERROR (Status
);
317 EfiShutdownRuntimeDriverLib (
324 This routine will free some resources which have been allocated in
325 EfiInitializeRuntimeDriverLib(). If a runtime driver exits with an error,
326 it must call this routine to free the allocated resource before the exiting.
334 EFI_SUCCESS - Shotdown the Runtime Driver Lib successfully
335 EFI_UNSUPPORTED - Runtime Driver lib was not initialized at all
341 if (!mRuntimeLibInitialized
) {
343 // You must call EfiInitializeRuntimeDriverLib() first
345 return EFI_UNSUPPORTED
;
348 mRuntimeLibInitialized
= FALSE
;
351 // Close our ExitBootServices () notify function
353 if (mRuntimeNotifyEvent
!= NULL
) {
354 Status
= gBS
->CloseEvent (mRuntimeNotifyEvent
);
355 ASSERT_EFI_ERROR (Status
);
359 // Close SetVirtualAddressMap () notify function
361 if (mEfiVirtualNotifyEvent
!= NULL
) {
362 Status
= gBS
->CloseEvent (mEfiVirtualNotifyEvent
);
363 ASSERT_EFI_ERROR (Status
);
370 EfiInitializeSmmDriverLib (
371 IN EFI_HANDLE ImageHandle
,
372 IN EFI_SYSTEM_TABLE
*SystemTable
378 Intialize runtime Driver Lib if it has not yet been initialized.
382 ImageHandle - The firmware allocated handle for the EFI image.
384 SystemTable - A pointer to the EFI System Table.
388 EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
394 if (mRuntimeLibInitialized
) {
395 return EFI_ALREADY_STARTED
;
398 mRuntimeLibInitialized
= TRUE
;
401 ASSERT (gST
!= NULL
);
403 gBS
= SystemTable
->BootServices
;
404 ASSERT (gBS
!= NULL
);
405 mRT
= SystemTable
->RuntimeServices
;
406 ASSERT (mRT
!= NULL
);
408 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
409 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&gStatusCode
);
410 if (EFI_ERROR (Status
)) {
415 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &gCpuIo
);
416 if (EFI_ERROR (Status
)) {
430 Return TRUE if ExitBootServices () has been called
436 TRUE - If ExitBootServices () has been called
440 return mEfiAtRuntime
;
450 Return TRUE if SetVirtualAddressMap () has been called
456 TRUE - If SetVirtualAddressMap () has been called
460 return mEfiGoneVirtual
;
463 // The following functions hide the mRT local global from the call to
464 // runtime service in the EFI system table.
469 OUT EFI_TIME_CAPABILITIES
*Capabilities
475 Returns the current time and date information, and the time-keeping
476 capabilities of the hardware platform.
480 Time - A pointer to storage to receive a snapshot of the current time.
481 Capabilities - An optional pointer to a buffer to receive the real time clock device's
490 return mRT
->GetTime (Time
, Capabilities
);
501 Sets the current local time and date information.
505 Time - A pointer to the current time.
513 return mRT
->SetTime (Time
);
518 OUT BOOLEAN
*Enabled
,
519 OUT BOOLEAN
*Pending
,
526 Returns the current wakeup alarm clock setting.
530 Enabled - Indicates if the alarm is currently enabled or disabled.
531 Pending - Indicates if the alarm signal is pending and requires acknowledgement.
532 Time - The current alarm setting.
540 return mRT
->GetWakeupTime (Enabled
, Pending
, Time
);
552 Sets the system wakeup alarm clock time.
556 Enable - Enable or disable the wakeup alarm.
557 Time - If Enable is TRUE, the time to set the wakeup alarm for.
558 If Enable is FALSE, then this parameter is optional, and may be NULL.
566 return mRT
->SetWakeupTime (Enable
, Time
);
571 IN CHAR16
*VariableName
,
572 IN EFI_GUID
* VendorGuid
,
573 OUT UINT32
*Attributes OPTIONAL
,
574 IN OUT UINTN
*DataSize
,
581 Returns the value of a variable.
585 VariableName - A Null-terminated Unicode string that is the name of the
587 VendorGuid - A unique identifier for the vendor.
588 Attributes - If not NULL, a pointer to the memory location to return the
589 attributes bitmask for the variable.
590 DataSize - On input, the size in bytes of the return Data buffer.
591 On output the size of data returned in Data.
592 Data - The buffer to return the contents of the variable.
600 return mRT
->GetVariable (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
604 EfiGetNextVariableName (
605 IN OUT UINTN
*VariableNameSize
,
606 IN OUT CHAR16
*VariableName
,
607 IN OUT EFI_GUID
*VendorGuid
613 Enumerates the current variable names.
617 VariableNameSize - The size of the VariableName buffer.
618 VariableName - On input, supplies the last VariableName that was returned
619 by GetNextVariableName().
620 On output, returns the Nullterminated Unicode string of the
622 VendorGuid - On input, supplies the last VendorGuid that was returned by
623 GetNextVariableName().
624 On output, returns the VendorGuid of the current variable.
632 return mRT
->GetNextVariableName (VariableNameSize
, VariableName
, VendorGuid
);
637 IN CHAR16
*VariableName
,
638 IN EFI_GUID
*VendorGuid
,
639 IN UINT32 Attributes
,
647 Sets the value of a variable.
651 VariableName - A Null-terminated Unicode string that is the name of the
653 VendorGuid - A unique identifier for the vendor.
654 Attributes - Attributes bitmask to set for the variable.
655 DataSize - The size in bytes of the Data buffer.
656 Data - The contents for the variable.
664 return mRT
->SetVariable (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
668 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
671 EfiQueryVariableInfo (
672 IN UINT32 Attributes
,
673 OUT UINT64
*MaximumVariableStorageSize
,
674 OUT UINT64
*RemainingVariableStorageSize
,
675 OUT UINT64
*MaximumVariableSize
682 This code returns information about the EFI variables.
686 Attributes Attributes bitmask to specify the type of variables
687 on which to return information.
688 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
689 for the EFI variables associated with the attributes specified.
690 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
691 for the EFI variables associated with the attributes specified.
692 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
693 associated with the attributes specified.
701 return mRT
->QueryVariableInfo (Attributes
, MaximumVariableStorageSize
, RemainingVariableStorageSize
, MaximumVariableSize
);
707 EfiGetNextHighMonotonicCount (
708 OUT UINT32
*HighCount
714 Returns the next high 32 bits of the platform's monotonic counter.
718 HighCount - Pointer to returned value.
726 return mRT
->GetNextHighMonotonicCount (HighCount
);
731 IN EFI_RESET_TYPE ResetType
,
732 IN EFI_STATUS ResetStatus
,
740 Resets the entire platform.
744 ResetType - The type of reset to perform.
745 ResetStatus - The status code for the reset.
746 DataSize - The size, in bytes, of ResetData.
747 ResetData - A data buffer that includes a Null-terminated Unicode string, optionally
748 followed by additional binary data.
756 mRT
->ResetSystem (ResetType
, ResetStatus
, DataSize
, ResetData
);
760 EfiReportStatusCode (
761 IN EFI_STATUS_CODE_TYPE CodeType
,
762 IN EFI_STATUS_CODE_VALUE Value
,
764 IN EFI_GUID
* CallerId
,
765 IN EFI_STATUS_CODE_DATA
* Data OPTIONAL
775 CodeType - Type of Status Code.
777 Value - Value to output for Status Code.
779 Instance - Instance Number of this status code.
781 CallerId - ID of the caller of this status code.
783 Data - Optional data associated with this status code.
793 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
794 if (gStatusCode
== NULL
) {
795 if (EfiAtRuntime ()) {
796 return EFI_UNSUPPORTED
;
798 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&gStatusCode
);
799 if (EFI_ERROR (Status
) || gStatusCode
== NULL
) {
800 return EFI_UNSUPPORTED
;
803 Status
= gStatusCode
->ReportStatusCode (CodeType
, Value
, Instance
, CallerId
, Data
);
806 return EFI_UNSUPPORTED
;
809 // Check whether EFI_RUNTIME_SERVICES has Tiano Extension
811 Status
= EFI_UNSUPPORTED
;
812 if (mRT
->Hdr
.Revision
== EFI_SPECIFICATION_VERSION
&&
813 mRT
->Hdr
.HeaderSize
== sizeof (EFI_RUNTIME_SERVICES
) &&
814 mRT
->ReportStatusCode
!= NULL
) {
815 Status
= mRT
->ReportStatusCode (CodeType
, Value
, Instance
, CallerId
, Data
);