biosvga.c

来自「Next BIOS Source code : Extensible Firmw」· C语言 代码 · 共 1,004 行 · 第 1/2 页

C
1,004
字号
/*++

Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.

Module Name:

  BiosVga.c
    
Abstract:

  ConsoleOut Routines that speak VGA.

Revision History

--*/

#include "BiosVga.h"

//
// EFI Driver Binding Protocol Instance
//
//   This driver has a version value of 0x00000000.  This is the
//   lowest possible priority for a driver.  This is done on purpose to help
//   the developers of UGA drivers.  This driver can bind if no UGA driver 
//   is present, so a console is available.  Then, when a UGA driver is loaded
//   this driver can be disconnected, and the UGA driver can be connected.
//   As long as the UGA driver has a version value greater than 0x00000000, it
//   will be connected first and will block this driver from connecting.
//
EFI_DRIVER_BINDING_PROTOCOL gBiosVgaDriverBinding = {
  BiosVgaDriverBindingSupported,
  BiosVgaDriverBindingStart,
  BiosVgaDriverBindingStop,
  0x00000000,
  NULL,
  NULL
};  

//
// Private driver variables
//
static CHAR16 CrLfString[3] = { CHAR_CARRIAGE_RETURN, CHAR_LINEFEED, CHAR_NULL };

//
// Private worker functions
//
static 
CHAR16 *
UnicodeToASCII (
  IN  CHAR16  *WString,
  OUT CHAR8   *String,
  IN  UINTN   MaxSize
  );

static
VOID
SetVideoRows(
  INTN Mode
  );

static 
VOID
SetVideoMode(
  INTN Mode
  );

static 
VOID 
GetVideoCursorPosition(
  OUT INT32   *Column,
  OUT INT32   *Row
  );

static 
VOID
WriteVgaString(
  IN  INT32   Column,
  IN  INT32   Row,
  IN  INT32   Color,
  IN  CHAR8   *String
  );

static 
VOID 
SetVideoCursorPosition(
  IN  UINTN   Column,
  IN  UINTN   Row
  );

static
BOOLEAN
BiosVgaLibIsValidTextGraphics (
  IN  CHAR16  Graphic,
  OUT CHAR8   *PcAnsi,    OPTIONAL
  OUT CHAR8   *Ascii      OPTIONAL
  );

static
BOOLEAN
BiosVgaLibIsValidAscii (
  IN  CHAR16  Ascii
  );

static
BOOLEAN
BiosVgaLibIsValidEfiCntlChar (
  IN  CHAR16  c
  );

static
UINTN
strlena (
  IN CHAR8    *s1
  );

//
// Driver Entry Point
//  

EFI_DRIVER_ENTRY_POINT(BiosVgaDriverEntryPoint)
    
EFI_STATUS
BiosVgaDriverEntryPoint(
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  ) 
/*++
  
  Routine Description:
    Driver Entry Point.
        
  Arguments:
    (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
  
  Returns:
    EFI_STATUS
--*/                
{
  return EfiLibInstallAllDriverProtocols (
           ImageHandle, 
           SystemTable, 
           &gBiosVgaDriverBinding,
           ImageHandle,
           &gBiosVgaComponentName,
           NULL,
           NULL
           );
} 

//
// EFI Driver Binding Protocol Functions
//

EFI_STATUS
BiosVgaDriverBindingSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN EFI_HANDLE                   Controller,
  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
  )
/*++
  
  Routine Description:
    Supported.
    
  Arguments:
    (Standard DriverBinding Protocol Supported() function)
    
  Returns:
    EFI_STATUS
  
--*/                
{
  EFI_STATUS           Status;
  EFI_PCI_IO_PROTOCOL  *PciIo;
  PCI_TYPE00           Pci;

  //
  // Open the IO Abstraction(s) needed to perform the supported test
  //
  Status = gBS->OpenProtocol (
                  Controller,           
                  &gEfiPciIoProtocolGuid, 
                  (VOID **)&PciIo,
                  This->DriverBindingHandle,   
                  Controller,   
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // See if this is a PCI VGA Controller by looking at the Command register and 
  // Class Code Register
  //
  Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0, sizeof (Pci) / sizeof (UINT32), &Pci);
  if (EFI_ERROR (Status)) {
    Status = EFI_UNSUPPORTED;
    goto Done;
  }
  
  Status = EFI_UNSUPPORTED;
#ifdef SOFT_SDV
    if(Pci.Hdr.ClassCode[2] == 1 && Pci.Hdr.ClassCode[1] == 1) {
      Status = EFI_SUCCESS;
    }
#else
  if ((Pci.Hdr.Command & 0x03) == 0x03) {
    if (IS_PCI_VGA (&Pci)) {
      Status = EFI_SUCCESS;
    }
  }
#endif

Done:
  gBS->CloseProtocol (
         Controller,  
         &gEfiPciIoProtocolGuid, 
         This->DriverBindingHandle,   
         Controller   
         );

  return Status;
}   

