]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtParser.c
ShellPkg: acpiview: GTDT: Validate global pointers before use
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / Parsers / Gtdt / GtdtParser.c
CommitLineData
a6eaba4d 1/** @file\r
ee4dc24f
RN
2 GTDT table parser\r
3\r
8da8daaf 4 Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.\r
56ba3746 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
ee4dc24f
RN
6\r
7 @par Reference(s):\r
98f98eb4 8 - ACPI 6.3 Specification - January 2019\r
ee4dc24f
RN
9 **/\r
10\r
11#include <IndustryStandard/Acpi.h>\r
12#include <Library/UefiLib.h>\r
13#include "AcpiParser.h"\r
14#include "AcpiTableParser.h"\r
15\r
2a219e19
KK
16// "The number of GT Block Timers must be less than or equal to 8"\r
17#define GT_BLOCK_TIMER_COUNT_MAX 8\r
18\r
ee4dc24f
RN
19// Local variables\r
20STATIC CONST UINT32* GtdtPlatformTimerCount;\r
21STATIC CONST UINT32* GtdtPlatformTimerOffset;\r
22STATIC CONST UINT8* PlatformTimerType;\r
23STATIC CONST UINT16* PlatformTimerLength;\r
24STATIC CONST UINT32* GtBlockTimerCount;\r
25STATIC CONST UINT32* GtBlockTimerOffset;\r
ee4dc24f
RN
26STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;\r
27\r
a6eaba4d
DB
28/**\r
29 This function validates the GT Block timer count.\r
ee4dc24f
RN
30\r
31 @param [in] Ptr Pointer to the start of the field data.\r
32 @param [in] Context Pointer to context specific information e.g. this\r
33 could be a pointer to the ACPI table header.\r
a6eaba4d 34**/\r
ee4dc24f
RN
35STATIC\r
36VOID\r
37EFIAPI\r
38ValidateGtBlockTimerCount (\r
39 IN UINT8* Ptr,\r
40 IN VOID* Context\r
2a219e19
KK
41 )\r
42{\r
43 UINT32 BlockTimerCount;\r
44\r
45 BlockTimerCount = *(UINT32*)Ptr;\r
46\r
47 if (BlockTimerCount > GT_BLOCK_TIMER_COUNT_MAX) {\r
48 IncrementErrorCount ();\r
49 Print (\r
50 L"\nERROR: Timer Count = %d. Max Timer Count is %d.",\r
51 BlockTimerCount,\r
52 GT_BLOCK_TIMER_COUNT_MAX\r
53 );\r
54 }\r
55}\r
ee4dc24f 56\r
8da8daaf
KK
57/**\r
58 This function validates the GT Frame Number.\r
59\r
60 @param [in] Ptr Pointer to the start of the field data.\r
61 @param [in] Context Pointer to context specific information e.g. this\r
62 could be a pointer to the ACPI table header.\r
63**/\r
64STATIC\r
65VOID\r
66EFIAPI\r
67ValidateGtFrameNumber (\r
68 IN UINT8* Ptr,\r
69 IN VOID* Context\r
2a219e19
KK
70 )\r
71{\r
72 UINT8 FrameNumber;\r
73\r
74 FrameNumber = *(UINT8*)Ptr;\r
75\r
76 if (FrameNumber >= GT_BLOCK_TIMER_COUNT_MAX) {\r
77 IncrementErrorCount ();\r
78 Print (\r
79 L"\nERROR: GT Frame Number = %d. GT Frame Number must be in range 0-%d.",\r
80 FrameNumber,\r
81 GT_BLOCK_TIMER_COUNT_MAX - 1\r
82 );\r
83 }\r
84}\r
8da8daaf 85\r
a6eaba4d
DB
86/**\r
87 An ACPI_PARSER array describing the ACPI GTDT Table.\r
88**/\r
ee4dc24f
RN
89STATIC CONST ACPI_PARSER GtdtParser[] = {\r
90 PARSE_ACPI_HEADER (&AcpiHdrInfo),\r
91 {L"CntControlBase Physical Address", 8, 36, L"0x%lx", NULL, NULL,\r
92 NULL, NULL},\r
93 {L"Reserved", 4, 44, L"0x%x", NULL, NULL, NULL, NULL},\r
94 {L"Secure EL1 timer GSIV", 4, 48, L"0x%x", NULL, NULL, NULL, NULL},\r
95 {L"Secure EL1 timer FLAGS", 4, 52, L"0x%x", NULL, NULL, NULL, NULL},\r
96\r
97 {L"Non-Secure EL1 timer GSIV", 4, 56, L"0x%x", NULL, NULL, NULL, NULL},\r
98 {L"Non-Secure EL1 timer FLAGS", 4, 60, L"0x%x", NULL, NULL, NULL, NULL},\r
99\r
100 {L"Virtual timer GSIV", 4, 64, L"0x%x", NULL, NULL, NULL, NULL},\r
101 {L"Virtual timer FLAGS", 4, 68, L"0x%x", NULL, NULL, NULL, NULL},\r
102\r
103 {L"Non-Secure EL2 timer GSIV", 4, 72, L"0x%x", NULL, NULL, NULL, NULL},\r
104 {L"Non-Secure EL2 timer FLAGS", 4, 76, L"0x%x", NULL, NULL, NULL, NULL},\r
105\r
106 {L"CntReadBase Physical address", 8, 80, L"0x%lx", NULL, NULL, NULL, NULL},\r
107 {L"Platform Timer Count", 4, 88, L"%d", NULL,\r
108 (VOID**)&GtdtPlatformTimerCount, NULL, NULL},\r
109 {L"Platform Timer Offset", 4, 92, L"0x%x", NULL,\r
98f98eb4
PG
110 (VOID**)&GtdtPlatformTimerOffset, NULL, NULL},\r
111 {L"Virtual EL2 Timer GSIV", 4, 96, L"0x%x", NULL, NULL, NULL, NULL},\r
112 {L"Virtual EL2 Timer Flags", 4, 100, L"0x%x", NULL, NULL, NULL, NULL}\r
ee4dc24f
RN
113};\r
114\r
a6eaba4d
DB
115/**\r
116 An ACPI_PARSER array describing the Platform timer header.\r
117**/\r
ee4dc24f
RN
118STATIC CONST ACPI_PARSER GtPlatformTimerHeaderParser[] = {\r
119 {L"Type", 1, 0, NULL, NULL, (VOID**)&PlatformTimerType, NULL, NULL},\r
120 {L"Length", 2, 1, NULL, NULL, (VOID**)&PlatformTimerLength, NULL, NULL},\r
121 {L"Reserved", 1, 3, NULL, NULL, NULL, NULL, NULL}\r
122};\r
123\r
a6eaba4d
DB
124/**\r
125 An ACPI_PARSER array describing the Platform GT Block.\r
126**/\r
ee4dc24f
RN
127STATIC CONST ACPI_PARSER GtBlockParser[] = {\r
128 {L"Type", 1, 0, L"%d", NULL, NULL, NULL, NULL},\r
1d12f0e6 129 {L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL},\r
ee4dc24f
RN
130 {L"Reserved", 1, 3, L"%x", NULL, NULL, NULL, NULL},\r
131 {L"Physical address (CntCtlBase)", 8, 4, L"0x%lx", NULL, NULL, NULL, NULL},\r
132 {L"Timer Count", 4, 12, L"%d", NULL, (VOID**)&GtBlockTimerCount,\r
133 ValidateGtBlockTimerCount, NULL},\r
134 {L"Timer Offset", 4, 16, L"%d", NULL, (VOID**)&GtBlockTimerOffset, NULL,\r
135 NULL}\r
136};\r
137\r
a6eaba4d
DB
138/**\r
139 An ACPI_PARSER array describing the GT Block timer.\r
140**/\r
ee4dc24f 141STATIC CONST ACPI_PARSER GtBlockTimerParser[] = {\r
8da8daaf 142 {L"Frame Number", 1, 0, L"%d", NULL, NULL, ValidateGtFrameNumber, NULL},\r
ee4dc24f
RN
143 {L"Reserved", 3, 1, L"%x %x %x", Dump3Chars, NULL, NULL, NULL},\r
144 {L"Physical address (CntBaseX)", 8, 4, L"0x%lx", NULL, NULL, NULL, NULL},\r
145 {L"Physical address (CntEL0BaseX)", 8, 12, L"0x%lx", NULL, NULL, NULL,\r
146 NULL},\r
147 {L"Physical Timer GSIV", 4, 20, L"0x%x", NULL, NULL, NULL, NULL},\r
148 {L"Physical Timer Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL},\r
149 {L"Virtual Timer GSIV", 4, 28, L"0x%x", NULL, NULL, NULL, NULL},\r
150 {L"Virtual Timer Flags", 4, 32, L"0x%x", NULL, NULL, NULL, NULL},\r
151 {L"Common Flags", 4, 36, L"0x%x", NULL, NULL, NULL, NULL}\r
152};\r
153\r
a6eaba4d
DB
154/**\r
155 An ACPI_PARSER array describing the Platform Watchdog.\r
156**/\r
ee4dc24f
RN
157STATIC CONST ACPI_PARSER SBSAGenericWatchdogParser[] = {\r
158 {L"Type", 1, 0, L"%d", NULL, NULL, NULL, NULL},\r
159 {L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL},\r
160 {L"Reserved", 1, 3, L"%x", NULL, NULL, NULL, NULL},\r
161 {L"RefreshFrame Physical address", 8, 4, L"0x%lx", NULL, NULL, NULL, NULL},\r
162 {L"ControlFrame Physical address", 8, 12, L"0x%lx", NULL, NULL, NULL, NULL},\r
163 {L"Watchdog Timer GSIV", 4, 20, L"0x%x", NULL, NULL, NULL, NULL},\r
164 {L"Watchdog Timer Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL}\r
165};\r
166\r
a6eaba4d
DB
167/**\r
168 This function parses the Platform GT Block.\r
ee4dc24f 169\r
1d12f0e6
KK
170 @param [in] Ptr Pointer to the start of the GT Block data.\r
171 @param [in] Length Length of the GT Block structure.\r
a6eaba4d 172**/\r
ee4dc24f
RN
173STATIC\r
174VOID\r
175DumpGTBlock (\r
176 IN UINT8* Ptr,\r
1d12f0e6 177 IN UINT16 Length\r
ee4dc24f
RN
178 )\r
179{\r
180 UINT32 Index;\r
181 UINT32 Offset;\r
ee4dc24f 182\r
1d12f0e6
KK
183 ParseAcpi (\r
184 TRUE,\r
185 2,\r
186 "GT Block",\r
187 Ptr,\r
188 Length,\r
189 PARSER_PARAMS (GtBlockParser)\r
190 );\r
191\r
214bc6e2
KK
192 // Check if the values used to control the parsing logic have been\r
193 // successfully read.\r
194 if ((GtBlockTimerCount == NULL) ||\r
195 (GtBlockTimerOffset == NULL)) {\r
196 IncrementErrorCount ();\r
197 Print (\r
198 L"ERROR: Insufficient GT Block Structure length. Length = %d.\n",\r
199 Length\r
200 );\r
201 return;\r
202 }\r
203\r
1d12f0e6
KK
204 Offset = *GtBlockTimerOffset;\r
205 Index = 0;\r
206\r
207 // Parse the specified number of GT Block Timer Structures or the GT Block\r
208 // Structure buffer length. Whichever is minimum.\r
209 while ((Index++ < *GtBlockTimerCount) &&\r
210 (Offset < Length)) {\r
211 Offset += ParseAcpi (\r
212 TRUE,\r
213 2,\r
214 "GT Block Timer",\r
215 Ptr + Offset,\r
216 Length - Offset,\r
217 PARSER_PARAMS (GtBlockTimerParser)\r
218 );\r
ee4dc24f
RN
219 }\r
220}\r
221\r
a6eaba4d
DB
222/**\r
223 This function parses the Platform Watchdog timer.\r
ee4dc24f
RN
224\r
225 @param [in] Ptr Pointer to the start of the watchdog timer data.\r
226 @param [in] Length Length of the watchdog timer structure.\r
a6eaba4d 227**/\r
ee4dc24f
RN
228STATIC\r
229VOID\r
230DumpWatchdogTimer (\r
231 IN UINT8* Ptr,\r
232 IN UINT16 Length\r
233 )\r
234{\r
235 ParseAcpi (\r
236 TRUE,\r
237 2,\r
238 "SBSA Generic Watchdog",\r
239 Ptr,\r
240 Length,\r
241 PARSER_PARAMS (SBSAGenericWatchdogParser)\r
242 );\r
243}\r
244\r
a6eaba4d
DB
245/**\r
246 This function parses the ACPI GTDT table.\r
ee4dc24f
RN
247 When trace is enabled this function parses the GTDT table and\r
248 traces the ACPI table fields.\r
249\r
250 This function also parses the following platform timer structures:\r
251 - GT Block timer\r
252 - Watchdog timer\r
253\r
254 This function also performs validation of the ACPI table fields.\r
255\r
256 @param [in] Trace If TRUE, trace the ACPI fields.\r
257 @param [in] Ptr Pointer to the start of the buffer.\r
258 @param [in] AcpiTableLength Length of the ACPI table.\r
259 @param [in] AcpiTableRevision Revision of the ACPI table.\r
a6eaba4d 260**/\r
ee4dc24f
RN
261VOID\r
262EFIAPI\r
263ParseAcpiGtdt (\r
264 IN BOOLEAN Trace,\r
265 IN UINT8* Ptr,\r
266 IN UINT32 AcpiTableLength,\r
267 IN UINT8 AcpiTableRevision\r
268 )\r
269{\r
270 UINT32 Index;\r
1d12f0e6 271 UINT32 Offset;\r
ee4dc24f
RN
272 UINT8* TimerPtr;\r
273\r
274 if (!Trace) {\r
275 return;\r
276 }\r
277\r
278 ParseAcpi (\r
279 TRUE,\r
280 0,\r
281 "GTDT",\r
282 Ptr,\r
283 AcpiTableLength,\r
284 PARSER_PARAMS (GtdtParser)\r
285 );\r
286\r
214bc6e2
KK
287 // Check if the values used to control the parsing logic have been\r
288 // successfully read.\r
289 if ((GtdtPlatformTimerCount == NULL) ||\r
290 (GtdtPlatformTimerOffset == NULL)) {\r
291 IncrementErrorCount ();\r
292 Print (\r
293 L"ERROR: Insufficient table length. AcpiTableLength = %d.\n",\r
294 AcpiTableLength\r
295 );\r
296 return;\r
297 }\r
298\r
1d12f0e6
KK
299 TimerPtr = Ptr + *GtdtPlatformTimerOffset;\r
300 Offset = *GtdtPlatformTimerOffset;\r
301 Index = 0;\r
302\r
303 // Parse the specified number of Platform Timer Structures or the GTDT\r
304 // buffer length. Whichever is minimum.\r
305 while ((Index++ < *GtdtPlatformTimerCount) &&\r
306 (Offset < AcpiTableLength)) {\r
307 // Parse the Platform Timer Header to obtain Length and Type\r
308 ParseAcpi (\r
309 FALSE,\r
310 0,\r
311 NULL,\r
312 TimerPtr,\r
313 AcpiTableLength - Offset,\r
314 PARSER_PARAMS (GtPlatformTimerHeaderParser)\r
315 );\r
316\r
214bc6e2
KK
317 // Check if the values used to control the parsing logic have been\r
318 // successfully read.\r
319 if ((PlatformTimerType == NULL) ||\r
320 (PlatformTimerLength == NULL)) {\r
321 IncrementErrorCount ();\r
322 Print (\r
323 L"ERROR: Insufficient remaining table buffer length to read the " \\r
324 L"Platform Timer Structure header. Length = %d.\n",\r
325 AcpiTableLength - Offset\r
326 );\r
327 return;\r
328 }\r
329\r
1d12f0e6
KK
330 // Make sure the Platform Timer is inside the table.\r
331 if ((Offset + *PlatformTimerLength) > AcpiTableLength) {\r
332 IncrementErrorCount ();\r
333 Print (\r
334 L"ERROR: Invalid Platform Timer Structure length. " \\r
335 L"PlatformTimerLength = %d. RemainingTableBufferLength = %d. " \\r
336 L"GTDT parsing aborted.\n",\r
337 *PlatformTimerLength,\r
338 AcpiTableLength - Offset\r
ee4dc24f 339 );\r
1d12f0e6
KK
340 return;\r
341 }\r
342\r
343 switch (*PlatformTimerType) {\r
344 case EFI_ACPI_6_3_GTDT_GT_BLOCK:\r
345 DumpGTBlock (TimerPtr, *PlatformTimerLength);\r
346 break;\r
347 case EFI_ACPI_6_3_GTDT_SBSA_GENERIC_WATCHDOG:\r
348 DumpWatchdogTimer (TimerPtr, *PlatformTimerLength);\r
349 break;\r
350 default:\r
351 IncrementErrorCount ();\r
352 Print (\r
353 L"ERROR: Invalid Platform Timer Type = %d\n",\r
354 *PlatformTimerType\r
355 );\r
356 break;\r
357 } // switch\r
358\r
359 TimerPtr += *PlatformTimerLength;\r
360 Offset += *PlatformTimerLength;\r
361 } // while\r
ee4dc24f 362}\r