]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/LibSmbiosView.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SmbiosView / LibSmbiosView.c
CommitLineData
5d73d92f 1/** @file\r
2 API for SMBIOS table.\r
3\r
ba0014b9 4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r
56ba3746 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
5d73d92f 6\r
7**/\r
ba0014b9 8\r
8a765da2 9#include "UefiShellDebug1CommandsLib.h"\r
2b578de0 10#include <Guid/SmBios.h>\r
5d73d92f 11#include "LibSmbiosView.h"\r
a1d4bfcc 12#include "SmbiosView.h"\r
5d73d92f 13\r
47d20b54
MK
14STATIC UINT8 mInit = 0;\r
15STATIC UINT8 m64Init = 0;\r
16STATIC SMBIOS_TABLE_ENTRY_POINT *mSmbiosTable = NULL;\r
17STATIC SMBIOS_TABLE_3_0_ENTRY_POINT *mSmbios64BitTable = NULL;\r
18STATIC SMBIOS_STRUCTURE_POINTER m_SmbiosStruct;\r
19STATIC SMBIOS_STRUCTURE_POINTER *mSmbiosStruct = &m_SmbiosStruct;\r
20STATIC SMBIOS_STRUCTURE_POINTER m_Smbios64BitStruct;\r
21STATIC SMBIOS_STRUCTURE_POINTER *mSmbios64BitStruct = &m_Smbios64BitStruct;\r
5d73d92f 22\r
a1d4bfcc 23/**\r
24 Init the SMBIOS VIEW API's environment.\r
25\r
26 @retval EFI_SUCCESS Successful to init the SMBIOS VIEW Lib.\r
27**/\r
5d73d92f 28EFI_STATUS\r
29LibSmbiosInit (\r
30 VOID\r
31 )\r
5d73d92f 32{\r
33 EFI_STATUS Status;\r
34\r
35 //\r
36 // Init only once\r
37 //\r
38 if (mInit == 1) {\r
39 return EFI_SUCCESS;\r
40 }\r
47d20b54 41\r
5d73d92f 42 //\r
43 // Get SMBIOS table from System Configure table\r
44 //\r
47d20b54 45 Status = GetSystemConfigurationTable (&gEfiSmbiosTableGuid, (VOID **)&mSmbiosTable);\r
5d73d92f 46\r
47 if (mSmbiosTable == NULL) {\r
5d73d92f 48 return EFI_NOT_FOUND;\r
49 }\r
50\r
51 if (EFI_ERROR (Status)) {\r
47d20b54 52 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_GET_TABLE_ERROR), gShellDebug1HiiHandle, Status);\r
5d73d92f 53 return Status;\r
54 }\r
47d20b54 55\r
5d73d92f 56 //\r
57 // Init SMBIOS structure table address\r
58 //\r
47d20b54 59 mSmbiosStruct->Raw = (UINT8 *)(UINTN)(mSmbiosTable->TableAddress);\r
5d73d92f 60\r
47d20b54 61 mInit = 1;\r
5d73d92f 62 return EFI_SUCCESS;\r
63}\r
64\r
ec8a502e
EL
65/**\r
66 Init the SMBIOS VIEW API's environment.\r
67\r
68 @retval EFI_SUCCESS Successful to init the SMBIOS VIEW Lib.\r
69**/\r
70EFI_STATUS\r
71LibSmbios64BitInit (\r
72 VOID\r
73 )\r
74{\r
75 EFI_STATUS Status;\r
76\r
77 //\r
78 // Init only once\r
79 //\r
80 if (m64Init == 1) {\r
81 return EFI_SUCCESS;\r
82 }\r
47d20b54 83\r
ec8a502e
EL
84 //\r
85 // Get SMBIOS table from System Configure table\r
86 //\r
47d20b54 87 Status = GetSystemConfigurationTable (&gEfiSmbios3TableGuid, (VOID **)&mSmbios64BitTable);\r
ec8a502e
EL
88\r
89 if (mSmbios64BitTable == NULL) {\r
90 return EFI_NOT_FOUND;\r
91 }\r
92\r
93 if (EFI_ERROR (Status)) {\r
47d20b54 94 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_GET_TABLE_ERROR), gShellDebug1HiiHandle, Status);\r
ec8a502e
EL
95 return Status;\r
96 }\r
47d20b54 97\r
ec8a502e
EL
98 //\r
99 // Init SMBIOS structure table address\r
100 //\r
47d20b54 101 mSmbios64BitStruct->Raw = (UINT8 *)(UINTN)(mSmbios64BitTable->TableAddress);\r
ec8a502e 102\r
47d20b54 103 m64Init = 1;\r
ec8a502e
EL
104 return EFI_SUCCESS;\r
105}\r
106\r
a1d4bfcc 107/**\r
108 Cleanup the Smbios information.\r
109**/\r
5d73d92f 110VOID\r
111LibSmbiosCleanup (\r
112 VOID\r
113 )\r
114{\r
115 //\r
116 // Release resources\r
117 //\r
118 if (mSmbiosTable != NULL) {\r
119 mSmbiosTable = NULL;\r
120 }\r
121\r
122 mInit = 0;\r
123}\r
124\r
ec8a502e
EL
125/**\r
126 Cleanup the Smbios information.\r
127**/\r
128VOID\r
129LibSmbios64BitCleanup (\r
130 VOID\r
131 )\r
132{\r
133 //\r
134 // Release resources\r
135 //\r
136 if (mSmbios64BitTable != NULL) {\r
137 mSmbios64BitTable = NULL;\r
138 }\r
139\r
140 m64Init = 0;\r
141}\r
142\r
a1d4bfcc 143/**\r
144 Get the entry point structure for the table.\r
145\r
146 @param[out] EntryPointStructure The pointer to populate.\r
147**/\r
5d73d92f 148VOID\r
149LibSmbiosGetEPS (\r
47d20b54 150 OUT SMBIOS_TABLE_ENTRY_POINT **EntryPointStructure\r
5d73d92f 151 )\r
152{\r
153 //\r
154 // return SMBIOS Table address\r
155 //\r
a1d4bfcc 156 *EntryPointStructure = mSmbiosTable;\r
5d73d92f 157}\r
158\r
ec8a502e
EL
159/**\r
160 Get the entry point structure for the table.\r
161\r
162 @param[out] EntryPointStructure The pointer to populate.\r
163**/\r
164VOID\r
165LibSmbios64BitGetEPS (\r
47d20b54 166 OUT SMBIOS_TABLE_3_0_ENTRY_POINT **EntryPointStructure\r
ec8a502e
EL
167 )\r
168{\r
169 //\r
170 // return SMBIOS Table address\r
171 //\r
172 *EntryPointStructure = mSmbios64BitTable;\r
173}\r
174\r
a1d4bfcc 175/**\r
187cb3dd
SZ
176 Return SMBIOS string for the given string number.\r
177\r
178 @param[in] Smbios Pointer to SMBIOS structure.\r
179 @param[in] StringNumber String number to return. -1 is used to skip all strings and\r
180 point to the next SMBIOS structure.\r
181\r
182 @return Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1\r
183**/\r
47d20b54 184CHAR8 *\r
187cb3dd 185LibGetSmbiosString (\r
47d20b54
MK
186 IN SMBIOS_STRUCTURE_POINTER *Smbios,\r
187 IN UINT16 StringNumber\r
187cb3dd
SZ
188 )\r
189{\r
190 UINT16 Index;\r
191 CHAR8 *String;\r
192\r
193 ASSERT (Smbios != NULL);\r
194\r
195 //\r
196 // Skip over formatted section\r
197 //\r
47d20b54 198 String = (CHAR8 *)(Smbios->Raw + Smbios->Hdr->Length);\r
187cb3dd
SZ
199\r
200 //\r
201 // Look through unformated section\r
202 //\r
203 for (Index = 1; Index <= StringNumber; Index++) {\r
204 if (StringNumber == Index) {\r
205 return String;\r
206 }\r
47d20b54 207\r
187cb3dd
SZ
208 //\r
209 // Skip string\r
210 //\r
47d20b54
MK
211 for ( ; *String != 0; String++) {\r
212 }\r
213\r
187cb3dd
SZ
214 String++;\r
215\r
216 if (*String == 0) {\r
217 //\r
218 // If double NULL then we are done.\r
219 // Return pointer to next structure in Smbios.\r
220 // if you pass in a -1 you will always get here\r
221 //\r
222 Smbios->Raw = (UINT8 *)++String;\r
223 return NULL;\r
224 }\r
225 }\r
226\r
227 return NULL;\r
228}\r
229\r
230/**\r
231 Get SMBIOS structure for the given Handle,\r
a1d4bfcc 232 Handle is changed to the next handle or 0xFFFF when the end is\r
233 reached or the handle is not found.\r
5d73d92f 234\r
4ff7e37b
ED
235 @param[in, out] Handle 0xFFFF: get the first structure\r
236 Others: get a structure according to this value.\r
187cb3dd
SZ
237 @param[out] Buffer The pointer to the pointer to the structure.\r
238 @param[out] Length Length of the structure.\r
5d73d92f 239\r
187cb3dd 240 @retval DMI_SUCCESS Handle is updated with next structure handle or\r
a1d4bfcc 241 0xFFFF(end-of-list).\r
5d73d92f 242\r
187cb3dd 243 @retval DMI_INVALID_HANDLE Handle is updated with first structure handle or\r
a1d4bfcc 244 0xFFFF(end-of-list).\r
5d73d92f 245**/\r
5d73d92f 246EFI_STATUS\r
247LibGetSmbiosStructure (\r
248 IN OUT UINT16 *Handle,\r
187cb3dd 249 OUT UINT8 **Buffer,\r
5d73d92f 250 OUT UINT16 *Length\r
251 )\r
5d73d92f 252{\r
253 SMBIOS_STRUCTURE_POINTER Smbios;\r
254 SMBIOS_STRUCTURE_POINTER SmbiosEnd;\r
255 UINT8 *Raw;\r
256\r
187cb3dd 257 if (*Handle == INVALID_HANDLE) {\r
5d73d92f 258 *Handle = mSmbiosStruct->Hdr->Handle;\r
259 return DMI_INVALID_HANDLE;\r
260 }\r
261\r
187cb3dd 262 if ((Buffer == NULL) || (Length == NULL)) {\r
47d20b54 263 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUFF_LEN_SPEC), gShellDebug1HiiHandle);\r
5d73d92f 264 return DMI_INVALID_HANDLE;\r
265 }\r
266\r
267 *Length = 0;\r
268 Smbios.Hdr = mSmbiosStruct->Hdr;\r
269 SmbiosEnd.Raw = Smbios.Raw + mSmbiosTable->TableLength;\r
270 while (Smbios.Raw < SmbiosEnd.Raw) {\r
271 if (Smbios.Hdr->Handle == *Handle) {\r
272 Raw = Smbios.Raw;\r
273 //\r
274 // Walk to next structure\r
275 //\r
47d20b54 276 LibGetSmbiosString (&Smbios, (UINT16)(-1));\r
5d73d92f 277 //\r
278 // Length = Next structure head - this structure head\r
279 //\r
47d20b54 280 *Length = (UINT16)(Smbios.Raw - Raw);\r
187cb3dd 281 *Buffer = Raw;\r
5d73d92f 282 //\r
283 // update with the next structure handle.\r
284 //\r
285 if (Smbios.Raw < SmbiosEnd.Raw) {\r
286 *Handle = Smbios.Hdr->Handle;\r
287 } else {\r
187cb3dd 288 *Handle = INVALID_HANDLE;\r
5d73d92f 289 }\r
47d20b54 290\r
5d73d92f 291 return DMI_SUCCESS;\r
292 }\r
47d20b54 293\r
5d73d92f 294 //\r
295 // Walk to next structure\r
296 //\r
47d20b54 297 LibGetSmbiosString (&Smbios, (UINT16)(-1));\r
5d73d92f 298 }\r
299\r
187cb3dd 300 *Handle = INVALID_HANDLE;\r
5d73d92f 301 return DMI_INVALID_HANDLE;\r
302}\r
303\r
ec8a502e
EL
304/**\r
305 Get SMBIOS structure for the given Handle,\r
306 Handle is changed to the next handle or 0xFFFF when the end is\r
307 reached or the handle is not found.\r
308\r
309 @param[in, out] Handle 0xFFFF: get the first structure\r
310 Others: get a structure according to this value.\r
311 @param[out] Buffer The pointer to the pointer to the structure.\r
312 @param[out] Length Length of the structure.\r
313\r
314 @retval DMI_SUCCESS Handle is updated with next structure handle or\r
315 0xFFFF(end-of-list).\r
316\r
317 @retval DMI_INVALID_HANDLE Handle is updated with first structure handle or\r
318 0xFFFF(end-of-list).\r
319**/\r
320EFI_STATUS\r
321LibGetSmbios64BitStructure (\r
322 IN OUT UINT16 *Handle,\r
323 OUT UINT8 **Buffer,\r
324 OUT UINT16 *Length\r
325 )\r
326{\r
327 SMBIOS_STRUCTURE_POINTER Smbios;\r
328 SMBIOS_STRUCTURE_POINTER SmbiosEnd;\r
329 UINT8 *Raw;\r
330\r
331 if (*Handle == INVALID_HANDLE) {\r
332 *Handle = mSmbios64BitStruct->Hdr->Handle;\r
333 return DMI_INVALID_HANDLE;\r
334 }\r
335\r
336 if ((Buffer == NULL) || (Length == NULL)) {\r
47d20b54 337 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SMBIOSVIEW_LIBSMBIOSVIEW_NO_BUFF_LEN_SPEC), gShellDebug1HiiHandle);\r
ec8a502e
EL
338 return DMI_INVALID_HANDLE;\r
339 }\r
340\r
47d20b54
MK
341 *Length = 0;\r
342 Smbios.Hdr = mSmbios64BitStruct->Hdr;\r
ec8a502e
EL
343\r
344 SmbiosEnd.Raw = Smbios.Raw + mSmbios64BitTableLength;\r
345 while (Smbios.Raw < SmbiosEnd.Raw) {\r
346 if (Smbios.Hdr->Handle == *Handle) {\r
347 Raw = Smbios.Raw;\r
348 //\r
349 // Walk to next structure\r
350 //\r
47d20b54 351 LibGetSmbiosString (&Smbios, (UINT16)(-1));\r
ec8a502e
EL
352 //\r
353 // Length = Next structure head - this structure head\r
354 //\r
47d20b54 355 *Length = (UINT16)(Smbios.Raw - Raw);\r
ec8a502e
EL
356 *Buffer = Raw;\r
357 //\r
358 // update with the next structure handle.\r
359 //\r
360 if (Smbios.Raw < SmbiosEnd.Raw) {\r
361 *Handle = Smbios.Hdr->Handle;\r
362 } else {\r
363 *Handle = INVALID_HANDLE;\r
364 }\r
47d20b54 365\r
ec8a502e
EL
366 return DMI_SUCCESS;\r
367 }\r
47d20b54 368\r
ec8a502e
EL
369 //\r
370 // Walk to next structure\r
371 //\r
47d20b54 372 LibGetSmbiosString (&Smbios, (UINT16)(-1));\r
ec8a502e
EL
373 }\r
374\r
375 *Handle = INVALID_HANDLE;\r
376 return DMI_INVALID_HANDLE;\r
88ff5ba7 377}\r