EFI_STATUS
BiosVgaDriverBindingStart (
  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN EFI_HANDLE                   Controller,
  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
  )
/*++
  
  Routine Description:
    Install VGA Mini Port Protocol onto VGA device handles
  
  Arguments:
    (Standard DriverBinding Protocol Start() function)
    
  Returns:
    EFI_STATUS
    
--*/                
{
  EFI_STATUS            Status;
  EFI_PCI_IO_PROTOCOL   *PciIo;
  BIOS_VGA_DEV          *BiosVgaPrivate;

  BiosVgaPrivate = NULL;

  //
  // Open the IO Abstraction(s) needed 
  //
  Status = gBS->OpenProtocol (
                  Controller,           
                  &gEfiPciIoProtocolGuid, 
                  (VOID **)&PciIo,
                  This->DriverBindingHandle,   
                  Controller,   
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }
      
  //
  // Allocate the private device structure
  //
  Status = gBS->AllocatePool (
                  EfiBootServicesData,
                  sizeof(BIOS_VGA_DEV),
                  &BiosVgaPrivate
                  );
  if (EFI_ERROR(Status)) {
    return Status;
  }
  EfiZeroMem (BiosVgaPrivate, sizeof (BIOS_VGA_DEV)) ;
  
  //
  // Initialize the private device structure
  //
  BiosVgaPrivate->Signature = BIOS_VGA_DEV_SIGNATURE;
  BiosVgaPrivate->Handle    = Controller;

  BiosVgaPrivate->SimpleTextOut.Reset             = BiosVgaReset;
  BiosVgaPrivate->SimpleTextOut.OutputString      = BiosVgaOutputString;
  BiosVgaPrivate->SimpleTextOut.TestString        = BiosVgaTestString;
  BiosVgaPrivate->SimpleTextOut.ClearScreen       = BiosVgaClearScreen;
  BiosVgaPrivate->SimpleTextOut.SetAttribute      = BiosVgaSetAttribute;
  BiosVgaPrivate->SimpleTextOut.SetCursorPosition = BiosVgaSetCursorPosition;
  BiosVgaPrivate->SimpleTextOut.EnableCursor      = BiosVgaEnableCursor;
  BiosVgaPrivate->SimpleTextOut.QueryMode         = BiosVgaQueryMode;
  BiosVgaPrivate->SimpleTextOut.SetMode           = BiosVgaSetMode;

  BiosVgaPrivate->SimpleTextOut.Mode              = &BiosVgaPrivate->SimpleTextOutputMode;
#ifdef SOFT_SDV
  BiosVgaPrivate->SimpleTextOutputMode.MaxMode    = 1;
#else
  BiosVgaPrivate->SimpleTextOutputMode.MaxMode    = 2;
#endif

  //
  // Allocate a one page buffer below 1 MB to support BIOS calls that require 
  // data in an input buffer.
  //
  BiosVgaPrivate->BufferPhysicalAddress = 0x000fffff;
  Status = gBS->AllocatePages (
                  AllocateMaxAddress, 
                  EfiBootServicesData, 
                  1, 
                  &BiosVgaPrivate->BufferPhysicalAddress
                  );
  if (EFI_ERROR (Status)) {
    goto Done;
  }
  BiosVgaPrivate->Buffer = (UINT8 *)BiosVgaPrivate->BufferPhysicalAddress;

  Status = BiosVgaPrivate->SimpleTextOut.SetAttribute (
                                           &BiosVgaPrivate->SimpleTextOut,
                                           EFI_TEXT_ATTR (EFI_WHITE, EFI_BLACK)
                                           );

  Status = BiosVgaPrivate->SimpleTextOut.Reset (
                                           &BiosVgaPrivate->SimpleTextOut,
                                           FALSE
                                           );

  Status = BiosVgaPrivate->SimpleTextOut.EnableCursor (
                                           &BiosVgaPrivate->SimpleTextOut,
                                           TRUE
                                           );

  Status = gBS->InstallMultipleProtocolInterfaces (
                  &Controller,
                  &gEfiSimpleTextOutProtocolGuid, &BiosVgaPrivate->SimpleTextOut,
                  NULL
                  );

Done:
  if (EFI_ERROR (Status)) {

    gBS->CloseProtocol (
           Controller,  
           &gEfiPciIoProtocolGuid, 
           This->DriverBindingHandle,   
           Controller   
           );

    if (BiosVgaPrivate != NULL) {
      if (BiosVgaPrivate->Buffer != NULL) {
        gBS->FreePages (BiosVgaPrivate->BufferPhysicalAddress, 1);
      }
      gBS->FreePool (BiosVgaPrivate);
    }
  }

  return Status;
}

