runtimelib.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 836 行 · 第 1/2 页

C
836
字号
/*++

Copyright (c) 2004 - 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

    RuntimeLib.c

Abstract:

  Light weight lib to support Tiano drivers.

--*/

#include "Tiano.h"
#include "EfiRuntimeLib.h"
#include EFI_PROTOCOL_DEFINITION (CpuIo)
#include EFI_PROTOCOL_DEFINITION (FirmwareVolumeBlock)
#include EFI_GUID_DEFINITION (StatusCodeCallerId)
#include EFI_ARCH_PROTOCOL_DEFINITION (StatusCode)

//
// Driver Lib Module Globals
//
static EFI_RUNTIME_SERVICES *mRT;
static EFI_EVENT            mRuntimeNotifyEvent     = NULL;
static EFI_EVENT            mEfiVirtualNotifyEvent  = NULL;
static BOOLEAN              mRuntimeLibInitialized  = FALSE;
static BOOLEAN              mEfiGoneVirtual         = FALSE;

//
// Runtime Global, but you should use the Lib functions
//
EFI_CPU_IO_PROTOCOL         *gCpuIo;
BOOLEAN                     mEfiAtRuntime = FALSE;
FVB_ENTRY                   *mFvbEntry;

#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
static EFI_STATUS_CODE_PROTOCOL  *gStatusCode = NULL;
#endif

EFI_STATUS
EfiConvertPointer (
  IN UINTN                     DebugDisposition,
  IN OUT VOID                  *Address
  )
/*++

Routine Description:

  Determines the new virtual address that is to be used on subsequent memory accesses.

Arguments:

  DebugDisposition  - Supplies type information for the pointer being converted.
  Address           - A pointer to a pointer that is to be fixed to be the value needed
                      for the new virtual address mappings being applied.

Returns:

  Status code

--*/
{
  return mRT->ConvertPointer (DebugDisposition, Address);
}

EFI_STATUS
EfiConvertInternalPointer (
  IN OUT VOID                  *Address
  )
/*++

Routine Description:

  Call EfiConvertPointer() to convert internal pointer.

Arguments:

  Address - A pointer to a pointer that is to be fixed to be the value needed
            for the new virtual address mappings being applied.

Returns:

  Status code

--*/
{
  return EfiConvertPointer (EFI_INTERNAL_POINTER, Address);
}

VOID
EFIAPI
EfiRuntimeLibFvbVirtualNotifyEvent (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
/*++

Routine Description:

  Convert all pointers in mFvbEntry after ExitBootServices.

Arguments:

  Event   - The Event that is being processed
  
  Context - Event Context

Returns:

  None

--*/
{
  UINTN Index;
  if (mFvbEntry != NULL) {
    for (Index = 0; Index < MAX_FVB_COUNT; Index++) {
      if (NULL != mFvbEntry[Index].Fvb) {
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->GetBlockSize);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->GetPhysicalAddress);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->GetVolumeAttributes);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->SetVolumeAttributes);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->Read);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->Write);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb->EraseBlocks);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].Fvb);
      }

      if (NULL != mFvbEntry[Index].FvbExtension) {
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].FvbExtension->EraseFvbCustomBlock);
        EfiConvertInternalPointer ((VOID **) &mFvbEntry[Index].FvbExtension);
      }
    }

    EfiConvertInternalPointer ((VOID **) &mFvbEntry);
  }
}

