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 mVerbose
;
29 STATIC BOOLEAN mConsistencyCheck
;
30 STATIC BOOLEAN mColourHighlighting
;
33 An array of acpiview command line parameters.
35 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
46 This function returns the colour highlighting status.
48 @retval TRUE if colour highlighting is enabled.
51 GetColourHighlighting (
55 return mColourHighlighting
;
59 This function sets the colour highlighting status.
61 @param Highlight The Highlight status.
65 SetColourHighlighting (
69 mColourHighlighting
= Highlight
;
73 This function returns the report options.
75 @retval Returns the report option.
87 This function returns the selected ACPI table.
89 @retval Returns signature of the selected ACPI table.
93 GetSelectedAcpiTable (
97 return mSelectedAcpiTable
;
101 This function dumps the ACPI table to a file.
103 @param [in] Ptr Pointer to the ACPI table data.
104 @param [in] Length The length of the ACPI table.
106 @retval TRUE Success.
107 @retval FALSE Failure.
111 DumpAcpiTableToFile (
113 IN CONST UINTN Length
117 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
118 SHELL_FILE_HANDLE DumpFileHandle
;
121 DumpFileHandle
= NULL
;
122 TransferBytes
= Length
;
126 sizeof (FileNameBuffer
),
128 mSelectedAcpiTableName
,
132 Status
= ShellOpenFileByName (
135 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
138 if (EFI_ERROR (Status
)) {
143 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
144 gShellAcpiViewHiiHandle
,
150 Print (L
"Dumping ACPI table to : %s ... ", FileNameBuffer
);
152 Status
= ShellWriteFile (
157 if (EFI_ERROR (Status
)) {
158 Print (L
"ERROR: Failed to dump table to binary file.\n");
164 ShellCloseFile (&DumpFileHandle
);
165 return (Length
== TransferBytes
);
169 This function processes the table reporting options for the ACPI table.
171 @param [in] Signature The ACPI table Signature.
172 @param [in] TablePtr Pointer to the ACPI table data.
173 @param [in] Length The length fo the ACPI table.
175 @retval Returns TRUE if the ACPI table should be traced.
178 ProcessTableReportOptions (
179 IN CONST UINT32 Signature
,
180 IN CONST UINT8
* TablePtr
,
181 IN CONST UINT32 Length
184 UINTN OriginalAttribute
;
189 SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
191 HighLight
= GetColourHighlighting ();
193 switch (GetReportOption ()) {
198 if (Signature
== GetSelectedAcpiTable ()) {
200 mSelectedAcpiTableFound
= TRUE
;
203 case ReportTableList
:
204 if (mTableCount
== 0) {
206 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
207 gST
->ConOut
->SetAttribute (
209 EFI_TEXT_ATTR(EFI_CYAN
,
210 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
213 Print (L
"\nInstalled Table(s):\n");
215 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
219 L
"\t%4d. %c%c%c%c\n",
227 case ReportDumpBinFile
:
228 if (Signature
== GetSelectedAcpiTable ()) {
229 mSelectedAcpiTableFound
= TRUE
;
230 DumpAcpiTableToFile (TablePtr
, Length
);
234 // We should never be here.
235 // This case is only present to prevent compiler warning.
241 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
242 gST
->ConOut
->SetAttribute (
244 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
245 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
249 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
256 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
264 This function converts a string to ACPI table signature.
266 @param [in] Str Pointer to the string to be converted to the
267 ACPI table signature.
269 @retval The ACPI table signature.
273 ConvertStrToAcpiSignature (
280 ZeroMem (Ptr
, sizeof (Ptr
));
283 // Convert to Upper case and convert to ASCII
284 while ((Index
< 4) && (Str
[Index
] != 0)) {
285 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
286 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
288 Ptr
[Index
] = (CHAR8
)Str
[Index
];
292 return *(UINT32
*)Ptr
;
296 This function iterates the configuration table entries in the
297 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.
299 @param [in] SystemTable Pointer to the EFI system table.
301 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
302 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
303 Returns EFI_SUCCESS if successful.
309 IN EFI_SYSTEM_TABLE
* SystemTable
314 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
315 BOOLEAN FoundAcpiTable
;
316 UINTN OriginalAttribute
;
317 UINTN PrintAttribute
;
318 EREPORT_OPTION ReportOption
;
322 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
325 // Search the table for an entry that matches the ACPI Table Guid
326 FoundAcpiTable
= FALSE
;
327 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
328 if (CompareGuid (&gEfiAcpiTableGuid
,
329 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
330 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
331 FoundAcpiTable
= TRUE
;
336 if (FoundAcpiTable
) {
337 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
339 // The RSDP revision is 1 byte starting at offset 15
340 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
342 if (RsdpRevision
< 2) {
344 L
"ERROR: RSDP version less than 2 is not supported.\n"
346 return EFI_UNSUPPORTED
;
349 // The RSDP length is 4 bytes starting at offset 20
350 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
352 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
354 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
355 if (EFI_ERROR (Status
)) {
357 L
"ERROR: No registered parser found for RSDP.\n"
370 IncrementErrorCount ();
372 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
374 return EFI_NOT_FOUND
;
377 ReportOption
= GetReportOption ();
378 if (ReportTableList
!= ReportOption
) {
379 if (((ReportSelected
== ReportOption
) ||
380 (ReportDumpBinFile
== ReportOption
)) &&
381 (!mSelectedAcpiTableFound
)) {
382 Print (L
"\nRequested ACPI Table not found.\n");
383 } else if (ReportDumpBinFile
!= ReportOption
) {
384 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
386 Print (L
"\nTable Statistics:\n");
388 if (GetColourHighlighting ()) {
389 PrintAttribute
= (GetErrorCount () > 0) ?
392 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
395 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
397 Print (L
"\t%d Error(s)\n", GetErrorCount ());
399 if (GetColourHighlighting ()) {
400 PrintAttribute
= (GetWarningCount () > 0) ?
403 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
407 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
409 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
411 if (GetColourHighlighting ()) {
412 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
420 Function for 'acpiview' command.
422 @param[in] ImageHandle Handle to the Image (NULL if Internal).
423 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
427 ShellCommandRunAcpiView (
428 IN EFI_HANDLE ImageHandle
,
429 IN EFI_SYSTEM_TABLE
* SystemTable
433 SHELL_STATUS ShellStatus
;
435 CHAR16
* ProblemParam
;
437 CHAR8 ColourOption
[8];
438 SHELL_FILE_HANDLE TmpDumpFileHandle
;
441 mReportType
= ReportAll
;
444 mSelectedAcpiTable
= 0;
445 mSelectedAcpiTableName
= NULL
;
446 mSelectedAcpiTableFound
= FALSE
;
448 mConsistencyCheck
= TRUE
;
450 ShellStatus
= SHELL_SUCCESS
;
452 TmpDumpFileHandle
= NULL
;
454 // Reset The error/warning counters
456 ResetWarningCount ();
458 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
459 if (EFI_ERROR (Status
)) {
460 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
465 STRING_TOKEN (STR_GEN_PROBLEM
),
466 gShellAcpiViewHiiHandle
,
470 FreePool (ProblemParam
);
472 Print (L
"acpiview: Error processing input parameter(s)\n");
474 ShellStatus
= SHELL_INVALID_PARAMETER
;
476 if (ShellCommandLineGetCount (Package
) > 1) {
481 STRING_TOKEN (STR_GEN_TOO_MANY
),
482 gShellAcpiViewHiiHandle
,
485 ShellStatus
= SHELL_INVALID_PARAMETER
;
486 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
491 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
492 gShellAcpiViewHiiHandle
,
495 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
496 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
501 STRING_TOKEN (STR_GEN_NO_VALUE
),
502 gShellAcpiViewHiiHandle
,
506 ShellStatus
= SHELL_INVALID_PARAMETER
;
507 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
508 ShellCommandLineGetFlag (Package
, L
"-l"))) {
513 STRING_TOKEN (STR_GEN_TOO_MANY
),
514 gShellAcpiViewHiiHandle
,
517 ShellStatus
= SHELL_INVALID_PARAMETER
;
518 } else if (ShellCommandLineGetFlag (Package
, L
"-h") &&
519 ShellCommandLineGetValue (Package
, L
"-h") == NULL
) {
524 STRING_TOKEN (STR_GEN_NO_VALUE
),
525 gShellAcpiViewHiiHandle
,
529 ShellStatus
= SHELL_INVALID_PARAMETER
;
530 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
531 !ShellCommandLineGetFlag (Package
, L
"-s")) {
536 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
537 gShellAcpiViewHiiHandle
,
542 ShellStatus
= SHELL_INVALID_PARAMETER
;
544 // Check if the colour option is set
545 Temp
= ShellCommandLineGetValue (Package
, L
"-h");
547 UnicodeStrToAsciiStrS (Temp
, ColourOption
, sizeof (ColourOption
));
548 if ((AsciiStriCmp (ColourOption
, "ON") == 0) ||
549 (AsciiStriCmp (ColourOption
, "TRUE") == 0)) {
550 SetColourHighlighting (TRUE
);
551 } else if ((AsciiStriCmp (ColourOption
, "OFF") == 0) ||
552 (AsciiStriCmp (ColourOption
, "FALSE") == 0)) {
553 SetColourHighlighting (FALSE
);
557 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
558 mReportType
= ReportTableList
;
560 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
561 if (mSelectedAcpiTableName
!= NULL
) {
562 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
563 mSelectedAcpiTableName
565 mReportType
= ReportSelected
;
567 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
568 // Create a temporary file to check if the media is writable.
569 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
570 mReportType
= ReportDumpBinFile
;
574 sizeof (FileNameBuffer
),
576 mSelectedAcpiTableName
,
580 Status
= ShellOpenFileByName (
583 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
584 EFI_FILE_MODE_CREATE
,
588 if (EFI_ERROR (Status
)) {
589 ShellStatus
= SHELL_INVALID_PARAMETER
;
590 TmpDumpFileHandle
= NULL
;
595 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
596 gShellAcpiViewHiiHandle
,
601 // Delete Temporary file.
602 ShellDeleteFile (&TmpDumpFileHandle
);
607 // Parse ACPI Table information
608 Status
= AcpiView (SystemTable
);
609 if (EFI_ERROR (Status
)) {
610 ShellStatus
= SHELL_NOT_FOUND
;
616 if (Package
!= NULL
) {
617 ShellCommandLineFreeVarList (Package
);