summaryrefslogtreecommitdiff
path: root/ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/LibSmbiosView.c
blob: 22a8065a0153c83fc996a9b1ed1989dd4d6f2d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/** @file
  API for SMBIOS table.

  Copyright (c) 2005 - 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.

**/
    

#include "../UefiShellDebug1CommandsLib.h"
#include <Guid/Smbios.h>
#include "LIbSmbios.h"
#include "LibSmbiosView.h"
#include "smbiosview.h"

STATIC UINT8                    mInit         = 0;
STATIC SMBIOS_STRUCTURE_TABLE   *mSmbiosTable = NULL;
STATIC SMBIOS_STRUCTURE_POINTER m_SmbiosStruct;
STATIC SMBIOS_STRUCTURE_POINTER *mSmbiosStruct = &m_SmbiosStruct;

EFI_STATUS
LibSmbiosInit (
  VOID
  )
/*++

Routine Description:
    Init the SMBIOS VIEW API's environment.

  Arguments:
    None

Returns:
    EFI_SUCCESS       - Successful to init the SMBIOS VIEW Lib
    Others            - Cannot get SMBIOS Table

**/
{
  EFI_STATUS  Status;

  //
  // Init only once
  //
  if (mInit == 1) {
    return EFI_SUCCESS;
  }
  //
  // Get SMBIOS table from System Configure table
  //
  Status = GetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID**)&mSmbiosTable);

  if (mSmbiosTable == NULL) {
    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_CANNOT_GET_TABLE), gShellDebug1HiiHandle);
    return EFI_NOT_FOUND;
  }

  if (EFI_ERROR (Status)) {
    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_GET_TABLE_ERROR), gShellDebug1HiiHandle, Status);
    return Status;
  }
  //
  // Init SMBIOS structure table address
  //
  mSmbiosStruct->Raw  = (UINT8 *) (UINTN) (mSmbiosTable->TableAddress);

  mInit               = 1;
  return EFI_SUCCESS;
}

VOID
LibSmbiosCleanup (
  VOID
  )
{
  //
  // Release resources
  //
  if (mSmbiosTable != NULL) {
    mSmbiosTable = NULL;
  }

  mInit = 0;
}

VOID
LibSmbiosGetEPS (
  SMBIOS_STRUCTURE_TABLE **pEntryPointStructure
  )
{
  //
  // return SMBIOS Table address
  //
  *pEntryPointStructure = mSmbiosTable;
}

VOID
LibSmbiosGetStructHead (
  SMBIOS_STRUCTURE_POINTER *pHead
  )
{
  //
  // return SMBIOS structure table address
  //
  pHead = mSmbiosStruct;
}

EFI_STATUS
LibGetSmbiosInfo (
  OUT CHAR8   *dmiBIOSRevision,
  OUT UINT16  *NumStructures,
  OUT UINT16  *StructureSize,
  OUT UINT32  *dmiStorageBase,
  OUT UINT16  *dmiStorageSize
  )
/*++

Routine Description:
    Get SMBIOS Information.

  Arguments:
    dmiBIOSRevision   - Revision of the SMBIOS Extensions.
    NumStructures     - Max. Number of Structures the BIOS will return.
    StructureSize     - Size of largest SMBIOS Structure.
    dmiStorageBase    - 32-bit physical base address for memory mapped SMBIOS data.
    dmiStorageSize    - Size of the memory-mapped SMBIOS data.

  Returns:
    DMI_SUCCESS                 - successful.
    DMI_FUNCTION_NOT_SUPPORTED  - Does not support SMBIOS calling interface capability.

**/
{
  //
  // If no SMIBOS table, unsupported.
  //
  if (mSmbiosTable == NULL) {
    return DMI_FUNCTION_NOT_SUPPORTED;
  }

  *dmiBIOSRevision  = mSmbiosTable->SmbiosBcdRevision;
  *NumStructures    = mSmbiosTable->NumberOfSmbiosStructures;
  *StructureSize    = mSmbiosTable->MaxStructureSize;
  *dmiStorageBase   = mSmbiosTable->TableAddress;
  *dmiStorageSize   = mSmbiosTable->TableLength;

  return DMI_SUCCESS;
}

EFI_STATUS
LibGetSmbiosStructure (
  IN  OUT UINT16  *Handle,
  IN  OUT UINT8   *Buffer,
  OUT UINT16      *Length
  )