VOID
EFIAPI
RuntimeDriverExitBootServices (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
/*++

Routine Description:

  Set AtRuntime flag as TRUE after ExitBootServices

Arguments:

  Event   - The Event that is being processed
  
  Context - Event Context

Returns: 

  None

--*/
{
  mEfiAtRuntime = TRUE;
}

extern BOOLEAN  gEfiFvbInitialized;

VOID
EFIAPI
EfiRuntimeLibVirtualNotifyEvent (
  IN EFI_EVENT        Event,
  IN VOID             *Context
  )
/*++

Routine Description:

  Fixup internal data so that EFI can be call in virtual mode.
  Call the passed in Child Notify event and convert any pointers in 
  lib to virtual mode.

Arguments:

  Event   - The Event that is being processed
  
  Context - Event Context

Returns: 

  None

--*/
{
  EFI_EVENT_NOTIFY  ChildNotifyEventHandler;

  if (Context != NULL) {
    ChildNotifyEventHandler = (EFI_EVENT_NOTIFY) (UINTN) Context;
    ChildNotifyEventHandler (Event, NULL);
  }

  if (gEfiFvbInitialized) {
    EfiRuntimeLibFvbVirtualNotifyEvent (Event, Context);
  }
  //
  // Update global for Runtime Services Table and IO
  //
  EfiConvertInternalPointer ((VOID **) &gCpuIo);
#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
  if (gStatusCode != NULL) {
    EfiConvertInternalPointer ((VOID **) &gStatusCode->ReportStatusCode);
    EfiConvertInternalPointer ((VOID **) &gStatusCode);
  }
#endif
  EfiConvertInternalPointer ((VOID **) &mRT);

  //
  // Clear out BootService globals
  //
  gBS             = NULL;
  gST             = NULL;
  mEfiGoneVirtual = TRUE;
}

EFI_STATUS
EfiInitializeRuntimeDriverLib (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable,
  IN EFI_EVENT_NOTIFY     GoVirtualChildEvent
  )
/*++

Routine Description:

  Intialize runtime Driver Lib if it has not yet been initialized. 

Arguments:

  ImageHandle     - The firmware allocated handle for the EFI image.
  
  SystemTable     - A pointer to the EFI System Table.

  GoVirtualChildEvent - Caller can register a virtual notification event.

Returns: 

  EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.

--*/
{
  EFI_STATUS  Status;

  if (mRuntimeLibInitialized) {
    return EFI_ALREADY_STARTED;
  }

  mRuntimeLibInitialized  = TRUE;

  gST = SystemTable;
  ASSERT (gST != NULL);

  gBS = SystemTable->BootServices;
  ASSERT (gBS != NULL);
  mRT = SystemTable->RuntimeServices;
  ASSERT (mRT != NULL);

  Status  = EfiLibGetSystemConfigurationTable (&gEfiDxeServicesTableGuid, (VOID **) &gDS);
  ASSERT_EFI_ERROR (Status);

#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
  Status = gBS->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid, NULL, (VOID **)&gStatusCode);
  if (EFI_ERROR (Status)) {
    gStatusCode = NULL;
  }
#endif

  Status = gBS->LocateProtocol (&gEfiCpuIoProtocolGuid, NULL, &gCpuIo);
  if (EFI_ERROR (Status)) {
    gCpuIo = NULL;
  }

  //
  // Register our ExitBootServices () notify function
  //
  Status = gBS->CreateEvent (
                  EFI_EVENT_SIGNAL_EXIT_BOOT_SERVICES,
                  EFI_TPL_NOTIFY,
                  RuntimeDriverExitBootServices,
                  NULL,
                  &mRuntimeNotifyEvent
                  );
  ASSERT_EFI_ERROR (Status);

  //
  // Register SetVirtualAddressMap () notify function
  //
  Status = gBS->CreateEvent (
                  EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
                  EFI_TPL_NOTIFY,
                  EfiRuntimeLibVirtualNotifyEvent,
                  (VOID *) (UINTN) GoVirtualChildEvent,
                  &mEfiVirtualNotifyEvent
                  );
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}

EFI_STATUS
EfiShutdownRuntimeDriverLib (
  VOID
  )
/*++

Routine Description:

  This routine will free some resources which have been allocated in
  EfiInitializeRuntimeDriverLib(). If a runtime driver exits with an error, 
  it must call this routine to free the allocated resource before the exiting.

Arguments:

  None

Returns: 

  EFI_SUCCESS     - Shotdown the Runtime Driver Lib successfully
  EFI_UNSUPPORTED - Runtime Driver lib was not initialized at all

--*/
{
  EFI_STATUS  Status;

  if (!mRuntimeLibInitialized) {
    //
    // You must call EfiInitializeRuntimeDriverLib() first
    //
    return EFI_UNSUPPORTED;
  }

  mRuntimeLibInitialized = FALSE;

  //
  // Close our ExitBootServices () notify function
  //
  if (mRuntimeNotifyEvent != NULL) {
    Status = gBS->CloseEvent (mRuntimeNotifyEvent);
    ASSERT_EFI_ERROR (Status);
  }

  //
  // Close SetVirtualAddressMap () notify function
  //
  if (mEfiVirtualNotifyEvent != NULL) {
    Status = gBS->CloseEvent (mEfiVirtualNotifyEvent);
    ASSERT_EFI_ERROR (Status);
  }

  return EFI_SUCCESS;
}

EFI_STATUS
EfiInitializeSmmDriverLib (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
/*++

Routine Description:

  Intialize runtime Driver Lib if it has not yet been initialized. 

Arguments:

  ImageHandle     - The firmware allocated handle for the EFI image.
  
  SystemTable     - A pointer to the EFI System Table.

Returns: 

  EFI_STATUS always returns EFI_SUCCESS except EFI_ALREADY_STARTED if already started.

--*/
{
  EFI_STATUS  Status;

  if (mRuntimeLibInitialized) {
    return EFI_ALREADY_STARTED;
  }

  mRuntimeLibInitialized  = TRUE;

  gST = SystemTable;
  ASSERT (gST != NULL);

  gBS = SystemTable->BootServices;
  ASSERT (gBS != NULL);
  mRT = SystemTable->RuntimeServices;
  ASSERT (mRT != NULL);

#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
  Status = gBS->LocateProtocol (&gEfiStatusCodeRuntimeProtocolGuid, NULL, (VOID **)&gStatusCode);
  if (EFI_ERROR (Status)) {
    gStatusCode = NULL;
  }
#endif

  Status  = gBS->LocateProtocol (&gEfiCpuIoProtocolGuid, NULL, &gCpuIo);
  if (EFI_ERROR (Status)) {
    gCpuIo = NULL;
  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?