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
;
38 /** An array of acpiview command line parameters.
40 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
51 /** This function returns the colour highlighting status.
53 @retval TRUE if colour highlighting is enabled.
56 GetColourHighlighting (
60 return mColourHighlighting
;
63 /** This function sets the colour highlighting status.
67 SetColourHighlighting (
71 mColourHighlighting
= Highlight
;
74 /** This function returns the report options.
76 @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
;
100 /** This function dumps the ACPI table to a file.
101 @param [in] Ptr Pointer to the ACPI table data.
102 @param [in] Length The length of the ACPI table.
104 @retval TRUE Success.
105 @retval FALSE Failure.
109 DumpAcpiTableToFile (
111 IN CONST UINTN Length
115 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
116 SHELL_FILE_HANDLE DumpFileHandle
= NULL
;
117 UINTN TransferBytes
= Length
;
121 sizeof (FileNameBuffer
),
123 mSelectedAcpiTableName
,
127 Status
= ShellOpenFileByName (
130 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
133 if (EFI_ERROR (Status
)) {
138 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
139 gShellAcpiViewHiiHandle
,
145 Print (L
"Dumping ACPI table to : %s ... ", FileNameBuffer
);
147 Status
= ShellWriteFile (
152 if (EFI_ERROR (Status
)) {
153 Print (L
"ERROR: Failed to dump table to binary file.\n");
159 ShellCloseFile (&DumpFileHandle
);
160 return (Length
== TransferBytes
);
163 /** This function processes the table reporting options for the ACPI table.
165 @param [in] Signature The ACPI table Signature.
166 @param [in] TablePtr Pointer to the ACPI table data.
167 @param [in] Length The length fo the ACPI table.
169 @retval Returns TRUE if the ACPI table should be traced.
172 ProcessTableReportOptions (
173 IN CONST UINT32 Signature
,
174 IN CONST UINT8
* TablePtr
,
175 IN CONST UINT32 Length
178 UINTN OriginalAttribute
;
179 UINT8
* SignaturePtr
= (UINT8
*)(UINTN
)&Signature
;
181 BOOLEAN HighLight
= GetColourHighlighting ();
182 switch (GetReportOption ()) {
186 case EREPORT_SELECTED
:
187 if (Signature
== GetSelectedAcpiTable ()) {
189 mSelectedAcpiTableFound
= TRUE
;
192 case EREPORT_TABLE_LIST
:
193 if (mTableCount
== 0) {
195 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
196 gST
->ConOut
->SetAttribute (
198 EFI_TEXT_ATTR(EFI_CYAN
,
199 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
202 Print (L
"\nInstalled Table(s):\n");
204 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
208 L
"\t%4d. %c%c%c%c\n",
216 case EREPORT_DUMP_BIN_FILE
:
217 if (Signature
== GetSelectedAcpiTable ()) {
218 mSelectedAcpiTableFound
= TRUE
;
219 DumpAcpiTableToFile (TablePtr
, Length
);
223 // We should never be here.
224 // This case is only present to prevent compiler warning.
230 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
231 gST
->ConOut
->SetAttribute (
233 EFI_TEXT_ATTR(EFI_LIGHTBLUE
,
234 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4))
238 L
"\n\n --------------- %c%c%c%c Table --------------- \n\n",
245 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
252 /** This function converts a string to ACPI table signature.
254 @param [in] Str Pointer to the string to be converted to the
255 ACPI table signature.
257 @retval The ACPI table signature.
261 ConvertStrToAcpiSignature (
268 // Convert to Upper case and convert to ASCII
269 while ((Index
< 4) && (Str
[Index
] != 0)) {
270 if (Str
[Index
] >= L
'a' && Str
[Index
] <= L
'z') {
271 Ptr
[Index
] = (CHAR8
)(Str
[Index
] - (L
'a' - L
'A'));
273 Ptr
[Index
] = (CHAR8
)Str
[Index
];
277 return *(UINT32
*)Ptr
;
280 /** This function iterates the configuration table entries in the
281 system table, retrieves the RSDP pointer and starts parsing
284 @param [in] SystemTable Pointer to the EFI system table.
286 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.
287 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.
288 Returns EFI_SUCCESS if successful.
294 IN EFI_SYSTEM_TABLE
* SystemTable
299 EFI_CONFIGURATION_TABLE
* EfiConfigurationTable
;
300 BOOLEAN FoundAcpiTable
;
301 UINTN OriginalAttribute
;
302 UINTN PrintAttribute
;
303 EREPORT_OPTION ReportOption
;
307 PARSE_ACPI_TABLE_PROC RsdpParserProc
;
310 // Search the table for an entry that matches the ACPI Table Guid
311 FoundAcpiTable
= FALSE
;
312 for (Index
= 0; Index
< SystemTable
->NumberOfTableEntries
; Index
++) {
313 if (CompareGuid (&gEfiAcpiTableGuid
,
314 &(SystemTable
->ConfigurationTable
[Index
].VendorGuid
))) {
315 EfiConfigurationTable
= &SystemTable
->ConfigurationTable
[Index
];
316 FoundAcpiTable
= TRUE
;
321 if (FoundAcpiTable
) {
322 RsdpPtr
= (UINT8
*)EfiConfigurationTable
->VendorTable
;
324 // The RSDP revision is 1 byte starting at offset 15
325 RsdpRevision
= *(RsdpPtr
+ RSDP_REVISION_OFFSET
);
327 if (RsdpRevision
< 2) {
329 L
"ERROR: RSDP version less than 2 is not supported.\n"
331 return EFI_UNSUPPORTED
;
334 // The RSDP length is 4 bytes starting at offset 20
335 RsdpLength
= *(UINT32
*)(RsdpPtr
+ RSDP_LENGTH_OFFSET
);
337 Trace
= ProcessTableReportOptions (RSDP_TABLE_INFO
, RsdpPtr
, RsdpLength
);
339 Status
= GetParser (RSDP_TABLE_INFO
, &RsdpParserProc
);
340 if (EFI_ERROR (Status
)) {
342 L
"ERROR: No registered parser found for RSDP.\n"
355 IncrementErrorCount ();
357 L
"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"
359 return EFI_NOT_FOUND
;
362 ReportOption
= GetReportOption ();
363 if (EREPORT_TABLE_LIST
!= ReportOption
) {
364 if (((EREPORT_SELECTED
== ReportOption
) ||
365 (EREPORT_DUMP_BIN_FILE
== ReportOption
)) &&
366 (!mSelectedAcpiTableFound
)) {
367 Print (L
"\nRequested ACPI Table not found.\n");
368 } else if (EREPORT_DUMP_BIN_FILE
!= ReportOption
) {
369 OriginalAttribute
= gST
->ConOut
->Mode
->Attribute
;
371 Print (L
"\nTable Statistics:\n");
373 if (GetColourHighlighting ()) {
374 PrintAttribute
= (GetErrorCount () > 0) ?
377 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
380 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
382 Print (L
"\t%d Error(s)\n", GetErrorCount ());
384 if (GetColourHighlighting ()) {
385 PrintAttribute
= (GetWarningCount () > 0) ?
388 ((OriginalAttribute
&(BIT4
|BIT5
|BIT6
))>>4)
392 gST
->ConOut
->SetAttribute (gST
->ConOut
, PrintAttribute
);
394 Print (L
"\t%d Warning(s)\n", GetWarningCount ());
396 if (GetColourHighlighting ()) {
397 gST
->ConOut
->SetAttribute (gST
->ConOut
, OriginalAttribute
);
405 Function for 'acpiview' command.
407 @param[in] ImageHandle Handle to the Image (NULL if Internal).
408 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
412 ShellCommandRunAcpiView (
413 IN EFI_HANDLE ImageHandle
,
414 IN EFI_SYSTEM_TABLE
* SystemTable
418 SHELL_STATUS ShellStatus
= SHELL_SUCCESS
;
419 LIST_ENTRY
* Package
= NULL
;
420 CHAR16
* ProblemParam
;
422 CHAR8 ColourOption
[8];
423 SHELL_FILE_HANDLE TmpDumpFileHandle
= NULL
;
426 mReportType
= EREPORT_ALL
;
429 mSelectedAcpiTable
= 0;
430 mSelectedAcpiTableName
= NULL
;
431 mSelectedAcpiTableFound
= FALSE
;
433 mConsistencyCheck
= TRUE
;
435 // Reset The error/warning counters
437 ResetWarningCount ();
439 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
440 if (EFI_ERROR (Status
)) {
441 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
446 STRING_TOKEN (STR_GEN_PROBLEM
),
447 gShellAcpiViewHiiHandle
,
451 FreePool (ProblemParam
);
453 Print (L
"acpiview: Error processing input parameter(s)\n");
455 ShellStatus
= SHELL_INVALID_PARAMETER
;
457 if (ShellCommandLineGetCount (Package
) > 1) {
462 STRING_TOKEN (STR_GEN_TOO_MANY
),
463 gShellAcpiViewHiiHandle
,
466 ShellStatus
= SHELL_INVALID_PARAMETER
;
467 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
472 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
473 gShellAcpiViewHiiHandle
,
476 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
477 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
482 STRING_TOKEN (STR_GEN_NO_VALUE
),
483 gShellAcpiViewHiiHandle
,
487 ShellStatus
= SHELL_INVALID_PARAMETER
;
488 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
489 ShellCommandLineGetFlag (Package
, L
"-l"))) {
494 STRING_TOKEN (STR_GEN_TOO_MANY
),
495 gShellAcpiViewHiiHandle
,
498 ShellStatus
= SHELL_INVALID_PARAMETER
;
499 } else if (ShellCommandLineGetFlag (Package
, L
"-h") &&
500 ShellCommandLineGetValue (Package
, L
"-h") == NULL
) {
505 STRING_TOKEN (STR_GEN_NO_VALUE
),
506 gShellAcpiViewHiiHandle
,
510 ShellStatus
= SHELL_INVALID_PARAMETER
;
511 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
512 !ShellCommandLineGetFlag (Package
, L
"-s")) {
517 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
518 gShellAcpiViewHiiHandle
,
523 ShellStatus
= SHELL_INVALID_PARAMETER
;
525 // Check if the colour option is set
526 Temp
= ShellCommandLineGetValue (Package
, L
"-h");
528 UnicodeStrToAsciiStrS (Temp
, ColourOption
, sizeof (ColourOption
));
529 if ((AsciiStriCmp (ColourOption
, "ON") == 0) ||
530 (AsciiStriCmp (ColourOption
, "TRUE") == 0)) {
531 SetColourHighlighting (TRUE
);
532 } else if ((AsciiStriCmp (ColourOption
, "OFF") == 0) ||
533 (AsciiStriCmp (ColourOption
, "FALSE") == 0)) {
534 SetColourHighlighting (FALSE
);
538 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
539 mReportType
= EREPORT_TABLE_LIST
;
541 mSelectedAcpiTableName
= ShellCommandLineGetValue (Package
, L
"-s");
542 if (mSelectedAcpiTableName
!= NULL
) {
543 mSelectedAcpiTable
= (UINT32
)ConvertStrToAcpiSignature (
544 mSelectedAcpiTableName
546 mReportType
= EREPORT_SELECTED
;
548 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
549 // Create a temporary file to check if the media is writable.
550 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
551 mReportType
= EREPORT_DUMP_BIN_FILE
;
555 sizeof (FileNameBuffer
),
557 mSelectedAcpiTableName
,
561 Status
= ShellOpenFileByName (
564 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
565 EFI_FILE_MODE_CREATE
,
569 if (EFI_ERROR (Status
)) {
570 ShellStatus
= SHELL_INVALID_PARAMETER
;
571 TmpDumpFileHandle
= NULL
;
576 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
577 gShellAcpiViewHiiHandle
,
582 // Delete Temporary file.
583 ShellDeleteFile (&TmpDumpFileHandle
);
588 // Parse ACPI Table information
589 Status
= AcpiView (SystemTable
);
590 if (EFI_ERROR (Status
)) {
591 ShellStatus
= SHELL_NOT_FOUND
;
597 if (Package
!= NULL
) {
598 ShellCommandLineFreeVarList (Package
);