/*++

  Routine Description:
    Get SMBIOS structure given the Handle,copy data to the Buffer,
    Handle is changed to the next handle or 0xFFFF when the end is
    reached or the handle is not found.

  Arguments:
    Handle:         - 0xFFFF: get the first structure
                    - Others: get a structure according to this value.
    Buffter:        - The pointer to the caller's memory buffer.
    Length:         - Length of return buffer in bytes.

  Returns:
    DMI_SUCCESS         - Buffer contains the required structure data
                        - Handle is updated with next structure handle or
                          0xFFFF(end-of-list).

    DMI_INVALID_HANDLE  - Buffer not contain the requiring structure data
                        - Handle is updated with next structure handle or
                          0xFFFF(end-of-list).
**/
{
  SMBIOS_STRUCTURE_POINTER  Smbios;
  SMBIOS_STRUCTURE_POINTER  SmbiosEnd;
  UINT8                     *Raw;

  if (*Handle == INVALIDE_HANDLE) {
    *Handle = mSmbiosStruct->Hdr->Handle;
    return DMI_INVALID_HANDLE;
  }

  if (Buffer == NULL) {
    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUFF_SPEC), gShellDebug1HiiHandle);
    return DMI_INVALID_HANDLE;
  }

  *Length       = 0;
  Smbios.Hdr    = mSmbiosStruct->Hdr;
  SmbiosEnd.Raw = Smbios.Raw + mSmbiosTable->TableLength;
  while (Smbios.Raw < SmbiosEnd.Raw) {
    if (Smbios.Hdr->Handle == *Handle) {
      Raw = Smbios.Raw;
      //
      // Walk to next structure
      //
      LibGetSmbiosString (&Smbios, (UINT16) (-1));
      //
      // Length = Next structure head - this structure head
      //
      *Length = (UINT16) (Smbios.Raw - Raw);
      CopyMem (Buffer, Raw, *Length);
      //
      // update with the next structure handle.
      //
      if (Smbios.Raw < SmbiosEnd.Raw) {
        *Handle = Smbios.Hdr->Handle;
      } else {
        *Handle = INVALIDE_HANDLE;
      }
      return DMI_SUCCESS;
    }
    //
    // Walk to next structure
    //
    LibGetSmbiosString (&Smbios, (UINT16) (-1));
  }

  *Handle = INVALIDE_HANDLE;
  return DMI_INVALID_HANDLE;
}

EFI_STATUS
SmbiosCheckStructure (
  IN  SMBIOS_STRUCTURE_POINTER *Smbios
  )
/*++

  Routine Description:
    Check the structure to see if it is legal.

  Arguments:
    Smbios    - Pointer to the structure that will be checked.

  Returns:
    DMI_SUCCESS     - Structure data is legal.
    DMI_BAD_PARAMETER - Structure data contains bad parameter

**/
{
  //
  // If key != value, then error.
  //
#define CHECK_VALUE(key, value) (((key) == (value)) ? EFI_SUCCESS : DMI_BAD_PARAMETER)

  EFI_STATUS  Status;
  //
  // Assume staus is EFI_SUCCESS,
  // but if check is error, then EFI_ERROR.
  //
  Status = EFI_SUCCESS;

  switch (Smbios->Hdr->Type) {
  case 0:
    break;

  case 1:
    if (Smbios->Type1->Hdr.Length == 0x08 || Smbios->Type0->Hdr.Length == 0x19) {
      Status = EFI_SUCCESS;
    } else {
      Status = DMI_BAD_PARAMETER;
    }
    break;

  case 2:
    Status = CHECK_VALUE (Smbios->Type2->Hdr.Length, 0x08);
    break;

  case 6:
    Status = CHECK_VALUE (Smbios->Type6->Hdr.Length, 0x0C);
    break;

  case 11:
    Status = CHECK_VALUE (Smbios->Type11->Hdr.Length, 0x05);
    break;

  case 12:
    Status = CHECK_VALUE (Smbios->Type12->Hdr.Length, 0x05);
    break;

  case 13:
    Status = CHECK_VALUE (Smbios->Type13->Hdr.Length, 0x16);
    break;

  case 16:
    Status = CHECK_VALUE (Smbios->Type16->Hdr.Length, 0x0F);
    break;

  case 19:
    Status = CHECK_VALUE (Smbios->Type19->Hdr.Length, 0x0F);
    break;

  case 20:
    Status = CHECK_VALUE (Smbios->Type20->Hdr.Length, 0x13);
    break;

  case 32:
    //
    // Because EFI_SUCCESS == 0,
    // So errors added up is also error.
    //
    Status = CHECK_VALUE (Smbios->Type32->Reserved[0], 0x00) +
      CHECK_VALUE (Smbios->Type32->Reserved[1], 0x00) +
      CHECK_VALUE (Smbios->Type32->Reserved[2], 0x00) +
      CHECK_VALUE (Smbios->Type32->Reserved[3], 0x00) +
      CHECK_VALUE (Smbios->Type32->Reserved[4], 0x00) +
      CHECK_VALUE (Smbios->Type32->Reserved[5], 0x00);
    break;

  default:
    Status = DMI_BAD_PARAMETER;
  }

  return Status;
}

VOID
SmbiosGetPendingString (
  IN  SMBIOS_STRUCTURE_POINTER      *Smbios,
  IN  UINT16                        StringNumber,
  OUT CHAR8                         *Buffer
  )
{
  CHAR8 *String;
  if (Buffer == NULL) {
    ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUF_SPEC_WHEN_STRUCT), gShellDebug1HiiHandle);
    return ;
  }
  //
  // Get string and copy to buffer.
  // Caller should provide the buffer.
  //
  String = LibGetSmbiosString (Smbios, StringNumber);
  if (String != NULL) {
    CopyMem (Buffer, String, AsciiStrLen(String));
  } else {
    Buffer = NULL;
  }
}