]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/UefiShellAcpiViewCommandLib.c
ShellPkg/AcpiView: Move parameter parsing
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / UefiShellAcpiViewCommandLib.c
1 /** @file
2 Main file for 'acpiview' Shell command function.
3
4 Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 **/
7
8 #include <Guid/ShellLibHiiGuid.h>
9 #include <IndustryStandard/Acpi.h>
10
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 <Uefi.h>
20
21 #include "AcpiParser.h"
22 #include "AcpiTableParser.h"
23 #include "AcpiView.h"
24 #include "AcpiViewConfig.h"
25 #include "UefiShellAcpiViewCommandLib.h"
26
27 CONST CHAR16 gShellAcpiViewFileName[] = L"ShellCommand";
28
29 /**
30 An array of acpiview command line parameters.
31 **/
32 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
33 {L"-q", TypeFlag},
34 {L"-d", TypeFlag},
35 {L"-h", TypeFlag},
36 {L"-l", TypeFlag},
37 {L"-s", TypeValue},
38 {L"-r", TypeValue},
39 {NULL, TypeMax}
40 };
41
42 /**
43 A list of available table parsers.
44 */
45 STATIC
46 CONST
47 ACPI_TABLE_PARSER ParserList[] = {
48 {EFI_ACPI_6_2_BOOT_GRAPHICS_RESOURCE_TABLE_SIGNATURE, ParseAcpiBgrt},
49 {EFI_ACPI_6_2_DEBUG_PORT_2_TABLE_SIGNATURE, ParseAcpiDbg2},
50 {EFI_ACPI_6_2_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE,
51 ParseAcpiDsdt},
52 {EFI_ACPI_6_3_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE, ParseAcpiFacs},
53 {EFI_ACPI_6_2_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE, ParseAcpiFadt},
54 {EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE_SIGNATURE, ParseAcpiGtdt},
55 {EFI_ACPI_6_2_IO_REMAPPING_TABLE_SIGNATURE, ParseAcpiIort},
56 {EFI_ACPI_6_2_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE, ParseAcpiMadt},
57 {EFI_ACPI_6_2_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE,
58 ParseAcpiMcfg},
59 {EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_STRUCTURE_SIGNATURE,
60 ParseAcpiPptt},
61 {RSDP_TABLE_INFO, ParseAcpiRsdp},
62 {EFI_ACPI_6_2_SYSTEM_LOCALITY_INFORMATION_TABLE_SIGNATURE, ParseAcpiSlit},
63 {EFI_ACPI_6_2_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE, ParseAcpiSpcr},
64 {EFI_ACPI_6_2_SYSTEM_RESOURCE_AFFINITY_TABLE_SIGNATURE, ParseAcpiSrat},
65 {EFI_ACPI_6_2_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE, ParseAcpiSsdt},
66 {EFI_ACPI_6_2_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE, ParseAcpiXsdt}
67 };
68
69 /**
70 This function registers all the available table parsers.
71
72 @retval EFI_SUCCESS The parser is registered.
73 @retval EFI_ALREADY_STARTED The parser for the ACPI Table
74 was already registered.
75 @retval EFI_INVALID_PARAMETER A parameter is invalid.
76 @retval EFI_OUT_OF_RESOURCES No space to register the
77 parser.
78 **/
79 EFI_STATUS
80 RegisterAllParsers (
81 )
82 {
83 EFI_STATUS Status;
84 UINTN Count;
85
86 Status = EFI_SUCCESS;
87 Count = sizeof (ParserList) / sizeof (ParserList[0]);
88
89 while (Count-- != 0) {
90 Status = RegisterParser (
91 ParserList[Count].Signature,
92 ParserList[Count].Parser
93 );
94 if (EFI_ERROR (Status)) {
95 return Status;
96 }
97 }
98 return Status;
99 }
100
101 /**
102 Return the file name of the help text file if not using HII.
103
104 @return The string pointer to the file name.
105 **/
106 CONST CHAR16*
107 EFIAPI
108 ShellCommandGetManFileNameAcpiView (
109 VOID
110 )
111 {
112 return gShellAcpiViewFileName;
113 }
114
115 /**
116 Function for 'acpiview' command.
117
118 @param[in] ImageHandle Handle to the Image (NULL if internal).
119 @param[in] SystemTable Pointer to the System Table (NULL if internal).
120
121 @retval SHELL_INVALID_PARAMETER The command line invocation could not be parsed
122 @retval SHELL_NOT_FOUND The command failed
123 @retval SHELL_SUCCESS The command was successful
124 **/
125 SHELL_STATUS
126 EFIAPI
127 ShellCommandRunAcpiView (
128 IN EFI_HANDLE ImageHandle,
129 IN EFI_SYSTEM_TABLE* SystemTable
130 )
131 {
132 EFI_STATUS Status;
133 SHELL_STATUS ShellStatus;
134 LIST_ENTRY* Package;
135 CHAR16* ProblemParam;
136 SHELL_FILE_HANDLE TmpDumpFileHandle;
137 CONST CHAR16* MandatoryTableSpecStr;
138 CONST CHAR16* SelectedTableName;
139
140 // Set configuration defaults
141 AcpiConfigSetDefaults ();
142
143 ShellStatus = SHELL_SUCCESS;
144 Package = NULL;
145 TmpDumpFileHandle = NULL;
146
147 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
148 if (EFI_ERROR (Status)) {
149 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
150 ShellPrintHiiEx (
151 -1,
152 -1,
153 NULL,
154 STRING_TOKEN (STR_GEN_PROBLEM),
155 gShellAcpiViewHiiHandle,
156 L"acpiview",
157 ProblemParam
158 );
159 FreePool (ProblemParam);
160 } else {
161 Print (L"acpiview: Error processing input parameter(s)\n");
162 }
163 ShellStatus = SHELL_INVALID_PARAMETER;
164 } else {
165 if (ShellCommandLineGetCount (Package) > 1) {
166 ShellPrintHiiEx (
167 -1,
168 -1,
169 NULL,
170 STRING_TOKEN (STR_GEN_TOO_MANY),
171 gShellAcpiViewHiiHandle,
172 L"acpiview"
173 );
174 ShellStatus = SHELL_INVALID_PARAMETER;
175 } else if (ShellCommandLineGetFlag (Package, L"-?")) {
176 ShellPrintHiiEx (
177 -1,
178 -1,
179 NULL,
180 STRING_TOKEN (STR_GET_HELP_ACPIVIEW),
181 gShellAcpiViewHiiHandle,
182 L"acpiview"
183 );
184 } else if (ShellCommandLineGetFlag (Package, L"-s") &&
185 ShellCommandLineGetValue (Package, L"-s") == NULL) {
186 ShellPrintHiiEx (
187 -1,
188 -1,
189 NULL,
190 STRING_TOKEN (STR_GEN_NO_VALUE),
191 gShellAcpiViewHiiHandle,
192 L"acpiview",
193 L"-s"
194 );
195 ShellStatus = SHELL_INVALID_PARAMETER;
196 } else if (ShellCommandLineGetFlag (Package, L"-r") &&
197 ShellCommandLineGetValue (Package, L"-r") == NULL) {
198 ShellPrintHiiEx (
199 -1,
200 -1,
201 NULL,
202 STRING_TOKEN (STR_GEN_NO_VALUE),
203 gShellAcpiViewHiiHandle,
204 L"acpiview",
205 L"-r"
206 );
207 ShellStatus = SHELL_INVALID_PARAMETER;
208 } else if ((ShellCommandLineGetFlag (Package, L"-s") &&
209 ShellCommandLineGetFlag (Package, L"-l"))) {
210 ShellPrintHiiEx (
211 -1,
212 -1,
213 NULL,
214 STRING_TOKEN (STR_GEN_TOO_MANY),
215 gShellAcpiViewHiiHandle,
216 L"acpiview"
217 );
218 ShellStatus = SHELL_INVALID_PARAMETER;
219 } else if (ShellCommandLineGetFlag (Package, L"-d") &&
220 !ShellCommandLineGetFlag (Package, L"-s")) {
221 ShellPrintHiiEx (
222 -1,
223 -1,
224 NULL,
225 STRING_TOKEN (STR_GEN_MISSING_OPTION),
226 gShellAcpiViewHiiHandle,
227 L"acpiview",
228 L"-s",
229 L"-d"
230 );
231 ShellStatus = SHELL_INVALID_PARAMETER;
232 } else {
233 // Turn on colour highlighting if requested
234 SetColourHighlighting (ShellCommandLineGetFlag (Package, L"-h"));
235
236 // Surpress consistency checking if requested
237 SetConsistencyChecking (!ShellCommandLineGetFlag (Package, L"-q"));
238
239 // Evaluate the parameters for mandatory ACPI table presence checks
240 SetMandatoryTableValidate (ShellCommandLineGetFlag (Package, L"-r"));
241 MandatoryTableSpecStr = ShellCommandLineGetValue (Package, L"-r");
242
243 if (MandatoryTableSpecStr != NULL) {
244 SetMandatoryTableSpec (ShellHexStrToUintn (MandatoryTableSpecStr));
245 }
246
247 if (ShellCommandLineGetFlag (Package, L"-l")) {
248 SetReportOption (ReportTableList);
249 } else {
250 SelectedTableName = ShellCommandLineGetValue (Package, L"-s");
251 if (SelectedTableName != NULL) {
252 SelectAcpiTable (SelectedTableName);
253 SetReportOption (ReportSelected);
254
255 if (ShellCommandLineGetFlag (Package, L"-d")) {
256 // Create a temporary file to check if the media is writable.
257 CHAR16 FileNameBuffer[MAX_FILE_NAME_LEN];
258 SetReportOption (ReportDumpBinFile);
259
260 UnicodeSPrint (
261 FileNameBuffer,
262 sizeof (FileNameBuffer),
263 L".\\%s0000.tmp",
264 SelectedTableName
265 );
266
267 Status = ShellOpenFileByName (
268 FileNameBuffer,
269 &TmpDumpFileHandle,
270 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
271 EFI_FILE_MODE_CREATE,
272 0
273 );
274
275 if (EFI_ERROR (Status)) {
276 ShellStatus = SHELL_INVALID_PARAMETER;
277 TmpDumpFileHandle = NULL;
278 ShellPrintHiiEx (
279 -1,
280 -1,
281 NULL,
282 STRING_TOKEN (STR_GEN_READONLY_MEDIA),
283 gShellAcpiViewHiiHandle,
284 L"acpiview"
285 );
286 goto Done;
287 }
288 // Delete Temporary file.
289 ShellDeleteFile (&TmpDumpFileHandle);
290 } // -d
291 } // -s
292 }
293
294 // Parse ACPI Table information
295 Status = AcpiView (SystemTable);
296 if (EFI_ERROR (Status)) {
297 ShellStatus = SHELL_NOT_FOUND;
298 }
299 }
300 }
301
302 Done:
303 if (Package != NULL) {
304 ShellCommandLineFreeVarList (Package);
305 }
306 return ShellStatus;
307 }
308
309 /**
310 Constructor for the Shell AcpiView Command library.
311
312 Install the handlers for acpiview UEFI Shell command.
313
314 @param ImageHandle The image handle of the process.
315 @param SystemTable The EFI System Table pointer.
316
317 @retval EFI_SUCCESS The Shell command handlers were installed
318 successfully.
319 @retval EFI_DEVICE_ERROR Hii package failed to install.
320 **/
321 EFI_STATUS
322 EFIAPI
323 UefiShellAcpiViewCommandLibConstructor (
324 IN EFI_HANDLE ImageHandle,
325 IN EFI_SYSTEM_TABLE *SystemTable
326 )
327 {
328 EFI_STATUS Status;
329 gShellAcpiViewHiiHandle = NULL;
330
331 // Check Shell Profile Debug1 bit of the profiles mask
332 if ((PcdGet8 (PcdShellProfileMask) & BIT1) == 0) {
333 return EFI_SUCCESS;
334 }
335
336 Status = RegisterAllParsers ();
337 if (EFI_ERROR (Status)) {
338 Print (L"acpiview: Error failed to register parser.\n");
339 return Status;
340 }
341
342 gShellAcpiViewHiiHandle = HiiAddPackages (
343 &gShellAcpiViewHiiGuid,
344 gImageHandle,
345 UefiShellAcpiViewCommandLibStrings,
346 NULL
347 );
348 if (gShellAcpiViewHiiHandle == NULL) {
349 return EFI_DEVICE_ERROR;
350 }
351 // Install our Shell command handler
352 ShellCommandRegisterCommandName (
353 L"acpiview",
354 ShellCommandRunAcpiView,
355 ShellCommandGetManFileNameAcpiView,
356 0,
357 L"acpiview",
358 TRUE,
359 gShellAcpiViewHiiHandle,
360 STRING_TOKEN (STR_GET_HELP_ACPIVIEW)
361 );
362
363 return EFI_SUCCESS;
364 }
365
366 /**
367 Destructor for the library. free any resources.
368
369 @param ImageHandle The image handle of the process.
370 @param SystemTable The EFI System Table pointer.
371 **/
372 EFI_STATUS
373 EFIAPI
374 UefiShellAcpiViewCommandLibDestructor (
375 IN EFI_HANDLE ImageHandle,
376 IN EFI_SYSTEM_TABLE *SystemTable
377 )
378 {
379 if (gShellAcpiViewHiiHandle != NULL) {
380 HiiRemovePackages (gShellAcpiViewHiiHandle);
381 }
382 return EFI_SUCCESS;
383 }