]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Slit/SlitParser.c
ShellPkg: acpiview: SLIT: Validate global pointer before use
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / Parsers / Slit / SlitParser.c
1 /** @file
2 SLIT 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 UINT64* SlitSystemLocalityCount;
19 STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
20
21 /**
22 An ACPI_PARSER array describing the ACPI SLIT table.
23 **/
24 STATIC CONST ACPI_PARSER SlitParser[] = {
25 PARSE_ACPI_HEADER (&AcpiHdrInfo),
26 {L"Number of System Localities", 8, 36, L"0x%lx", NULL,
27 (VOID**)&SlitSystemLocalityCount, NULL, NULL}
28 };
29
30 /**
31 Macro to get the value of a System Locality
32 **/
33 #define SLIT_ELEMENT(Ptr, i, j) *(Ptr + (MultU64x64 (i, LocalityCount)) + j)
34
35 /**
36 This function parses the ACPI SLIT table.
37 When trace is enabled this function parses the SLIT table and
38 traces the ACPI table fields.
39
40 This function also validates System Localities for the following:
41 - Diagonal elements have a normalized value of 10
42 - Relative distance from System Locality at i*N+j is same as
43 j*N+i
44
45 @param [in] Trace If TRUE, trace the ACPI fields.
46 @param [in] Ptr Pointer to the start of the buffer.
47 @param [in] AcpiTableLength Length of the ACPI table.
48 @param [in] AcpiTableRevision Revision of the ACPI table.
49 **/
50 VOID
51 EFIAPI
52 ParseAcpiSlit (
53 IN BOOLEAN Trace,
54 IN UINT8* Ptr,
55 IN UINT32 AcpiTableLength,
56 IN UINT8 AcpiTableRevision
57 )
58 {
59 UINT32 Offset;
60 UINT64 Count;
61 UINT64 Index;
62 UINT64 LocalityCount;
63 UINT8* LocalityPtr;
64 CHAR16 Buffer[80]; // Used for AsciiName param of ParseAcpi
65
66 if (!Trace) {
67 return;
68 }
69
70 Offset = ParseAcpi (
71 TRUE,
72 0,
73 "SLIT",
74 Ptr,
75 AcpiTableLength,
76 PARSER_PARAMS (SlitParser)
77 );
78
79 // Check if the values used to control the parsing logic have been
80 // successfully read.
81 if (SlitSystemLocalityCount == NULL) {
82 IncrementErrorCount ();
83 Print (
84 L"ERROR: Insufficient table length. AcpiTableLength = %d.\n",
85 AcpiTableLength
86 );
87 return;
88 }
89
90 LocalityPtr = Ptr + Offset;
91 LocalityCount = *SlitSystemLocalityCount;
92
93 // We only print the Localities if the count is less than 16
94 // If the locality count is more than 16 then refer to the
95 // raw data dump.
96 if (LocalityCount < 16) {
97 UnicodeSPrint (
98 Buffer,
99 sizeof (Buffer),
100 L"Entry[0x%lx][0x%lx]",
101 LocalityCount,
102 LocalityCount
103 );
104 PrintFieldName (0, Buffer);
105 Print (L"\n");
106 Print (L" ");
107 for (Index = 0; Index < LocalityCount; Index++) {
108 Print (L" (%3d) ", Index);
109 }
110 Print (L"\n");
111 for (Count = 0; Count< LocalityCount; Count++) {
112 Print (L" (%3d) ", Count);
113 for (Index = 0; Index < LocalityCount; Index++) {
114 Print (L" %3d ", SLIT_ELEMENT (LocalityPtr, Count, Index));
115 }
116 Print (L"\n");
117 }
118 }
119
120 // Validate
121 for (Count = 0; Count < LocalityCount; Count++) {
122 for (Index = 0; Index < LocalityCount; Index++) {
123 // Element[x][x] must be equal to 10
124 if ((Count == Index) && (SLIT_ELEMENT (LocalityPtr, Count,Index) != 10)) {
125 IncrementErrorCount ();
126 Print (
127 L"ERROR: Diagonal Element[0x%lx][0x%lx] (%3d)."
128 L" Normalized Value is not 10\n",
129 Count,
130 Index,
131 SLIT_ELEMENT (LocalityPtr, Count, Index)
132 );
133 }
134 // Element[i][j] must be equal to Element[j][i]
135 if (SLIT_ELEMENT (LocalityPtr, Count, Index) !=
136 SLIT_ELEMENT (LocalityPtr, Index, Count)) {
137 IncrementErrorCount ();
138 Print (
139 L"ERROR: Relative distances for Element[0x%lx][0x%lx] (%3d) and \n"
140 L"Element[0x%lx][0x%lx] (%3d) do not match.\n",
141 Count,
142 Index,
143 SLIT_ELEMENT (LocalityPtr, Count, Index),
144 Index,
145 Count,
146 SLIT_ELEMENT (LocalityPtr, Index, Count)
147 );
148 }
149 }
150 }
151 }