runtime.c

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

C
529
字号
/*++

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:

  Runtime.c

Abstract:

  Runtime Architectural Protocol as defined in the DXE CIS

  This code is used to produce the EFI runtime virtual switch over

  THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT

  The transition for calling EFI Runtime functions in physical mode to calling
  them in virtual mode is very very complex. Every pointer in needs to be 
  converted from physical mode to virtual mode. Be very careful walking linked
  lists! Then to make it really hard the code it's self needs be relocated into
  the new virtual address space.

  So here is the concept. The code in this module will never ever be called in
  virtual mode. This is the code that collects the information needed to convert
  to virtual mode (DXE core registers runtime stuff with this code). Since this 
  code is used to fixup all runtime images, it CAN NOT fix it's self up. So some
  code has to stay behind and that is us.

  Also you need to be careful about when you allocate memory, as once we are in 
  runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer 
  allocate memory.

  Any runtime driver that gets loaded before us will not be callable in virtual 
  mode. This is due to the fact that the DXE core can not register the info 
  needed with us. This is good, since it keeps the code in this file from 
  getting registered.


Revision History:

  - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.
  Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service
  Table now contains an item named CalculateCrc32.

--*/

#include "Runtime.h"

//
// Global Variables
//

#if (EFI_SPECIFICATION_VERSION < 0x00020000)
EFI_GUID                      mLocalEfiUgaIoProtocolGuid  = EFI_UGA_IO_PROTOCOL_GUID;
#endif
#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
EFI_GUID                      mEfiCapsuleHeaderGuid       = EFI_CAPSULE_GUID;
#endif
EFI_MEMORY_DESCRIPTOR         *mVirtualMap                = NULL;
UINTN                         mVirtualMapDescriptorSize;
UINTN                         mVirtualMapMaxIndex;

VOID                          *mMyImageBase;
EFI_SYSTEM_TABLE              *mMyST;
EFI_RUNTIME_SERVICES          *mMyRT;

//
// The handle onto which the Runtime Architectural Protocol instance is installed
//
EFI_HANDLE                    mRuntimeHandle = NULL;

//
// The Runtime Architectural Protocol instance produced by this driver
//
EFI_RUNTIME_ARCH_PROTOCOL     mRuntime = {
  INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),
  INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),

  //
  // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
  // prevent people from having pointer math bugs in their code.
  // now you have to use *DescriptorSize to make things work.
  //
  sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),  
  EFI_MEMORY_DESCRIPTOR_VERSION, 
  0,
  NULL,
  NULL,
  FALSE,
  FALSE
};

//
// Worker Functions
//
VOID
RuntimeDriverCalculateEfiHdrCrc (
  IN OUT EFI_TABLE_HEADER  *Hdr
  )
/*++

Routine Description:

  Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
  internal function.  The EFI Boot Services Table can not be used because
  the EFI Boot Services Table was destroyed at ExitBootServices().

Arguments:

  Hdr  - Pointer to an EFI standard header.

Returns:

  None.

--*/
{
  UINT32  Crc;

  Hdr->CRC32  = 0;

  Crc         = 0;
  RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);
  Hdr->CRC32 = Crc;
}

EFI_STATUS
EFIAPI
RuntimeDriverConvertPointer (
  IN     UINTN    DebugDisposition,
  IN OUT VOID     **ConvertAddress
  )
/*++

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.
  ConvertAddress      - A pointer to a pointer that is to be fixed to be the value needed
                        for the new virtual address mappings being applied.

Returns:

  EFI_SUCCESS             - The pointer pointed to by Address was modified.
  EFI_NOT_FOUND           - The pointer pointed to by Address was not found to be part
                            of the current memory map. This is normally fatal.
  EFI_INVALID_PARAMETER   - One of the parameters has an invalid value.

--*/
{
  UINTN                 Address;
  VOID                  *PlabelConvertAddress;
  UINT64                VirtEndOfRange;
  EFI_MEMORY_DESCRIPTOR *VirtEntry;
  UINTN                 Index;

  //
  // Make sure ConvertAddress is a valid pointer
  //
  if (ConvertAddress == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Get the address to convert
  //
  Address = (UINTN) *ConvertAddress;

  //
  // If this is a null pointer, return if it's allowed
  //
  if (Address == 0) {
    if (DebugDisposition & EFI_OPTIONAL_POINTER) {
      return EFI_SUCCESS;
    }

    return EFI_INVALID_PARAMETER;
  }

  PlabelConvertAddress  = NULL;
  VirtEntry             = mVirtualMap;
  for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
    //
    // To prevent the inclusion of 64-bit math functions a UINTN was placed in
    //  front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
    //  platforms. If you get this ASSERT remove the UINTN and do a 64-bit
    //  multiply.
    //
    ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));

    if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
      if (Address >= VirtEntry->PhysicalStart) {
        VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);
        if (Address < VirtEndOfRange) {
          //
          // Compute new address
          //
          *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);
          return EFI_SUCCESS;
        } else if (Address < (VirtEndOfRange + 0x200000)) {
          //
          // On Itanium GP defines a window +/- 2 MB inside an image.
          // The compiler may asign a GP value outside of the image. Thus
          // it could fall out side of any of our valid regions
          //
          PlabelConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);
        }
      }
    }

    VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);
  }

  if (DebugDisposition & EFI_IPF_GP_POINTER) {
    //
    // If it's an IPF GP and the GP was outside the image handle that case.
    //
    if (PlabelConvertAddress != NULL) {
      *ConvertAddress = PlabelConvertAddress;
      return EFI_SUCCESS;
    }
  }

  return EFI_NOT_FOUND;
}

EFI_STATUS
RuntimeDriverConvertInternalPointer (
  IN OUT VOID   **ConvertAddress
  )
/*++

Routine Description:

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

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

Returns:

  EFI_SUCCESS             - The pointer pointed to by Address was modified.
  EFI_NOT_FOUND           - The pointer pointed to by Address was not found to be part
                            of the current memory map. This is normally fatal.
  EFI_INVALID_PARAMETER   - One of the parameters has an invalid value.

--*/
{
  return RuntimeDriverConvertPointer (EFI_INTERNAL_FUNCTION, ConvertAddress);
}

EFI_STATUS
EFIAPI

⌨️ 快捷键说明

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