⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcirw.c

📁 PCI总线读写源码
💻 C
📖 第 1 页 / 共 2 页
字号:
//;******************************************************************************
//;* Copyright (c) 1983-2008, Insyde Software Corporation. All Rights Reserved.
//;*
//;* You may not reproduce, distribute, publish, display, perform, modify, adapt,
//;* transmit, broadcast, present, recite, release, license or otherwise exploit
//;* any part of this publication in any form, by any means, without the prior
//;* written permission of Insyde Software Corporation.
//;*
//;******************************************************************************
//; Revision History:
//;
//; $Log: /Application/Shell/PciRW/PciRW.c $
//; 
//; 1     2008/03/28 17:44 Bini.Yi
//;  Issue#: N/A for study EFI Application
//;  Category: Application, Shell
//;  Symptom: Update new Shell source code and binary file.
//;  RootCause:
//;  RelatedFiles:
//;    1. Application\*.* r1
//;    2. Platform\Common.dsc r3
//;    3. Edk\Other\Maintained\Application\Shell\bin\ia32\Shell.bin r2
//;    4. Edk\Other\Maintained\Application\Shell\bin\ia32\Apps\StartupDelay
//; .efi r1
//;
//;------------------------------------------------------------------------------
/*++

The Example for study PCI access , memory allocate and destory and keyboard input and display. For study EFI Application

Module Name:
  PciRW.c

Abstract:

--*/

#include "Tiano.h"
#include "EfiShellLib.h"
#include "EfiDriverLib.h"

#include EFI_PROTOCOL_DEFINITION (PciRootBridgeIo)

// {1EDD13C1-62EF-4262-A1AA-B93D70DE9806}
#define EFI_PCIRW_GUID \
  { \
    0x1edd13c1, 0x62ef, 0x4262, 0xa1, 0xaa, 0xb9, 0x3d, 0x70, 0xde, 0x98, 0x6 \
  }

#define Out                 ST->ConOut
#define In                  ST->ConIn

#define SCAN_CODE_NULL      0x00
#define SCAN_CODE_UP        0x01
#define SCAN_CODE_DOWN      0x02
#define SCAN_CODE_RIGHT     0x03
#define SCAN_CODE_LEFT      0x04
#define SCAN_CODE_HOME      0x05
#define SCAN_CODE_END       0x06
#define SCAN_CODE_INS       0x07
#define SCAN_CODE_DEL       0x08
#define SCAN_CODE_PGUP      0x09
#define SCAN_CODE_PGDN      0x0A
#define SCAN_CODE_F1        0x0B
#define SCAN_CODE_F2        0x0C
#define SCAN_CODE_F3        0x0D
#define SCAN_CODE_F4        0x0E
#define SCAN_CODE_F5        0x0F
#define SCAN_CODE_F6        0x10
#define SCAN_CODE_F7        0x11
#define SCAN_CODE_F8        0x12
#define SCAN_CODE_F9        0x13
#define SCAN_CODE_F10       0x14
#define SCAN_CODE_F11       0x15
#define SCAN_CODE_F12       0x16
#define SCAN_CODE_ESC       0x17
#define CHAR_BS             0x08
#define CHAR_LF             0x0a
#define CHAR_CR             0x0d

#define KEY_EXIT_CODE       SCAN_CODE_F12

#define IS_VALID_CHAR(x)    (x == SCAN_CODE_NULL)
#define IS_DIRECTION_KEY(x) ((x >= SCAN_CODE_UP) && (x <= SCAN_CODE_PGDN))
#define IS_FUNCTION_KEY(x)  ((x >= SCAN_CODE_F1) && x <= (SCAN_CODE_F12))
#define IS_ESCAPE(x)        (x == SCAN_CODE_ESC)
#define IS_HEXCODE_KEY(x)   (((x <= '9') && (x >= '0')) || ((x <= 'F') && (x >= 'A'))  || ((x <= 'f') && (x >= 'a')))
#define IS_DECCODE_KEY(x)   ((x <= '9') && (x>= '0'))


