LibSmbiosView.c revision 187cb3dd8d665619ff489ad8a8dc58c6aeab5aec
1/** @file
2  API for SMBIOS table.
3
4  Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
5  This program and the accompanying materials
6  are licensed and made available under the terms and conditions of the BSD License
7  which accompanies this distribution.  The full text of the license may be found at
8  http://opensource.org/licenses/bsd-license.php
9
10  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13**/
14
15
16#include "../UefiShellDebug1CommandsLib.h"
17#include <Guid/SmBios.h>
18#include "LibSmbiosView.h"
19#include "SmbiosView.h"
20
21STATIC UINT8                    mInit         = 0;
22STATIC SMBIOS_TABLE_ENTRY_POINT *mSmbiosTable = NULL;
23STATIC SMBIOS_STRUCTURE_POINTER m_SmbiosStruct;
24STATIC SMBIOS_STRUCTURE_POINTER *mSmbiosStruct = &m_SmbiosStruct;
25
26/**
27  Init the SMBIOS VIEW API's environment.
28
29  @retval EFI_SUCCESS  Successful to init the SMBIOS VIEW Lib.
30**/
31EFI_STATUS
32LibSmbiosInit (
33  VOID
34  )
35{
36  EFI_STATUS  Status;
37
38  //
39  // Init only once
40  //
41  if (mInit == 1) {
42    return EFI_SUCCESS;
43  }
44  //
45  // Get SMBIOS table from System Configure table
46  //
47  Status = GetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID**)&mSmbiosTable);
48
49  if (mSmbiosTable == NULL) {
50    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_CANNOT_GET_TABLE), gShellDebug1HiiHandle);
51    return EFI_NOT_FOUND;
52  }
53
54  if (EFI_ERROR (Status)) {
55    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_GET_TABLE_ERROR), gShellDebug1HiiHandle, Status);
56    return Status;
57  }
58  //
59  // Init SMBIOS structure table address
60  //
61  mSmbiosStruct->Raw  = (UINT8 *) (UINTN) (mSmbiosTable->TableAddress);
62
63  mInit               = 1;
64  return EFI_SUCCESS;
65}
66
67/**
68  Cleanup the Smbios information.
69**/
70VOID
71LibSmbiosCleanup (
72  VOID
73  )
74{
75  //
76  // Release resources
77  //
78  if (mSmbiosTable != NULL) {
79    mSmbiosTable = NULL;
80  }
81
82  mInit = 0;
83}
84
85/**
86  Get the entry point structure for the table.
87
88  @param[out] EntryPointStructure  The pointer to populate.
89**/
90VOID
91LibSmbiosGetEPS (
92  OUT SMBIOS_TABLE_ENTRY_POINT **EntryPointStructure
93  )
94{
95  //
96  // return SMBIOS Table address
97  //
98  *EntryPointStructure = mSmbiosTable;
99}
100
101/**
102  Return SMBIOS string for the given string number.
103
104  @param[in] Smbios         Pointer to SMBIOS structure.
105  @param[in] StringNumber   String number to return. -1 is used to skip all strings and
106                            point to the next SMBIOS structure.
107
108  @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1
109**/
110CHAR8*
111LibGetSmbiosString (
112  IN  SMBIOS_STRUCTURE_POINTER    *Smbios,
113  IN  UINT16                      StringNumber
114  )
115{
116  UINT16  Index;
117  CHAR8   *String;
118
119  ASSERT (Smbios != NULL);
120
121  //
122  // Skip over formatted section
123  //
124  String = (CHAR8 *) (Smbios->Raw + Smbios->Hdr->Length);
125
126  //
127  // Look through unformated section
128  //
129  for (Index = 1; Index <= StringNumber; Index++) {
130    if (StringNumber == Index) {
131      return String;
132    }
133    //
134    // Skip string
135    //
136    for (; *String != 0; String++);
137    String++;
138
139    if (*String == 0) {
140      //
141      // If double NULL then we are done.
142      //  Return pointer to next structure in Smbios.
143      //  if you pass in a -1 you will always get here
144      //
145      Smbios->Raw = (UINT8 *)++String;
146      return NULL;
147    }
148  }
149
150  return NULL;
151}
152
153/**
154    Get SMBIOS structure for the given Handle,
155    Handle is changed to the next handle or 0xFFFF when the end is
156    reached or the handle is not found.
157
158    @param[in, out] Handle     0xFFFF: get the first structure
159                               Others: get a structure according to this value.
160    @param[out] Buffer         The pointer to the pointer to the structure.
161    @param[out] Length         Length of the structure.
162
163    @retval DMI_SUCCESS   Handle is updated with next structure handle or
164                          0xFFFF(end-of-list).
165
166    @retval DMI_INVALID_HANDLE  Handle is updated with first structure handle or
167                                0xFFFF(end-of-list).
168**/
169EFI_STATUS
170LibGetSmbiosStructure (
171  IN  OUT UINT16  *Handle,
172  OUT UINT8       **Buffer,
173  OUT UINT16      *Length
174  )
175{
176  SMBIOS_STRUCTURE_POINTER  Smbios;
177  SMBIOS_STRUCTURE_POINTER  SmbiosEnd;
178  UINT8                     *Raw;
179
180  if (*Handle == INVALID_HANDLE) {
181    *Handle = mSmbiosStruct->Hdr->Handle;
182    return DMI_INVALID_HANDLE;
183  }
184
185  if ((Buffer == NULL) || (Length == NULL)) {
186    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUFF_LEN_SPEC), gShellDebug1HiiHandle);
187    return DMI_INVALID_HANDLE;
188  }
189
190  *Length       = 0;
191  Smbios.Hdr    = mSmbiosStruct->Hdr;
192  SmbiosEnd.Raw = Smbios.Raw + mSmbiosTable->TableLength;
193  while (Smbios.Raw < SmbiosEnd.Raw) {
194    if (Smbios.Hdr->Handle == *Handle) {
195      Raw = Smbios.Raw;
196      //
197      // Walk to next structure
198      //
199      LibGetSmbiosString (&Smbios, (UINT16) (-1));
200      //
201      // Length = Next structure head - this structure head
202      //
203      *Length = (UINT16) (Smbios.Raw - Raw);
204      *Buffer = Raw;
205      //
206      // update with the next structure handle.
207      //
208      if (Smbios.Raw < SmbiosEnd.Raw) {
209        *Handle = Smbios.Hdr->Handle;
210      } else {
211        *Handle = INVALID_HANDLE;
212      }
213      return DMI_SUCCESS;
214    }
215    //
216    // Walk to next structure
217    //
218    LibGetSmbiosString (&Smbios, (UINT16) (-1));
219  }
220
221  *Handle = INVALID_HANDLE;
222  return DMI_INVALID_HANDLE;
223}
224
225