debugport.c

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

C
874
字号
/*++

Copyright (c) 2004 - 2005, 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:

    DebugPort.c

Abstract:

    Top level C file for debugport driver.  Contains initialization function.
    This driver layers on top of SerialIo.
    
    ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM
    INTERRUPT CONTEXT.

Revision History

--*/

#include "DebugPort.h"

//
// Misc. functions local to this module
//
STATIC
VOID
GetDebugPortVariable (
  DEBUGPORT_DEVICE  *DebugPortDevice
  );

STATIC
EFI_STATUS
EFIAPI
ImageUnloadHandler (
  EFI_HANDLE ImageHandle
  );

//
// Globals
//
DEBUGPORT_DEVICE  *gDebugPortDevice;
static UINT32     mHid16550;
static UINT32     mHidStdPcComPort;

//
// implementation code
//
EFI_DRIVER_ENTRY_POINT (InitializeDebugPortDriver)

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

Routine Description:
  Driver entry point.  Reads DebugPort variable to determine what device and settings
  to use as the debug port.  Binds exclusively to SerialIo. Reverts to defaults \
  if no variable is found.  
  
  Creates debugport and devicepath protocols on new handle.

Arguments:
  ImageHandle,
  SystemTable

Returns:

  EFI_UNSUPPORTED
  EFI_OUT_OF_RESOURCES

--*/
{
  EFI_LOADED_IMAGE_PROTOCOL *LoadedImageInterface;

  mHid16550         = EFI_ACPI_16550UART_HID;
  mHidStdPcComPort  = EFI_ACPI_PC_COMPORT_HID;

  //
  // Check to be sure we have an EFI 1.1 core
  //
  if (SystemTable->Hdr.Revision < EFI_1_10_SYSTEM_TABLE_REVISION) {
    return EFI_UNSUPPORTED;
  }
  //
  //  Initialize EFI driver library
  //
  EfiInitializeDriverLib (ImageHandle, SystemTable);

  //
  // Allocate and Initialize dev structure
  //
  gDebugPortDevice = EfiLibAllocateZeroPool (sizeof (DEBUGPORT_DEVICE));
  if (gDebugPortDevice == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Fill in static and default pieces of device structure first.
  //
  gDebugPortDevice->Signature = DEBUGPORT_DEVICE_SIGNATURE;

  gDebugPortDevice->DriverBindingInterface.Supported = DebugPortSupported;
  gDebugPortDevice->DriverBindingInterface.Start = DebugPortStart;
  gDebugPortDevice->DriverBindingInterface.Stop = DebugPortStop;
  gDebugPortDevice->DriverBindingInterface.Version = DEBUGPORT_DRIVER_VERSION;
  gDebugPortDevice->DriverBindingInterface.ImageHandle = ImageHandle;
  gDebugPortDevice->DriverBindingInterface.DriverBindingHandle = ImageHandle;

  gDebugPortDevice->ComponentNameInterface.GetDriverName = DebugPortComponentNameGetDriverName;
  gDebugPortDevice->ComponentNameInterface.GetControllerName = DebugPortComponentNameGetControllerName;
  gDebugPortDevice->ComponentNameInterface.SupportedLanguages = "eng";

  gDebugPortDevice->DebugPortInterface.Reset = DebugPortReset;
  gDebugPortDevice->DebugPortInterface.Read = DebugPortRead;
  gDebugPortDevice->DebugPortInterface.Write = DebugPortWrite;
  gDebugPortDevice->DebugPortInterface.Poll = DebugPortPoll;

  gDebugPortDevice->BaudRate = DEBUGPORT_UART_DEFAULT_BAUDRATE;
  gDebugPortDevice->ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;
  gDebugPortDevice->Timeout = DEBUGPORT_UART_DEFAULT_TIMEOUT;
  gDebugPortDevice->Parity = DEBUGPORT_UART_DEFAULT_PARITY;
  gDebugPortDevice->DataBits = DEBUGPORT_UART_DEFAULT_DATA_BITS;
  gDebugPortDevice->StopBits = DEBUGPORT_UART_DEFAULT_STOP_BITS;

  //
  //  Install unload handler...
  //
  gBS->OpenProtocol (
        ImageHandle,
        &gEfiLoadedImageProtocolGuid,
        &LoadedImageInterface,
        NULL,
        NULL,
        EFI_OPEN_PROTOCOL_GET_PROTOCOL
        );
  LoadedImageInterface->Unload = ImageUnloadHandler;

  //
  // Publish EFI 1.10 driver model protocols
  //
  return EfiLibInstallAllDriverProtocols (
          ImageHandle,
          SystemTable,
          &gDebugPortDevice->DriverBindingInterface,
          ImageHandle,
          &gDebugPortDevice->ComponentNameInterface,
          NULL,
          NULL
          );
}
//
// DebugPort driver binding member functions...
//
EFI_STATUS
EFIAPI
DebugPortSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN EFI_HANDLE                     ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
  )
