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
;
128 DumpFileHandle
= NULL
;
129 TransferBytes
= Length
;
133 sizeof (FileNameBuffer
),
135 mSelectedAcpiTableName
,
139 Status
= ShellOpenFileByName (
142 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
145 if (EFI_ERROR (Status
)) {
150 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
151 gShellAcpiViewHiiHandle
,
157 Print (L
"Dumping ACPI table to : %s ... ", FileNameBuffer
);
159 Status
= ShellWriteFile (
164 if (EFI_ERROR (Status
)) {
165 Print (L
"ERROR: Failed to dump table to binary file.\n");
171 ShellCloseFile (&DumpFileHandle
);
172 return (Length
== TransferBytes
);
176 This function processes the table reporting options for the ACPI table.
178 @param [in] Signature The ACPI table Signature.
179 @param [in] TablePtr Pointer to the ACPI table data.
180 @param [in] Length The length fo the ACPI table.
182 @retval Returns TRUE if the ACPI table should be traced.
185 ProcessTableReportOptions (
186 IN CONST UINT32 Signature
,
187 IN CONST UINT8
* TablePtr
,
188 IN CONST UINT32 Length
191 UINTN OriginalAttribute
;
196 SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
198 HighLight
= GetColourHighlighting ();
200 switch (GetReportOption ()) {
205 if (Signature
== GetSelectedAcpiTable ()) {
207 mSelectedAcpiTableFound
= TRUE
;
210 case ReportTableList
:
211 if (mTableCount
== 0) {
213 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
214 gST
->ConOut
->SetAttribute (
216 EFI_TEXT_ATTR(EFI_CYAN
,
217 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
220 Print (L
"\nInstalled Table(s):\n");
222 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
226 L
"\t%4d. %c%c%c%c\n",
234 case ReportDumpBinFile
:
235 if (Signature
== GetSelectedAcpiTable ()) {
236 mSelectedAcpiTableFound
= TRUE
;
237 DumpAcpiTableToFile (TablePtr
, Length
);
241 // We should never be here.
242 // This case is only present to prevent compiler warning.
248 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
249 gST
->ConOut
->SetAttribute (
251 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
252 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
256 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
263 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
271 This function converts a string to ACPI table signature.
273 @param [in] Str Pointer to the string to be converted to the
274 ACPI table signature.
276 @retval The ACPI table signature.
280 ConvertStrToAcpiSignature (
289 // Convert to Upper case and convert to ASCII
290 while ((Index
< 4) && (Str
[Index
] != 0)) {
291 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
292 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
294 Ptr
[Index
] = (CHAR8
)Str
[Index
];
298 return *(UINT32
*)Ptr
;
302 This function iterates the configuration table entries in the
303 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.
305 @param [in] SystemTable Pointer to the EFI system table.
307 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
308 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
309 Returns EFI_SUCCESS if successful.
315 IN EFI_SYSTEM_TABLE
* SystemTable
320 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
321 BOOLEAN FoundAcpiTable
;
322 UINTN OriginalAttribute
;
323 UINTN PrintAttribute
;
324 EREPORT_OPTION ReportOption
;
328 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
331 // Search the table for an entry that matches the ACPI Table Guid
332 FoundAcpiTable
= FALSE
;
333 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
334 if (CompareGuid (&gEfiAcpiTableGuid
,
335 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
336 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
337 FoundAcpiTable
= TRUE
;
342 if (FoundAcpiTable
) {
343 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
345 // The RSDP revision is 1 byte starting at offset 15
346 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
348 if (RsdpRevision
< 2) {
350 L
"ERROR: RSDP version less than 2 is not supported.\n"
352 return EFI_UNSUPPORTED
;
355 // The RSDP length is 4 bytes starting at offset 20
356 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
358 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
360 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
361 if (EFI_ERROR (Status
)) {
363 L
"ERROR: No registered parser found for RSDP.\n"
376 IncrementErrorCount ();
378 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
380 return EFI_NOT_FOUND
;
383 ReportOption
= GetReportOption ();
384 if (ReportTableList
!= ReportOption
) {
385 if (((ReportSelected
== ReportOption
) ||
386 (ReportDumpBinFile
== ReportOption
)) &&
387 (!mSelectedAcpiTableFound
)) {
388 Print (L
"\nRequested ACPI Table not found.\n");
389 } else if (ReportDumpBinFile
!= ReportOption
) {
390 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
392 Print (L
"\nTable Statistics:\n");
394 if (GetColourHighlighting ()) {
395 PrintAttribute
= (GetErrorCount () > 0) ?
398 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
401 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
403 Print (L
"\t%d Error(s)\n", GetErrorCount ());
405 if (GetColourHighlighting ()) {
406 PrintAttribute
= (GetWarningCount () > 0) ?
409 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
413 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
415 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
417 if (GetColourHighlighting ()) {
418 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
426 Function for 'acpiview' command.
428 @param[in] ImageHandle Handle to the Image (NULL if Internal).
429 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
433 ShellCommandRunAcpiView (
434 IN EFI_HANDLE ImageHandle
,
435 IN EFI_SYSTEM_TABLE
* SystemTable
439 SHELL_STATUS ShellStatus
;
441 CHAR16
* ProblemParam
;
443 CHAR8 ColourOption
[8];
444 SHELL_FILE_HANDLE TmpDumpFileHandle
;
447 mReportType
= ReportAll
;
450 mSelectedAcpiTable
= 0;
451 mSelectedAcpiTableName
= NULL
;
452 mSelectedAcpiTableFound
= FALSE
;
454 mConsistencyCheck
= TRUE
;
456 ShellStatus
= SHELL_SUCCESS
;
458 TmpDumpFileHandle
= NULL
;
460 // Reset The error/warning counters
462 ResetWarningCount ();
464 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
465 if (EFI_ERROR (Status
)) {
466 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
471 STRING_TOKEN (STR_GEN_PROBLEM
),
472 gShellAcpiViewHiiHandle
,
476 FreePool (ProblemParam
);
478 Print (L
"acpiview: Error processing input parameter(s)\n");
480 ShellStatus
= SHELL_INVALID_PARAMETER
;
482 if (ShellCommandLineGetCount (Package
) > 1) {
487 STRING_TOKEN (STR_GEN_TOO_MANY
),
488 gShellAcpiViewHiiHandle
,
491 ShellStatus
= SHELL_INVALID_PARAMETER
;
492 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
497 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
498 gShellAcpiViewHiiHandle
,
501 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
502 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
507 STRING_TOKEN (STR_GEN_NO_VALUE
),
508 gShellAcpiViewHiiHandle
,
512 ShellStatus
= SHELL_INVALID_PARAMETER
;
513 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
514 ShellCommandLineGetFlag (Package
, L
"-l"))) {
519 STRING_TOKEN (STR_GEN_TOO_MANY
),
520 gShellAcpiViewHiiHandle
,
523 ShellStatus
= SHELL_INVALID_PARAMETER
;
524 } else if (ShellCommandLineGetFlag (Package
, L
"-h") &&
525 ShellCommandLineGetValue (Package
, L
"-h") == NULL
) {
530 STRING_TOKEN (STR_GEN_NO_VALUE
),
531 gShellAcpiViewHiiHandle
,
535 ShellStatus
= SHELL_INVALID_PARAMETER
;
536 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
537 !ShellCommandLineGetFlag (Package
, L
"-s")) {
542 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
543 gShellAcpiViewHiiHandle
,
548 ShellStatus
= SHELL_INVALID_PARAMETER
;
550 // Check if the colour option is set
551 Temp
= ShellCommandLineGetValue (Package
, L
"-h");
553 UnicodeStrToAsciiStrS (Temp
, ColourOption
, sizeof (ColourOption
));
554 if ((AsciiStriCmp (ColourOption
, "ON") == 0) ||
555 (AsciiStriCmp (ColourOption
, "TRUE") == 0)) {
556 SetColourHighlighting (TRUE
);
557 } else if ((AsciiStriCmp (ColourOption
, "OFF") == 0) ||
558 (AsciiStriCmp (ColourOption
, "FALSE") == 0)) {
559 SetColourHighlighting (FALSE
);
563 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
564 mReportType
= ReportTableList
;
566 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
567 if (mSelectedAcpiTableName
!= NULL
) {
568 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
569 mSelectedAcpiTableName
571 mReportType
= ReportSelected
;
573 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
574 // Create a temporary file to check if the media is writable.
575 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
576 mReportType
= ReportDumpBinFile
;
580 sizeof (FileNameBuffer
),
582 mSelectedAcpiTableName
,
586 Status
= ShellOpenFileByName (
589 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
590 EFI_FILE_MODE_CREATE
,
594 if (EFI_ERROR (Status
)) {
595 ShellStatus
= SHELL_INVALID_PARAMETER
;
596 TmpDumpFileHandle
= NULL
;
601 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
602 gShellAcpiViewHiiHandle
,
607 // Delete Temporary file.
608 ShellDeleteFile (&TmpDumpFileHandle
);
613 // Parse ACPI Table information
614 Status
= AcpiView (SystemTable
);
615 if (EFI_ERROR (Status
)) {
616 ShellStatus
= SHELL_NOT_FOUND
;
622 if (Package
!= NULL
) {
623 ShellCommandLineFreeVarList (Package
);