關於我自己

2017年9月24日 星期日

RW Memory


// program
//DJgpp compiler
//

#include <stdio.h>
#include <string.h>

#ifndef OS_TYPE
#define OS_TYPE 0
#endif

#ifdef  __linux
#undef  OS_TYPE
#define OS_TYPE 2
#endif


#if (OS_TYPE == 0)
//----------------------------------------------------------------------------
//          DOS_SUPPORT(Default)
//----------------------------------------------------------------------------
//  <  --  http://www.delorie.com/djgpp/doc/libc/libc_473.html --  >
#include <pc.h>  //nportb inportl outportb outportl
//  <  --  http://www.delorie.com/djgpp/doc/libc/libc_128.html --  >
#include <dos.h> // 1sec = delay(1000)                       */
//#include  <unistd.h> //1s=sleep(1) 1s=usleep(1000000)


#elif (OS_TYPE ==2) || (OS_TYPE ==5)
//----------------------------------------------------------------------------
//          LINUX_SUPPORT
//----------------------------------------------------------------------------
//  <  --  http://man7.org/linux/man-pages/man2/outb_p.2.html --  >
#include <sys/io.h> //IO

//  <  --  http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html --  >
#include <unistd.h> //1s=sleep(1) 1s=usleep(1000000)
                                       
                                       
#elif (OS_TYPE ==1) || (OS_TYPE ==4)

     #if WINIO_SUPPORT_BUILD && WINIO_SUPPORT
            //----------------------------------------------------------------------------
            //   WINDOWSx86/x64_SUPPORT
            //----------------------------------------------------------------------------
        //  <  -- WINIO http://www.internals.com/ --  >
         #include <windows.h> //IO. Sleep   1s=Sleep(1000)
                                               
                                                   
   #endif//#if WINIO_SUPPORT_BUILD && WINIO_SUPPORT
#endif//#elif (OS_TYPE ==1) || (OS_TYPE ==4)


#define DEV_PACKAGE_ID      mini_v1.01.8

//----------------------------------------------------------------------------
//      Global Define  
//----------------------------------------------------------------------------

#ifndef IN
#define IN
#endif

#ifndef OUT
#define OUT
#endif


#ifndef NULL
#define NULL  ((VOID *) 0)
#endif

#ifndef TRUE
#define TRUE  ((BOOLEAN) (1 == 1))
#endif

#ifndef FALSE
#define FALSE ((BOOLEAN) (0 == 1))
#endif
//----------------------------------------------------------------------------
//        ERROR CODE      
//----------------------------------------------------------------------------
//typedef UINT8 EFI_STATUS;

#define EFI_SUCCESS               0
#define EFI_FAIL               0x01
#define EFI_SHOW_NOTHING       0x02
#define EFI_SHOW_DATA          0x03



//----------------------------------------------------------------------------
//      Define  BIT MAP    
//----------------------------------------------------------------------------
#ifndef BIT0          
#define BIT0              0x01
#define BIT1              0x02
#define BIT2              0x04
#define BIT3              0x08
#define BIT4              0x10
#define BIT5              0x20
#define BIT6              0x40
#define BIT7              0x80
#endif

#ifndef BIT00        
#define BIT00             0x01
#define BIT01             0x02
#define BIT02             0x04
#define BIT03             0x08
#define BIT04             0x10
#define BIT05             0x20
#define BIT06             0x40
#define BIT07             0x80
#define BIT08           0x0100
#define BIT09           0x0200
#define BIT10           0x0400
#endif

#ifndef BIT11
#define BIT11           0x0800
#define BIT12           0x1000
#define BIT13           0x2000
#define BIT14           0x4000
#define BIT15           0x8000
#define BIT16       0x00010000
#define BIT17       0x00020000
#define BIT18       0x00040000
#define BIT19       0x00080000
#define BIT20       0x00100000
#define BIT21       0x00200000
#define BIT22       0x00400000
#define BIT23       0x00800000
#define BIT24       0x01000000
#define BIT25       0x02000000
#define BIT26       0x04000000
#define BIT27       0x08000000
#define BIT28       0x10000000
#define BIT29       0x20000000
#define BIT30       0x40000000
#define BIT31       0x80000000
#endif


//---------------------------//
//      Define GPIO         //
//---------------------------//
#define IS_GPIO                 1
#define IS_NOT_GPIO             0
#define IS_HIGH                         1
#define IS_LOW                          0
#define IS_GPI                        1
#define IS_GPO                        0
#define IS_OD                   1



