]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/LibSmbiosView.c
22a8065a0153c83fc996a9b1ed1989dd4d6f2d8f
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SmbiosView / LibSmbiosView.c
1 /** @file
2 API for SMBIOS table.
3
4 Copyright (c) 2005 - 2010, 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 "LIbSmbios.h"
19 #include "LibSmbiosView.h"
20 #include "smbiosview.h"
21
22 STATIC UINT8 mInit = 0;
23 STATIC SMBIOS_STRUCTURE_TABLE *mSmbiosTable = NULL;
24 STATIC SMBIOS_STRUCTURE_POINTER m_SmbiosStruct;
25 STATIC SMBIOS_STRUCTURE_POINTER *mSmbiosStruct = &m_SmbiosStruct;
26
27 EFI_STATUS
28 LibSmbiosInit (
29 VOID
30 )
31 /*++
32
33 Routine Description:
34 Init the SMBIOS VIEW API's environment.
35
36 Arguments:
37 None
38
39 Returns:
40 EFI_SUCCESS - Successful to init the SMBIOS VIEW Lib
41 Others - Cannot get SMBIOS Table
42
43 **/
44 {
45 EFI_STATUS Status;
46
47 //
48 // Init only once
49 //
50 if (mInit == 1) {
51 return EFI_SUCCESS;
52 }
53 //
54 // Get SMBIOS table from System Configure table
55 //
56 Status = GetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID**)&mSmbiosTable);
57
58 if (mSmbiosTable == NULL) {
59 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_CANNOT_GET_TABLE), gShellDebug1HiiHandle);
60 return EFI_NOT_FOUND;
61 }
62
63 if (EFI_ERROR (Status)) {
64 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_GET_TABLE_ERROR), gShellDebug1HiiHandle, Status);
65 return Status;
66 }
67 //
68 // Init SMBIOS structure table address
69 //
70 mSmbiosStruct->Raw = (UINT8 *) (UINTN) (mSmbiosTable->TableAddress);
71
72 mInit = 1;
73 return EFI_SUCCESS;
74 }
75
76 VOID
77 LibSmbiosCleanup (
78 VOID
79 )
80 {
81 //
82 // Release resources
83 //
84 if (mSmbiosTable != NULL) {
85 mSmbiosTable = NULL;
86 }
87
88 mInit = 0;
89 }
90
91 VOID
92 LibSmbiosGetEPS (
93 SMBIOS_STRUCTURE_TABLE **pEntryPointStructure
94 )
95 {
96 //
97 // return SMBIOS Table address
98 //
99 *pEntryPointStructure = mSmbiosTable;
100 }
101
102 VOID
103 LibSmbiosGetStructHead (
104 SMBIOS_STRUCTURE_POINTER *pHead
105 )
106 {
107 //
108 // return SMBIOS structure table address
109 //
110 pHead = mSmbiosStruct;
111 }
112
113 EFI_STATUS
114 LibGetSmbiosInfo (
115 OUT CHAR8 *dmiBIOSRevision,
116 OUT UINT16 *NumStructures,
117 OUT UINT16 *StructureSize,
118 OUT UINT32 *dmiStorageBase,
119 OUT UINT16 *dmiStorageSize
120 )
121 /*++
122
123 Routine Description:
124 Get SMBIOS Information.
125
126 Arguments:
127 dmiBIOSRevision - Revision of the SMBIOS Extensions.
128 NumStructures - Max. Number of Structures the BIOS will return.
129 StructureSize - Size of largest SMBIOS Structure.
130 dmiStorageBase - 32-bit physical base address for memory mapped SMBIOS data.
131 dmiStorageSize - Size of the memory-mapped SMBIOS data.
132
133 Returns:
134 DMI_SUCCESS - successful.
135 DMI_FUNCTION_NOT_SUPPORTED - Does not support SMBIOS calling interface capability.
136
137 **/
138 {
139 //
140 // If no SMIBOS table, unsupported.
141 //
142 if (mSmbiosTable == NULL) {
143 return DMI_FUNCTION_NOT_SUPPORTED;
144 }
145
146 *dmiBIOSRevision = mSmbiosTable->SmbiosBcdRevision;
147 *NumStructures = mSmbiosTable->NumberOfSmbiosStructures;
148 *StructureSize = mSmbiosTable->MaxStructureSize;
149 *dmiStorageBase = mSmbiosTable->TableAddress;
150 *dmiStorageSize = mSmbiosTable->TableLength;
151
152 return DMI_SUCCESS;
153 }
154
155 EFI_STATUS
156 LibGetSmbiosStructure (
157 IN OUT UINT16 *Handle,
158 IN OUT UINT8 *Buffer,
159 OUT UINT16 *Length
160 )
161 /*++
162
163 Routine Description:
164 Get SMBIOS structure given the Handle,copy data to the Buffer,
165 Handle is changed to the next handle or 0xFFFF when the end is
166 reached or the handle is not found.
167
168 Arguments:
169 Handle: - 0xFFFF: get the first structure
170 - Others: get a structure according to this value.
171 Buffter: - The pointer to the caller's memory buffer.
172 Length: - Length of return buffer in bytes.
173
174 Returns:
175 DMI_SUCCESS - Buffer contains the required structure data
176 - Handle is updated with next structure handle or
177 0xFFFF(end-of-list).
178
179 DMI_INVALID_HANDLE - Buffer not contain the requiring structure data
180 - Handle is updated with next structure handle or
181 0xFFFF(end-of-list).
182 **/
183 {
184 SMBIOS_STRUCTURE_POINTER Smbios;
185 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
186 UINT8 *Raw;
187
188 if (*Handle == INVALIDE_HANDLE) {
189 *Handle = mSmbiosStruct->Hdr->Handle;
190 return DMI_INVALID_HANDLE;
191 }
192
193 if (Buffer == NULL) {
194 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUFF_SPEC), gShellDebug1HiiHandle);
195 return DMI_INVALID_HANDLE;
196 }
197
198 *Length = 0;
199 Smbios.Hdr = mSmbiosStruct->Hdr;
200 SmbiosEnd.Raw = Smbios.Raw + mSmbiosTable->TableLength;
201 while (Smbios.Raw < SmbiosEnd.Raw) {
202 if (Smbios.Hdr->Handle == *Handle) {
203 Raw = Smbios.Raw;
204 //
205 // Walk to next structure
206 //
207 LibGetSmbiosString (&Smbios, (UINT16) (-1));
208 //
209 // Length = Next structure head - this structure head
210 //
211 *Length = (UINT16) (Smbios.Raw - Raw);
212 CopyMem (Buffer, Raw, *Length);
213 //
214 // update with the next structure handle.
215 //
216 if (Smbios.Raw < SmbiosEnd.Raw) {
217 *Handle = Smbios.Hdr->Handle;
218 } else {
219 *Handle = INVALIDE_HANDLE;
220 }
221 return DMI_SUCCESS;
222 }
223 //
224 // Walk to next structure
225 //
226 LibGetSmbiosString (&Smbios, (UINT16) (-1));
227 }
228
229 *Handle = INVALIDE_HANDLE;
230 return DMI_INVALID_HANDLE;
231 }
232
233 EFI_STATUS
234 SmbiosCheckStructure (
235 IN SMBIOS_STRUCTURE_POINTER *Smbios
236 )
237 /*++
238
239 Routine Description:
240 Check the structure to see if it is legal.
241
242 Arguments:
243 Smbios - Pointer to the structure that will be checked.
244
245 Returns:
246 DMI_SUCCESS - Structure data is legal.
247 DMI_BAD_PARAMETER - Structure data contains bad parameter
248
249 **/
250 {
251 //
252 // If key != value, then error.
253 //
254 #define CHECK_VALUE(key, value) (((key) == (value)) ? EFI_SUCCESS : DMI_BAD_PARAMETER)
255
256 EFI_STATUS Status;
257 //
258 // Assume staus is EFI_SUCCESS,
259 // but if check is error, then EFI_ERROR.
260 //
261 Status = EFI_SUCCESS;
262
263 switch (Smbios->Hdr->Type) {
264 case 0:
265 break;
266
267 case 1:
268 if (Smbios->Type1->Hdr.Length == 0x08 || Smbios->Type0->Hdr.Length == 0x19) {
269 Status = EFI_SUCCESS;
270 } else {
271 Status = DMI_BAD_PARAMETER;
272 }
273 break;
274
275 case 2:
276 Status = CHECK_VALUE (Smbios->Type2->Hdr.Length, 0x08);
277 break;
278
279 case 6:
280 Status = CHECK_VALUE (Smbios->Type6->Hdr.Length, 0x0C);
281 break;
282
283 case 11:
284 Status = CHECK_VALUE (Smbios->Type11->Hdr.Length, 0x05);
285 break;
286
287 case 12:
288 Status = CHECK_VALUE (Smbios->Type12->Hdr.Length, 0x05);
289 break;
290
291 case 13:
292 Status = CHECK_VALUE (Smbios->Type13->Hdr.Length, 0x16);
293 break;
294
295 case 16:
296 Status = CHECK_VALUE (Smbios->Type16->Hdr.Length, 0x0F);
297 break;
298
299 case 19:
300 Status = CHECK_VALUE (Smbios->Type19->Hdr.Length, 0x0F);
301 break;
302
303 case 20:
304 Status = CHECK_VALUE (Smbios->Type20->Hdr.Length, 0x13);
305 break;
306
307 case 32:
308 //
309 // Because EFI_SUCCESS == 0,
310 // So errors added up is also error.
311 //
312 Status = CHECK_VALUE (Smbios->Type32->Reserved[0], 0x00) +
313 CHECK_VALUE (Smbios->Type32->Reserved[1], 0x00) +
314 CHECK_VALUE (Smbios->Type32->Reserved[2], 0x00) +
315 CHECK_VALUE (Smbios->Type32->Reserved[3], 0x00) +
316 CHECK_VALUE (Smbios->Type32->Reserved[4], 0x00) +
317 CHECK_VALUE (Smbios->Type32->Reserved[5], 0x00);
318 break;
319
320 default:
321 Status = DMI_BAD_PARAMETER;
322 }
323
324 return Status;
325 }
326
327 VOID
328 SmbiosGetPendingString (
329 IN SMBIOS_STRUCTURE_POINTER *Smbios,
330 IN UINT16 StringNumber,
331 OUT CHAR8 *Buffer
332 )
333 {
334 CHAR8 *String;
335 if (Buffer == NULL) {
336 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUF_SPEC_WHEN_STRUCT), gShellDebug1HiiHandle);
337 return ;
338 }
339 //
340 // Get string and copy to buffer.
341 // Caller should provide the buffer.
342 //
343 String = LibGetSmbiosString (Smbios, StringNumber);
344 if (String != NULL) {
345 CopyMem (Buffer, String, AsciiStrLen(String));
346 } else {
347 Buffer = NULL;
348 }
349 }