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