BIOS学习实战之SMBIOS

最近事情太多,又有一段时间没做笔记了,有时候也在研究这个百敖的UIAPP框架和昆仑的UIAPP框架,思绪短时间内难理清,还是先写下SMBIOS的有关内容吧,本节主要以实践为主,仅做记录

EFI_SMBIOS_PROTOCOL

这个protocol就是操作smbios内容的,smbios怎么加载的,这个就有待去了解了,而且代码本身大部分是做好的,来看下这个protocol的里面包含了什么

struct _EFI_SMBIOS_PROTOCOL {
  EFI_SMBIOS_ADD           Add;
  EFI_SMBIOS_UPDATE_STRING UpdateString;
  EFI_SMBIOS_REMOVE        Remove;
  EFI_SMBIOS_GET_NEXT      GetNext;
  UINT8                    MajorVersion;    ///< The major revision of the SMBIOS specification supported.
  UINT8                    MinorVersion;    ///< The minor revision of the SMBIOS specification supported.
};

SMBIOS的操作给我的感觉就跟套公式一样的,咱们打开这个protocol:

  
  // open the SMBIOS protocol
  EFI_SMBIOS_PROTOCOL   *Smbios;
  Status = gBS->LocateProtocol (
    &gEfiSmbiosProtocolGuid,
    NULL,
    (VOID**)&Smbios
    );
  if (EFI_ERROR (Status)) {
    return Status;
  }

打开之后,咱们使用里面的方法:

Add

在SmbiosTablePublishEntry函数,也就是SMBIOS驱动的入口,一般逻辑是这样的,很多需要的type都会进行初始化,并通过一个void的指针去逐一的加载这些type,然后有些type可能需要写一些自己主板的特性,那么你再去单独加入更新,咱们以增加type21为例子,在打开smbios的protocol之后,咱们在它的下面新建一个函数:

Status= InstallType21Seructures(Smbios);

这个函数给我们的smbiostable新增了一个type,看下这个type的里面是啥:

typedef struct {
  SMBIOS_STRUCTURE                  Hdr;
  UINT8                             Type;                   ///< The enumeration value from BUILTIN_POINTING_DEVICE_TYPE.
  UINT8                             Interface;              ///< The enumeration value from BUILTIN_POINTING_DEVICE_INTERFACE.
  UINT8                             NumberOfButtons;
} SMBIOS_TABLE_TYPE21;
typedef struct {
  SMBIOS_TYPE    Type;
  UINT8          Length;
  SMBIOS_HANDLE  Handle;
} SMBIOS_STRUCTURE;

 通过上面的基础咱们就可以安装这个type,具体的值我瞎写的,有些值不在范围内它在系统内是反馈无效的,下面就是安装的过程,非常容易

EFI_STATUS InstallType21Seructures (IN EFI_SMBIOS_PROTOCOL   *Smbios)

{

  EFI_STATUS  Status = EFI_SUCCESS;

  SMBIOS_TABLE_TYPE21   *Type21Record;  

  EFI_SMBIOS_HANDLE     BuiltSmbiosHandle;

  Type21Record        = NULL;

  Type21Record = AllocateZeroPool(sizeof(SMBIOS_TABLE_TYPE21) + 2);

	if(Type21Record == NULL){

	  Status = EFI_OUT_OF_RESOURCES;

	}


	BuiltSmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;

	Type21Record->Hdr.Type   = SMBIOS_TYPE_BUILT_IN_POINTING_DEVICE;

    Type21Record->Hdr.Length = sizeof(SMBIOS_TABLE_TYPE21);

    Type21Record->Hdr.Handle = 0;

	Type21Record->Type = 21;

	Type21Record->Interface=0;

	Type21Record->NumberOfButtons=0;

	Status = Smbios->Add (Smbios, NULL, &BuiltSmbiosHandle, (EFI_SMBIOS_TABLE_HEADER*)Type21Record);

	FreePool (Type21Record);
  
    return Status;

}

