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