//---------------------------//
//      Define Macro         //
//---------------------------//
//MATH
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )

//TO UPPER CASE
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )

//GET ARRY SIZE
#define ARRY_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )


//println
#define printfln(x); printf((x)); printf("\n");

//Strize
#define _STR(s) #s
#define STR(s) _STR(s)

//Check Hex
#define HEXCHK( c )  \
                                      ( \
                                       ((c) >= '0' && (c) <= '9') || \
                                         ((c) >= 'A' && (c) <= 'F') || \
                                         ((c) >= 'a' && (c) <= 'f')    \
                                        )


//---------------------------//
//      RW MARCO             //
//---------------------------//
#define IOWrite8(x , y)      WriteIO8( x , y)
#define IORead8( x )         ReadIO8( x )
#define IOWrite32(x , y)     WriteIO32( x , y)
#define IORead32(x )         ReadIO32( x )



//---------------------------//
//      Define   Size        //
//---------------------------//
#if OS_TYPE == 0 //  DOS  //
  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;

#elif OS_TYPE == 1 //  winx86 //

  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;


#elif OS_TYPE == 2//  linux86 //
  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;
#elif OS_TYPE == 3 //  EFI_SHELLx86 //
  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;
#elif OS_TYPE == 4//  winx64 //
  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;
#elif OS_TYPE == 5//  linxux64 //
  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;
el#if OS_TYPE == 6 //  EFI_SHELLx64 //
  typedef unsigned long  long      UINT64;
  typedef unsigned int             UINT32;
  typedef unsigned short           UINT16;
  typedef unsigned char            UINT8;
  typedef UINT64                   UINTN;
#endif

typedef UINT8 boolean;
typedef boolean BOOLEAN;
//---------------------------//
//      IO  RW  typedefine   //
//---------------------------//

  #if (OS_TYPE ==1) || (OS_TYPE ==4) //WINDOWSx86/x64_SUPPORT
    #if WINIO_SUPPORT_BUILD && WINIO_SUPPORT
      typedef BOOL (WINAPI   *  DLL_InitializeWinIo)();
      DLL_InitializeWinIo InitializeWinIo;
      typedef BOOL (WINAPI   *  DLL_RemoveWinIoDriver)();
      DLL_RemoveWinIoDriver RemoveWinIoDriver;
      typedef void (WINAPI   *  DLL_ShutdownWinIo)();
      DLL_ShutdownWinIo ShutdownWinIo;
      typedef BOOL (WINAPI   *  DLL_GetPortVal)(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize);
      DLL_GetPortVal GetPortVal;
      typedef BOOL (WINAPI   *  DLL_SetPortVal)(WORD wPortAddr, DWORD dwPortVal, BYTE bSize);
      DLL_SetPortVal SetPortVal;
    #elif !WINIO_SUPPORT_BUILD && WINIO_SUPPORT      
      typedef UINT8 BOOL;
      typedef UINT16 WORD;
      typedef UINT32 DWORD;
      typedef UINT32 *PDWORD;
    #endif//WINIO_SUPPORT_BUILD && WINIO_SUPPORT
  #endif

   
//--------------------------//
//    Argv typedef          //
//--------------------------//

  typedef int (*Project_ptr)(int *argc, char *argv[]) ;
  typedef Project_ptr FUNC_PTR;
  typedef struct _ONE_COMMAND{
  char        *argv1;
  char        *argv1_t;
  FUNC_PTR    Func_ptr;
  }ONE_COMMAND;
 


//--------------------------//
//    Global typedef        //
//--------------------------//
typedef UINT8 EFI_STATUS;


//--------------------------//
//    Global String        //
//--------------------------//

#if OS_TYPE == 0
char OSstring []="DOS";
#elif OS_TYPE == 1
char OSstring []="Windows_x86";
#elif OS_TYPE == 2
char OSstring []="Linux";
#elif OS_TYPE == 3
char OSstring []="UEFI_SHELL";
#elif OS_TYPE == 4
char OSstring []="Windows_x64";
#elif OS_TYPE == 5
char OSstring []="Linux";
#elif OS_TYPE == 6
char OSstring []="UEFI_SHELL";
#endif



//--------------------------//
//     RW  Variable         //
//--------------------------//
#if (OS_TYPE ==1) || (OS_TYPE ==4)
#if WINIO_SUPPORT_BUILD && WINIO_SUPPORT
HINSTANCE hDLL;

#endif
#endif


