]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiView.c
ShellPkg: Add AcpiViewCommandLib
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / AcpiView.c
CommitLineData
a6eaba4d 1/** @file\r
ee4dc24f 2\r
8af507c1 3 Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.\r
56ba3746 4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
faef5a36
KK
5\r
6 @par Glossary:\r
7 - Sbbr or SBBR - Server Base Boot Requirements\r
8\r
9 @par Reference(s):\r
10 - Arm Server Base Boot Requirements 1.2, September 2019\r
ee4dc24f
RN
11**/\r
12\r
13#include <Library/PrintLib.h>\r
14#include <Library/UefiLib.h>\r
15#include <Library/ShellLib.h>\r
16#include <Library/UefiBootServicesTableLib.h>\r
17#include <Library/BaseMemoryLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
db77d8f7 20#include <Library/AcpiViewCommandLib.h>\r
ee4dc24f
RN
21#include "AcpiParser.h"\r
22#include "AcpiTableParser.h"\r
23#include "AcpiView.h"\r
e18ac66d 24#include "AcpiViewConfig.h"\r
ee4dc24f 25\r
faef5a36
KK
26#if defined(MDE_CPU_ARM) || defined (MDE_CPU_AARCH64)\r
27#include "Arm/SbbrValidator.h"\r
28#endif\r
29\r
ee4dc24f
RN
30STATIC UINT32 mTableCount;\r
31STATIC UINT32 mBinTableCount;\r
ee4dc24f 32\r
a6eaba4d
DB
33/**\r
34 This function dumps the ACPI table to a file.\r
35\r
ee4dc24f
RN
36 @param [in] Ptr Pointer to the ACPI table data.\r
37 @param [in] Length The length of the ACPI table.\r
38\r
39 @retval TRUE Success.\r
40 @retval FALSE Failure.\r
a6eaba4d 41**/\r
ee4dc24f
RN
42STATIC\r
43BOOLEAN\r
44DumpAcpiTableToFile (\r
45 IN CONST UINT8* Ptr,\r
46 IN CONST UINTN Length\r
47 )\r
48{\r
e18ac66d 49 CHAR16 FileNameBuffer[MAX_FILE_NAME_LEN];\r
e18ac66d
TP
50 UINTN TransferBytes;\r
51 SELECTED_ACPI_TABLE *SelectedTable;\r
f75c7478 52\r
e18ac66d 53 GetSelectedAcpiTable (&SelectedTable);\r
ee4dc24f
RN
54\r
55 UnicodeSPrint (\r
56 FileNameBuffer,\r
57 sizeof (FileNameBuffer),\r
58 L".\\%s%04d.bin",\r
e18ac66d 59 SelectedTable->Name,\r
ee4dc24f
RN
60 mBinTableCount++\r
61 );\r
62\r
ee4dc24f
RN
63 Print (L"Dumping ACPI table to : %s ... ", FileNameBuffer);\r
64\r
d45cf5ff 65 TransferBytes = ShellDumpBufferToFile (FileNameBuffer, Ptr, Length);\r
ee4dc24f
RN
66 return (Length == TransferBytes);\r
67}\r
68\r
a6eaba4d
DB
69/**\r
70 This function processes the table reporting options for the ACPI table.\r
ee4dc24f
RN
71\r
72 @param [in] Signature The ACPI table Signature.\r
73 @param [in] TablePtr Pointer to the ACPI table data.\r
74 @param [in] Length The length fo the ACPI table.\r
75\r
76 @retval Returns TRUE if the ACPI table should be traced.\r
a6eaba4d 77**/\r
ee4dc24f
RN
78BOOLEAN\r
79ProcessTableReportOptions (\r
80 IN CONST UINT32 Signature,\r
81 IN CONST UINT8* TablePtr,\r
82 IN CONST UINT32 Length\r
83 )\r
84{\r
e18ac66d
TP
85 UINTN OriginalAttribute;\r
86 UINT8 *SignaturePtr;\r
87 BOOLEAN Log;\r
88 BOOLEAN HighLight;\r
89 SELECTED_ACPI_TABLE *SelectedTable;\r
f75c7478 90\r
0154e02d
SZ
91 //\r
92 // set local variables to suppress incorrect compiler/analyzer warnings\r
93 //\r
94 OriginalAttribute = 0;\r
f75c7478
DB
95 SignaturePtr = (UINT8*)(UINTN)&Signature;\r
96 Log = FALSE;\r
97 HighLight = GetColourHighlighting ();\r
e18ac66d 98 GetSelectedAcpiTable (&SelectedTable);\r
f75c7478 99\r
ee4dc24f 100 switch (GetReportOption ()) {\r
f75c7478 101 case ReportAll:\r
ee4dc24f
RN
102 Log = TRUE;\r
103 break;\r
f75c7478 104 case ReportSelected:\r
e18ac66d 105 if (Signature == SelectedTable->Type) {\r
ee4dc24f 106 Log = TRUE;\r
e18ac66d 107 SelectedTable->Found = TRUE;\r
ee4dc24f
RN
108 }\r
109 break;\r
f75c7478 110 case ReportTableList:\r
ee4dc24f
RN
111 if (mTableCount == 0) {\r
112 if (HighLight) {\r
113 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
114 gST->ConOut->SetAttribute (\r
115 gST->ConOut,\r
116 EFI_TEXT_ATTR(EFI_CYAN,\r
117 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))\r
118 );\r
119 }\r
120 Print (L"\nInstalled Table(s):\n");\r
121 if (HighLight) {\r
122 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);\r
123 }\r
124 }\r
125 Print (\r
126 L"\t%4d. %c%c%c%c\n",\r
127 ++mTableCount,\r
128 SignaturePtr[0],\r
129 SignaturePtr[1],\r
130 SignaturePtr[2],\r
131 SignaturePtr[3]\r
132 );\r
133 break;\r
f75c7478 134 case ReportDumpBinFile:\r
e18ac66d
TP
135 if (Signature == SelectedTable->Type) {\r
136 SelectedTable->Found = TRUE;\r
ee4dc24f
RN
137 DumpAcpiTableToFile (TablePtr, Length);\r
138 }\r
139 break;\r
f75c7478 140 case ReportMax:\r
ee4dc24f
RN
141 // We should never be here.\r
142 // This case is only present to prevent compiler warning.\r
143 break;\r
144 } // switch\r
145\r
146 if (Log) {\r
147 if (HighLight) {\r
148 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
149 gST->ConOut->SetAttribute (\r
150 gST->ConOut,\r
151 EFI_TEXT_ATTR(EFI_LIGHTBLUE,\r
152 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4))\r
153 );\r
154 }\r
155 Print (\r
156 L"\n\n --------------- %c%c%c%c Table --------------- \n\n",\r
157 SignaturePtr[0],\r
158 SignaturePtr[1],\r
159 SignaturePtr[2],\r
160 SignaturePtr[3]\r
161 );\r
162 if (HighLight) {\r
163 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);\r
164 }\r
165 }\r
166\r
167 return Log;\r
168}\r
169\r
ee4dc24f 170\r
ee4dc24f 171\r
a6eaba4d
DB
172/**\r
173 This function iterates the configuration table entries in the\r
174 system table, retrieves the RSDP pointer and starts parsing the ACPI tables.\r
ee4dc24f
RN
175\r
176 @param [in] SystemTable Pointer to the EFI system table.\r
177\r
178 @retval Returns EFI_NOT_FOUND if the RSDP pointer is not found.\r
179 Returns EFI_UNSUPPORTED if the RSDP version is less than 2.\r
180 Returns EFI_SUCCESS if successful.\r
a6eaba4d 181**/\r
ee4dc24f
RN
182EFI_STATUS\r
183EFIAPI\r
184AcpiView (\r
185 IN EFI_SYSTEM_TABLE* SystemTable\r
186 )\r
187{\r
188 EFI_STATUS Status;\r
189 UINTN Index;\r
190 EFI_CONFIGURATION_TABLE* EfiConfigurationTable;\r
191 BOOLEAN FoundAcpiTable;\r
192 UINTN OriginalAttribute;\r
193 UINTN PrintAttribute;\r
194 EREPORT_OPTION ReportOption;\r
195 UINT8* RsdpPtr;\r
196 UINT32 RsdpLength;\r
197 UINT8 RsdpRevision;\r
198 PARSE_ACPI_TABLE_PROC RsdpParserProc;\r
199 BOOLEAN Trace;\r
e18ac66d 200 SELECTED_ACPI_TABLE *SelectedTable;\r
ee4dc24f 201\r
0154e02d
SZ
202 //\r
203 // set local variables to suppress incorrect compiler/analyzer warnings\r
204 //\r
205 EfiConfigurationTable = NULL;\r
206 OriginalAttribute = 0;\r
207\r
e46e3040
TP
208 // Reset Table counts\r
209 mTableCount = 0;\r
210 mBinTableCount = 0;\r
211\r
cae974be
TP
212 // Reset The error/warning counters\r
213 ResetErrorCount ();\r
214 ResetWarningCount ();\r
215\r
e18ac66d
TP
216 // Retrieve the user selection of ACPI table to process\r
217 GetSelectedAcpiTable (&SelectedTable);\r
218\r
ee4dc24f
RN
219 // Search the table for an entry that matches the ACPI Table Guid\r
220 FoundAcpiTable = FALSE;\r
221 for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {\r
222 if (CompareGuid (&gEfiAcpiTableGuid,\r
223 &(SystemTable->ConfigurationTable[Index].VendorGuid))) {\r
224 EfiConfigurationTable = &SystemTable->ConfigurationTable[Index];\r
225 FoundAcpiTable = TRUE;\r
226 break;\r
227 }\r
228 }\r
229\r
230 if (FoundAcpiTable) {\r
231 RsdpPtr = (UINT8*)EfiConfigurationTable->VendorTable;\r
232\r
233 // The RSDP revision is 1 byte starting at offset 15\r
234 RsdpRevision = *(RsdpPtr + RSDP_REVISION_OFFSET);\r
235\r
236 if (RsdpRevision < 2) {\r
237 Print (\r
238 L"ERROR: RSDP version less than 2 is not supported.\n"\r
239 );\r
240 return EFI_UNSUPPORTED;\r
241 }\r
242\r
faef5a36
KK
243#if defined(MDE_CPU_ARM) || defined (MDE_CPU_AARCH64)\r
244 if (GetMandatoryTableValidate ()) {\r
245 ArmSbbrResetTableCounts ();\r
246 }\r
247#endif\r
248\r
ee4dc24f
RN
249 // The RSDP length is 4 bytes starting at offset 20\r
250 RsdpLength = *(UINT32*)(RsdpPtr + RSDP_LENGTH_OFFSET);\r
251\r
252 Trace = ProcessTableReportOptions (RSDP_TABLE_INFO, RsdpPtr, RsdpLength);\r
253\r
254 Status = GetParser (RSDP_TABLE_INFO, &RsdpParserProc);\r
255 if (EFI_ERROR (Status)) {\r
256 Print (\r
257 L"ERROR: No registered parser found for RSDP.\n"\r
258 );\r
259 return Status;\r
260 }\r
261\r
262 RsdpParserProc (\r
263 Trace,\r
264 RsdpPtr,\r
265 RsdpLength,\r
266 RsdpRevision\r
267 );\r
268\r
269 } else {\r
270 IncrementErrorCount ();\r
271 Print (\r
272 L"ERROR: Failed to find ACPI Table Guid in System Configuration Table.\n"\r
273 );\r
274 return EFI_NOT_FOUND;\r
275 }\r
276\r
faef5a36
KK
277#if defined(MDE_CPU_ARM) || defined (MDE_CPU_AARCH64)\r
278 if (GetMandatoryTableValidate ()) {\r
279 ArmSbbrReqsValidate ((ARM_SBBR_VERSION)GetMandatoryTableSpec ());\r
280 }\r
281#endif\r
282\r
ee4dc24f 283 ReportOption = GetReportOption ();\r
f75c7478
DB
284 if (ReportTableList != ReportOption) {\r
285 if (((ReportSelected == ReportOption) ||\r
286 (ReportDumpBinFile == ReportOption)) &&\r
e18ac66d 287 (!SelectedTable->Found)) {\r
ee4dc24f 288 Print (L"\nRequested ACPI Table not found.\n");\r
f73843d5
KK
289 } else if (GetConsistencyChecking () &&\r
290 (ReportDumpBinFile != ReportOption)) {\r
ee4dc24f
RN
291 OriginalAttribute = gST->ConOut->Mode->Attribute;\r
292\r
293 Print (L"\nTable Statistics:\n");\r
294\r
295 if (GetColourHighlighting ()) {\r
296 PrintAttribute = (GetErrorCount () > 0) ?\r
297 EFI_TEXT_ATTR (\r
298 EFI_RED,\r
299 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)\r
300 ) :\r
301 OriginalAttribute;\r
302 gST->ConOut->SetAttribute (gST->ConOut, PrintAttribute);\r
303 }\r
304 Print (L"\t%d Error(s)\n", GetErrorCount ());\r
305\r
306 if (GetColourHighlighting ()) {\r
307 PrintAttribute = (GetWarningCount () > 0) ?\r
308 EFI_TEXT_ATTR (\r
309 EFI_RED,\r
310 ((OriginalAttribute&(BIT4|BIT5|BIT6))>>4)\r
311 ) :\r
312 OriginalAttribute;\r
313\r
314 gST->ConOut->SetAttribute (gST->ConOut, PrintAttribute);\r
315 }\r
316 Print (L"\t%d Warning(s)\n", GetWarningCount ());\r
317\r
318 if (GetColourHighlighting ()) {\r
319 gST->ConOut->SetAttribute (gST->ConOut, OriginalAttribute);\r
320 }\r
321 }\r
322 }\r
323 return EFI_SUCCESS;\r
324}\r