kaurohy 发表于 2009-11-11 16:21:18

我寫了一個 File io 的 EFI shell app

/*
* myfileio.c
* Apps
*/

#include "efi.h"
#include "efilib.h"

#define FILE_ATTRIB_CREATENEW         EFI_FILE_MODE_READ| EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE

static EFI_STATUS WaitForKeyOrReset(VOID)
{
    EFI_STATUS          Status;
    EFI_INPUT_KEY       key;
    UINTN               index;
   
    for(;;) {
      Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
      if (Status == EFI_NOT_READY)
            BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
      else
            break;
    }
    if (!EFI_ERROR(Status)) {
      if (key.ScanCode == SCAN_ESC)
            RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
    }
   
    return Status;
}

EFI_STATUS
EFIAPI
MyfileioMain (IN EFI_HANDLE         ImageHandle,
             IN EFI_SYSTEM_TABLE   *SystemTable)
{
    EFI_STATUS                Status;
    EFI_HANDLE                *DestAddr;       
    EFI_LOADED_IMAGE        *FileHandle01;
    EFI_DEVICE_PATH        *FileHandle02;       
    EFI_FILE_IO_INTERFACE        *FileHandle03;
    EFI_FILE                *FileHandle04;
    EFI_FILE                *FileHandle05;
    CHAR16                *FileName;
        CHAR16      *BufferA, *BufferB, *BufferC, *Space_Key;
    UINTN                BufferSize = 8, BufferSizeKeySpace = 2;
    int         Bit0, Bit1, Key_Space;       

    FileName = L"NewFile.txt";
        Space_Key = L" ";
       
        BufferA = L"ABCD";
        BufferB = L"EFGH";
        BufferC = L"IJKL";

        Bit0 = 0xff;
        Bit1 = 0xfe;
       
        Key_Space = 0x0020;
       
    InitializeLib (ImageHandle, SystemTable);       

    DestAddr = AllocatePool (BufferSize);       

    Print (L"Value of Bit0 is %x\n", Bit0);
    Print (L"Value of Bit1 is %x\n", Bit1);       

       
    Status = BS->HandleProtocol(ImageHandle, &LoadedImageProtocol, (VOID*)&FileHandle01);
    if (EFI_ERROR(Status)) {
      Print (L"Could not obtain Image, FileHandle01 err code is %x\n",Status);
      return EFI_LOAD_ERROR;
    }

    Status = BS->HandleProtocol(FileHandle01->DeviceHandle,&DevicePathProtocol,(VOID*)&FileHandle02);
    if (EFI_ERROR(Status)) {
            Print (L"Could not obtain Device Path, FileHandle02 err code is %x\n",Status);
            return EFI_LOAD_ERROR;
      }       
       
    Status = BS->HandleProtocol (FileHandle01->DeviceHandle,&FileSystemProtocol,(VOID*)&FileHandle03);
    if (EFI_ERROR(Status)) {
            Print (L"Could not obtain File System, FileHandle03 err code is %x\n",Status);
            return EFI_LOAD_ERROR;
      }

    Status = FileHandle03->OpenVolume(FileHandle03, &FileHandle04);
    if (EFI_ERROR(Status)) {
      Print (L"Could not open volume, FileHandle04 err code is %x\n",Status);
      return EFI_LOAD_ERROR;
    }               
               
    Status = FileHandle04->Open(FileHandle04, &FileHandle05, FileName, FILE_ATTRIB_CREATENEW, 0x20);
    if (EFI_ERROR(Status)) {
      Print (L"Could not open file, FileHandle05 err code is %x\n",Status);
      return EFI_LOAD_ERROR;
    }
       
    Status = FileHandle05->SetPosition(FileHandle05, 0);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit0);
        Print (L"File Buffersize is %x\n\n", BufferSize);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
      return EFI_SUCCESS;
    }       

    Status = FileHandle05->SetPosition(FileHandle05, 1);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSize, &Bit1);
        Print (L"File Buffersize is %x\n\n", BufferSize);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
      return EFI_SUCCESS;
    }               
       
    Status = FileHandle05->SetPosition(FileHandle05, 2);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferA);
        Print (L"File Buffersize is %x\n\n", BufferSize);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n\n",Status);
      return EFI_SUCCESS;
    }       

//--------------------------------------------------------------------------------------------------------------------------------------------------------------->>>
    Print (L"Before Read FileHandle05 file Data of BufferB is %s\n\n",BufferB);
    Status = FileHandle05->SetPosition(FileHandle05, 2);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }

    Status = FileHandle05->Read(FileHandle05, &BufferSize, BufferB);
    if (EFI_ERROR(Status)) {
      Print (L"Could not read file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }       
        Print(L"1. After Read FileHandle05 file of Data of BufferSize is %d and Data of BufferB is %s\n\n", BufferSize, BufferB);
//---------------------------------------------------------------------------------------------------------------------------------------------------------------<<<

    Status = FileHandle05->SetPosition(FileHandle05, 10);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }

    Status = FileHandle05->SetPosition(FileHandle05, 12);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }

    Status = FileHandle05->SetPosition(FileHandle05, 14);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }

    Status = FileHandle05->SetPosition(FileHandle05, 16);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSizeKeySpace, Space_Key);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }       