//--------------------------//
//     RW  Function Link    //
//--------------------------//
void CHECK_EFI_SIZE();
void WriteIO8(IN UINT16  address,IN UINT8   value);
void WriteIO32(IN UINT16  address,IN UINT32   value);
UINT8 ReadIO8(IN UINT16);
UINT32 ReadIO32(IN UINT16);
#if (OS_TYPE !=0)
void outportb(UINT16 reg, UINT8 val);
void outportl(UINT16 reg, UINT32 val);
UINT8 inportb(UINT16 reg);
UINT32 inportl(UINT16 reg);
void delay(int val);
#endif

#if (OS_TYPE ==1) || (OS_TYPE ==4)
  #if WINIO_SUPPORT_BUILD && WINIO_SUPPORT
    BOOL Enable_WinIO();
    BOOL Free_WinIO();
  #elif !WINIO_SUPPORT_BUILD && WINIO_SUPPORT
    void SetPortVal (UINT32 address , UINT32  data , UINT8 byte);
    void GetPortVal (UINT32 address , UINT32 *data , UINT8 byte);
    void Sleep(UINT32 input);
    BOOL Enable_WinIO();
    BOOL Free_WinIO();
  #endif

#endif
//--------------------------//
//    debug Function Link    //
//--------------------------//
void UnusedVariable_error(void *  compilerWarning);

//--------------------------//
//    Argv Function Link    //
//--------------------------//




//<_START>
//-----------------------------------------------------------------------------------------------//
// FUNCTION   : Argv_Init
// INPUT      : argc,argv,*CommandTable,CommandTableSize
// OUTPUT     :
// DESCRIPTION:
//
//-----------------------------------------------------------------------------------------------//
//<_END>
int
Argv_Init(
  IN    int                 *argc,
  IN    char                *argv[],
  IN  int         checkPoint,
  IN    ONE_COMMAND *CommandTable,
  IN    int                 CommandTableSize
){
  int           status;
  UINT16            i,size;
  ONE_COMMAND *table;


  status     = EFI_FAIL;
  table          = CommandTable;
  size       = CommandTableSize;
 


  for(i=0;i<size;i++)
  {
      if(( strcmp(*(argv+checkPoint), (table+i)->argv1 ) == 0 ) || (strcmp(*(argv+checkPoint), (table+i)->argv1_t ) == 0))
       {
       
        status = (table+i)->Func_ptr(argc,argv);
 
        break;
       
        }
      else
         status = EFI_FAIL;      
     }

 
return status;
}




//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure    : CHECK_EFI_SIZE
// input        : address,value
// output       :
// Description  : check unsigned size by RD
//
//-----------------------------------------------------------------------------------------------//
//<_START>
void
CHECK_EFI_SIZE(
)
{
int *p;
            printf("sizeof(unsigned long )        = %d \n", (UINT32)sizeof(unsigned long)*8);
            printf("sizeof(unsigned long long)    = %d \n", (UINT32)sizeof(unsigned long long)*8);
            printf("sizeof(unsigned char)         = %d \n", (UINT32)sizeof(unsigned char)*8);
            printf("sizeof(unsigned short)        = %d \n", (UINT32)sizeof(unsigned short)*8);
            printf("sizeof(unsigned int)          = %d \n", (UINT32)sizeof(unsigned int)*8);
            printf("sizeof(*p)                    = %d \n", (UINT32)sizeof(p)*8);
}


//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : WriteIO8
// input        : UINT16 address,UINT8 value
// output       :
// Description      : Provid WritIO with delay
//
//-----------------------------------------------------------------------------------------------//
//<_START>
void
WriteIO8(
      IN UINT16  address,
      IN UINT8   value
){
 delay(1);//Don't Change

 outportb( address , value);
}

//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : ReadIO8
// input          : UINT16 address
// output         : UINT8  data
// Description    : Provid ReadIO with delay
//
//-----------------------------------------------------------------------------------------------//
//<_START>
UINT8
ReadIO8(
  IN UINT16 address
){
 delay(1);
 return inportb(address);//Don't Change
}


//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : WriteIO32
// input          : UINT16 address,UINT8 value
// output         :
// Description    : Provid WritIO with delay
//
//-----------------------------------------------------------------------------------------------//
//<_START>
void
WriteIO32(
      IN UINT16  address,
      IN UINT32  value
){
 delay(1);
 outportl( address , value);//Don't Change
}

//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : ReadIO32
// input          : UINT16 address
// output         : UINT32  data
// Description    : Provid ReadIO with delay
//
//-----------------------------------------------------------------------------------------------//
//<_START>
UINT32
ReadIO32(
  IN UINT16 address
){
 delay(1);
 return inportl(address);//Don't Change
}


