3 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #include <Library/PrintLib.h>
14 #include <Library/UefiLib.h>
15 #include <Library/ShellLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include "AcpiParser.h"
21 #include "AcpiTableParser.h"
23 #include "UefiShellAcpiViewCommandLib.h"
25 EFI_HII_HANDLE gShellAcpiViewHiiHandle
= NULL
;
28 STATIC UINT32 mSelectedAcpiTable
;
29 STATIC CONST CHAR16
* mSelectedAcpiTableName
;
30 STATIC BOOLEAN mSelectedAcpiTableFound
;
31 STATIC EREPORT_OPTION mReportType
;
32 STATIC UINT32 mTableCount
;
33 STATIC UINT32 mBinTableCount
;
34 STATIC BOOLEAN mVerbose
;
35 STATIC BOOLEAN mConsistencyCheck
;
36 STATIC BOOLEAN mColourHighlighting
;
39 An array of acpiview command line parameters.
41 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
53 This function returns the colour highlighting status.
55 @retval TRUE if colour highlighting is enabled.
58 GetColourHighlighting (
62 return mColourHighlighting
;
66 This function sets the colour highlighting status.
68 @param Highlight The Highlight status.
72 SetColourHighlighting (
76 mColourHighlighting
= Highlight
;
80 This function returns the report options.
82 @retval Returns the report option.
94 This function returns the selected ACPI table.
96 @retval Returns signature of the selected ACPI table.
100 GetSelectedAcpiTable (
104 return mSelectedAcpiTable
;
108 This function dumps the ACPI table to a file.
110 @param [in] Ptr Pointer to the ACPI table data.
111 @param [in] Length The length of the ACPI table.
113 @retval TRUE Success.
114 @retval FALSE Failure.
118 DumpAcpiTableToFile (
120 IN CONST UINTN Length
124 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
125 SHELL_FILE_HANDLE DumpFileHandle
= NULL
;
126 UINTN TransferBytes
= Length
;
130 sizeof (FileNameBuffer
),
132 mSelectedAcpiTableName
,
136 Status
= ShellOpenFileByName (
139 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
142 if (EFI_ERROR (Status
)) {
147 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
148 gShellAcpiViewHiiHandle
,
154 Print (L
"Dumping ACPI table to : %s ... ", FileNameBuffer
);
156 Status
= ShellWriteFile (
161 if (EFI_ERROR (Status
)) {
162 Print (L
"ERROR: Failed to dump table to binary file.\n");
168 ShellCloseFile (&DumpFileHandle
);
169 return (Length
== TransferBytes
);
173 This function processes the table reporting options for the ACPI table.
175 @param [in] Signature The ACPI table Signature.
176 @param [in] TablePtr Pointer to the ACPI table data.
177 @param [in] Length The length fo the ACPI table.
179 @retval Returns TRUE if the ACPI table should be traced.
182 ProcessTableReportOptions (
183 IN CONST UINT32 Signature
,
184 IN CONST UINT8
* TablePtr
,
185 IN CONST UINT32 Length
188 UINTN OriginalAttribute
;
189 UINT8
* SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
191 BOOLEAN HighLight
= GetColourHighlighting ();
192 switch (GetReportOption ()) {
196 case EREPORT_SELECTED
:
197 if (Signature
== GetSelectedAcpiTable ()) {
199 mSelectedAcpiTableFound
= TRUE
;
202 case EREPORT_TABLE_LIST
:
203 if (mTableCount
== 0) {
205 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
206 gST
->ConOut
->SetAttribute (
208 EFI_TEXT_ATTR(EFI_CYAN
,
209 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
212 Print (L
"\nInstalled Table(s):\n");
214 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
218 L
"\t%4d. %c%c%c%c\n",
226 case EREPORT_DUMP_BIN_FILE
:
227 if (Signature
== GetSelectedAcpiTable ()) {
228 mSelectedAcpiTableFound
= TRUE
;
229 DumpAcpiTableToFile (TablePtr
, Length
);
233 // We should never be here.
234 // This case is only present to prevent compiler warning.
240 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
241 gST
->ConOut
->SetAttribute (
243 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
244 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
248 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
255 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
263 This function converts a string to ACPI table signature.
265 @param [in] Str Pointer to the string to be converted to the
266 ACPI table signature.
268 @retval The ACPI table signature.
272 ConvertStrToAcpiSignature (
279 // Convert to Upper case and convert to ASCII
280 while ((Index
< 4) && (Str
[Index
] != 0)) {
281 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
282 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
284 Ptr
[Index
] = (CHAR8
)Str
[Index
];
288 return *(UINT32
*)Ptr
;
292 This function iterates the configuration table entries in the
293 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.
295 @param [in] SystemTable Pointer to the EFI system table.
297 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
298 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
299 Returns EFI_SUCCESS if successful.
305 IN EFI_SYSTEM_TABLE
* SystemTable
310 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
311 BOOLEAN FoundAcpiTable
;
312 UINTN OriginalAttribute
;
313 UINTN PrintAttribute
;
314 EREPORT_OPTION ReportOption
;
318 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
321 // Search the table for an entry that matches the ACPI Table Guid
322 FoundAcpiTable
= FALSE
;
323 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
324 if (CompareGuid (&gEfiAcpiTableGuid
,
325 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
326 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
327 FoundAcpiTable
= TRUE
;
332 if (FoundAcpiTable
) {
333 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
335 // The RSDP revision is 1 byte starting at offset 15
336 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
338 if (RsdpRevision
< 2) {
340 L
"ERROR: RSDP version less than 2 is not supported.\n"
342 return EFI_UNSUPPORTED
;
345 // The RSDP length is 4 bytes starting at offset 20
346 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
348 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
350 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
351 if (EFI_ERROR (Status
)) {
353 L
"ERROR: No registered parser found for RSDP.\n"
366 IncrementErrorCount ();
368 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
370 return EFI_NOT_FOUND
;
373 ReportOption
= GetReportOption ();
374 if (EREPORT_TABLE_LIST
!= ReportOption
) {
375 if (((EREPORT_SELECTED
== ReportOption
) ||
376 (EREPORT_DUMP_BIN_FILE
== ReportOption
)) &&
377 (!mSelectedAcpiTableFound
)) {
378 Print (L
"\nRequested ACPI Table not found.\n");
379 } else if (EREPORT_DUMP_BIN_FILE
!= ReportOption
) {
380 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
382 Print (L
"\nTable Statistics:\n");
384 if (GetColourHighlighting ()) {
385 PrintAttribute
= (GetErrorCount () > 0) ?
388 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
391 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
393 Print (L
"\t%d Error(s)\n", GetErrorCount ());
395 if (GetColourHighlighting ()) {
396 PrintAttribute
= (GetWarningCount () > 0) ?
399 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
403 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
405 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
407 if (GetColourHighlighting ()) {
408 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
416 Function for 'acpiview' command.
418 @param[in] ImageHandle Handle to the Image (NULL if Internal).
419 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
423 ShellCommandRunAcpiView (
424 IN EFI_HANDLE ImageHandle
,
425 IN EFI_SYSTEM_TABLE
* SystemTable
429 SHELL_STATUS ShellStatus
= SHELL_SUCCESS
;
430 LIST_ENTRY
* Package
= NULL
;
431 CHAR16
* ProblemParam
;
433 CHAR8 ColourOption
[8];
434 SHELL_FILE_HANDLE TmpDumpFileHandle
= NULL
;
437 mReportType
= EREPORT_ALL
;
440 mSelectedAcpiTable
= 0;
441 mSelectedAcpiTableName
= NULL
;
442 mSelectedAcpiTableFound
= FALSE
;
444 mConsistencyCheck
= TRUE
;
446 // Reset The error/warning counters
448 ResetWarningCount ();
450 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
451 if (EFI_ERROR (Status
)) {
452 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
457 STRING_TOKEN (STR_GEN_PROBLEM
),
458 gShellAcpiViewHiiHandle
,
462 FreePool (ProblemParam
);
464 Print (L
"acpiview: Error processing input parameter(s)\n");
466 ShellStatus
= SHELL_INVALID_PARAMETER
;
468 if (ShellCommandLineGetCount (Package
) > 1) {
473 STRING_TOKEN (STR_GEN_TOO_MANY
),
474 gShellAcpiViewHiiHandle
,
477 ShellStatus
= SHELL_INVALID_PARAMETER
;
478 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
483 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
484 gShellAcpiViewHiiHandle
,
487 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
488 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
493 STRING_TOKEN (STR_GEN_NO_VALUE
),
494 gShellAcpiViewHiiHandle
,
498 ShellStatus
= SHELL_INVALID_PARAMETER
;
499 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
500 ShellCommandLineGetFlag (Package
, L
"-l"))) {
505 STRING_TOKEN (STR_GEN_TOO_MANY
),
506 gShellAcpiViewHiiHandle
,
509 ShellStatus
= SHELL_INVALID_PARAMETER
;
510 } else if (ShellCommandLineGetFlag (Package
, L
"-h") &&
511 ShellCommandLineGetValue (Package
, L
"-h") == NULL
) {
516 STRING_TOKEN (STR_GEN_NO_VALUE
),
517 gShellAcpiViewHiiHandle
,
521 ShellStatus
= SHELL_INVALID_PARAMETER
;
522 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
523 !ShellCommandLineGetFlag (Package
, L
"-s")) {
528 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
529 gShellAcpiViewHiiHandle
,
534 ShellStatus
= SHELL_INVALID_PARAMETER
;
536 // Check if the colour option is set
537 Temp
= ShellCommandLineGetValue (Package
, L
"-h");
539 UnicodeStrToAsciiStrS (Temp
, ColourOption
, sizeof (ColourOption
));
540 if ((AsciiStriCmp (ColourOption
, "ON") == 0) ||
541 (AsciiStriCmp (ColourOption
, "TRUE") == 0)) {
542 SetColourHighlighting (TRUE
);
543 } else if ((AsciiStriCmp (ColourOption
, "OFF") == 0) ||
544 (AsciiStriCmp (ColourOption
, "FALSE") == 0)) {
545 SetColourHighlighting (FALSE
);
549 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
550 mReportType
= EREPORT_TABLE_LIST
;
552 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
553 if (mSelectedAcpiTableName
!= NULL
) {
554 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
555 mSelectedAcpiTableName
557 mReportType
= EREPORT_SELECTED
;
559 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
560 // Create a temporary file to check if the media is writable.
561 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
562 mReportType
= EREPORT_DUMP_BIN_FILE
;
566 sizeof (FileNameBuffer
),
568 mSelectedAcpiTableName
,
572 Status
= ShellOpenFileByName (
575 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
576 EFI_FILE_MODE_CREATE
,
580 if (EFI_ERROR (Status
)) {
581 ShellStatus
= SHELL_INVALID_PARAMETER
;
582 TmpDumpFileHandle
= NULL
;
587 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
588 gShellAcpiViewHiiHandle
,
593 // Delete Temporary file.
594 ShellDeleteFile (&TmpDumpFileHandle
);
599 // Parse ACPI Table information
600 Status
= AcpiView (SystemTable
);
601 if (EFI_ERROR (Status
)) {
602 ShellStatus
= SHELL_NOT_FOUND
;
608 if (Package
!= NULL
) {
609 ShellCommandLineFreeVarList (Package
);