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>
10 #include <IndustryStandard/ArmErrorSourceTable.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/HiiLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/PrintLib.h>
16 #include <Library/ShellCommandLib.h>
17 #include <Library/ShellLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/UefiLib.h>
20 #include <Library/AcpiViewCommandLib.h>
23 #include "AcpiParser.h"
24 #include "AcpiTableParser.h"
26 #include "AcpiViewConfig.h"
28 CONST CHAR16 gShellAcpiViewFileName
[] = L
"ShellCommand";
29 EFI_HII_HANDLE gShellAcpiViewHiiHandle
= NULL
;
32 An array of acpiview command line parameters.
34 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
45 A list of available table parsers.
49 ACPI_TABLE_PARSER ParserList
[] = {
50 {EFI_ACPI_6_3_ARM_ERROR_SOURCE_TABLE_SIGNATURE
, ParseAcpiAest
},
51 {EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE_SIGNATURE
, ParseAcpiBgrt
},
52 {EFI_ACPI_6_2_DEBUG_PORT_2_TABLE_SIGNATURE
, ParseAcpiDbg2
},
53 {EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
,
55 {EFI_ACPI_6_3_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
, ParseAcpiFacs
},
56 {EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiFadt
},
57 {EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiGtdt
},
58 {EFI_ACPI_6_3_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_SIGNATURE
, ParseAcpiHmat
},
59 {EFI_ACPI_6_2_IO_REMAPPING_TABLE_SIGNATURE
, ParseAcpiIort
},
60 {EFI_ACPI_6_2_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiMadt
},
61 {EFI_ACPI_6_2_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE
,
63 {EFI_ACPI_6_2_PLATFORM_COMMUNICATIONS_CHANNEL_TABLE_SIGNATURE
,
65 {EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_STRUCTURE_SIGNATURE
,
67 {RSDP_TABLE_INFO
, ParseAcpiRsdp
},
68 {EFI_ACPI_6_2_SYSTEM_LOCALITY_INFORMATION_TABLE_SIGNATURE
, ParseAcpiSlit
},
69 {EFI_ACPI_6_2_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE
, ParseAcpiSpcr
},
70 {EFI_ACPI_6_2_SYSTEM_RESOURCE_AFFINITY_TABLE_SIGNATURE
, ParseAcpiSrat
},
71 {EFI_ACPI_6_2_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiSsdt
},
72 {EFI_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
, ParseAcpiXsdt
}
76 This function registers all the available table parsers.
78 @retval EFI_SUCCESS The parser is registered.
79 @retval EFI_ALREADY_STARTED The parser for the ACPI Table
80 was already registered.
81 @retval EFI_INVALID_PARAMETER A parameter is invalid.
82 @retval EFI_OUT_OF_RESOURCES No space to register the
93 Count
= sizeof (ParserList
) / sizeof (ParserList
[0]);
95 while (Count
-- != 0) {
96 Status
= RegisterParser (
97 ParserList
[Count
].Signature
,
98 ParserList
[Count
].Parser
100 if (EFI_ERROR (Status
)) {
108 Dump a buffer to a file. Print error message if a file cannot be created.
110 @param[in] FileName The filename that shall be created to contain the buffer.
111 @param[in] Buffer Pointer to buffer that shall be dumped.
112 @param[in] BufferSize The size of buffer to be dumped in bytes.
114 @return The number of bytes that were written
118 ShellDumpBufferToFile (
119 IN CONST CHAR16
* FileNameBuffer
,
120 IN CONST VOID
* Buffer
,
121 IN CONST UINTN BufferSize
125 SHELL_FILE_HANDLE DumpFileHandle
;
128 Status
= ShellOpenFileByName (
131 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
135 if (EFI_ERROR (Status
)) {
140 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
141 gShellAcpiViewHiiHandle
,
147 TransferBytes
= BufferSize
;
148 Status
= ShellWriteFile (
154 if (EFI_ERROR (Status
)) {
155 Print (L
"ERROR: Failed to write binary file.\n");
161 ShellCloseFile (&DumpFileHandle
);
162 return TransferBytes
;
166 Return the file name of the help text file if not using HII.
168 @return The string pointer to the file name.
172 ShellCommandGetManFileNameAcpiView (
176 return gShellAcpiViewFileName
;
180 Function for 'acpiview' command.
182 @param[in] ImageHandle Handle to the Image (NULL if internal).
183 @param[in] SystemTable Pointer to the System Table (NULL if internal).
185 @retval SHELL_INVALID_PARAMETER The command line invocation could not be parsed
186 @retval SHELL_NOT_FOUND The command failed
187 @retval SHELL_SUCCESS The command was successful
191 ShellCommandRunAcpiView (
192 IN EFI_HANDLE ImageHandle
,
193 IN EFI_SYSTEM_TABLE
* SystemTable
197 SHELL_STATUS ShellStatus
;
199 CHAR16
* ProblemParam
;
200 SHELL_FILE_HANDLE TmpDumpFileHandle
;
201 CONST CHAR16
* MandatoryTableSpecStr
;
202 CONST CHAR16
* SelectedTableName
;
204 // Set configuration defaults
205 AcpiConfigSetDefaults ();
207 ShellStatus
= SHELL_SUCCESS
;
209 TmpDumpFileHandle
= NULL
;
211 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
212 if (EFI_ERROR (Status
)) {
213 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
218 STRING_TOKEN (STR_GEN_PROBLEM
),
219 gShellAcpiViewHiiHandle
,
223 FreePool (ProblemParam
);
225 Print (L
"acpiview: Error processing input parameter(s)\n");
227 ShellStatus
= SHELL_INVALID_PARAMETER
;
229 if (ShellCommandLineGetCount (Package
) > 1) {
234 STRING_TOKEN (STR_GEN_TOO_MANY
),
235 gShellAcpiViewHiiHandle
,
238 ShellStatus
= SHELL_INVALID_PARAMETER
;
239 } else if (ShellCommandLineGetFlag (Package
, L
"-?")) {
244 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
),
245 gShellAcpiViewHiiHandle
,
248 } else if (ShellCommandLineGetFlag (Package
, L
"-s") &&
249 ShellCommandLineGetValue (Package
, L
"-s") == NULL
) {
254 STRING_TOKEN (STR_GEN_NO_VALUE
),
255 gShellAcpiViewHiiHandle
,
259 ShellStatus
= SHELL_INVALID_PARAMETER
;
260 } else if (ShellCommandLineGetFlag (Package
, L
"-r") &&
261 ShellCommandLineGetValue (Package
, L
"-r") == NULL
) {
266 STRING_TOKEN (STR_GEN_NO_VALUE
),
267 gShellAcpiViewHiiHandle
,
271 ShellStatus
= SHELL_INVALID_PARAMETER
;
272 } else if ((ShellCommandLineGetFlag (Package
, L
"-s") &&
273 ShellCommandLineGetFlag (Package
, L
"-l"))) {
278 STRING_TOKEN (STR_GEN_TOO_MANY
),
279 gShellAcpiViewHiiHandle
,
282 ShellStatus
= SHELL_INVALID_PARAMETER
;
283 } else if (ShellCommandLineGetFlag (Package
, L
"-d") &&
284 !ShellCommandLineGetFlag (Package
, L
"-s")) {
289 STRING_TOKEN (STR_GEN_MISSING_OPTION
),
290 gShellAcpiViewHiiHandle
,
295 ShellStatus
= SHELL_INVALID_PARAMETER
;
297 // Turn on colour highlighting if requested
298 SetColourHighlighting (ShellCommandLineGetFlag (Package
, L
"-h"));
300 // Surpress consistency checking if requested
301 SetConsistencyChecking (!ShellCommandLineGetFlag (Package
, L
"-q"));
303 // Evaluate the parameters for mandatory ACPI table presence checks
304 SetMandatoryTableValidate (ShellCommandLineGetFlag (Package
, L
"-r"));
305 MandatoryTableSpecStr
= ShellCommandLineGetValue (Package
, L
"-r");
307 if (MandatoryTableSpecStr
!= NULL
) {
308 SetMandatoryTableSpec (ShellHexStrToUintn (MandatoryTableSpecStr
));
311 if (ShellCommandLineGetFlag (Package
, L
"-l")) {
312 SetReportOption (ReportTableList
);
314 SelectedTableName
= ShellCommandLineGetValue (Package
, L
"-s");
315 if (SelectedTableName
!= NULL
) {
316 SelectAcpiTable (SelectedTableName
);
317 SetReportOption (ReportSelected
);
319 if (ShellCommandLineGetFlag (Package
, L
"-d")) {
320 // Create a temporary file to check if the media is writable.
321 CHAR16 FileNameBuffer
[MAX_FILE_NAME_LEN
];
322 SetReportOption (ReportDumpBinFile
);
326 sizeof (FileNameBuffer
),
331 Status
= ShellOpenFileByName (
334 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
|
335 EFI_FILE_MODE_CREATE
,
339 if (EFI_ERROR (Status
)) {
340 ShellStatus
= SHELL_INVALID_PARAMETER
;
341 TmpDumpFileHandle
= NULL
;
346 STRING_TOKEN (STR_GEN_READONLY_MEDIA
),
347 gShellAcpiViewHiiHandle
,
352 // Delete Temporary file.
353 ShellDeleteFile (&TmpDumpFileHandle
);
358 // Parse ACPI Table information
359 Status
= AcpiView (SystemTable
);
360 if (EFI_ERROR (Status
)) {
361 ShellStatus
= SHELL_NOT_FOUND
;
367 if (Package
!= NULL
) {
368 ShellCommandLineFreeVarList (Package
);
374 Constructor for the Shell AcpiView Command library.
376 Install the handlers for acpiview UEFI Shell command.
378 @param ImageHandle The image handle of the process.
379 @param SystemTable The EFI System Table pointer.
381 @retval EFI_SUCCESS The Shell command handlers were installed
383 @retval EFI_DEVICE_ERROR Hii package failed to install.
387 UefiShellAcpiViewCommandLibConstructor (
388 IN EFI_HANDLE ImageHandle
,
389 IN EFI_SYSTEM_TABLE
*SystemTable
393 gShellAcpiViewHiiHandle
= NULL
;
395 // Check Shell Profile Debug1 bit of the profiles mask
396 if ((PcdGet8 (PcdShellProfileMask
) & BIT1
) == 0) {
400 Status
= RegisterAllParsers ();
401 if (EFI_ERROR (Status
)) {
402 Print (L
"acpiview: Error failed to register parser.\n");
406 gShellAcpiViewHiiHandle
= HiiAddPackages (
407 &gShellAcpiViewHiiGuid
,
409 UefiShellAcpiViewCommandLibStrings
,
412 if (gShellAcpiViewHiiHandle
== NULL
) {
413 return EFI_DEVICE_ERROR
;
415 // Install our Shell command handler
416 ShellCommandRegisterCommandName (
418 ShellCommandRunAcpiView
,
419 ShellCommandGetManFileNameAcpiView
,
423 gShellAcpiViewHiiHandle
,
424 STRING_TOKEN (STR_GET_HELP_ACPIVIEW
)
431 Destructor for the library. free any resources.
433 @param ImageHandle The image handle of the process.
434 @param SystemTable The EFI System Table pointer.
438 UefiShellAcpiViewCommandLibDestructor (
439 IN EFI_HANDLE ImageHandle
,
440 IN EFI_SYSTEM_TABLE
*SystemTable
443 if (gShellAcpiViewHiiHandle
!= NULL
) {
444 HiiRemovePackages (gShellAcpiViewHiiHandle
);