]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtParser.c
ShellPkg: acpiview: GTDT: Prevent buffer overruns
[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
192 Offset = *GtBlockTimerOffset;\r
193 Index = 0;\r
194\r
195 // Parse the specified number of GT Block Timer Structures or the GT Block\r
196 // Structure buffer length. Whichever is minimum.\r
197 while ((Index++ < *GtBlockTimerCount) &&\r
198 (Offset < Length)) {\r
199 Offset += ParseAcpi (\r
200 TRUE,\r
201 2,\r
202 "GT Block Timer",\r
203 Ptr + Offset,\r
204 Length - Offset,\r
205 PARSER_PARAMS (GtBlockTimerParser)\r
206 );\r
ee4dc24f
RN
207 }\r
208}\r
209\r
a6eaba4d
DB
210/**\r
211 This function parses the Platform Watchdog timer.\r
ee4dc24f
RN
212\r
213 @param [in] Ptr Pointer to the start of the watchdog timer data.\r
214 @param [in] Length Length of the watchdog timer structure.\r
a6eaba4d 215**/\r
ee4dc24f
RN
216STATIC\r
217VOID\r
218DumpWatchdogTimer (\r
219 IN UINT8* Ptr,\r
220 IN UINT16 Length\r
221 )\r
222{\r
223 ParseAcpi (\r
224 TRUE,\r
225 2,\r
226 "SBSA Generic Watchdog",\r
227 Ptr,\r
228 Length,\r
229 PARSER_PARAMS (SBSAGenericWatchdogParser)\r
230 );\r
231}\r
232\r
a6eaba4d
DB
233/**\r
234 This function parses the ACPI GTDT table.\r
ee4dc24f
RN
235 When trace is enabled this function parses the GTDT table and\r
236 traces the ACPI table fields.\r
237\r
238 This function also parses the following platform timer structures:\r
239 - GT Block timer\r
240 - Watchdog timer\r
241\r
242 This function also performs validation of the ACPI table fields.\r
243\r
244 @param [in] Trace If TRUE, trace the ACPI fields.\r
245 @param [in] Ptr Pointer to the start of the buffer.\r
246 @param [in] AcpiTableLength Length of the ACPI table.\r
247 @param [in] AcpiTableRevision Revision of the ACPI table.\r
a6eaba4d 248**/\r
ee4dc24f
RN
249VOID\r
250EFIAPI\r
251ParseAcpiGtdt (\r
252 IN BOOLEAN Trace,\r
253 IN UINT8* Ptr,\r
254 IN UINT32 AcpiTableLength,\r
255 IN UINT8 AcpiTableRevision\r
256 )\r
257{\r
258 UINT32 Index;\r
1d12f0e6 259 UINT32 Offset;\r
ee4dc24f
RN
260 UINT8* TimerPtr;\r
261\r
262 if (!Trace) {\r
263 return;\r
264 }\r
265\r
266 ParseAcpi (\r
267 TRUE,\r
268 0,\r
269 "GTDT",\r
270 Ptr,\r
271 AcpiTableLength,\r
272 PARSER_PARAMS (GtdtParser)\r
273 );\r
274\r
1d12f0e6
KK
275 TimerPtr = Ptr + *GtdtPlatformTimerOffset;\r
276 Offset = *GtdtPlatformTimerOffset;\r
277 Index = 0;\r
278\r
279 // Parse the specified number of Platform Timer Structures or the GTDT\r
280 // buffer length. Whichever is minimum.\r
281 while ((Index++ < *GtdtPlatformTimerCount) &&\r
282 (Offset < AcpiTableLength)) {\r
283 // Parse the Platform Timer Header to obtain Length and Type\r
284 ParseAcpi (\r
285 FALSE,\r
286 0,\r
287 NULL,\r
288 TimerPtr,\r
289 AcpiTableLength - Offset,\r
290 PARSER_PARAMS (GtPlatformTimerHeaderParser)\r
291 );\r
292\r
293 // Make sure the Platform Timer is inside the table.\r
294 if ((Offset + *PlatformTimerLength) > AcpiTableLength) {\r
295 IncrementErrorCount ();\r
296 Print (\r
297 L"ERROR: Invalid Platform Timer Structure length. " \\r
298 L"PlatformTimerLength = %d. RemainingTableBufferLength = %d. " \\r
299 L"GTDT parsing aborted.\n",\r
300 *PlatformTimerLength,\r
301 AcpiTableLength - Offset\r
ee4dc24f 302 );\r
1d12f0e6
KK
303 return;\r
304 }\r
305\r
306 switch (*PlatformTimerType) {\r
307 case EFI_ACPI_6_3_GTDT_GT_BLOCK:\r
308 DumpGTBlock (TimerPtr, *PlatformTimerLength);\r
309 break;\r
310 case EFI_ACPI_6_3_GTDT_SBSA_GENERIC_WATCHDOG:\r
311 DumpWatchdogTimer (TimerPtr, *PlatformTimerLength);\r
312 break;\r
313 default:\r
314 IncrementErrorCount ();\r
315 Print (\r
316 L"ERROR: Invalid Platform Timer Type = %d\n",\r
317 *PlatformTimerType\r
318 );\r
319 break;\r
320 } // switch\r
321\r
322 TimerPtr += *PlatformTimerLength;\r
323 Offset += *PlatformTimerLength;\r
324 } // while\r
ee4dc24f 325}\r