3 Copyright (c) 2016 - 2018, 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
[] = {
47 This function returns the colour highlighting status.
49 @retval TRUE if colour highlighting is enabled.
52 GetColourHighlighting (
56 return mColourHighlighting
;
60 This function sets the colour highlighting status.
62 @param Highlight The Highlight status.
66 SetColourHighlighting (
70 mColourHighlighting
= Highlight
;
74 This function returns the report options.
76 @retval Returns the report option.
88 This function returns the selected ACPI table.
90 @retval Returns signature of the selected ACPI table.
94 GetSelectedAcpiTable (
98 return mSelectedAcpiTable
;
102 This function dumps the ACPI table to a file.
104 @param [in] Ptr Pointer to the ACPI table data.
105 @param [in] Length The length of the ACPI table.
107 @retval TRUE Success.
108 @retval FALSE Failure.
112 DumpAcpiTableToFile (
114 IN CONST UINTN Length
118 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
119 SHELL_FILE_HANDLE DumpFileHandle
;
122 DumpFileHandle
= NULL
;
123 TransferBytes
= Length
;
127 sizeof (FileNameBuffer
),
129 mSelectedAcpiTableName
,
133 Status
= ShellOpenFileByName (
136 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
139 if (EFI_ERROR (Status
)) {
144 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
145 gShellAcpiViewHiiHandle
,
151 Print (L
"Dumping ACPI table to : %s ... ", FileNameBuffer
);
153 Status
= ShellWriteFile (
158 if (EFI_ERROR (Status
)) {
159 Print (L
"ERROR: Failed to dump table to binary file.\n");
165 ShellCloseFile (&DumpFileHandle
);
166 return (Length
== TransferBytes
);
170 This function processes the table reporting options for the ACPI table.
172 @param [in] Signature The ACPI table Signature.
173 @param [in] TablePtr Pointer to the ACPI table data.
174 @param [in] Length The length fo the ACPI table.
176 @retval Returns TRUE if the ACPI table should be traced.
179 ProcessTableReportOptions (
180 IN CONST UINT32 Signature
,
181 IN CONST UINT8
* TablePtr
,
182 IN CONST UINT32 Length
185 UINTN OriginalAttribute
;
190 SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
192 HighLight
= GetColourHighlighting ();
194 switch (GetReportOption ()) {
199 if (Signature
== GetSelectedAcpiTable ()) {
201 mSelectedAcpiTableFound
= TRUE
;
204 case ReportTableList
:
205 if (mTableCount
== 0) {
207 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
208 gST
->ConOut
->SetAttribute (
210 EFI_TEXT_ATTR(EFI_CYAN
,
211 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
214 Print (L
"\nInstalled Table(s):\n");
216 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
220 L
"\t%4d. %c%c%c%c\n",
228 case ReportDumpBinFile
:
229 if (Signature
== GetSelectedAcpiTable ()) {
230 mSelectedAcpiTableFound
= TRUE
;
231 DumpAcpiTableToFile (TablePtr
, Length
);
235 // We should never be here.
236 // This case is only present to prevent compiler warning.
242 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
243 gST
->ConOut
->SetAttribute (
245 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
246 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
250 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
257 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
265 This function converts a string to ACPI table signature.
267 @param [in] Str Pointer to the string to be converted to the
268 ACPI table signature.
270 @retval The ACPI table signature.
274 ConvertStrToAcpiSignature (
281 ZeroMem (Ptr
, sizeof (Ptr
));
284 // Convert to Upper case and convert to ASCII
285 while ((Index
< 4) && (Str
[Index
] != 0)) {
286 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
287 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
289 Ptr
[Index
] = (CHAR8
)Str
[Index
];
293 return *(UINT32
*)Ptr
;
297 This function iterates the configuration table entries in the
298 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.
300 @param [in] SystemTable Pointer to the EFI system table.
302 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
303 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
304 Returns EFI_SUCCESS if successful.
310 IN EFI_SYSTEM_TABLE
* SystemTable
315 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
316 BOOLEAN FoundAcpiTable
;
317 UINTN OriginalAttribute
;
318 UINTN PrintAttribute
;
319 EREPORT_OPTION ReportOption
;
323 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
326 // Search the table for an entry that matches the ACPI Table Guid
327 FoundAcpiTable
= FALSE
;
328 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
329 if (CompareGuid (&gEfiAcpiTableGuid
,
330 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
331 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
332 FoundAcpiTable
= TRUE
;
337 if (FoundAcpiTable
) {
338 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
340 // The RSDP revision is 1 byte starting at offset 15
341 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
343 if (RsdpRevision
< 2) {
345 L
"ERROR: RSDP version less than 2 is not supported.\n"
347 return EFI_UNSUPPORTED
;
350 // The RSDP length is 4 bytes starting at offset 20
351 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
353 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
355 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
356 if (EFI_ERROR (Status
)) {
358 L
"ERROR: No registered parser found for RSDP.\n"
371 IncrementErrorCount ();
373 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
375 return EFI_NOT_FOUND
;
378 ReportOption
= GetReportOption ();
379 if (ReportTableList
!= ReportOption
) {
380 if (((ReportSelected
== ReportOption
) ||
381 (ReportDumpBinFile
== ReportOption
)) &&
382 (!mSelectedAcpiTableFound
)) {
383 Print (L
"\nRequested ACPI Table not found.\n");
384 } else if (ReportDumpBinFile
!= ReportOption
) {
385 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
387 Print (L
"\nTable Statistics:\n");
389 if (GetColourHighlighting ()) {
390 PrintAttribute
= (GetErrorCount () > 0) ?
393 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
396 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
398 Print (L
"\t%d Error(s)\n", GetErrorCount ());
400 if (GetColourHighlighting ()) {
401 PrintAttribute
= (GetWarningCount () > 0) ?
404 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
408 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
410 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
412 if (GetColourHighlighting ()) {
413 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
421 Function for 'acpiview' command.
423 @param[in] ImageHandle Handle to the Image (NULL if Internal).
424 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
428 ShellCommandRunAcpiView (
429 IN EFI_HANDLE ImageHandle
,
430 IN EFI_SYSTEM_TABLE
* SystemTable
434 SHELL_STATUS ShellStatus
;
436 CHAR16
* ProblemParam
;
438 CHAR8 ColourOption
[8];
439 SHELL_FILE_HANDLE TmpDumpFileHandle
;
442 mReportType
= ReportAll
;
445 mSelectedAcpiTable
= 0;
446 mSelectedAcpiTableName
= NULL
;
447 mSelectedAcpiTableFound
= FALSE
;
449 mConsistencyCheck
= TRUE
;
451 ShellStatus
= SHELL_SUCCESS
;
453 TmpDumpFileHandle
= NULL
;
455 // Reset The error/warning counters
457 ResetWarningCount ();
459 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
460 if (EFI_ERROR (Status
)) {
461 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
466 STRING_TOKEN (STR_GEN_PROBLEM
),
467 gShellAcpiViewHiiHandle
,
471 FreePool (ProblemParam
);
473 Print (L
"acpiview: Error processing input parameter(s)\n");
475 ShellStatus
= SHELL_INVALID_PARAMETER
;
477 if (ShellCommandLineGetCount (Package
) > 1) {
482 STRING_TOKEN (STR_GEN_TOO_MANY
),
483 gShellAcpiViewHiiHandle
,
486 ShellStatus
= SHELL_INVALID_PARAMETER
;
487 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
492 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
493 gShellAcpiViewHiiHandle
,
496 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
497 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
502 STRING_TOKEN (STR_GEN_NO_VALUE
),
503 gShellAcpiViewHiiHandle
,
507 ShellStatus
= SHELL_INVALID_PARAMETER
;
508 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
509 ShellCommandLineGetFlag (Package
, L
"-l"))) {
514 STRING_TOKEN (STR_GEN_TOO_MANY
),
515 gShellAcpiViewHiiHandle
,
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
519 } else if (ShellCommandLineGetFlag (Package
, L
"-h") &&
520 ShellCommandLineGetValue (Package
, L
"-h") == NULL
) {
525 STRING_TOKEN (STR_GEN_NO_VALUE
),
526 gShellAcpiViewHiiHandle
,
530 ShellStatus
= SHELL_INVALID_PARAMETER
;
531 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
532 !ShellCommandLineGetFlag (Package
, L
"-s")) {
537 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
538 gShellAcpiViewHiiHandle
,
543 ShellStatus
= SHELL_INVALID_PARAMETER
;
545 // Check if the colour option is set
546 Temp
= ShellCommandLineGetValue (Package
, L
"-h");
548 UnicodeStrToAsciiStrS (Temp
, ColourOption
, sizeof (ColourOption
));
549 if ((AsciiStriCmp (ColourOption
, "ON") == 0) ||
550 (AsciiStriCmp (ColourOption
, "TRUE") == 0)) {
551 SetColourHighlighting (TRUE
);
552 } else if ((AsciiStriCmp (ColourOption
, "OFF") == 0) ||
553 (AsciiStriCmp (ColourOption
, "FALSE") == 0)) {
554 SetColourHighlighting (FALSE
);
558 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
559 mReportType
= ReportTableList
;
561 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
562 if (mSelectedAcpiTableName
!= NULL
) {
563 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
564 mSelectedAcpiTableName
566 mReportType
= ReportSelected
;
568 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
569 // Create a temporary file to check if the media is writable.
570 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
571 mReportType
= ReportDumpBinFile
;
575 sizeof (FileNameBuffer
),
577 mSelectedAcpiTableName
,
581 Status
= ShellOpenFileByName (
584 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
585 EFI_FILE_MODE_CREATE
,
589 if (EFI_ERROR (Status
)) {
590 ShellStatus
= SHELL_INVALID_PARAMETER
;
591 TmpDumpFileHandle
= NULL
;
596 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
597 gShellAcpiViewHiiHandle
,
602 // Delete Temporary file.
603 ShellDeleteFile (&TmpDumpFileHandle
);
608 // Parse ACPI Table information
609 Status
= AcpiView (SystemTable
);
610 if (EFI_ERROR (Status
)) {
611 ShellStatus
= SHELL_NOT_FOUND
;
617 if (Package
!= NULL
) {
618 ShellCommandLineFreeVarList (Package
);