3 Copyright (c) 2005 - 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 static EFI_STATUS_CODE_PROTOCOL
*gStatusCode
= NULL
;
49 IN UINTN DebugDisposition
,
56 Determines the new virtual address that is to be used on subsequent memory accesses.
60 DebugDisposition - Supplies type information for the pointer being converted.
61 Address - A pointer to a pointer that is to be fixed to be the value needed
62 for the new virtual address mappings being applied.
70 return mRT
->ConvertPointer (DebugDisposition
, Address
);
74 EfiConvertInternalPointer (
81 Call EfiConvertPointer() to convert internal pointer.
85 Address - A pointer to a pointer that is to be fixed to be the value needed
86 for the new virtual address mappings being applied.
94 return EfiConvertPointer (EFI_INTERNAL_POINTER
, Address
);
99 EfiRuntimeLibFvbVirtualNotifyEvent (
107 Convert all pointers in mFvbEntry after ExitBootServices.
111 Event - The Event that is being processed
113 Context - Event Context
122 if (mFvbEntry
!= NULL
) {
123 for (Index
= 0; Index
< MAX_FVB_COUNT
; Index
++) {
124 if (NULL
!= mFvbEntry
[Index
].Fvb
) {
125 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->GetBlockSize
);
126 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->GetPhysicalAddress
);
127 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->GetVolumeAttributes
);
128 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->SetVolumeAttributes
);
129 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->Read
);
130 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->Write
);
131 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
->EraseBlocks
);
132 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].Fvb
);
135 if (NULL
!= mFvbEntry
[Index
].FvbExtension
) {
136 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].FvbExtension
->EraseFvbCustomBlock
);
137 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
[Index
].FvbExtension
);
141 EfiConvertInternalPointer ((VOID
**) &mFvbEntry
);
147 RuntimeDriverExitBootServices (
155 Set AtRuntime flag as TRUE after ExitBootServices
159 Event - The Event that is being processed
161 Context - Event Context
169 mEfiAtRuntime
= TRUE
;
172 extern BOOLEAN gEfiFvbInitialized
;
176 EfiRuntimeLibVirtualNotifyEvent (
184 Fixup internal data so that EFI can be call in virtual mode.
185 Call the passed in Child Notify event and convert any pointers in
190 Event - The Event that is being processed
192 Context - Event Context
200 EFI_EVENT_NOTIFY ChildNotifyEventHandler
;
202 if (Context
!= NULL
) {
203 ChildNotifyEventHandler
= (EFI_EVENT_NOTIFY
) (UINTN
) Context
;
204 ChildNotifyEventHandler (Event
, NULL
);
207 if (gEfiFvbInitialized
) {
208 EfiRuntimeLibFvbVirtualNotifyEvent (Event
, Context
);
211 // Update global for Runtime Services Table and IO
213 EfiConvertInternalPointer ((VOID
**) &gCpuIo
);
214 if (gStatusCode
!= NULL
) {
215 EfiConvertInternalPointer ((VOID
**) &gStatusCode
->ReportStatusCode
);
216 EfiConvertInternalPointer ((VOID
**) &gStatusCode
);
218 EfiConvertInternalPointer ((VOID
**) &mRT
);
221 // Clear out BootService globals
225 mEfiGoneVirtual
= TRUE
;
229 EfiInitializeRuntimeDriverLib (
230 IN EFI_HANDLE ImageHandle
,
231 IN EFI_SYSTEM_TABLE
*SystemTable
,
232 IN EFI_EVENT_NOTIFY GoVirtualChildEvent
238 Intialize runtime Driver Lib if it has not yet been initialized.
242 ImageHandle - The firmware allocated handle for the EFI image.
244 SystemTable - A pointer to the EFI System Table.
246 GoVirtualChildEvent - Caller can register a virtual notification event.
250 EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
256 if (mRuntimeLibInitialized
) {
257 return EFI_ALREADY_STARTED
;
260 mRuntimeLibInitialized
= TRUE
;
263 ASSERT (gST
!= NULL
);
265 gBS
= SystemTable
->BootServices
;
266 ASSERT (gBS
!= NULL
);
267 mRT
= SystemTable
->RuntimeServices
;
268 ASSERT (mRT
!= NULL
);
270 Status
= EfiLibGetSystemConfigurationTable (&gEfiDxeServicesTableGuid
, (VOID
**) &gDS
);
271 ASSERT_EFI_ERROR (Status
);
273 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&gStatusCode
);
274 if (EFI_ERROR (Status
)) {
278 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &gCpuIo
);
279 if (EFI_ERROR (Status
)) {
284 // Register our ExitBootServices () notify function
286 Status
= gBS
->CreateEvent (
287 EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES
,
289 RuntimeDriverExitBootServices
,
293 ASSERT_EFI_ERROR (Status
);
296 // Register SetVirtualAddressMap () notify function
298 Status
= gBS
->CreateEvent (
299 EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
,
301 EfiRuntimeLibVirtualNotifyEvent
,
302 (VOID
*) (UINTN
) GoVirtualChildEvent
,
303 &mEfiVirtualNotifyEvent
305 ASSERT_EFI_ERROR (Status
);
311 EfiShutdownRuntimeDriverLib (
318 This routine will free some resources which have been allocated in
319 EfiInitializeRuntimeDriverLib(). If a runtime driver exits with an error,
320 it must call this routine to free the allocated resource before the exiting.
328 EFI_SUCCESS - Shotdown the Runtime Driver Lib successfully
329 EFI_UNSUPPORTED - Runtime Driver lib was not initialized at all
335 if (!mRuntimeLibInitialized
) {
337 // You must call EfiInitializeRuntimeDriverLib() first
339 return EFI_UNSUPPORTED
;
342 mRuntimeLibInitialized
= FALSE
;
345 // Close our ExitBootServices () notify function
347 if (mRuntimeNotifyEvent
!= NULL
) {
348 Status
= gBS
->CloseEvent (mRuntimeNotifyEvent
);
349 ASSERT_EFI_ERROR (Status
);
353 // Close SetVirtualAddressMap () notify function
355 if (mEfiVirtualNotifyEvent
!= NULL
) {
356 Status
= gBS
->CloseEvent (mEfiVirtualNotifyEvent
);
357 ASSERT_EFI_ERROR (Status
);
364 EfiInitializeSmmDriverLib (
365 IN EFI_HANDLE ImageHandle
,
366 IN EFI_SYSTEM_TABLE
*SystemTable
372 Intialize runtime Driver Lib if it has not yet been initialized.
376 ImageHandle - The firmware allocated handle for the EFI image.
378 SystemTable - A pointer to the EFI System Table.
382 EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.
388 if (mRuntimeLibInitialized
) {
389 return EFI_ALREADY_STARTED
;
392 mRuntimeLibInitialized
= TRUE
;
395 ASSERT (gST
!= NULL
);
397 gBS
= SystemTable
->BootServices
;
398 ASSERT (gBS
!= NULL
);
399 mRT
= SystemTable
->RuntimeServices
;
400 ASSERT (mRT
!= NULL
);
402 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&gStatusCode
);
403 if (EFI_ERROR (Status
)) {
407 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &gCpuIo
);
408 if (EFI_ERROR (Status
)) {
422 Return TRUE if ExitBootServices () has been called
428 TRUE - If ExitBootServices () has been called
432 return mEfiAtRuntime
;
442 Return TRUE if SetVirtualAddressMap () has been called
448 TRUE - If SetVirtualAddressMap () has been called
452 return mEfiGoneVirtual
;
455 // The following functions hide the mRT local global from the call to
456 // runtime service in the EFI system table.
461 OUT EFI_TIME_CAPABILITIES
*Capabilities
467 Returns the current time and date information, and the time-keeping
468 capabilities of the hardware platform.
472 Time - A pointer to storage to receive a snapshot of the current time.
473 Capabilities - An optional pointer to a buffer to receive the real time clock device's
482 return mRT
->GetTime (Time
, Capabilities
);
493 Sets the current local time and date information.
497 Time - A pointer to the current time.
505 return mRT
->SetTime (Time
);
510 OUT BOOLEAN
*Enabled
,
511 OUT BOOLEAN
*Pending
,
518 Returns the current wakeup alarm clock setting.
522 Enabled - Indicates if the alarm is currently enabled or disabled.
523 Pending - Indicates if the alarm signal is pending and requires acknowledgement.
524 Time - The current alarm setting.
532 return mRT
->GetWakeupTime (Enabled
, Pending
, Time
);
544 Sets the system wakeup alarm clock time.
548 Enable - Enable or disable the wakeup alarm.
549 Time - If Enable is TRUE, the time to set the wakeup alarm for.
550 If Enable is FALSE, then this parameter is optional, and may be NULL.
558 return mRT
->SetWakeupTime (Enable
, Time
);
563 IN CHAR16
*VariableName
,
564 IN EFI_GUID
* VendorGuid
,
565 OUT UINT32
*Attributes OPTIONAL
,
566 IN OUT UINTN
*DataSize
,
573 Returns the value of a variable.
577 VariableName - A Null-terminated Unicode string that is the name of the
579 VendorGuid - A unique identifier for the vendor.
580 Attributes - If not NULL, a pointer to the memory location to return the
581 attributes bitmask for the variable.
582 DataSize - On input, the size in bytes of the return Data buffer.
583 On output the size of data returned in Data.
584 Data - The buffer to return the contents of the variable.
592 return mRT
->GetVariable (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
596 EfiGetNextVariableName (
597 IN OUT UINTN
*VariableNameSize
,
598 IN OUT CHAR16
*VariableName
,
599 IN OUT EFI_GUID
*VendorGuid
605 Enumerates the current variable names.
609 VariableNameSize - The size of the VariableName buffer.
610 VariableName - On input, supplies the last VariableName that was returned
611 by GetNextVariableName().
612 On output, returns the Nullterminated Unicode string of the
614 VendorGuid - On input, supplies the last VendorGuid that was returned by
615 GetNextVariableName().
616 On output, returns the VendorGuid of the current variable.
624 return mRT
->GetNextVariableName (VariableNameSize
, VariableName
, VendorGuid
);
629 IN CHAR16
*VariableName
,
630 IN EFI_GUID
*VendorGuid
,
631 IN UINT32 Attributes
,
639 Sets the value of a variable.
643 VariableName - A Null-terminated Unicode string that is the name of the
645 VendorGuid - A unique identifier for the vendor.
646 Attributes - Attributes bitmask to set for the variable.
647 DataSize - The size in bytes of the Data buffer.
648 Data - The contents for the variable.
656 return mRT
->SetVariable (VariableName
, VendorGuid
, Attributes
, DataSize
, Data
);
659 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
662 EfiQueryVariableInfo (
663 IN UINT32 Attributes
,
664 OUT UINT64
*MaximumVariableStorageSize
,
665 OUT UINT64
*RemainingVariableStorageSize
,
666 OUT UINT64
*MaximumVariableSize
673 This code returns information about the EFI variables.
677 Attributes Attributes bitmask to specify the type of variables
678 on which to return information.
679 MaximumVariableStorageSize Pointer to the maximum size of the storage space available
680 for the EFI variables associated with the attributes specified.
681 RemainingVariableStorageSize Pointer to the remaining size of the storage space available
682 for the EFI variables associated with the attributes specified.
683 MaximumVariableSize Pointer to the maximum size of the individual EFI variables
684 associated with the attributes specified.
692 return mRT
->QueryVariableInfo (Attributes
, MaximumVariableStorageSize
, RemainingVariableStorageSize
, MaximumVariableSize
);
699 EfiGetNextHighMonotonicCount (
700 OUT UINT32
*HighCount
706 Returns the next high 32 bits of the platform's monotonic counter.
710 HighCount - Pointer to returned value.
718 return mRT
->GetNextHighMonotonicCount (HighCount
);
723 IN EFI_RESET_TYPE ResetType
,
724 IN EFI_STATUS ResetStatus
,
732 Resets the entire platform.
736 ResetType - The type of reset to perform.
737 ResetStatus - The status code for the reset.
738 DataSize - The size, in bytes, of ResetData.
739 ResetData - A data buffer that includes a Null-terminated Unicode string, optionally
740 followed by additional binary data.
748 mRT
->ResetSystem (ResetType
, ResetStatus
, DataSize
, ResetData
);
752 EfiReportStatusCode (
753 IN EFI_STATUS_CODE_TYPE CodeType
,
754 IN EFI_STATUS_CODE_VALUE Value
,
756 IN EFI_GUID
* CallerId
,
757 IN EFI_STATUS_CODE_DATA
* Data OPTIONAL
767 CodeType - Type of Status Code.
769 Value - Value to output for Status Code.
771 Instance - Instance Number of this status code.
773 CallerId - ID of the caller of this status code.
775 Data - Optional data associated with this status code.
785 Status
= EFI_UNSUPPORTED
;
787 if (mRT
->Hdr
.Revision
>= 0x00020000) {
788 if (gStatusCode
== NULL
) {
789 if (EfiAtRuntime ()) {
790 return EFI_UNSUPPORTED
;
792 Status
= gBS
->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid
, NULL
, (VOID
**)&gStatusCode
);
793 if (EFI_ERROR (Status
) || gStatusCode
== NULL
) {
794 return EFI_UNSUPPORTED
;
797 Status
= gStatusCode
->ReportStatusCode (CodeType
, Value
, Instance
, CallerId
, Data
);
800 return EFI_UNSUPPORTED
;
803 // Check whether EFI_RUNTIME_SERVICES has Tiano Extension
805 Status
= EFI_UNSUPPORTED
;
806 #if (EFI_SPECIFICATION_VERSION < 0x00020000)
807 if (mRT
->Hdr
.Revision
== EFI_SPECIFICATION_VERSION
&&
808 mRT
->Hdr
.HeaderSize
== sizeof (EFI_RUNTIME_SERVICES
) &&
809 mRT
->ReportStatusCode
!= NULL
) {
810 Status
= mRT
->ReportStatusCode (CodeType
, Value
, Instance
, CallerId
, Data
);