ami uefi bios 借鸡下蛋 之 在线更新bios 之内容

UEFI SPI Flash 操作详解
本文详细介绍了 UEFI SPI Flash 的操作方法,包括 SPI Flash 的读写、擦除等核心功能实现。通过具体的代码示例,展示了如何利用 UEFI 的相关 API 进行 SPI Flash 的编程,并提供了实用的函数和流程说明。

#include  <Uefi.h>
#include  <Library/UefiLib.h>
//#include  <Library/ShellCEntryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include  <Protocol/UsbIo.h>

#include  <Protocol/AmiUsbController.h>


#include <AmiLib.h>
#include <AmiDxeLib.h>
#include <AmiDxeLib.h>

#include <Protocol/Smbios.h>
#include "EfiKey.h"


#include <Uefi.h>  
#include <Library/UefiLib.h>
#include <IndustryStandard/SmBios.h>
//#include <Library/UefiShellDebug1CommandsLib.h>
//#include <Library/LibSmbiosView.h> 


#include "HomeWorkDxe.h"

extern EFI_BOOT_SERVICES         *gBS;

extern EFI_BOOT_SERVICES     *pBS;

extern EFI_HANDLE                     gImageHandle;


#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include  <Protocol/UsbIo.h>

#include  <Protocol/AmiUsbController.h>


#include <AmiDxeLib.h>

#include <Protocol/Smbios.h>
#include "EfiKey.h"


#include  <Uefi.h>
#include  <Library/UefiLib.h>
//#include  <Library/ShellCEntryLib.h>
#include  <Protocol/Spi.h>
#include  <Library/UefiBootServicesTableLib.h>


EFI_GUID  gEfiUsbIoProtocolGuid   = 
    { 0x2B2F68D6, 0x0CD2, 0x44CF, 
        { 0x8E, 0x8B, 0xBB, 0xA2, 0x0B, 0x1B, 0x5B, 0x75 }};

EFI_GUID  gEfiSimpleTextInputExProtocolGuid = 
    {0xdd9e7534, 0x7762, 0x4698, 
        { 0x8c, 0x14, 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa }};


EFI_GUID  ggEfiHomeWorkProtocolGuid = 
    {0x47590bea, 0x6178, 0x498d, 
        { 0xa9, 0x5, 0x3c, 0xe6, 0x63, 0xc3, 0x84, 0xd9 }};