//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : RWIO8
// input          : UINT16 address,UINT8 mask,UINT8 value
// output         :
// Description    : Provid WritIO with delay
//
//-----------------------------------------------------------------------------------------------//
//<_START>          
void
RWIO8(
      IN UINT16    address,
      IN UINT8     mask,
      IN UINT8     value
){

  WriteIO8(address,  (ReadIO8(address)  & mask )| value  );

}
//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : RWIO32  
// input          : UINT16 address,UINT32 mask,UINT32 value
// output         :
// Description    : Provid WritIO with delay
//
//-----------------------------------------------------------------------------------------------//
//<_START>          
void
RWIO32(
      IN UINT16    address,
      IN UINT32     mask,
      IN UINT32     value
){

  WriteIO32(address,  (ReadIO32(address)  & mask )| value  );

}

//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : UnusedVariable_error
// input          : void *
// output         :
// Description    : some variable for debug,but no use to user,
//          those variable will cause warning, but no matter.
//-----------------------------------------------------------------------------------------------//
//<_START>          
void
UnusedVariable_error(
        IN void *compilerWarning
){

}



//<_START>
//-----------------------------------------------------------------------------------------------//
// FUNCTION   : LOW LEVEL IO Porting
//-----------------------------------------------------------------------------------------------//
//<_END>
#if (OS_TYPE ==2) || (OS_TYPE == 5)//LINUX_SUPPORT

//WRITE
void
  outportb(UINT16 reg, UINT8 val){
  outb_p(val, reg);
}


void
  outportl(UINT16 reg, UINT32 val){
  outl_p(val, reg);
}

//READ
UINT8
  inportb(UINT16 reg){
  return inb_p(reg);
}

UINT32
inportl
(UINT16 reg)
{
  return inl_p(reg);
}

//Delay

void
  delay(int val)
{
usleep(val*1000); //Linux
//1 sec = usleep(1000000) //value = 1,000,000~0
//1 sec = sleep(1)
}
#elif (OS_TYPE ==1) || (OS_TYPE ==4) //WINDOWSx86/x64_SUPPORT

  #if WINIO_SUPPORT_BUILD && WINIO_SUPPORT
    BOOL Enable_WinIO(){
    BOOL bRet = EFI_FAIL;
   
  #if defined(_WIN64)
    hDLL = GetModuleHandle((LPCWSTR)L"WinIo64.dll");
    if(hDLL == NULL)
      hDLL = LoadLibrary((LPCWSTR)L"WinIo64.dll");
  #elif defined(_WIN32)
    hDLL = GetModuleHandle((LPCWSTR)L"WinIo32.dll");
    if(hDLL == NULL)
      hDLL = LoadLibrary((LPCWSTR)L"WinIo32.dll");
  #endif
   


    if (hDLL == NULL)
        {
          printf("Error load winio.dll fail. %08X\n", GetLastError());  
          return EFI_FAIL;
        }

    InitializeWinIo = (DLL_InitializeWinIo)GetProcAddress(hDLL, ("InitializeWinIo"));
    if (!InitializeWinIo)
        {
          // handle the error
          printf("Error InitializeWinIo fail. %08X\n", GetLastError());  
          FreeLibrary(hDLL);
          return EFI_FAIL;
        }

    RemoveWinIoDriver = (DLL_RemoveWinIoDriver)GetProcAddress(hDLL, ("RemoveWinIoDriver"));
    if (!RemoveWinIoDriver)
        {
          // handle the error
          printf("Error RemoveWinIoDriver fail. %08X\n", GetLastError());  
          FreeLibrary(hDLL);
          return EFI_FAIL;
        }  

    ShutdownWinIo = (DLL_ShutdownWinIo)GetProcAddress(hDLL, ("ShutdownWinIo"));
    if (!ShutdownWinIo)
        {
          // handle the error
          printf("Error ShutdownWinIo fail. %08X\n", GetLastError());  
          FreeLibrary(hDLL);
          return EFI_FAIL;
        }


    GetPortVal = (DLL_GetPortVal)GetProcAddress(hDLL, ("GetPortVal"));
    if (!GetPortVal)
        {
          // handle the error
          printf("Error GetPortVal fail. %08X\n", GetLastError());  
          FreeLibrary(hDLL);
          return EFI_FAIL;
        }

    SetPortVal = (DLL_SetPortVal)GetProcAddress(hDLL, ("SetPortVal"));
    if (!SetPortVal)
        {
        // handle the error
        printf("Error SetPortVal fail. %08X\n", GetLastError());  
        FreeLibrary(hDLL);
        return EFI_FAIL;
        }


        bRet = InitializeWinIo();
        if (!bRet)  
        {
          printf("Error during initialization of WinIo. %08X\n", GetLastError());
          FreeLibrary(hDLL);
          return EFI_FAIL;
        }
    return EFI_SUCCESS;
  }

  BOOL Free_WinIO(){
    BOOL bRet = FALSE;
    //if(OI_status != FALSE)
    bRet = RemoveWinIoDriver();
    ShutdownWinIo ();
    FreeLibrary(hDLL);  
    return bRet;
  }

