2 Main file for 'acpiview' Shell command function.
4 Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include <Guid/ShellLibHiiGuid.h>
9 #include <IndustryStandard/Acpi.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/HiiLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/PrintLib.h>
15 #include <Library/ShellCommandLib.h>
16 #include <Library/ShellLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/AcpiViewCommandLib.h>
22 #include "AcpiParser.h"
23 #include "AcpiTableParser.h"
25 #include "AcpiViewConfig.h"
27 CONST CHAR16 gShellAcpiViewFileName
[] = L
"ShellCommand";
28 EFI_HII_HANDLE gShellAcpiViewHiiHandle
= NULL
;
31 An array of acpiview command line parameters.
33 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
44 A list of available table parsers.
48 ACPI_TABLE_PARSER ParserList
[] = {
49 {EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE_SIGNATURE
, ParseAcpiBgrt
},
50 {EFI_ACPI_6_2_DEBUG_PORT_2_TABLE_SIGNATURE
, ParseAcpiDbg2
},
51 {EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
,
53 {EFI_ACPI_6_3_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
, ParseAcpiFacs
},
54 {EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiFadt
},
55 {EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiGtdt
},
56 {EFI_ACPI_6_2_IO_REMAPPING_TABLE_SIGNATURE
, ParseAcpiIort
},
57 {EFI_ACPI_6_2_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiMadt
},
58 {EFI_ACPI_6_2_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE
,
60 {EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_STRUCTURE_SIGNATURE
,
62 {RSDP_TABLE_INFO
, ParseAcpiRsdp
},
63 {EFI_ACPI_6_2_SYSTEM_LOCALITY_INFORMATION_TABLE_SIGNATURE
, ParseAcpiSlit
},
64 {EFI_ACPI_6_2_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE
, ParseAcpiSpcr
},
65 {EFI_ACPI_6_2_SYSTEM_RESOURCE_AFFINITY_TABLE_SIGNATURE
, ParseAcpiSrat
},
66 {EFI_ACPI_6_2_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiSsdt
},
67 {EFI_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiXsdt
}
71 This function registers all the available table parsers.
73 @retval EFI_SUCCESS The parser is registered.
74 @retval EFI_ALREADY_STARTED The parser for the ACPI Table
75 was already registered.
76 @retval EFI_INVALID_PARAMETER A parameter is invalid.
77 @retval EFI_OUT_OF_RESOURCES No space to register the
88 Count
= sizeof (ParserList
) / sizeof (ParserList
[0]);
90 while (Count
-- != 0) {
91 Status
= RegisterParser (
92 ParserList
[Count
].Signature
,
93 ParserList
[Count
].Parser
95 if (EFI_ERROR (Status
)) {
103 Dump a buffer to a file. Print error message if a file cannot be created.
105 @param[in] FileName The filename that shall be created to contain the buffer.
106 @param[in] Buffer Pointer to buffer that shall be dumped.
107 @param[in] BufferSize The size of buffer to be dumped in bytes.
109 @return The number of bytes that were written
113 ShellDumpBufferToFile (
114 IN CONST CHAR16
* FileNameBuffer
,
115 IN CONST VOID
* Buffer
,
116 IN CONST UINTN BufferSize
120 SHELL_FILE_HANDLE DumpFileHandle
;
123 Status
= ShellOpenFileByName (
126 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
130 if (EFI_ERROR (Status
)) {
135 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
136 gShellAcpiViewHiiHandle
,
142 TransferBytes
= BufferSize
;
143 Status
= ShellWriteFile (
149 if (EFI_ERROR (Status
)) {
150 Print (L
"ERROR: Failed to write binary file.\n");
156 ShellCloseFile (&DumpFileHandle
);
157 return TransferBytes
;
161 Return the file name of the help text file if not using HII.
163 @return The string pointer to the file name.
167 ShellCommandGetManFileNameAcpiView (
171 return gShellAcpiViewFileName
;
175 Function for 'acpiview' command.
177 @param[in] ImageHandle Handle to the Image (NULL if internal).
178 @param[in] SystemTable Pointer to the System Table (NULL if internal).
180 @retval SHELL_INVALID_PARAMETER The command line invocation could not be parsed
181 @retval SHELL_NOT_FOUND The command failed
182 @retval SHELL_SUCCESS The command was successful
186 ShellCommandRunAcpiView (
187 IN EFI_HANDLE ImageHandle
,
188 IN EFI_SYSTEM_TABLE
* SystemTable
192 SHELL_STATUS ShellStatus
;
194 CHAR16
* ProblemParam
;
195 SHELL_FILE_HANDLE TmpDumpFileHandle
;
196 CONST CHAR16
* MandatoryTableSpecStr
;
197 CONST CHAR16
* SelectedTableName
;
199 // Set configuration defaults
200 AcpiConfigSetDefaults ();
202 ShellStatus
= SHELL_SUCCESS
;
204 TmpDumpFileHandle
= NULL
;
206 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
207 if (EFI_ERROR (Status
)) {
208 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
213 STRING_TOKEN (STR_GEN_PROBLEM
),
214 gShellAcpiViewHiiHandle
,
218 FreePool (ProblemParam
);
220 Print (L
"acpiview: Error processing input parameter(s)\n");
222 ShellStatus
= SHELL_INVALID_PARAMETER
;
224 if (ShellCommandLineGetCount (Package
) > 1) {
229 STRING_TOKEN (STR_GEN_TOO_MANY
),
230 gShellAcpiViewHiiHandle
,
233 ShellStatus
= SHELL_INVALID_PARAMETER
;
234 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
239 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
240 gShellAcpiViewHiiHandle
,
243 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
244 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
249 STRING_TOKEN (STR_GEN_NO_VALUE
),
250 gShellAcpiViewHiiHandle
,
254 ShellStatus
= SHELL_INVALID_PARAMETER
;
255 } else if (ShellCommandLineGetFlag (Package
, L
"-r") &&
256 ShellCommandLineGetValue (Package
, L
"-r") == NULL
) {
261 STRING_TOKEN (STR_GEN_NO_VALUE
),
262 gShellAcpiViewHiiHandle
,
266 ShellStatus
= SHELL_INVALID_PARAMETER
;
267 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
268 ShellCommandLineGetFlag (Package
, L
"-l"))) {
273 STRING_TOKEN (STR_GEN_TOO_MANY
),
274 gShellAcpiViewHiiHandle
,
277 ShellStatus
= SHELL_INVALID_PARAMETER
;
278 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
279 !ShellCommandLineGetFlag (Package
, L
"-s")) {
284 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
285 gShellAcpiViewHiiHandle
,
290 ShellStatus
= SHELL_INVALID_PARAMETER
;
292 // Turn on colour highlighting if requested
293 SetColourHighlighting (ShellCommandLineGetFlag (Package
, L
"-h"));
295 // Surpress consistency checking if requested
296 SetConsistencyChecking (!ShellCommandLineGetFlag (Package
, L
"-q"));
298 // Evaluate the parameters for mandatory ACPI table presence checks
299 SetMandatoryTableValidate (ShellCommandLineGetFlag (Package
, L
"-r"));
300 MandatoryTableSpecStr
= ShellCommandLineGetValue (Package
, L
"-r");
302 if (MandatoryTableSpecStr
!= NULL
) {
303 SetMandatoryTableSpec (ShellHexStrToUintn (MandatoryTableSpecStr
));
306 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
307 SetReportOption (ReportTableList
);
309 SelectedTableName
= ShellCommandLineGetValue (Package
, L
"-s");
310 if (SelectedTableName
!= NULL
) {
311 SelectAcpiTable (SelectedTableName
);
312 SetReportOption (ReportSelected
);
314 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
315 // Create a temporary file to check if the media is writable.
316 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
317 SetReportOption (ReportDumpBinFile
);
321 sizeof (FileNameBuffer
),
326 Status
= ShellOpenFileByName (
329 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
330 EFI_FILE_MODE_CREATE
,
334 if (EFI_ERROR (Status
)) {
335 ShellStatus
= SHELL_INVALID_PARAMETER
;
336 TmpDumpFileHandle
= NULL
;
341 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
342 gShellAcpiViewHiiHandle
,
347 // Delete Temporary file.
348 ShellDeleteFile (&TmpDumpFileHandle
);
353 // Parse ACPI Table information
354 Status
= AcpiView (SystemTable
);
355 if (EFI_ERROR (Status
)) {
356 ShellStatus
= SHELL_NOT_FOUND
;
362 if (Package
!= NULL
) {
363 ShellCommandLineFreeVarList (Package
);
369 Constructor for the Shell AcpiView Command library.
371 Install the handlers for acpiview UEFI Shell command.
373 @param ImageHandle The image handle of the process.
374 @param SystemTable The EFI System Table pointer.
376 @retval EFI_SUCCESS The Shell command handlers were installed
378 @retval EFI_DEVICE_ERROR Hii package failed to install.
382 UefiShellAcpiViewCommandLibConstructor (
383 IN EFI_HANDLE ImageHandle
,
384 IN EFI_SYSTEM_TABLE
*SystemTable
388 gShellAcpiViewHiiHandle
= NULL
;
390 // Check Shell Profile Debug1 bit of the profiles mask
391 if ((PcdGet8 (PcdShellProfileMask
) & BIT1
) == 0) {
395 Status
= RegisterAllParsers ();
396 if (EFI_ERROR (Status
)) {
397 Print (L
"acpiview: Error failed to register parser.\n");
401 gShellAcpiViewHiiHandle
= HiiAddPackages (
402 &gShellAcpiViewHiiGuid
,
404 UefiShellAcpiViewCommandLibStrings
,
407 if (gShellAcpiViewHiiHandle
== NULL
) {
408 return EFI_DEVICE_ERROR
;
410 // Install our Shell command handler
411 ShellCommandRegisterCommandName (
413 ShellCommandRunAcpiView
,
414 ShellCommandGetManFileNameAcpiView
,
418 gShellAcpiViewHiiHandle
,
419 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
)
426 Destructor for the library. free any resources.
428 @param ImageHandle The image handle of the process.
429 @param SystemTable The EFI System Table pointer.
433 UefiShellAcpiViewCommandLibDestructor (
434 IN EFI_HANDLE ImageHandle
,
435 IN EFI_SYSTEM_TABLE
*SystemTable
438 if (gShellAcpiViewHiiHandle
!= NULL
) {
439 HiiRemovePackages (gShellAcpiViewHiiHandle
);