#if 1

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Sprintf
//
// Description:
//  UINTN Sprintf(OUT CHAR8 *Buffer, IN CHAR8 *Format, IN ...) produces a
// null-terminated ASCII string in the output Buffer.  The ASCII string is
// produced by parsing the format string specified by Format.  Arguments are 
// pulled from the variable argument list based on the contents of the format
// string.  The number of ASCII characters in the produced output buffer is
// returned, not including the null-terminator. See notes for format string 
// information.
//
// Input:
//  OUT CHAR8 *Buffer
// Pointer to a null-terminated output ASCII string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN CHAR8 *Format
// Pointer to a null-terminated format ASCII string.
//
//  IN ...
// Variable argument list which provides the data/variables used within the
// format string.
//
// Output:
//  UINTN number of ASCII characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  va_start
//  Sprintf_va_list
//  va_end
// 
// Notes:
//  Objects inside the format string have the following syntax.
//   %[flags][width]type
//
//  *** [flags] ***
//  
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
// . Flag  .  Description
// .       . 
// . -     .  The field is left justified.  If flag is not specified, then 
// .       . the field is right justified.
// .       . 
// . space .  Prefix a space character to a number.  Only valid for types X, 
// .       . x, and d.
// .       .  
// . +     .  Prefix a plus character to a number.  Only valid for types X,
// .       . x, and d.  If both space and `+' are specified, then space is
// .       . ignored.
// .       .
// . 0     .  Pad with `0' characters to the left of a number.  Only valid
// .       . for types X, x, and d.
// .       .
// . L, l  .  The number being printed is a UINT64.  Only valid for types X,
// .       . x, and d.  If this flag is not specified, then the number being
// .       . printed is an int.
// .       .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
//  
//  NOTE
//   All invalid [flags] are ignored.
//
// *** [width] ***
//
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
// . Width  .  Description
// .        .
// . *      .  The width of the field is specified by a UINTN argument in the
// .        . argument list.
// .        .
// . Number .  The number specified as a decimal value represents the width of
// .        . the field.
// .        .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
//
// NOTE
//  If [width] is not specified, then a field width of 0 is assumed.
//
// *** type ***
// 
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
// . Type  .  Description
// .       .
// . %     .  Print a `%'.
// .       .
// . c     .  The argument is an ASCII character. 
// .       .
// . x     .  The argument is a hexadecimal number.  The characters used are 
// .       . 0..9 and a..f.  If the flag `l' is not specified, then the
// .       . argument is assumed to be an int.
// .       .
// . X     .  The argument is a hexadecimal number.  The characters used are 
// .       . 0..9 and A..F.  If the flag `l' is not specified, then the
// .       . argument is assumed to be an int.
// .       .
// . d     .  The argument is a decimal number.  If the flag `l' is not
// .       . specified, then the argument is assumed to be an int.
// .       .
// . i     .  The same as `d'.
// .       .
// . s     .  The argument is a pointer to null-terminated ASCII string.
// .       .
// . a     .  The same as `s'.
// .       .
// . S     .  The argument is a pointer to a null-terminated Unicode string.
// .       .
// . g     .  The argument is a pointer to a GUID structure.  The GUID is
// .       . printed in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
// .       .
// . G     .  The argument is a pointer to a GUID structure.  The GUID is
// .       . printed in the format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
// .       .
// . r     .  The argument is an EFI_STATUS value.  This value is converted
// .       . to a string.
// .       .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
//
// NOTE
//  All invalid type characters are copied into the result string.
//
//<AMI_PHDR_END>
//*************************************************************************


int macdbg_dmphex_kernel(const char* buff_in, int len)
{
    int retval = 0; 
    int x, y, tot, lineoff;
    const char* curr;
    CHAR16  buff[1024];
    UINTN index;
                                           

    DEBUG( (EFI_D_INFO, "dump addr -> 0x%x: \n", buff_in) );
    //DEBUG((EFI_D_INFO, "len = 0x%x.\n", len ));

    
    
    lineoff = 0;
    curr = buff_in;
    tot = 0;

    for( x = 0; x+16 < len; ){         
         index = 0x00;
         memset( buff, 0x00, sizeof(buff) );       
         Swprintf( &buff[index], L"0x%04x:  ", lineoff );
         index = Wcslen(buff);

         //DEBUG((EFI_D_INFO, "index = %d\n", index ));
         //DEBUG((EFI_D_INFO, "debug.0 = %s\n", buff  ));
         //DEBUG((EFI_D_INFO, "debug.1 = %S\n", buff  ));

         

         
         for( y = 0; y < 16; y++ ){
             Swprintf( &buff[index], L"%02x ", (unsigned char)*(curr + y) );             
              index = Wcslen(buff);
         }

         //DEBUG((EFI_D_INFO, "debug.2 = %s\n", buff  ));

         
         Swprintf( &buff[index], L"%s", L"    " );
         index = Wcslen(buff);
         for( y = 0; y < 16; y++ ){
              char c;
              c = *(curr + y);
              if( c > 31 && c < 127 ){
                  Swprintf( &buff[index], L"%c", c );
              }else{
                  Swprintf( &buff[index], L"%c", L'.' );
              }
              index = Wcslen(buff);
              tot++;
         }
         curr += 16;
         x += 16;
         lineoff+=16;
         Swprintf( &buff[index], L"%s", L"\n" );
         //printk("%s", buff);

         
         //DEBUG((EFI_D_INFO, "debug.2 = %s\n", buff    ));
         DEBUG((EFI_D_INFO, "%s", buff));
         
         //DEBUG((EFI_D_INFO, "debug.3 = %S\n", buff    ));
    }
     
    //do last line
    //Ser_Printf("tot %d.\r\n", tot );
    //Ser_Printf("len %d.\r\n", len );
    index = 0x00;
    memset( buff, 0x00, sizeof(buff) );
    if( tot < len ){
        curr = (buff_in + tot);
        Swprintf( &buff[index], L"0x%04x:  ", lineoff );
        index = Wcslen(buff);
        for( y = 0; y < (len - tot); y++ ){
             Swprintf( &buff[index], L"%02x ", (unsigned char)*(curr + y) );
             index = Wcslen(buff);
        }
        //padding with spaces
        //printk("(len - tot) %d.\r\n", (len - tot) );
        if( (len - tot) < 16 ){
            for( y = 0; y<(16-(len-tot)); y++ ){
                 Swprintf( &buff[index], L"%s", L"   " );  
                 index = index + 3;   
            }
        }
        
        Swprintf( &buff[index], L"%s", L"    " );  
        index = Wcslen(buff);           
        //Ser_Printf("(len - tot) %d.\r\n", (len - tot) );
        for( y = 0; y < (len - tot); y++ ){
             char c;
             c = *(curr + y);
             if( c > 31 && c < 127 ){
                 Swprintf( &buff[index], L"%c", c );
             }else{
                 Swprintf( &buff[index], L"%c", L'.' );
             }
             index = Wcslen(buff);
        }
    }
    Swprintf( &buff[index], L"%s", L"\n" );
    //printk("%s", buff);       
    DEBUG((EFI_D_INFO, "%s\n", buff));
    return retval;
}