解释一下几个关键的内容,SMBIOS_TYPE_BUILT_IN_POINTING_DEVICE的值为21,SMBIOS_HANDLE_PI_RESERVED为0xFFFE,这个值不是随便写的,而是自动分配的一个编号,也就是说你取值为0xFFFE的时候,会自动给这个type分配编号,在什么smbios信息中,这个handle的值就是这么来的,写完后编译,我们进入系统查看一下,是否生成安装了type21

 可以看到,一个新的type就这样生成了,有了这个type21,那么怎么去获取并修改它呢,咱们就以type1为例子,需要用到GetNext和UpdateString,一般如果你要提取smbiostable里面的新信息,使用前者就行,如果需要修改信息,那么两者都需要使用。

EFI_STATUS UpdateType1Seructures (IN EFI_SMBIOS_PROTOCOL   *Smbios)

{

EFI_STATUS                        Status;
EFI_SMBIOS_HANDLE                 SmbiosHandle;
EFI_SMBIOS_TYPE                   SmbiosType;
EFI_SMBIOS_TABLE_HEADER           *SmbiosHdr;
SMBIOS_STRUCTURE_POINTER          p;
UINTN                             StringNum;


  SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
  SmbiosType = SMBIOS_TYPE_SYSTEM_INFORMATION;
  Status = Smbios->GetNext(Smbios, &SmbiosHandle, &SmbiosType, &SmbiosHdr, NULL);

  if (!EFI_ERROR (Status)){
    // update board version
    p.Hdr         = SmbiosHdr;
    StringNum = p.Type1->ProductName;
    Status = Smbios->UpdateString(Smbios,&SmbiosHandle,&StringNum, "Anthony-PC");
  }
  else{ 
    return Status;
  }

return Status;

}

编译,我们查看效果,Product Name是不是变成了我们想要的名称呢

 如果我们的BIOS界面有需要用到这些信息,比如product name需要在BIOS界面下显示,应该如何操作呢,我们可以通过如下的代码形式进行;

  EFI_SMBIOS_HANDLE                 SmbiosHandle;
  EFI_SMBIOS_PROTOCOL               *Smbios;
  SMBIOS_TABLE_TYPE1                *Type1Record;
  EFI_SMBIOS_TABLE_HEADER           *Record;


 Status = gBS->LocateProtocol (&gEfiSmbiosProtocolGuid, NULL, (VOID **) &Smbios);

 SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
 Status = Smbios->GetNext (Smbios, &SmbiosHandle, NULL, &Record, NULL);

 while (!EFI_ERROR(Status)) {
   if (Record->Type == SMBIOS_TYPE_SYSTEM_INFORMATION) {
      Type1Record = (SMBIOS_TABLE_TYPE1 *) Record;
      StrIndex = Type1Record->ProductName;
      GetOptionalStringByIndex ((CHAR8*)((UINT8*)Type1Record + Type1Record->Hdr.Length), StrIndex, &NewString);
      UiCustomizeFrontPageBanner (1, TRUE, &NewString);
      HiiSetString (gFrontPagePrivate.HiiHandle, STRING_TOKEN (STR_FRONT_PAGE_COMPUTER_MODEL), NewString, NULL);
      FreePool (NewString);
    }

 }

//其中GetOptionalStringByIndex函数如下,供代码理解

EFI_STATUS
GetOptionalStringByIndex (
  IN      CHAR8                   *OptionalStrStart,
  IN      UINT8                   Index,
  OUT     CHAR16                  **String
  )
{
  UINTN          StrSize;

  if (Index == 0) {
    *String = AllocateZeroPool (sizeof (CHAR16));
    return EFI_SUCCESS;
  }

  StrSize = 0;
  do {
    Index--;
    OptionalStrStart += StrSize;
    StrSize           = AsciiStrSize (OptionalStrStart);
  } while (OptionalStrStart[StrSize] != 0 && Index != 0);

  if ((Index != 0) || (StrSize == 1)) {
    //
    // Meet the end of strings set but Index is non-zero, or
    // Find an empty string
    //
    *String = GetStringById (STRING_TOKEN (STR_MISSING_STRING));
  } else {
    *String = AllocatePool (StrSize * sizeof (CHAR16));
    AsciiStrToUnicodeStrS (OptionalStrStart, *String, StrSize);
  }

  return EFI_SUCCESS;
}