//---------------------------------------------------------------------------------------------------------------------------------------------------------------

    Status = FileHandle05->SetPosition(FileHandle05, 18);       
    if (EFI_ERROR(Status)) {
      Print (L"Could not do SetPositioon to the file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Status = FileHandle05->Write(FileHandle05, &BufferSize, BufferC);
    if (EFI_ERROR(Status)) {
      Print (L"Could not write file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }       
       
    FreePool(DestAddr);       

    Status = FileHandle05->Close(FileHandle05);
    if (EFI_ERROR(Status)) {
      Print (L"Could not close file, FileHandle05 err code is %x\n",Status);
      return EFI_SUCCESS;
    }
       
    Print(L"File Name = %s has already been created.\n", FileName);

    Print(L"\nPress ESC to reboot or any other key to return control to the firmware.\n\n");
    WaitForKeyOrReset();

    return Status;
}

chenhy 发表于 2009-11-30 09:50:02

pci register access sample

//**********************************************************************
//**********************************************************************
//** Demo code for PCI configuration space I/O access program **
//** By Lawrence 2009/11/20 @Taiwan**                        
//**********************************************************************
//**********************************************************************

#include "efi.h"
#include "efilib.h"

#define        Not_Found_Me        0x00000000
#define        Found_Me        0x00000001

static EFI_STATUS WaitForKeyOrReset(VOID)
{
    EFI_STATUS          Status;
    EFI_INPUT_KEY       key;
    UINTN               index;
   
    for(;;) {
      Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &key);
      if (Status == EFI_NOT_READY)
            BS->WaitForEvent(1, &ST->ConIn->WaitForKey, &index);
      else
            break;
    }
    if (!EFI_ERROR(Status)) {
      if (key.ScanCode == SCAN_ESC)
            RT->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
    }
   
    return Status;
}

UINTN
AccessPciConfig (
    INEFI_PCI_IO_PROTOCOL            *PCI_DEVICE_TARGET
    )
{
        UINT8i;
        UINTNPCI_ATTR;
        UINT8PCI_REG, pci_offset=0;

        //get device attr
        PCI_DEVICE_TARGET->GetLocation(PCI_DEVICE_TARGET,PCI_ATTR,PCI_ATTR+1,PCI_ATTR+2,PCI_ATTR+3);       
    if ((PCI_ATTR == 0x00) && (PCI_ATTR == 0x1e) && (PCI_ATTR == 0x00)){
          Print (L"Device found at BUS = %02x         DEV= %02x      FUN= %02x\n",PCI_ATTR,PCI_ATTR,PCI_ATTR);
          ST->ConOut->SetAttribute (ST->ConOut, EFI_TEXT_ATTR (EFI_GREEN, EFI_BLACK));       
          //print register value
          for (i = 0;i < 4;i++)
          {
      PCI_DEVICE_TARGET->Pci.Read (PCI_DEVICE_TARGET,0,pci_offset,4,PCI_REG);
                Print(L"Register0x%d value = %02x \n",i, PCI_REG);
          }
          Print(L"\n");          
          ST->ConOut->SetAttribute (ST->ConOut, EFI_TEXT_ATTR (EFI_WHITE, EFI_BLACK));                  
          return Found_Me;
    }

        return Not_Found_Me;
}

EFI_STATUS
InitializePciApplication (
    IN EFI_HANDLE         ImageHandle,
    IN EFI_SYSTEM_TABLE   *SystemTable
    )
{
EFI_STATUS            Status;
EFI_HANDLE            *Handle;
UINTN                   BufferSize, Result;
UINTN                   NoHandles, Index;
EFI_PCI_IO_PROTOCOL          *PCI_DEVICE_TARGET;

    //
    // Initialize the Library.
    //
    InitializeLib (ImageHandle, SystemTable);
    //
    // Find the PCI driver
    //
    Handle = NULL;
    BufferSize = 1;
    do
    {
      if (Handle)
      {
      FreePool (Handle);
      }
               
      Handle = AllocatePool (BufferSize);
      Status = BS->LocateHandle (ByProtocol, &PciIoProtocol, NULL, &BufferSize, Handle);

    } while (Status == EFI_BUFFER_TOO_SMALL);
    ASSERT (!EFI_ERROR(Status));

    NoHandles= BufferSize / sizeof(EFI_HANDLE);
    Index = 0;
    ST->ConOut->ClearScreen (ST->ConOut);
    Result = Not_Found_Me;       
    do
    {       
        if(Index == NoHandles - 1)
         Index=0;
          else
    Status = BS->HandleProtocol (Handle, &PciIoProtocol, (VOID*)&PCI_DEVICE_TARGET);
        Print(L"THIS IS DEVICE %d \n",Index);
    if (!EFI_ERROR(Status))
    {
          Result = AccessPciConfig(PCI_DEVICE_TARGET);
//-          Print(L"Again now Result is %x \n",Result);          
    }
    Index++;               
    } while(Result == Not_Found_Me);
       
        Print(L"Please hit any key to exit this image.");       
    WaitForSingleEvent (ST->ConIn->WaitForKey, 0);
               
    return EFI_SUCCESS;
}

xjf2088 发表于 2010-4-16 14:47:39

感激不尽

小弟对你有如滔滔江水。。。

dhua 发表于 2010-4-30 14:51:38

都是强人。向你们学习!

shaopy 发表于 2010-8-23 12:52:26

楼主 好厉害 我接受bios tranning 两周也没有写出这样的的底层程式!楼主强人哈!学习了!

蓝色永恒 发表于 2010-8-23 14:07:13

景仰一下,哈哈

zywwin 发表于 2012-5-21 08:57:06

新手,学习
页: 1 [2]
查看完整版本: [原创]EFI application:PCI read & write 程序源码