3 Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.
4 SPDX-License-Identifier: BSD-2-Clause-Patent
7 #include <Library/PrintLib.h>
8 #include <Library/UefiLib.h>
9 #include <Library/ShellLib.h>
10 #include <Library/UefiBootServicesTableLib.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include "AcpiParser.h"
15 #include "AcpiTableParser.h"
17 #include "UefiShellAcpiViewCommandLib.h"
19 EFI_HII_HANDLE gShellAcpiViewHiiHandle
= NULL
;
22 STATIC UINT32 mSelectedAcpiTable
;
23 STATIC CONST CHAR16
* mSelectedAcpiTableName
;
24 STATIC BOOLEAN mSelectedAcpiTableFound
;
25 STATIC EREPORT_OPTION mReportType
;
26 STATIC UINT32 mTableCount
;
27 STATIC UINT32 mBinTableCount
;
28 STATIC BOOLEAN mConsistencyCheck
;
29 STATIC BOOLEAN mColourHighlighting
;
32 An array of acpiview command line parameters.
34 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
44 This function returns the colour highlighting status.
46 @retval TRUE if colour highlighting is enabled.
49 GetColourHighlighting (
53 return mColourHighlighting
;
57 This function sets the colour highlighting status.
59 @param Highlight The Highlight status.
63 SetColourHighlighting (
67 mColourHighlighting
= Highlight
;
71 This function returns the consistency checking status.
73 @retval TRUE if consistency checking is enabled.
76 GetConsistencyChecking (
80 return mConsistencyCheck
;
84 This function sets the consistency checking status.
86 @param ConsistencyChecking The consistency checking status.
90 SetConsistencyChecking (
91 BOOLEAN ConsistencyChecking
94 mConsistencyCheck
= ConsistencyChecking
;
98 This function returns the report options.
100 @retval Returns the report option.
112 This function returns the selected ACPI table.
114 @retval Returns signature of the selected ACPI table.
118 GetSelectedAcpiTable (
122 return mSelectedAcpiTable
;
126 This function dumps the ACPI table to a file.
128 @param [in] Ptr Pointer to the ACPI table data.
129 @param [in] Length The length of the ACPI table.
131 @retval TRUE Success.
132 @retval FALSE Failure.
136 DumpAcpiTableToFile (
138 IN CONST UINTN Length
142 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
143 SHELL_FILE_HANDLE DumpFileHandle
;
146 DumpFileHandle
= NULL
;
147 TransferBytes
= Length
;
151 sizeof (FileNameBuffer
),
153 mSelectedAcpiTableName
,
157 Status
= ShellOpenFileByName (
160 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
163 if (EFI_ERROR (Status
)) {
168 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
169 gShellAcpiViewHiiHandle
,
175 Print (L
"Dumping ACPI table to : %s ... ", FileNameBuffer
);
177 Status
= ShellWriteFile (
182 if (EFI_ERROR (Status
)) {
183 Print (L
"ERROR: Failed to dump table to binary file.\n");
189 ShellCloseFile (&DumpFileHandle
);
190 return (Length
== TransferBytes
);
194 This function processes the table reporting options for the ACPI table.
196 @param [in] Signature The ACPI table Signature.
197 @param [in] TablePtr Pointer to the ACPI table data.
198 @param [in] Length The length fo the ACPI table.
200 @retval Returns TRUE if the ACPI table should be traced.
203 ProcessTableReportOptions (
204 IN CONST UINT32 Signature
,
205 IN CONST UINT8
* TablePtr
,
206 IN CONST UINT32 Length
209 UINTN OriginalAttribute
;
215 // set local variables to suppress incorrect compiler/analyzer warnings
217 OriginalAttribute
= 0;
218 SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
220 HighLight
= GetColourHighlighting ();
222 switch (GetReportOption ()) {
227 if (Signature
== GetSelectedAcpiTable ()) {
229 mSelectedAcpiTableFound
= TRUE
;
232 case ReportTableList
:
233 if (mTableCount
== 0) {
235 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
236 gST
->ConOut
->SetAttribute (
238 EFI_TEXT_ATTR(EFI_CYAN
,
239 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
242 Print (L
"\nInstalled Table(s):\n");
244 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
248 L
"\t%4d. %c%c%c%c\n",
256 case ReportDumpBinFile
:
257 if (Signature
== GetSelectedAcpiTable ()) {
258 mSelectedAcpiTableFound
= TRUE
;
259 DumpAcpiTableToFile (TablePtr
, Length
);
263 // We should never be here.
264 // This case is only present to prevent compiler warning.
270 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
271 gST
->ConOut
->SetAttribute (
273 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
274 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
278 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
285 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
293 This function converts a string to ACPI table signature.
295 @param [in] Str Pointer to the string to be converted to the
296 ACPI table signature.
298 @retval The ACPI table signature.
302 ConvertStrToAcpiSignature (
309 ZeroMem (Ptr
, sizeof (Ptr
));
312 // Convert to Upper case and convert to ASCII
313 while ((Index
< 4) && (Str
[Index
] != 0)) {
314 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
315 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
317 Ptr
[Index
] = (CHAR8
)Str
[Index
];
321 return *(UINT32
*)Ptr
;
325 This function iterates the configuration table entries in the
326 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.
328 @param [in] SystemTable Pointer to the EFI system table.
330 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
331 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
332 Returns EFI_SUCCESS if successful.
338 IN EFI_SYSTEM_TABLE
* SystemTable
343 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
344 BOOLEAN FoundAcpiTable
;
345 UINTN OriginalAttribute
;
346 UINTN PrintAttribute
;
347 EREPORT_OPTION ReportOption
;
351 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
355 // set local variables to suppress incorrect compiler/analyzer warnings
357 EfiConfigurationTable
= NULL
;
358 OriginalAttribute
= 0;
360 // Search the table for an entry that matches the ACPI Table Guid
361 FoundAcpiTable
= FALSE
;
362 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
363 if (CompareGuid (&gEfiAcpiTableGuid
,
364 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
365 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
366 FoundAcpiTable
= TRUE
;
371 if (FoundAcpiTable
) {
372 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
374 // The RSDP revision is 1 byte starting at offset 15
375 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
377 if (RsdpRevision
< 2) {
379 L
"ERROR: RSDP version less than 2 is not supported.\n"
381 return EFI_UNSUPPORTED
;
384 // The RSDP length is 4 bytes starting at offset 20
385 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
387 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
389 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
390 if (EFI_ERROR (Status
)) {
392 L
"ERROR: No registered parser found for RSDP.\n"
405 IncrementErrorCount ();
407 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
409 return EFI_NOT_FOUND
;
412 ReportOption
= GetReportOption ();
413 if (ReportTableList
!= ReportOption
) {
414 if (((ReportSelected
== ReportOption
) ||
415 (ReportDumpBinFile
== ReportOption
)) &&
416 (!mSelectedAcpiTableFound
)) {
417 Print (L
"\nRequested ACPI Table not found.\n");
418 } else if (GetConsistencyChecking () &&
419 (ReportDumpBinFile
!= ReportOption
)) {
420 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
422 Print (L
"\nTable Statistics:\n");
424 if (GetColourHighlighting ()) {
425 PrintAttribute
= (GetErrorCount () > 0) ?
428 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
431 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
433 Print (L
"\t%d Error(s)\n", GetErrorCount ());
435 if (GetColourHighlighting ()) {
436 PrintAttribute
= (GetWarningCount () > 0) ?
439 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
443 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
445 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
447 if (GetColourHighlighting ()) {
448 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
456 Function for 'acpiview' command.
458 @param[in] ImageHandle Handle to the Image (NULL if Internal).
459 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
463 ShellCommandRunAcpiView (
464 IN EFI_HANDLE ImageHandle
,
465 IN EFI_SYSTEM_TABLE
* SystemTable
469 SHELL_STATUS ShellStatus
;
471 CHAR16
* ProblemParam
;
472 SHELL_FILE_HANDLE TmpDumpFileHandle
;
475 mReportType
= ReportAll
;
478 mSelectedAcpiTable
= 0;
479 mSelectedAcpiTableName
= NULL
;
480 mSelectedAcpiTableFound
= FALSE
;
481 mConsistencyCheck
= TRUE
;
483 ShellStatus
= SHELL_SUCCESS
;
485 TmpDumpFileHandle
= NULL
;
487 // Reset The error/warning counters
489 ResetWarningCount ();
491 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
492 if (EFI_ERROR (Status
)) {
493 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
498 STRING_TOKEN (STR_GEN_PROBLEM
),
499 gShellAcpiViewHiiHandle
,
503 FreePool (ProblemParam
);
505 Print (L
"acpiview: Error processing input parameter(s)\n");
507 ShellStatus
= SHELL_INVALID_PARAMETER
;
509 if (ShellCommandLineGetCount (Package
) > 1) {
514 STRING_TOKEN (STR_GEN_TOO_MANY
),
515 gShellAcpiViewHiiHandle
,
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
519 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
524 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
525 gShellAcpiViewHiiHandle
,
528 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
529 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
534 STRING_TOKEN (STR_GEN_NO_VALUE
),
535 gShellAcpiViewHiiHandle
,
539 ShellStatus
= SHELL_INVALID_PARAMETER
;
540 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
541 ShellCommandLineGetFlag (Package
, L
"-l"))) {
546 STRING_TOKEN (STR_GEN_TOO_MANY
),
547 gShellAcpiViewHiiHandle
,
550 ShellStatus
= SHELL_INVALID_PARAMETER
;
551 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
552 !ShellCommandLineGetFlag (Package
, L
"-s")) {
557 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
558 gShellAcpiViewHiiHandle
,
563 ShellStatus
= SHELL_INVALID_PARAMETER
;
565 // Turn on colour highlighting if requested
566 SetColourHighlighting (ShellCommandLineGetFlag (Package
, L
"-h"));
568 // Surpress consistency checking if requested
569 SetConsistencyChecking (!ShellCommandLineGetFlag (Package
, L
"-q"));
571 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
572 mReportType
= ReportTableList
;
574 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
575 if (mSelectedAcpiTableName
!= NULL
) {
576 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
577 mSelectedAcpiTableName
579 mReportType
= ReportSelected
;
581 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
582 // Create a temporary file to check if the media is writable.
583 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
584 mReportType
= ReportDumpBinFile
;
588 sizeof (FileNameBuffer
),
590 mSelectedAcpiTableName
,
594 Status
= ShellOpenFileByName (
597 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
598 EFI_FILE_MODE_CREATE
,
602 if (EFI_ERROR (Status
)) {
603 ShellStatus
= SHELL_INVALID_PARAMETER
;
604 TmpDumpFileHandle
= NULL
;
609 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
610 gShellAcpiViewHiiHandle
,
615 // Delete Temporary file.
616 ShellDeleteFile (&TmpDumpFileHandle
);
621 // Parse ACPI Table information
622 Status
= AcpiView (SystemTable
);
623 if (EFI_ERROR (Status
)) {
624 ShellStatus
= SHELL_NOT_FOUND
;
630 if (Package
!= NULL
) {
631 ShellCommandLineFreeVarList (Package
);