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