#define TITLE_BAR               0
#define PCI_ADD_INPUT_BAR       2 
#define PCI_INFO_VENDOR_BAR     4
#define PCI_REG_NOTE_BAR        22
#define PCI_ADD_NOTE_BAR        23
#define PCI_REG_VALUE_BAR       4
#define HELP_INFO_BAR           10

#define PCI_INFO_COL            58

//
// EFI Console Colours
//
#define EFI_BLACK                 0x00
#define EFI_BLUE                  0x01
#define EFI_GREEN                 0x02
#define EFI_CYAN                  (EFI_BLUE | EFI_GREEN)
#define EFI_RED                   0x04
#define EFI_MAGENTA               (EFI_BLUE | EFI_RED)
#define EFI_BROWN                 (EFI_GREEN | EFI_RED)
#define EFI_LIGHTGRAY             (EFI_BLUE | EFI_GREEN | EFI_RED)
#define EFI_BRIGHT                0x08
#define EFI_DARKGRAY              (EFI_BRIGHT)
#define EFI_LIGHTBLUE             (EFI_BLUE | EFI_BRIGHT)
#define EFI_LIGHTGREEN            (EFI_GREEN | EFI_BRIGHT)
#define EFI_LIGHTCYAN             (EFI_CYAN | EFI_BRIGHT)
#define EFI_LIGHTRED              (EFI_RED | EFI_BRIGHT)
#define EFI_LIGHTMAGENTA          (EFI_MAGENTA | EFI_BRIGHT)
#define EFI_YELLOW                (EFI_BROWN | EFI_BRIGHT)
#define EFI_WHITE                 (EFI_BLUE | EFI_GREEN | EFI_RED | EFI_BRIGHT)

#define EFI_TEXT_ATTR(f, b)       ((f) | ((b) << 4))

#define EFI_BACKGROUND_BLACK      0x00
#define EFI_BACKGROUND_BLUE       0x10
#define EFI_BACKGROUND_GREEN      0x20
#define EFI_BACKGROUND_CYAN       (EFI_BACKGROUND_BLUE | EFI_BACKGROUND_GREEN)
#define EFI_BACKGROUND_RED        0x40
#define EFI_BACKGROUND_MAGENTA    (EFI_BACKGROUND_BLUE | EFI_BACKGROUND_RED)
#define EFI_BACKGROUND_BROWN      (EFI_BACKGROUND_GREEN | EFI_BACKGROUND_RED)
#define EFI_BACKGROUND_LIGHTGRAY  (EFI_BACKGROUND_BLUE | EFI_BACKGROUND_GREEN | EFI_BACKGROUND_RED)


#define CALC_EFI_PCI_ADDRESS(Bus,Dev,Func,Reg) \
  ( (UINT64) ( (((UINTN)Bus) << 24) + (((UINTN)Dev) << 16) + \
  (((UINTN)Func) << 8) + ((UINTN)Reg) ))

#define FIRST_RUN_MARK            0xFF
#define CHANGE_DIGITAL_MARK       0
#define FIRST_DIGITAL_MARK        1
#define END_DIGITAL_MARK          3

//EFI_HII_HANDLE    HiiHandle;
EFI_GUID          EfiPciRWGuid = EFI_PCIRW_GUID;

typedef struct {
  UINT32  Foreground : 4;
  UINT32  Background : 4;
} PEFI_FRM_COLOR_ATTRIBUTES;

typedef union {
  PEFI_FRM_COLOR_ATTRIBUTES  Colors;
  UINT8                         Data;
} PEFI_FRM_COLOR_UNION;