/*++

Routine Description:
  Checks to see that there's not already a DebugPort interface somewhere.  If so,
  fail.
  
  If there's a DEBUGPORT variable, the device path must match exactly.  If there's
  no DEBUGPORT variable, then device path is not checked and does not matter.
  
  Checks to see that there's a serial io interface on the controller handle
  that can be bound BY_DRIVER | EXCLUSIVE.
  
  If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED
  or other error returned by OpenProtocol.

Arguments:
  This
  ControllerHandle
  RemainingDevicePath
  
Returns:
  EFI_UNSUPPORTED
  EFI_OUT_OF_RESOURCES
  EFI_SUCCESS
  
--*/
{
  EFI_STATUS                Status;
  EFI_DEVICE_PATH_PROTOCOL  *Dp1;
  EFI_DEVICE_PATH_PROTOCOL  *Dp2;
  EFI_SERIAL_IO_PROTOCOL    *SerialIo;
  EFI_DEBUGPORT_PROTOCOL    *DebugPortInterface;
  EFI_HANDLE                TempHandle;

  //
  // Check to see that there's not a debugport protocol already published
  //
  if (gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **) &DebugPortInterface) != EFI_NOT_FOUND) {
    return EFI_UNSUPPORTED;
  }
  //
  // Read DebugPort variable to determine debug port selection and parameters
  //
  GetDebugPortVariable (gDebugPortDevice);

  if (gDebugPortDevice->DebugPortVariable != NULL) {
    //
    // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if
    // the closest matching handle matches the controller handle, and if it does,
    // check to see that the remaining device path has the DebugPort GUIDed messaging
    // device path only.  Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.
    //
    Dp1 = EfiDuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) gDebugPortDevice->DebugPortVariable);
    if (Dp1 == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }

    Dp2 = Dp1;

    Status = gBS->LocateDevicePath (
                    &gEfiSerialIoProtocolGuid,
                    &Dp2,
                    &TempHandle
                    );

    if (Status == EFI_SUCCESS && TempHandle != ControllerHandle) {
      Status = EFI_UNSUPPORTED;
    }

    if (Status == EFI_SUCCESS && (Dp2->Type != 3 || Dp2->SubType != 10 || *((UINT16 *) Dp2->Length) != 20)) {
      Status = EFI_UNSUPPORTED;
    }

    if (Status == EFI_SUCCESS && EfiCompareMem (&gEfiDebugPortDevicePathGuid, Dp2 + 1, sizeof (EFI_GUID))) {
      Status = EFI_UNSUPPORTED;
    }

    gBS->FreePool (Dp1);
    if (EFI_ERROR (Status)) {
      return Status;
    }
  }

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiSerialIoProtocolGuid,
                  &SerialIo,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  gBS->CloseProtocol (
        ControllerHandle,
        &gEfiSerialIoProtocolGuid,
        This->DriverBindingHandle,
        ControllerHandle
        );

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
DebugPortStart (
  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN EFI_HANDLE                     ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
  )
