]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Srat/SratParser.c
ShellPkg: acpiview: SRAT: Prevent buffer overruns
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / Parsers / Srat / SratParser.c
1 /** @file
2 SRAT 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.2 Specification - Errata A, September 2017
9 **/
10
11 #include <IndustryStandard/Acpi.h>
12 #include <Library/PrintLib.h>
13 #include <Library/UefiLib.h>
14 #include "AcpiParser.h"
15 #include "AcpiTableParser.h"
16
17 // Local Variables
18 STATIC CONST UINT8* SratRAType;
19 STATIC CONST UINT8* SratRALength;
20 STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
21
22 /**
23 This function validates the Reserved field in the SRAT table header.
24
25 @param [in] Ptr Pointer to the start of the field data.
26 @param [in] Context Pointer to context specific information e.g. this
27 could be a pointer to the ACPI table header.
28 **/
29 STATIC
30 VOID
31 EFIAPI
32 ValidateSratReserved (
33 IN UINT8* Ptr,
34 IN VOID* Context
35 )
36 {
37 if (*(UINT32*)Ptr != 1) {
38 IncrementErrorCount ();
39 Print (L"\nERROR: Reserved should be 1 for backward compatibility.\n");
40 }
41 }
42
43 /**
44 This function traces the APIC Proximity Domain field.
45
46 @param [in] Format Format string for tracing the data.
47 @param [in] Ptr Pointer to the start of the buffer.
48 **/
49 STATIC
50 VOID
51 EFIAPI
52 DumpSratApicProximity (
53 IN CONST CHAR16* Format,
54 IN UINT8* Ptr
55 )
56 {
57 UINT32 ProximityDomain;
58
59 ProximityDomain = Ptr[0] | (Ptr[1] << 8) | (Ptr[2] << 16);
60
61 Print (Format, ProximityDomain);
62 }
63
64 /**
65 An ACPI_PARSER array describing the SRAT Table.
66 **/
67 STATIC CONST ACPI_PARSER SratParser[] = {
68 PARSE_ACPI_HEADER (&AcpiHdrInfo),
69 {L"Reserved", 4, 36, L"0x%x", NULL, NULL, ValidateSratReserved, NULL},
70 {L"Reserved", 8, 40, L"0x%lx", NULL, NULL, NULL, NULL}
71 };
72
73 /**
74 An ACPI_PARSER array describing the Resource Allocation structure header.
75 **/
76 STATIC CONST ACPI_PARSER SratResourceAllocationParser[] = {
77 {L"Type", 1, 0, NULL, NULL, (VOID**)&SratRAType, NULL, NULL},
78 {L"Length", 1, 1, NULL, NULL, (VOID**)&SratRALength, NULL, NULL}
79 };
80
81 /**
82 An ACPI_PARSER array describing the GICC Affinity structure.
83 **/
84 STATIC CONST ACPI_PARSER SratGicCAffinityParser[] = {
85 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
86 {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},
87
88 {L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL},
89 {L"ACPI Processor UID", 4, 6, L"0x%x", NULL, NULL, NULL, NULL},
90 {L"Flags", 4, 10, L"0x%x", NULL, NULL, NULL, NULL},
91 {L"Clock Domain", 4, 14, L"0x%x", NULL, NULL, NULL, NULL}
92 };
93
94 /**
95 An ACPI_PARSER array describing the GIC ITS Affinity structure.
96 **/
97 STATIC CONST ACPI_PARSER SratGicITSAffinityParser[] = {
98 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
99 {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},
100
101 {L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL},
102 {L"Reserved", 2, 6, L"0x%x", NULL, NULL, NULL, NULL},
103 {L"ITS Id", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
104 };
105
106 /**
107 An ACPI_PARSER array describing the Memory Affinity structure.
108 **/
109 STATIC CONST ACPI_PARSER SratMemAffinityParser[] = {
110 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
111 {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},
112
113 {L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL},
114 {L"Reserved", 2, 6, L"0x%x", NULL, NULL, NULL, NULL},
115 {L"Base Address Low", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
116 {L"Base Address High", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
117 {L"Length Low", 4, 16, L"0x%x", NULL, NULL, NULL, NULL},
118 {L"Length High", 4, 20, L"0x%x", NULL, NULL, NULL, NULL},
119 {L"Reserved", 4, 24, L"0x%x", NULL, NULL, NULL, NULL},
120 {L"Flags", 4, 28, L"0x%x", NULL, NULL, NULL, NULL},
121 {L"Reserved", 8, 32, L"0x%lx", NULL, NULL, NULL, NULL}
122 };
123
124 /**
125 An ACPI_PARSER array describing the APIC/SAPIC Affinity structure.
126 **/
127 STATIC CONST ACPI_PARSER SratApciSapicAffinityParser[] = {
128 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
129 {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},
130
131 {L"Proximity Domain [7:0]", 1, 2, L"0x%x", NULL, NULL, NULL, NULL},
132 {L"APIC ID", 1, 3, L"0x%x", NULL, NULL, NULL, NULL},
133 {L"Flags", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
134 {L"Local SAPIC EID", 1, 8, L"0x%x", NULL, NULL, NULL, NULL},
135 {L"Proximity Domain [31:8]", 3, 9, L"0x%x", DumpSratApicProximity,
136 NULL, NULL, NULL},
137 {L"Clock Domain", 4, 12, L"0x%x", NULL, NULL, NULL, NULL}
138 };
139
140 /**
141 An ACPI_PARSER array describing the Processor Local x2APIC Affinity structure.
142 **/
143 STATIC CONST ACPI_PARSER SratX2ApciAffinityParser[] = {
144 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
145 {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},
146
147 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
148 {L"Proximity Domain", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
149 {L"X2APIC ID", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
150 {L"Flags", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
151 {L"Clock Domain", 4, 16, L"0x%x", NULL, NULL, NULL, NULL},
152 {L"Reserved", 4, 20, L"0x%x", NULL, NULL, NULL, NULL}
153 };
154
155 /**
156 This function parses the ACPI SRAT table.
157 When trace is enabled this function parses the SRAT table and
158 traces the ACPI table fields.
159
160 This function parses the following Resource Allocation Structures:
161 - Processor Local APIC/SAPIC Affinity Structure
162 - Memory Affinity Structure
163 - Processor Local x2APIC Affinity Structure
164 - GICC Affinity Structure
165
166 This function also performs validation of the ACPI table fields.
167
168 @param [in] Trace If TRUE, trace the ACPI fields.
169 @param [in] Ptr Pointer to the start of the buffer.
170 @param [in] AcpiTableLength Length of the ACPI table.
171 @param [in] AcpiTableRevision Revision of the ACPI table.
172 **/
173 VOID
174 EFIAPI
175 ParseAcpiSrat (
176 IN BOOLEAN Trace,
177 IN UINT8* Ptr,
178 IN UINT32 AcpiTableLength,
179 IN UINT8 AcpiTableRevision
180 )
181 {
182 UINT32 Offset;
183 UINT8* ResourcePtr;
184 UINT32 GicCAffinityIndex;
185 UINT32 GicITSAffinityIndex;
186 UINT32 MemoryAffinityIndex;
187 UINT32 ApicSapicAffinityIndex;
188 UINT32 X2ApicAffinityIndex;
189 CHAR8 Buffer[80]; // Used for AsciiName param of ParseAcpi
190
191 GicCAffinityIndex = 0;
192 GicITSAffinityIndex = 0;
193 MemoryAffinityIndex = 0;
194 ApicSapicAffinityIndex = 0;
195 X2ApicAffinityIndex = 0;
196
197 if (!Trace) {
198 return;
199 }
200
201 Offset = ParseAcpi (
202 TRUE,
203 0,
204 "SRAT",
205 Ptr,
206 AcpiTableLength,
207 PARSER_PARAMS (SratParser)
208 );
209
210 ResourcePtr = Ptr + Offset;
211
212 while (Offset < AcpiTableLength) {
213 ParseAcpi (
214 FALSE,
215 0,
216 NULL,
217 ResourcePtr,
218 AcpiTableLength - Offset,
219 PARSER_PARAMS (SratResourceAllocationParser)
220 );
221
222 // Make sure the SRAT structure lies inside the table
223 if ((Offset + *SratRALength) > AcpiTableLength) {
224 IncrementErrorCount ();
225 Print (
226 L"ERROR: Invalid SRAT structure length. SratRALength = %d. " \
227 L"RemainingTableBufferLength = %d. SRAT parsing aborted.\n",
228 *SratRALength,
229 AcpiTableLength - Offset
230 );
231 return;
232 }
233
234 switch (*SratRAType) {
235 case EFI_ACPI_6_2_GICC_AFFINITY:
236 AsciiSPrint (
237 Buffer,
238 sizeof (Buffer),
239 "GICC Affinity Structure [%d]",
240 GicCAffinityIndex++
241 );
242 ParseAcpi (
243 TRUE,
244 2,
245 Buffer,
246 ResourcePtr,
247 *SratRALength,
248 PARSER_PARAMS (SratGicCAffinityParser)
249 );
250 break;
251
252 case EFI_ACPI_6_2_GIC_ITS_AFFINITY:
253 AsciiSPrint (
254 Buffer,
255 sizeof (Buffer),
256 "GIC ITS Affinity Structure [%d]",
257 GicITSAffinityIndex++
258 );
259 ParseAcpi (
260 TRUE,
261 2,
262 Buffer,
263 ResourcePtr,
264 *SratRALength,
265 PARSER_PARAMS (SratGicITSAffinityParser)
266 );
267 break;
268
269 case EFI_ACPI_6_2_MEMORY_AFFINITY:
270 AsciiSPrint (
271 Buffer,
272 sizeof (Buffer),
273 "Memory Affinity Structure [%d]",
274 MemoryAffinityIndex++
275 );
276 ParseAcpi (
277 TRUE,
278 2,
279 Buffer,
280 ResourcePtr,
281 *SratRALength,
282 PARSER_PARAMS (SratMemAffinityParser)
283 );
284 break;
285
286 case EFI_ACPI_6_2_PROCESSOR_LOCAL_APIC_SAPIC_AFFINITY:
287 AsciiSPrint (
288 Buffer,
289 sizeof (Buffer),
290 "APIC/SAPIC Affinity Structure [%d]",
291 ApicSapicAffinityIndex++
292 );
293 ParseAcpi (
294 TRUE,
295 2,
296 Buffer,
297 ResourcePtr,
298 *SratRALength,
299 PARSER_PARAMS (SratApciSapicAffinityParser)
300 );
301 break;
302
303 case EFI_ACPI_6_2_PROCESSOR_LOCAL_X2APIC_AFFINITY:
304 AsciiSPrint (
305 Buffer,
306 sizeof (Buffer),
307 "X2APIC Affinity Structure [%d]",
308 X2ApicAffinityIndex++
309 );
310 ParseAcpi (
311 TRUE,
312 2,
313 Buffer,
314 ResourcePtr,
315 *SratRALength,
316 PARSER_PARAMS (SratX2ApciAffinityParser)
317 );
318 break;
319
320 default:
321 IncrementErrorCount ();
322 Print (L"ERROR: Unknown SRAT Affinity type = 0x%x\n", *SratRAType);
323 break;
324 }
325
326 ResourcePtr += (*SratRALength);
327 Offset += (*SratRALength);
328 }
329 }