ok,smbios的一些内容的笔记就先做到这里

System Management BIOS (SMBIOS) Reference 6 Specification 7 Supersedes: 3.1.1 8 Document Class: Normative 9 Document Status: Published 10 Document Language: en-US 11System Management BIOS (SMBIOS) Reference Specification DSP0134 2 Published Version 3.2.0 12 Copyright Notice 13 Copyright © 2000, 2002, 2004–2016 Distributed Management Task Force, Inc. (DMTF). All rights 14 reserved. 15 DMTF is a not-for-profit association of industry members dedicated to promoting enterprise and systems 16 management and interoperability. Members and non-members may reproduce DMTF specifications and 17 documents, provided that correct attribution is given. As DMTF specifications may be revised from time to 18 time, the particular version and release date should always be noted. 19 Implementation of certain elements of this standard or proposed standard may be subject to third party 20 patent rights, including provisional patent rights (herein "patent rights"). DMTF makes no representations 21 to users of the standard as to the existence of such rights, and is not responsible to recognize, disclose, 22 or identify any or all such third party patent right, owners or claimants, nor for any incomplete or 23 inaccurate identification or disclosure of such rights, owners or claimants. DMTF shall have no liability to 24 any party, in any manner or circumstance, under any legal theory whatsoever, for failure to recognize, 25 disclose, or identify any such third party patent rights, or for such party’s reliance on the standard or 26 incorporation thereof in its product, protocols or testing procedures. DMTF shall have no liability to any 27 party implementing such standard, whether such implementation is foreseeable or not, nor to any patent 28 owner or claimant, and shall have no liability or responsibility for costs or losses incurred if a standard is 29 withdrawn or modified after publication, and shall be indemnified and held harmless by any party 30 implementing the standard from any and all claims of infringement by a patent owner for such 31 implementations. 32 For information about patents held by third-parties which have notified the DMTF that, in their opinion, 33 such patent may relate to or impact implementations of DMTF standards, visit 34 http://www.dmtf.org/about/policies/disclosures.php. 35 This document’s normative language is English. Translation into other languages is permitted.DSP0134 System Management BIOS (SMBIOS) Reference Specification Version 3.2.0 Published 3 36 CONTENTS 37 Foreword ....................................................................................................................................................... 9 38 Introduction.................................................................................................................................................. 10 39 Document conventions........................................................................................................................ 10 40 Typographical conventions ....................................................................................................... 10 41 Document version number conventions ................................................................................... 10 42 1 Scope .................................................................................................................................................. 13 43 1.1 Supported processor architectures........................................................................................... 13 44 2 Normative references .......................................................................................................................... 13 45 3 Terms and definitions .......................................................................................................................... 15 46 4 Symbols and abbreviated terms.......................................................................................................... 15 47 5 Accessing SMBIOS information .......................................................................................................... 21 48 5.1 General ..................................................................................................................................... 21 49 5.2 Table convention....................................................................................................................... 21 50 5.2.1 SMBIOS 2.1 (32-bit) Entry Point.................................................................................. 22 51 5.2.2 SMBIOS 3.0 (64-bit) Entry Point.................................................................................. 23 52 6 SMBIOS structures.............................................................................................................................. 24 53 6.1 Structure standards................................................................................................................... 24 54 6.1.1 Structure evolution and usage guidelines.................................................................... 25 55 6.1.2 Structure header format............................................................................................... 26 56 6.1.3 Text strings .................................................................................................................. 26 57 6.2 Required structures and data ................................................................................................... 27 58 6.3 SMBIOS fields and CIM MOF properties.................................................................................. 28 59 7 Structure definitions............................................................................................................................. 29 60 7.1 BIOS Information (Type 0)........................................................................................................ 29 61 7.1.1 BIOS Characteristics.................................................................................................... 31 62 7.1.2 BIOS Characteristics Extension Bytes......................................................................... 32 63 7.2 System Information (Type 1) .................................................................................................... 33 64 7.2.1 System — UUID........................................................................................................... 34 65 7.2.2 System — Wake-up Type............................................................................................ 35 66 7.3 Baseboard (or Module) Information (Type 2) ........................................................................... 35 67 7.3.1 Baseboard — feature flags .......................................................................................... 36 68 7.3.2 Baseboard — Board Type ........................................................................................... 37 69 7.4 System Enclosure or Chassis (Type 3) .................................................................................... 37 70 7.4.1 System Enclosure or Chassis Types........................................................................... 39 71 7.4.2 System Enclosure or Chassis States........................................................................... 40 72 7.4.3 System Enclosure or Chassis Security Status ............................................................ 41 73 7.4.4 System Enclosure or Chassis — Contained Elements................................................ 41 74 7.5 Processor Information (Type 4) ................................................................................................ 42 75 7.5.1 Processor Information — Processor Type................................................................... 45 76 7.5.2 Processor Information — Processor Family ................................................................ 46 77 7.5.3 Processor ID field format ............................................................................................. 52 78 7.5.4 Processor Information — Voltage................................................................................ 52 79 7.5.5 Processor Information — Processor Upgrade............................................................. 53 80 7.5.6 Processor Information — Core Count.......................................................................... 55 81 7.5.7 Processor Information — Core Enabled...................................................................... 55 82 7.5.8 Processor Information — Thread Count...................................................................... 56 83 7.5.9 Processor Characteristics............................................................................................ 56 84 7.6 Memory Controller Information (Type 5, Obsolete) .................................................................. 57 85 7.6.1 Memory Controller Error Detecting Method................................................................. 58 86 7.6.2 Memory Controller Error Correcting Capability............................................................ 58 87 7.6.3 Memory Controller Information — Interleave Support................................................. 58System Management BIOS (SMBIOS) Reference Specification DSP0134 4 Published Version 3.2.0 88 7.6.4 Memory Controller Information — Memory Speeds .................................................... 59 89 7.7 Memory Module Information (Type 6, Obsolete) ...................................................................... 59 90 7.7.1 Memory Module Information — Memory Types .......................................................... 60 91 7.7.2 Memory Module Information — Memory Size ............................................................. 60 92 7.7.3 Memory subsystem example ....................................................................................... 61 93 7.8 Cache Information (Type 7) ...................................................................................................... 63 94 7.8.1 Cache Information — Maximum Cache Size and Installed Size ................................. 65 95 7.8.2 Cache Information — SRAM Type .............................................................................. 65 96 7.8.3 Cache Information — Error Correction Type ............................................................... 66 97 7.8.4 Cache Information — System Cache Type ................................................................. 66 98 7.8.5 Cache Information — Associativity.............................................................................. 66 99 7.9 Port Connector Information (Type 8) ........................................................................................ 67 100 7.9.1 Port Information example............................................................................................. 68 101 7.9.2 Port Information — Connector Types .......................................................................... 68 102 7.9.3 Port Types.................................................................................................................... 69 103 7.10 System Slots (Type 9)............................................................................................................... 70 104 7.10.1 System Slots — Slot Type ........................................................................................... 71 105 7.10.2 System Slots — Slot Data Bus Width.......................................................................... 73 106 7.10.3 System Slots — Current Usage................................................................................... 74 107 7.10.4 System Slots — Slot Length ........................................................................................ 74 108 7.10.5 System Slots — Slot ID ............................................................................................... 74 109 7.10.6 Slot Characteristics 1................................................................................................... 75 110 7.10.7 Slot Characteristics 2................................................................................................... 75 111 7.10.8 Segment Group Number, Bus Number, Device/Func
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值