/*++

Routine Description:
  Binds exclusively to serial io on the controller handle.  Produces DebugPort
  protocol and DevicePath on new handle.

Arguments:
  This
  ControllerHandle
  RemainingDevicePath
  
Returns:
  EFI_OUT_OF_RESOURCES
  EFI_SUCCESS
--*/
{
  EFI_STATUS                Status;
  DEBUGPORT_DEVICE_PATH     DebugPortDP;
  EFI_DEVICE_PATH_PROTOCOL  EndDP;
  EFI_DEVICE_PATH_PROTOCOL  *Dp1;

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiSerialIoProtocolGuid,
                  &gDebugPortDevice->SerialIoBinding,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  gDebugPortDevice->SerialIoDeviceHandle = ControllerHandle;

  //
  // Initialize the Serial Io interface...
  //
  Status = gDebugPortDevice->SerialIoBinding->SetAttributes (
                                                gDebugPortDevice->SerialIoBinding,
                                                gDebugPortDevice->BaudRate,
                                                gDebugPortDevice->ReceiveFifoDepth,
                                                gDebugPortDevice->Timeout,
                                                gDebugPortDevice->Parity,
                                                gDebugPortDevice->DataBits,
                                                gDebugPortDevice->StopBits
                                                );
  if (EFI_ERROR (Status)) {
    gDebugPortDevice->BaudRate          = 0;
    gDebugPortDevice->Parity            = DefaultParity;
    gDebugPortDevice->DataBits          = 0;
    gDebugPortDevice->StopBits          = DefaultStopBits;
    gDebugPortDevice->ReceiveFifoDepth  = 0;
    Status = gDebugPortDevice->SerialIoBinding->SetAttributes (
                                                  gDebugPortDevice->SerialIoBinding,
                                                  gDebugPortDevice->BaudRate,
                                                  gDebugPortDevice->ReceiveFifoDepth,
                                                  gDebugPortDevice->Timeout,
                                                  gDebugPortDevice->Parity,
                                                  gDebugPortDevice->DataBits,
                                                  gDebugPortDevice->StopBits
                                                  );
    if (EFI_ERROR (Status)) {
      gBS->CloseProtocol (
            ControllerHandle,
            &gEfiSerialIoProtocolGuid,
            This->DriverBindingHandle,
            ControllerHandle
            );
      return Status;
    }
  }

  gDebugPortDevice->SerialIoBinding->Reset (gDebugPortDevice->SerialIoBinding);

  //
  // Create device path instance for DebugPort
  //
  DebugPortDP.Header.Type     = MESSAGING_DEVICE_PATH;
  DebugPortDP.Header.SubType  = MSG_VENDOR_DP;
  SetDevicePathNodeLength (&(DebugPortDP.Header), sizeof (DebugPortDP));
  gBS->CopyMem (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid, sizeof (EFI_GUID));

  Dp1 = EfiDevicePathFromHandle (ControllerHandle);
  if (Dp1 == NULL) {
    Dp1 = &EndDP;
    SetDevicePathEndNode (Dp1);
  }

  gDebugPortDevice->DebugPortDevicePath = EfiAppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);
  if (gDebugPortDevice->DebugPortDevicePath == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Publish DebugPort and Device Path protocols
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &gDebugPortDevice->DebugPortDeviceHandle,
                  &gEfiDevicePathProtocolGuid,
                  gDebugPortDevice->DebugPortDevicePath,
                  &gEfiDebugPortProtocolGuid,
                  &gDebugPortDevice->DebugPortInterface,
                  NULL
                  );

  if (EFI_ERROR (Status)) {
    gBS->CloseProtocol (
          ControllerHandle,
          &gEfiSerialIoProtocolGuid,
          This->DriverBindingHandle,
          ControllerHandle
          );
    return Status;
  }
  //
  // Connect debugport child to serial io
  //
  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiSerialIoProtocolGuid,
                  &gDebugPortDevice->SerialIoBinding,
                  This->DriverBindingHandle,
                  gDebugPortDevice->DebugPortDeviceHandle,
                  EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
                  );

  if (EFI_ERROR (Status)) {
    DEBUG_CODE (
    {
      UINTN BufferSize = 48;
      DebugPortWrite (
        &gDebugPortDevice->DebugPortInterface,
        0,
        &BufferSize,
        "DebugPort driver failed to open child controller\n\n"
        );
    }
    )

    gBS->CloseProtocol (
          ControllerHandle,
          &gEfiSerialIoProtocolGuid,
          This->DriverBindingHandle,
          ControllerHandle
          );
    return Status;
  }

  DEBUG_CODE (
  {
    UINTN BufferSize = 38;
    DebugPortWrite (
      &gDebugPortDevice->DebugPortInterface,
      0,
      &BufferSize,

⌨️ 快捷键说明

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