EFI_STATUS
BiosVgaDriverBindingStop (
  IN  EFI_DRIVER_BINDING_PROTOCOL     *This,
  IN  EFI_HANDLE                      Controller,
  IN  UINTN                           NumberOfChildren,
  IN  EFI_HANDLE                      *ChildHandleBuffer
  )
/*++
  
  Routine Description:
    Stop.
  
  Arguments:
    (Standard DriverBinding Protocol Stop() function)
  
  Returns:
    EFI_STATUS
  
--*/                
{
  EFI_STATUS                    Status;
  EFI_SIMPLE_TEXT_OUT_PROTOCOL  *SimpleTextOut;
  BIOS_VGA_DEV                  *BiosVgaPrivate;

  Status = gBS->OpenProtocol (
                  Controller,
                  &gEfiSimpleTextOutProtocolGuid,  
                  (VOID **)&SimpleTextOut,
                  This->DriverBindingHandle,             
                  Controller,   
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  BiosVgaPrivate = BIOS_VGA_DEV_FROM_THIS (SimpleTextOut);

  Status = gBS->UninstallProtocolInterface (
                  Controller, 
                  &gEfiSimpleTextOutProtocolGuid, &BiosVgaPrivate->SimpleTextOut
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }
    
  //
  // Release PCI I/O and VGA Mini Port Protocols on the controller handle.
  //
  gBS->CloseProtocol (
         Controller, 
         &gEfiPciIoProtocolGuid, 
         This->DriverBindingHandle, 
         Controller
         );

  gBS->FreePages (BiosVgaPrivate->BufferPhysicalAddress, 1);
  gBS->FreePool (BiosVgaPrivate);

  return EFI_SUCCESS;
}

//
// EFI Simple Text Output Protocol Functions
//
EFI_STATUS 
EFIAPI
BiosVgaReset (
  IN  EFI_SIMPLE_TEXT_OUT_PROTOCOL        *This,
  IN  BOOLEAN                             ExtendedVerification
  )
{
  //
  //reset the background and keep the foreground unchanged
  //
  This->SetAttribute(This,This->Mode->Attribute & 0xf);
  
  return This->SetMode (This, 0); 
}

EFI_STATUS 
EFIAPI
BiosVgaOutputString (
  IN  EFI_SIMPLE_TEXT_OUT_PROTOCOL    *This,
  IN  CHAR16                          *WString
  )
{
  BIOS_VGA_DEV                 *BiosVgaPrivate;
  EFI_SIMPLE_TEXT_OUTPUT_MODE  *Mode;
  UINTN                        MaxColumn;
  UINTN                        MaxRow;
  UINTN                        Index;
  CHAR8                        GraphicChar;

  BiosVgaPrivate = BIOS_VGA_DEV_FROM_THIS (This);

  Mode = This->Mode;
  This->QueryMode (
          This, 
          Mode->Mode, 
          &MaxColumn, 
          &MaxRow
          );

  for(;*WString != CHAR_NULL; WString++) {
      
    switch (*WString) {
    case CHAR_NULL :
      break;
    case CHAR_BACKSPACE : 
      if (Mode->CursorColumn > 0) {
          Mode->CursorColumn--;
      }
      break;
    case CHAR_LINEFEED :
      if (Mode->CursorRow == (INT32)(MaxRow-1)) {
        //
        // Scroll Screen and Print Blank Line
        //
        BiosVgaPrivate->Buffer[0] = '\r';
        BiosVgaPrivate->Buffer[1] = '\n';
#ifdef SOFT_SDV
        for (Index = 0; Index < MaxColumn ; Index++) {
#else
        for (Index = 0; Index < MaxColumn - 1 ; Index++) {
#endif
          BiosVgaPrivate->Buffer[Index + 2] = ' ';
        }
        BiosVgaPrivate->Buffer[Index + 2] = '\r';
        BiosVgaPrivate->Buffer[Index + 3] = 0;
        WriteVgaString (
          0, 
          Mode->CursorRow, 
          Mode->Attribute, 
          BiosVgaPrivate->Buffer
          );

⌨️ 快捷键说明

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