//WRITE
void
outportb
(UINT16 reg, UINT8 val)
{
  SetPortVal(reg, (UINT32)val, 1 );
}


void
outportl
(UINT16 reg, UINT32 val)
{
  SetPortVal(reg, val , 4 );
}

//READ
UINT8
inportb
(UINT16 reg)
{
  UINT32 data;

  GetPortVal(reg , (PDWORD)&data , 1);
  return  (UINT8)(data);
}

UINT32
inportl
(UINT16 reg)
{
  UINT32 data;
  GetPortVal(reg , (PDWORD)&data , 4);
  return  data;
}

//Delay
void
  delay(int val)
{
  Sleep(val);
  //1 sec = Sleep(1000) //Windows
}
#elif !WINIO_SUPPORT_BUILD && WINIO_SUPPORT
  void SetPortVal (UINT32 address , UINT32 data , UINT8 byte){

  }

  void GetPortVal (UINT32 address , UINT32 *data , UINT8 byte){

   }


  void Sleep(UINT32 input){

 }


  BOOL Enable_WinIO(){

  return EFI_SUCCESS;
}

  BOOL Free_WinIO(){
    return EFI_SUCCESS;

}

 
//WRITE
void
outportb
(UINT16 reg, UINT8 val)
{

}


void
outportl
(UINT16 reg, UINT32 val)
{

}

//READ
UINT8
inportb
(UINT16 reg)
{

  UINT32 data=0;


  return  (UINT8)(data);
}

UINT32
inportl
(UINT16 reg)
{
  UINT32 data=0;
 
  return  data;
}

//Delay

 void
  delay(int val)
 {
 
 }
#endif //END notsupport WinIO in build

#endif //END OS_TYPE

//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : IO_Init
// input          :
// output         :
// Description    : If init IO fail,will report fail to stop project
//-----------------------------------------------------------------------------------------------//
//<_START>
int
IO_Init(
){
  int               status;
  BOOLEAN     OI_status;

  status        = EFI_SUCCESS;
  OI_status = FALSE;
 
       
  // - Linux - Enable High IO//
  #if( (OS_TYPE ==2) || (OS_TYPE == 5) )
    OI_status = iopl(3);
    if (OI_status == 0xFF ) {
    printf("Inital IO Fail\n");
    status = EFI_FAIL;
    }  
   
  //Suppoer Windows Enable Win IO
    #elif (WINIO_SUPPORT ==1) && ( (OS_TYPE ==1) || (OS_TYPE ==4) ) //Winx86 & Win_x64
    OI_status = Enable_WinIO();
    if(OI_status == EFI_FAIL) {
    printf("Inital IO Fail");
    getchar();
    status = EFI_FAIL;
    }
  #endif
 
  UnusedVariable_error( (void *)&OI_status);
  return status;
}

//<_START>
//-----------------------------------------------------------------------------------------------//
// Procedure      : IO_Free
// input          : void *
// output         :
// Description    : If free IO fail, don't have any error message
//-----------------------------------------------------------------------------------------------//
//<_START>
void
IO_Free(
){
  BOOLEAN    OI_status;
 
  OI_status = FALSE;
 
  // - Linux - //
  #if( (OS_TYPE ==2) || (OS_TYPE == 5) )
  iopl(0);
  //Suppoer Windows Free WinIO
  #elif (WINIO_SUPPORT ==1) && ( (OS_TYPE ==1) || (OS_TYPE ==4) ) //Winx86 & Win_x64
  OI_status = Free_WinIO();
  //  getchar();
  #endif


  //Windows compiler stop before release
  #if  (WINIO_SUPPORT_BUILD == 0) && ( (OS_TYPE ==1) || (OS_TYPE ==4) ) //Winx86 & Win_x64
  printf("Finish\n");
  getchar();
  #endif
 
  UnusedVariable_error((void *)&OI_status);
}





沒有留言: