废话不多说 直接code
#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>
extern EFI_BOOT_SERVICES *gBS;
extern EFI_HANDLE gImageHandle;
#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;
}
EFI_STATUS UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable )
{
EFI_STATUS Status;
EFI_SMBIOS_HANDLE SmbiosHandle = 0;
EFI_SMBIOS_TABLE_HEADER *Record;
EFI_SMBIOS_PROTOCOL *Smbios;
UINTN OrigStringNumber = 5;
CHAR8 *AsciiData = "smbios_test_str = 12345";
DEBUG( (EFI_D_WARN, "start UefiMain...\n") );
Status = gBS->LocateProtocol(&gEfiSmbiosProtocolGuid, NULL, (VOID**)&Smbios);
Status = Smbios->GetNext (Smbios, &SmbiosHandle, NULL, &Record, NULL);
DEBUG( (EFI_D_INFO, "Type : %x\n", Record->Type) );
DEBUG( (EFI_D_INFO, "Length : %x\n", Record->Length) );
DEBUG( (EFI_D_INFO, "Handle : %x\n", Record->Handle) );
macdbg_dmphex_kernel( (const char *)Record, 256 );
Status = Smbios->UpdateString (Smbios, &SmbiosHandle, &OrigStringNumber, AsciiData);
macdbg_dmphex_kernel((const char *)Record, 256 );
test_smbios_table();
return EFI_SUCCESS;
}
inf文件内容:
## @file
# Component description file for EFI Shell module.
#
# This is a binary module containing multiple binary shell applications.
# All .efi file tagged with "ToolCode="DUMMY"" in following file list are raw EFI application
# file, and they are can be run in shell environment.
# Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
#
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
#
##
[Defines]
INF_VERSION = 0x00010006
BASE_NAME = FullShell
## FILE_GUID = c57ad6b7-0515-40a8-9d21-551652854e37
FILE_GUID = 7C04A583-9E3E-4f1c-AD65-E05268D0B4D2
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 1.0
ENTRY_POINT = UefiMain
#
# VALID_ARCHITECTURES = IA32 X64 IPF
#
[Sources]
kbLEDTest.c
[Packages]
MdePkg/MdePkg.dec
ServerCommonPkg/ServerCommonPkg.dec
IntelFrameworkPkg/IntelFrameworkPkg.dec
AmiCompatibilityPkg/AmiCompatibilityPkg.dec
#ShellPkg/ShellPkg.dec
MdeModulePkg/MdeModulePkg.dec
AmiPkg/AmiPkg.dec
MdeModulePkg/MdeModulePkg.dec
IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec
[LibraryClasses]
# UefiDriverEntryPoint
MemoryAllocationLib
UefiBootServicesTableLib
DebugLib
UefiLib
WheaPlatformHooksLib
UefiApplicationEntryPoint
AmiDxeLib
UefiUsbLib
UefiLib
IoLib
# ShellCEntryLib
# UefiShellDebug1CommandsLib
[Protocols]
gEfiSmbiosProtocolGuid
[Guids]
gEfiSmbiosTableGuid