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