]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/SmbiosView.c
8ef3050b053c905e3eafb2d46a325a43267d83ef
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / SmbiosView / SmbiosView.c
1 /** @file
2 Tools of clarify the content of the smbios table.
3
4 Copyright (c) 2015, Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "../UefiShellDebug1CommandsLib.h"
17 #include "LibSmbiosView.h"
18 #include "SmbiosView.h"
19 #include "PrintInfo.h"
20 #include "QueryTable.h"
21
22 UINT8 gShowType = SHOW_DETAIL;
23 STATIC STRUCTURE_STATISTICS *mStatisticsTable = NULL;
24
25 UINT8 SmbiosMajorVersion;
26 UINT8 SmbiosMinorVersion;
27
28 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
29 {L"-t", TypeValue},
30 {L"-h", TypeValue},
31 {L"-s", TypeFlag},
32 {L"-a", TypeFlag},
33 {NULL, TypeMax}
34 };
35
36 /**
37 Function for 'smbiosview' command.
38
39 @param[in] ImageHandle Handle to the Image (NULL if Internal).
40 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
41 **/
42 SHELL_STATUS
43 EFIAPI
44 ShellCommandRunSmbiosView (
45 IN EFI_HANDLE ImageHandle,
46 IN EFI_SYSTEM_TABLE *SystemTable
47 )
48 {
49 UINT8 StructType;
50 UINT16 StructHandle;
51 EFI_STATUS Status;
52 BOOLEAN RandomView;
53 LIST_ENTRY *Package;
54 CHAR16 *ProblemParam;
55 SHELL_STATUS ShellStatus;
56 CONST CHAR16 *Temp;
57
58 mStatisticsTable = NULL;
59 Package = NULL;
60 ShellStatus = SHELL_SUCCESS;
61
62 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
63 if (EFI_ERROR(Status)) {
64 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
65 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"smbiosview", ProblemParam);
66 FreePool(ProblemParam);
67 ShellStatus = SHELL_INVALID_PARAMETER;
68 } else {
69 ASSERT(FALSE);
70 }
71 } else {
72 if (ShellCommandLineGetCount(Package) > 1) {
73 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"smbiosview");
74 ShellStatus = SHELL_INVALID_PARAMETER;
75 } else if (ShellCommandLineGetFlag(Package, L"-t") && ShellCommandLineGetValue(Package, L"-t") == NULL) {
76 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"smbiosview", L"-t");
77 ShellStatus = SHELL_INVALID_PARAMETER;
78 } else if (ShellCommandLineGetFlag(Package, L"-h") && ShellCommandLineGetValue(Package, L"-h") == NULL) {
79 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"smbiosview", L"-h");
80 ShellStatus = SHELL_INVALID_PARAMETER;
81 } else if (
82 (ShellCommandLineGetFlag(Package, L"-t") && ShellCommandLineGetFlag(Package, L"-h")) ||
83 (ShellCommandLineGetFlag(Package, L"-t") && ShellCommandLineGetFlag(Package, L"-s")) ||
84 (ShellCommandLineGetFlag(Package, L"-t") && ShellCommandLineGetFlag(Package, L"-a")) ||
85 (ShellCommandLineGetFlag(Package, L"-h") && ShellCommandLineGetFlag(Package, L"-s")) ||
86 (ShellCommandLineGetFlag(Package, L"-h") && ShellCommandLineGetFlag(Package, L"-a")) ||
87 (ShellCommandLineGetFlag(Package, L"-s") && ShellCommandLineGetFlag(Package, L"-a"))
88 ) {
89 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"smbiosview");
90 ShellStatus = SHELL_INVALID_PARAMETER;
91 } else {
92
93 //
94 // Init Lib
95
96 Status = LibSmbiosInit ();
97 if (EFI_ERROR (Status)) {
98 ShellStatus = SHELL_NOT_FOUND;
99 goto Done;
100 }
101 //
102 // build statistics table
103 //
104 Status = InitSmbiosTableStatistics ();
105 if (EFI_ERROR (Status)) {
106 ShellStatus = SHELL_NOT_FOUND;
107 goto Done;
108 }
109
110 StructType = STRUCTURE_TYPE_RANDOM;
111 RandomView = TRUE;
112 //
113 // Initialize the StructHandle to be the first handle
114 //
115 StructHandle = INVALID_HANDLE;
116 LibGetSmbiosStructure (&StructHandle, NULL, NULL);
117
118 Temp = ShellCommandLineGetValue(Package, L"-t");
119 if (Temp != NULL) {
120 StructType = (UINT8) ShellStrToUintn (Temp);
121 }
122
123 Temp = ShellCommandLineGetValue(Package, L"-h");
124 if (Temp != NULL) {
125 RandomView = FALSE;
126 StructHandle = (UINT16) ShellStrToUintn(Temp);
127 }
128
129 if (ShellCommandLineGetFlag(Package, L"-s")) {
130 Status = DisplayStatisticsTable (SHOW_DETAIL);
131 if (EFI_ERROR(Status)) {
132 ShellStatus = SHELL_NOT_FOUND;
133 }
134 goto Done;
135 }
136
137 if (ShellCommandLineGetFlag(Package, L"-a")) {
138 gShowType = SHOW_ALL;
139 }
140 //
141 // Show SMBIOS structure information
142 //
143 Status = SMBiosView (StructType, StructHandle, gShowType, RandomView);
144 if (EFI_ERROR(Status)) {
145 ShellStatus = SHELL_NOT_FOUND;
146 }
147 }
148 }
149 Done:
150 //
151 // Release resources
152 //
153 if (mStatisticsTable != NULL) {
154 //
155 // Release statistics table
156 //
157 FreePool (mStatisticsTable);
158 mStatisticsTable = NULL;
159 }
160
161 if (Package != NULL) {
162 ShellCommandLineFreeVarList (Package);
163 }
164
165 LibSmbiosCleanup ();
166
167 return ShellStatus;
168 }
169
170 /**
171 Query all structures Data from SMBIOS table and Display
172 the information to users as required display option.
173
174 @param[in] QueryType Structure type to view.
175 @param[in] QueryHandle Structure handle to view.
176 @param[in] Option Display option: none,outline,normal,detail.
177 @param[in] RandomView Support for -h parameter.
178
179 @retval EFI_SUCCESS print is successful.
180 @retval EFI_BAD_BUFFER_SIZE structure is out of the range of SMBIOS table.
181 **/
182 EFI_STATUS
183 EFIAPI
184 SMBiosView (
185 IN UINT8 QueryType,
186 IN UINT16 QueryHandle,
187 IN UINT8 Option,
188 IN BOOLEAN RandomView
189 )
190 {
191 UINT16 Handle;
192 UINT8 *Buffer;
193 UINT16 Length;
194 UINTN Index;
195
196 SMBIOS_STRUCTURE_POINTER SmbiosStruct;
197 SMBIOS_TABLE_ENTRY_POINT *SMBiosTable;
198
199 SMBiosTable = NULL;
200 LibSmbiosGetEPS (&SMBiosTable);
201 if (SMBiosTable == NULL) {
202 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_CANNOT_ACCESS_TABLE), gShellDebug1HiiHandle);
203 return EFI_BAD_BUFFER_SIZE;
204 }
205
206 if (CompareMem (SMBiosTable->AnchorString, "_SM_", 4) == 0) {
207 //
208 // Have got SMBIOS table
209 //
210 SmbiosPrintEPSInfo (SMBiosTable, Option);
211
212 SmbiosMajorVersion = SMBiosTable->MajorVersion;
213 SmbiosMinorVersion = SMBiosTable->MinorVersion;
214
215 ShellPrintEx(-1,-1,L"=========================================================\n");
216 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_QUERY_STRUCT_COND), gShellDebug1HiiHandle);
217
218 if (QueryType == STRUCTURE_TYPE_RANDOM) {
219 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_QUERYTYPE_RANDOM), gShellDebug1HiiHandle);
220 } else {
221 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_QUERYTYPE), gShellDebug1HiiHandle, QueryType);
222 }
223
224 if (RandomView) {
225 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_QUERYHANDLE_RANDOM), gShellDebug1HiiHandle);
226 } else {
227 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_QUERYHANDLE), gShellDebug1HiiHandle, QueryHandle);
228 }
229
230 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_SHOWTYPE), gShellDebug1HiiHandle);
231 ShellPrintEx(-1,-1,GetShowTypeString (gShowType));
232 ShellPrintEx(-1,-1,L"\n\n");
233
234 /*
235 //
236 // Get internal commands, such as change options.
237 //
238 Status = WaitEnter ();
239 if (EFI_ERROR (Status)) {
240 if (Status == EFI_ABORTED) {
241 return EFI_SUCCESS;
242 }
243
244 return Status;
245 }
246 */
247
248 //
249 // Searching and display structure info
250 //
251 Handle = QueryHandle;
252 for (Index = 0; Index < SMBiosTable->NumberOfSmbiosStructures; Index++) {
253 //
254 // if reach the end of table, break..
255 //
256 if (Handle == INVALID_HANDLE) {
257 break;
258 }
259 //
260 // handle then point to the next!
261 //
262 if (LibGetSmbiosStructure (&Handle, &Buffer, &Length) != DMI_SUCCESS) {
263 break;
264 }
265
266 SmbiosStruct.Raw = Buffer;
267
268 //
269 // if QueryType==Random, print this structure.
270 // if QueryType!=Random, but Hdr->Type==QueryType, also print it.
271 // only if QueryType != Random and Hdr->Type != QueryType, skiped it.
272 //
273 if (QueryType != STRUCTURE_TYPE_RANDOM && SmbiosStruct.Hdr->Type != QueryType) {
274 continue;
275 }
276
277 ShellPrintEx(-1,-1,L"\n=========================================================\n");
278 ShellPrintHiiEx(-1,-1,NULL,
279 STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_TYPE_HANDLE_DUMP_STRUCT),
280 gShellDebug1HiiHandle,
281 SmbiosStruct.Hdr->Type,
282 SmbiosStruct.Hdr->Handle
283 );
284 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_INDEX_LENGTH), gShellDebug1HiiHandle, Index, Length);
285 //
286 // Addr of structure in structure in table
287 //
288 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_ADDR), gShellDebug1HiiHandle, (UINTN) Buffer);
289 DumpHex (0, 0, Length, Buffer);
290
291 /*
292 //
293 // Get internal commands, such as change options.
294 //
295 Status = WaitEnter ();
296 if (EFI_ERROR (Status)) {
297 if (Status == EFI_ABORTED) {
298 return EFI_SUCCESS;
299 }
300
301 return Status;
302 }
303 */
304
305 if (gShowType != SHOW_NONE) {
306 //
307 // Print structure information
308 //
309 SmbiosPrintStructure (&SmbiosStruct, gShowType);
310 ShellPrintEx(-1,-1,L"\n");
311
312 /*
313 //
314 // Get internal commands, such as change options.
315 //
316 Status = WaitEnter ();
317 if (EFI_ERROR (Status)) {
318 if (Status == EFI_ABORTED) {
319 return EFI_SUCCESS;
320 }
321
322 return Status;
323 }
324 */
325 }
326 if (!RandomView) {
327 break;
328 }
329 //
330 // Support Execution Interrupt.
331 //
332 if (ShellGetExecutionBreakFlag ()) {
333 return EFI_ABORTED;
334 }
335 }
336
337 ShellPrintEx(-1,-1,L"\n=========================================================\n");
338 return EFI_SUCCESS;
339 }
340
341 return EFI_BAD_BUFFER_SIZE;
342 }
343
344 /**
345 Function to initialize the global mStatisticsTable object.
346
347 @retval EFI_SUCCESS print is successful.
348 **/
349 EFI_STATUS
350 EFIAPI
351 InitSmbiosTableStatistics (
352 VOID
353 )
354 {
355 UINT16 Handle;
356 UINT8 *Buffer;
357 UINT16 Length;
358 UINT16 Offset;
359 UINT16 Index;
360
361 SMBIOS_STRUCTURE_POINTER SmbiosStruct;
362 SMBIOS_TABLE_ENTRY_POINT *SMBiosTable;
363 STRUCTURE_STATISTICS *StatisticsPointer;
364
365 SMBiosTable = NULL;
366 LibSmbiosGetEPS (&SMBiosTable);
367 if (SMBiosTable == NULL) {
368 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_CANNOT_ACCESS_TABLE), gShellDebug1HiiHandle);
369 return EFI_NOT_FOUND;
370 }
371
372 if (CompareMem (SMBiosTable->AnchorString, "_SM_", 4) != 0) {
373 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_SMBIOS_TABLE), gShellDebug1HiiHandle);
374 return EFI_INVALID_PARAMETER;
375 }
376 //
377 // Allocate memory to mStatisticsTable
378 //
379 if (mStatisticsTable != NULL) {
380 FreePool (mStatisticsTable);
381 mStatisticsTable = NULL;
382 }
383
384 mStatisticsTable = (STRUCTURE_STATISTICS *) AllocateZeroPool (SMBiosTable->NumberOfSmbiosStructures * sizeof (STRUCTURE_STATISTICS));
385
386 if (mStatisticsTable == NULL) {
387 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_OUT_OF_MEM), gShellDebug1HiiHandle);
388 return EFI_OUT_OF_RESOURCES;
389 }
390
391 Offset = 0;
392 StatisticsPointer = mStatisticsTable;
393
394 //
395 // search from the first one
396 //
397 Handle = INVALID_HANDLE;
398 LibGetSmbiosStructure (&Handle, NULL, NULL);
399 for (Index = 1; Index <= SMBiosTable->NumberOfSmbiosStructures; Index++) {
400 //
401 // If reach the end of table, break..
402 //
403 if (Handle == INVALID_HANDLE) {
404 break;
405 }
406 //
407 // After LibGetSmbiosStructure(), handle then point to the next!
408 //
409 if (LibGetSmbiosStructure (&Handle, &Buffer, &Length) != DMI_SUCCESS) {
410 break;
411 }
412
413 SmbiosStruct.Raw = Buffer;
414
415 //
416 // general statistics
417 //
418 StatisticsPointer->Index = Index;
419 StatisticsPointer->Type = SmbiosStruct.Hdr->Type;
420 StatisticsPointer->Handle = SmbiosStruct.Hdr->Handle;
421 StatisticsPointer->Length = Length;
422 StatisticsPointer->Addr = Offset;
423
424 Offset = (UINT16) (Offset + Length);
425
426 StatisticsPointer = &mStatisticsTable[Index];
427 }
428
429 return EFI_SUCCESS;
430 }
431
432 /**
433 Function to display the global mStatisticsTable object.
434
435 @param[in] Option ECHO, NORMAL, or DETAIL control the amount of detail displayed.
436
437 @retval EFI_SUCCESS print is successful.
438 **/
439 EFI_STATUS
440 EFIAPI
441 DisplayStatisticsTable (
442 IN UINT8 Option
443 )
444 {
445 UINTN Index;
446 UINTN Num;
447 STRUCTURE_STATISTICS *StatisticsPointer;
448 SMBIOS_TABLE_ENTRY_POINT *SMBiosTable;
449
450 SMBiosTable = NULL;
451 if (Option < SHOW_OUTLINE) {
452 return EFI_SUCCESS;
453 }
454 //
455 // display EPS information firstly
456 //
457 LibSmbiosGetEPS (&SMBiosTable);
458 if (SMBiosTable == NULL) {
459 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_CANNOT_ACCESS_TABLE), gShellDebug1HiiHandle);
460 return EFI_UNSUPPORTED;
461 }
462
463 ShellPrintEx(-1,-1,L"\n============================================================\n");
464 SmbiosPrintEPSInfo (SMBiosTable, Option);
465
466 if (Option < SHOW_NORMAL) {
467 return EFI_SUCCESS;
468 }
469
470 if (mStatisticsTable == NULL) {
471 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_CANNOT_ACCESS_STATS), gShellDebug1HiiHandle);
472 return EFI_NOT_FOUND;
473 }
474
475 ShellPrintEx(-1,-1,L"============================================================\n");
476 StatisticsPointer = &mStatisticsTable[0];
477 Num = SMBiosTable->NumberOfSmbiosStructures;
478 //
479 // display statistics table content
480 //
481 for (Index = 1; Index <= Num; Index++) {
482 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_INDEX), gShellDebug1HiiHandle, StatisticsPointer->Index);
483 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_TYPE), gShellDebug1HiiHandle, StatisticsPointer->Type);
484 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_HANDLE), gShellDebug1HiiHandle, StatisticsPointer->Handle);
485 if (Option >= SHOW_DETAIL) {
486 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_OFFSET), gShellDebug1HiiHandle, StatisticsPointer->Addr);
487 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_LENGTH), gShellDebug1HiiHandle, StatisticsPointer->Length);
488 }
489
490 ShellPrintEx(-1,-1,L"\n");
491 StatisticsPointer = &mStatisticsTable[Index];
492 /*
493 //
494 // Display 20 lines and wait for a page break
495 //
496 if (Index % 20 == 0) {
497 ShellPrintHiiEx(-1,-1,NULL,STRING_TOKEN (STR_SMBIOSVIEW_SMBIOSVIEW_ENTER_CONTINUE), gShellDebug1HiiHandle);
498 Status = WaitEnter ();
499 if (EFI_ERROR (Status)) {
500 if (Status == EFI_ABORTED) {
501 return EFI_SUCCESS;
502 }
503
504 return Status;
505 }
506 }
507 */
508 }
509
510 return EFI_SUCCESS;
511 }
512
513 /**
514 function to return a string of the detail level.
515
516 @param[in] ShowType The detail level whose name is desired in clear text.
517
518 @return A pointer to a string representing the ShowType (or 'undefined type' if not known).
519 **/
520 CHAR16 *
521 EFIAPI
522 GetShowTypeString (
523 UINT8 ShowType
524 )
525 {
526 //
527 // show type
528 //
529 switch (ShowType) {
530
531 case SHOW_NONE:
532 return L"SHOW_NONE";
533
534 case SHOW_OUTLINE:
535 return L"SHOW_OUTLINE";
536
537 case SHOW_NORMAL:
538 return L"SHOW_NORMAL";
539
540 case SHOW_DETAIL:
541 return L"SHOW_DETAIL";
542
543 case SHOW_ALL:
544 return L"SHOW_ALL";
545
546 default:
547 return L"Undefined type";
548 }
549 }
550