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