EFI_STATUS
SetFrmMode (
  IN UINT8  FrmMode
  )
{
  Out->EnableCursor (Out, TRUE);
  switch (FrmMode) {
    //
    //SetAttribute ( Foreground | Background<<4) colors.
    //
  case 0:
    Out->SetAttribute (Out, EFI_TEXT_ATTR (EFI_WHITE, EFI_BLACK));
    break;
  case 1:
    Out->SetAttribute (Out, EFI_TEXT_ATTR (EFI_YELLOW, EFI_RED));
    break;
  case 2:
    Out->SetAttribute (Out, EFI_TEXT_ATTR (EFI_YELLOW, EFI_BLUE));
    Out->EnableCursor (Out, FALSE);
    break;
  default:
    Out->SetAttribute (Out, EFI_TEXT_ATTR (EFI_WHITE, EFI_BLACK));
  }
  return EFI_SUCCESS;
}

EFI_APPLICATION_ENTRY_POINT(PciRW)

/*++

Routine Description:
  Initialize the PEI emulation shell command

Arguments:
  (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)

Returns:

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

Routine Description:

  Pci read & write and screen text input & output, and memory allocate & free.

Arguments:

  ImageHandle  - The image handle
  SystemTable  - The system table

Returns:

--*/

{
  EFI_INPUT_KEY                       Key;
  EFI_STATUS                          Status;
  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL			*PciIo = NULL;
  UINT8                               Bus;
  UINT8                               Dev;
  UINT8                               Func; 
  UINT8                               Cflag,Iflag;
  UINT8                               KeyStep;
  UINT8                               Reg8;
  UINT8                               Index;
  UINT8                               IndexCell,IndexRegOld;
  UINT16                              Reg16;
  CHAR16                              *DigitalString;
  UINT64                              Address; 

//
//Initilize Variables and InitializeShellApplication
//
  EFI_SHELL_APP_INIT (ImageHandle, SystemTable);

  Status          = EFI_SUCCESS;
  Cflag           = CHANGE_DIGITAL_MARK;  
  KeyStep         = 0;
  Bus             = 0;
  Dev             = 0;
  Func            = 0;
  Reg16           = 0;
  Reg8            = 0;
  Address         = 0;
  Index           = 0;
  IndexCell       = 0;
  Iflag           = FIRST_RUN_MARK;
  IndexRegOld     = 0;
  
  //Sizeof (unicode char) * ( 2 DEC number + 1 terminal char'\0' ) 
  DigitalString   = AllocatePool (2 * (2 + 1));

  Out->ClearScreen (Out);
  Out->EnableCursor (Out, TRUE);
  SetFrmMode(0);
  
  Status  = BS->LocateProtocol ( 
  						&gEfiPciRootBridgeIoProtocolGuid,
  						NULL, 
              &PciIo
              );
  
//  PrintAt (0, PROTOCOL_STATUS_BAR, L"Locate PCI Root Bridge Protocol Status = %x", Status);

  if (EFI_ERROR (Status)) {
  	return EFI_UNSUPPORTED;
  }
/*
  Bus = 0;
  Dev = 0x1f;
  Func = 0;
  
  Address = CALC_EFI_PCI_ADDRESS(Bus, Dev, Func, 0);

	Status  = PciIo->Pci.Read (
	         	        PciIo, 
	                  EfiPciWidthUint16, 
	                  Address, 
	                  1, 
	                  &Reg16
	                  );
  PrintAt (0, 22, L"\nRead Pci Status = %x\n", Status);
*/
//--------------Initilize end----------------------
  SetFrmMode(1);
  PrintAt (2 , TITLE_BAR, L"PCI Read Write Utility 0.1 by bini for study EFI application -2008.3.28");
  SetFrmMode(0);
  PrintAt (0, PCI_ADD_INPUT_BAR, L"PCI bus number:00");
  PrintAt (25, PCI_ADD_INPUT_BAR, L"PCI device number:00");
  PrintAt (50, PCI_ADD_INPUT_BAR, L"PCI function number:00");
  PrintAt (PCI_INFO_COL, HELP_INFO_BAR, L"Exit: F12");
  PrintAt (PCI_INFO_COL, HELP_INFO_BAR+1, L"Input PCI Addr:ESC");
  PrintAt (PCI_INFO_COL, HELP_INFO_BAR+2, L"Confirm Input: Enter");
  do {
    //

⌨️ 快捷键说明

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