#endif


        

/**
  Return SMBIOS string for the given string number.

  @param[in] Smbios         Pointer to SMBIOS structure.
  @param[in] StringNumber   String number to return. -1 is used to skip all strings and
                            point to the next SMBIOS structure.

  @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1
**/
CHAR8*
LibGetSmbiosString (
  IN  SMBIOS_STRUCTURE_POINTER    *Smbios,
  IN  UINT16                      StringNumber
  )
{
  UINT16  Index;
  CHAR8   *String;

  ASSERT (Smbios != NULL);

  //
  // Skip over formatted section
  //
  String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);

  //
  // Look through unformated section
  //
  for (Index = 1; Index <= StringNumber; Index++) {
    if (StringNumber == Index) {
      return String;
    }
    //
    // Skip string
    //
    for (; *String != 0; String++);
    String++;

    if (*String == 0) {
      //
      // If double NULL then we are done.
      //  Return pointer to next structure in Smbios.
      //  if you pass in a -1 you will always get here
      //
      Smbios->Raw = (UINT8 *)++String;
      return NULL;
    }
  }

  return NULL;
}

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   EfiLibGetSystemConfigurationTable
//
// Description: Get table from configuration table by name
//
// Input:       IN EFI_GUID *TableGuid,
//              IN OUT VOID **Table
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS EfiLibGetSystemConfigurationTable(
    IN EFI_GUID *TableGuid,
    IN OUT VOID **Table )
{
    UINTN Index;

    *Table = NULL;

    for ( Index = 0; Index < gST->NumberOfTableEntries; Index++ )
    {
        if ( !CompareMem( TableGuid, &(gST->ConfigurationTable[Index].VendorGuid),
                          sizeof(EFI_GUID)))
        {
            *Table = gST->ConfigurationTable[Index].VendorTable;
            return EFI_SUCCESS;
        }
    }

    return EFI_NOT_FOUND;
}


void test_fuck(void)
{
    CHAR16  debug_buff[128];
    UINTN index;
    int len;
    
    memset( debug_buff, 0x00, sizeof(debug_buff) );

    index = 0x00;
    Swprintf( &debug_buff[index], L"0x%04x:  ", 0x1234 );
    index = Wcslen(debug_buff);
      
    len = sizeof(debug_buff);
    macdbg_dmphex_kernel((const char *)debug_buff, len );

    DEBUG( (EFI_D_INFO, "index = %d\n", index) );  
    DEBUG( (EFI_D_INFO, "in test_fuck 4 = %s\n", debug_buff) );
    
    

}


