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
;
214 SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
216 HighLight
= GetColourHighlighting ();
218 switch (GetReportOption ()) {
223 if (Signature
== GetSelectedAcpiTable ()) {
225 mSelectedAcpiTableFound
= TRUE
;
228 case ReportTableList
:
229 if (mTableCount
== 0) {
231 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
232 gST
->ConOut
->SetAttribute (
234 EFI_TEXT_ATTR(EFI_CYAN
,
235 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
238 Print (L
"\nInstalled Table(s):\n");
240 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
244 L
"\t%4d. %c%c%c%c\n",
252 case ReportDumpBinFile
:
253 if (Signature
== GetSelectedAcpiTable ()) {
254 mSelectedAcpiTableFound
= TRUE
;
255 DumpAcpiTableToFile (TablePtr
, Length
);
259 // We should never be here.
260 // This case is only present to prevent compiler warning.
266 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
267 gST
->ConOut
->SetAttribute (
269 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
270 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
274 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
281 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
289 This function converts a string to ACPI table signature.
291 @param [in] Str Pointer to the string to be converted to the
292 ACPI table signature.
294 @retval The ACPI table signature.
298 ConvertStrToAcpiSignature (
305 ZeroMem (Ptr
, sizeof (Ptr
));
308 // Convert to Upper case and convert to ASCII
309 while ((Index
< 4) && (Str
[Index
] != 0)) {
310 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
311 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
313 Ptr
[Index
] = (CHAR8
)Str
[Index
];
317 return *(UINT32
*)Ptr
;
321 This function iterates the configuration table entries in the
322 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.
324 @param [in] SystemTable Pointer to the EFI system table.
326 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
327 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
328 Returns EFI_SUCCESS if successful.
334 IN EFI_SYSTEM_TABLE
* SystemTable
339 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
340 BOOLEAN FoundAcpiTable
;
341 UINTN OriginalAttribute
;
342 UINTN PrintAttribute
;
343 EREPORT_OPTION ReportOption
;
347 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
350 // Search the table for an entry that matches the ACPI Table Guid
351 FoundAcpiTable
= FALSE
;
352 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
353 if (CompareGuid (&gEfiAcpiTableGuid
,
354 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
355 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
356 FoundAcpiTable
= TRUE
;
361 if (FoundAcpiTable
) {
362 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
364 // The RSDP revision is 1 byte starting at offset 15
365 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
367 if (RsdpRevision
< 2) {
369 L
"ERROR: RSDP version less than 2 is not supported.\n"
371 return EFI_UNSUPPORTED
;
374 // The RSDP length is 4 bytes starting at offset 20
375 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
377 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
379 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
380 if (EFI_ERROR (Status
)) {
382 L
"ERROR: No registered parser found for RSDP.\n"
395 IncrementErrorCount ();
397 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
399 return EFI_NOT_FOUND
;
402 ReportOption
= GetReportOption ();
403 if (ReportTableList
!= ReportOption
) {
404 if (((ReportSelected
== ReportOption
) ||
405 (ReportDumpBinFile
== ReportOption
)) &&
406 (!mSelectedAcpiTableFound
)) {
407 Print (L
"\nRequested ACPI Table not found.\n");
408 } else if (GetConsistencyChecking () &&
409 (ReportDumpBinFile
!= ReportOption
)) {
410 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
412 Print (L
"\nTable Statistics:\n");
414 if (GetColourHighlighting ()) {
415 PrintAttribute
= (GetErrorCount () > 0) ?
418 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
421 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
423 Print (L
"\t%d Error(s)\n", GetErrorCount ());
425 if (GetColourHighlighting ()) {
426 PrintAttribute
= (GetWarningCount () > 0) ?
429 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
433 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
435 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
437 if (GetColourHighlighting ()) {
438 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
446 Function for 'acpiview' command.
448 @param[in] ImageHandle Handle to the Image (NULL if Internal).
449 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
453 ShellCommandRunAcpiView (
454 IN EFI_HANDLE ImageHandle
,
455 IN EFI_SYSTEM_TABLE
* SystemTable
459 SHELL_STATUS ShellStatus
;
461 CHAR16
* ProblemParam
;
463 CHAR8 ColourOption
[8];
464 SHELL_FILE_HANDLE TmpDumpFileHandle
;
467 mReportType
= ReportAll
;
470 mSelectedAcpiTable
= 0;
471 mSelectedAcpiTableName
= NULL
;
472 mSelectedAcpiTableFound
= FALSE
;
473 mConsistencyCheck
= TRUE
;
475 ShellStatus
= SHELL_SUCCESS
;
477 TmpDumpFileHandle
= NULL
;
479 // Reset The error/warning counters
481 ResetWarningCount ();
483 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
484 if (EFI_ERROR (Status
)) {
485 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
490 STRING_TOKEN (STR_GEN_PROBLEM
),
491 gShellAcpiViewHiiHandle
,
495 FreePool (ProblemParam
);
497 Print (L
"acpiview: Error processing input parameter(s)\n");
499 ShellStatus
= SHELL_INVALID_PARAMETER
;
501 if (ShellCommandLineGetCount (Package
) > 1) {
506 STRING_TOKEN (STR_GEN_TOO_MANY
),
507 gShellAcpiViewHiiHandle
,
510 ShellStatus
= SHELL_INVALID_PARAMETER
;
511 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
516 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
517 gShellAcpiViewHiiHandle
,
520 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
521 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
526 STRING_TOKEN (STR_GEN_NO_VALUE
),
527 gShellAcpiViewHiiHandle
,
531 ShellStatus
= SHELL_INVALID_PARAMETER
;
532 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
533 ShellCommandLineGetFlag (Package
, L
"-l"))) {
538 STRING_TOKEN (STR_GEN_TOO_MANY
),
539 gShellAcpiViewHiiHandle
,
542 ShellStatus
= SHELL_INVALID_PARAMETER
;
543 } else if (ShellCommandLineGetFlag (Package
, L
"-h") &&
544 ShellCommandLineGetValue (Package
, L
"-h") == NULL
) {
549 STRING_TOKEN (STR_GEN_NO_VALUE
),
550 gShellAcpiViewHiiHandle
,
554 ShellStatus
= SHELL_INVALID_PARAMETER
;
555 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
556 !ShellCommandLineGetFlag (Package
, L
"-s")) {
561 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
562 gShellAcpiViewHiiHandle
,
567 ShellStatus
= SHELL_INVALID_PARAMETER
;
569 // Check if the colour option is set
570 Temp
= ShellCommandLineGetValue (Package
, L
"-h");
572 UnicodeStrToAsciiStrS (Temp
, ColourOption
, sizeof (ColourOption
));
573 if ((AsciiStriCmp (ColourOption
, "ON") == 0) ||
574 (AsciiStriCmp (ColourOption
, "TRUE") == 0)) {
575 SetColourHighlighting (TRUE
);
576 } else if ((AsciiStriCmp (ColourOption
, "OFF") == 0) ||
577 (AsciiStriCmp (ColourOption
, "FALSE") == 0)) {
578 SetColourHighlighting (FALSE
);
582 // Surpress consistency checking if requested
583 SetConsistencyChecking (!ShellCommandLineGetFlag (Package
, L
"-q"));
585 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
586 mReportType
= ReportTableList
;
588 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
589 if (mSelectedAcpiTableName
!= NULL
) {
590 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
591 mSelectedAcpiTableName
593 mReportType
= ReportSelected
;
595 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
596 // Create a temporary file to check if the media is writable.
597 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
598 mReportType
= ReportDumpBinFile
;
602 sizeof (FileNameBuffer
),
604 mSelectedAcpiTableName
,
608 Status
= ShellOpenFileByName (
611 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
612 EFI_FILE_MODE_CREATE
,
616 if (EFI_ERROR (Status
)) {
617 ShellStatus
= SHELL_INVALID_PARAMETER
;
618 TmpDumpFileHandle
= NULL
;
623 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
624 gShellAcpiViewHiiHandle
,
629 // Delete Temporary file.
630 ShellDeleteFile (&TmpDumpFileHandle
);
635 // Parse ACPI Table information
636 Status
= AcpiView (SystemTable
);
637 if (EFI_ERROR (Status
)) {
638 ShellStatus
= SHELL_NOT_FOUND
;
644 if (Package
!= NULL
) {
645 ShellCommandLineFreeVarList (Package
);