]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/LibSmbiosView.c
Refine comments and two code style.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SmbiosView / LibSmbiosView.c
1 /** @file
2 API for SMBIOS table.
3
4 Copyright (c) 2005 - 2011, 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 /**
28 Init the SMBIOS VIEW API's environment.
29
30 @retval EFI_SUCCESS Successful to init the SMBIOS VIEW Lib.
31 **/
32 EFI_STATUS
33 LibSmbiosInit (
34 VOID
35 )
36 {
37 EFI_STATUS Status;
38
39 //
40 // Init only once
41 //
42 if (mInit == 1) {
43 return EFI_SUCCESS;
44 }
45 //
46 // Get SMBIOS table from System Configure table
47 //
48 Status = GetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID**)&mSmbiosTable);
49
50 if (mSmbiosTable == NULL) {
51 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_CANNOT_GET_TABLE), gShellDebug1HiiHandle);
52 return EFI_NOT_FOUND;
53 }
54
55 if (EFI_ERROR (Status)) {
56 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_GET_TABLE_ERROR), gShellDebug1HiiHandle, Status);
57 return Status;
58 }
59 //
60 // Init SMBIOS structure table address
61 //
62 mSmbiosStruct->Raw = (UINT8 *) (UINTN) (mSmbiosTable->TableAddress);
63
64 mInit = 1;
65 return EFI_SUCCESS;
66 }
67
68 /**
69 Cleanup the Smbios information.
70 **/
71 VOID
72 LibSmbiosCleanup (
73 VOID
74 )
75 {
76 //
77 // Release resources
78 //
79 if (mSmbiosTable != NULL) {
80 mSmbiosTable = NULL;
81 }
82
83 mInit = 0;
84 }
85
86 /**
87 Get the entry point structure for the table.
88
89 @param[out] EntryPointStructure The pointer to populate.
90 **/
91 VOID
92 LibSmbiosGetEPS (
93 OUT SMBIOS_STRUCTURE_TABLE **EntryPointStructure
94 )
95 {
96 //
97 // return SMBIOS Table address
98 //
99 *EntryPointStructure = mSmbiosTable;
100 }
101
102 /**
103 Get SMBIOS structure given the Handle,copy data to the Buffer,
104 Handle is changed to the next handle or 0xFFFF when the end is
105 reached or the handle is not found.
106
107 @param[in, out] Handle 0xFFFF: get the first structure
108 Others: get a structure according to this value.
109 @param[in, out] Buffer The pointer to the caller's memory buffer.
110 @param[out] Length Length of return buffer in bytes.
111
112 @retval DMI_SUCCESS Buffer contains the required structure data
113 Handle is updated with next structure handle or
114 0xFFFF(end-of-list).
115
116 @retval DMI_INVALID_HANDLE Buffer not contain the requiring structure data.
117 Handle is updated with next structure handle or
118 0xFFFF(end-of-list).
119 **/
120 EFI_STATUS
121 LibGetSmbiosStructure (
122 IN OUT UINT16 *Handle,
123 IN OUT UINT8 *Buffer,
124 OUT UINT16 *Length
125 )
126 {
127 SMBIOS_STRUCTURE_POINTER Smbios;
128 SMBIOS_STRUCTURE_POINTER SmbiosEnd;
129 UINT8 *Raw;
130
131 if (*Handle == INVALIDE_HANDLE) {
132 *Handle = mSmbiosStruct->Hdr->Handle;
133 return DMI_INVALID_HANDLE;
134 }
135
136 if (Buffer == NULL) {
137 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUFF_SPEC), gShellDebug1HiiHandle);
138 return DMI_INVALID_HANDLE;
139 }
140
141 *Length = 0;
142 Smbios.Hdr = mSmbiosStruct->Hdr;
143 SmbiosEnd.Raw = Smbios.Raw + mSmbiosTable->TableLength;
144 while (Smbios.Raw < SmbiosEnd.Raw) {
145 if (Smbios.Hdr->Handle == *Handle) {
146 Raw = Smbios.Raw;
147 //
148 // Walk to next structure
149 //
150 LibGetSmbiosString (&Smbios, (UINT16) (-1));
151 //
152 // Length = Next structure head - this structure head
153 //
154 *Length = (UINT16) (Smbios.Raw - Raw);
155 CopyMem (Buffer, Raw, *Length);
156 //
157 // update with the next structure handle.
158 //
159 if (Smbios.Raw < SmbiosEnd.Raw) {
160 *Handle = Smbios.Hdr->Handle;
161 } else {
162 *Handle = INVALIDE_HANDLE;
163 }
164 return DMI_SUCCESS;
165 }
166 //
167 // Walk to next structure
168 //
169 LibGetSmbiosString (&Smbios, (UINT16) (-1));
170 }
171
172 *Handle = INVALIDE_HANDLE;
173 return DMI_INVALID_HANDLE;
174 }
175
176 /**
177 Check the structure to see if it is legal.
178
179 @param[in] Smbios - Pointer to the structure that will be checked.
180
181 @retval DMI_SUCCESS Structure data is legal.
182 @retval DMI_BAD_PARAMETER Structure data contains bad parameter.
183 **/
184 EFI_STATUS
185 SmbiosCheckStructure (
186 IN SMBIOS_STRUCTURE_POINTER *Smbios
187 )
188 {
189 //
190 // If key != value, then error.
191 //
192 #define CHECK_VALUE(key, value) (((key) == (value)) ? EFI_SUCCESS : DMI_BAD_PARAMETER)
193
194 EFI_STATUS Status;
195 //
196 // Assume staus is EFI_SUCCESS,
197 // but if check is error, then EFI_ERROR.
198 //
199 Status = EFI_SUCCESS;
200
201 switch (Smbios->Hdr->Type) {
202 case 0:
203 break;
204
205 case 1:
206 if (Smbios->Type1->Hdr.Length == 0x08 || Smbios->Type0->Hdr.Length == 0x19) {
207 Status = EFI_SUCCESS;
208 } else {
209 Status = DMI_BAD_PARAMETER;
210 }
211 break;
212
213 case 2:
214 Status = CHECK_VALUE (Smbios->Type2->Hdr.Length, 0x08);
215 break;
216
217 case 6:
218 Status = CHECK_VALUE (Smbios->Type6->Hdr.Length, 0x0C);
219 break;
220
221 case 11:
222 Status = CHECK_VALUE (Smbios->Type11->Hdr.Length, 0x05);
223 break;
224
225 case 12:
226 Status = CHECK_VALUE (Smbios->Type12->Hdr.Length, 0x05);
227 break;
228
229 case 13:
230 Status = CHECK_VALUE (Smbios->Type13->Hdr.Length, 0x16);
231 break;
232
233 case 16:
234 Status = CHECK_VALUE (Smbios->Type16->Hdr.Length, 0x0F);
235 break;
236
237 case 19:
238 Status = CHECK_VALUE (Smbios->Type19->Hdr.Length, 0x0F);
239 break;
240
241 case 20:
242 Status = CHECK_VALUE (Smbios->Type20->Hdr.Length, 0x13);
243 break;
244
245 case 32:
246 //
247 // Because EFI_SUCCESS == 0,
248 // So errors added up is also error.
249 //
250 Status = CHECK_VALUE (Smbios->Type32->Reserved[0], 0x00) +
251 CHECK_VALUE (Smbios->Type32->Reserved[1], 0x00) +
252 CHECK_VALUE (Smbios->Type32->Reserved[2], 0x00) +
253 CHECK_VALUE (Smbios->Type32->Reserved[3], 0x00) +
254 CHECK_VALUE (Smbios->Type32->Reserved[4], 0x00) +
255 CHECK_VALUE (Smbios->Type32->Reserved[5], 0x00);
256 break;
257
258 default:
259 Status = DMI_BAD_PARAMETER;
260 }
261
262 return Status;
263 }
264
265 /**
266 Get a string from the smbios information.
267
268 @param[in] Smbios The pointer to the smbios information.
269 @param[in] StringNumber The index to the string to get.
270 @param[out] Buffer The buffer to fill with the string when retrieved.
271 **/
272 VOID
273 SmbiosGetPendingString (
274 IN SMBIOS_STRUCTURE_POINTER *Smbios,
275 IN UINT16 StringNumber,
276 OUT CHAR8 *Buffer
277 )
278 {
279 CHAR8 *String;
280 if (Buffer == NULL) {
281 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUF_SPEC_WHEN_STRUCT), gShellDebug1HiiHandle);
282 return ;
283 }
284 //
285 // Get string and copy to buffer.
286 // Caller should provide the buffer.
287 //
288 String = LibGetSmbiosString (Smbios, StringNumber);
289 if (String != NULL) {
290 CopyMem (Buffer, String, AsciiStrLen(String));
291 } else {
292 Buffer = NULL;
293 }
294 }