EFI_STATUS test_smbios_table()
{
    EFI_STATUS                      Status;
    SMBIOS_TABLE_ENTRY_POINT      *mSmbiosTable   = NULL;
    SMBIOS_STRUCTURE_POINTER      m_SmbiosStruct;
    SMBIOS_STRUCTURE_POINTER      *mSmbiosStruct = &m_SmbiosStruct;
               
    SMBIOS_STRUCTURE_POINTER  Smbios;
    SMBIOS_STRUCTURE_POINTER  SmbiosEnd;
    UINT8                      *Raw;
              
    UINT16                      Handle1 = 0;
    UINT8                      *Buffer1;
    UINT16                      Length1;
         
    UINT16                      *Handle;
    UINT8                      **Buffer;
    UINT16                      *Length;
    UINT8                      Type;
                                                                    
    mSmbiosTable = NULL;
      
    //Get SMBIOS table from System Configure table
    Status = EfiLibGetSystemConfigurationTable(&gEfiSmbiosTableGuid,(VOID**)&mSmbiosTable);
    if( mSmbiosTable == NULL ){       
        Print(L"%r.\n",Status);
    }
                
    //Init SMBIOS structure table address
    mSmbiosStruct->Raw    = (UINT8 *)(UINTN)(mSmbiosTable->TableAddress);  
    //Find the structure
    Handle = &Handle1;
    Length = &Length1;
    Buffer = &Buffer1;
   
    *Length       = 0;
    Smbios.Hdr      = mSmbiosStruct->Hdr;
    SmbiosEnd.Raw = Smbios.Raw + mSmbiosTable->TableLength;
    Print(L"TableLenth:%02d\n",mSmbiosTable->TableLength);
          
    while( Smbios.Raw < SmbiosEnd.Raw ){
      if( Smbios.Hdr->Handle == *Handle ){
          Raw = Smbios.Raw;
          Type = Smbios.Hdr->Type;
          //Walk to next structure
          LibGetSmbiosString(&Smbios,(UINT16)(-1));
          //Length = Next structure head - this structure head
          *Length = (UINT16)(Smbios.Raw - Raw);
          *Buffer = Raw;          
          //update with the next structure handle.
          if( Smbios.Raw < SmbiosEnd.Raw ){
              *Handle = Smbios.Hdr->Handle;
          } else{
              *Handle = (UINT16)(-1);
          }
          DEBUG( (EFI_D_INFO, "Handle:0x%04x Type:0x%02x Address:%08x Length:%04x.\n", *Handle - 1, Type, *Buffer, *Length) );
          macdbg_dmphex_kernel(*Buffer, *Length );
      }
    }
    *Handle = (UINT16)(-1);
    return EFI_SUCCESS;
}

           


#include <Token.h>
#include <AmiDxeLib.h>
#include<Protocol/SmmBase2.h>
#include <Protocol/SmmSxDispatch2.h>
#include <Setup.h>
#if RTCWAKEUP_POWERBUTTON_SUPPORT
#include <Protocol/SmmPowerButtonDispatch.h>
#endif

#define CMOS_INDEX  0x70
#define CMOS_DATA   0x71
#define RTC_SECONDS_ALARM   1
#define RTC_MIN_ALARM       3
#define RTC_HOUR_ALARM      5
#define RTC_STATUS_REG_A    0xA
#define RTC_STATUS_REG_B    0xB
#define RTC_STATUS_REG_C    0xC


EFI_GUID    gSetupGuid = SETUP_GUID;
SETUP_DATA  gSetupData ;

UINT8   RtcWakeType=0;
UINT8   RTCWakeupTimeHour;
UINT8   RTCWakeupTimeMinute;
UINT8 &nb

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值