]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parser.c
ShellPkg: Add acpiview tool to dump ACPI tables
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / Parsers / Dbg2 / Dbg2Parser.c
1 /**
2 DBG2 table parser
3
4 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 @par Reference(s):
14 - Microsoft Debug Port Table 2 (DBG2) Specification - December 10, 2015.
15 **/
16
17 #include <IndustryStandard/DebugPort2Table.h>
18 #include <Library/UefiLib.h>
19 #include "AcpiParser.h"
20 #include "AcpiTableParser.h"
21
22 // Local variables pointing to the table fields
23 STATIC CONST UINT32* OffsetDbgDeviceInfo;
24 STATIC CONST UINT32* NumberDbgDeviceInfo;
25 STATIC CONST UINT16* DbgDevInfoLen;
26 STATIC CONST UINT8* GasCount;
27 STATIC CONST UINT16* NameSpaceStringLength;
28 STATIC CONST UINT16* NameSpaceStringOffset;
29 STATIC CONST UINT16* OEMDataLength;
30 STATIC CONST UINT16* OEMDataOffset;
31 STATIC CONST UINT16* BaseAddrRegOffset;
32 STATIC CONST UINT16* AddrSizeOffset;
33 STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
34
35 /** This function Validates the NameSpace string length.
36
37 @param [in] Ptr Pointer to the start of the buffer.
38 @param [in] Context Pointer to context specific information e.g. this
39 could be a pointer to the ACPI table header.
40 */
41 STATIC
42 VOID
43 EFIAPI
44 ValidateNameSpaceStrLen (
45 IN UINT8* Ptr,
46 IN VOID* Context
47 );
48
49 /** This function parses the debug device information structure.
50
51 @param [in] Ptr Pointer to the start of the buffer.
52 @param [out] Length Pointer in which the length of the debug
53 device information is returned.
54 */
55 STATIC
56 VOID
57 EFIAPI
58 DumpDbgDeviceInfo (
59 IN UINT8* Ptr,
60 OUT UINT32* Length
61 );
62
63 /// An ACPI_PARSER array describing the ACPI DBG2 table.
64 STATIC CONST ACPI_PARSER Dbg2Parser[] = {
65 PARSE_ACPI_HEADER (&AcpiHdrInfo),
66 {L"OffsetDbgDeviceInfo", 4, 36, L"0x%x", NULL,
67 (VOID**)&OffsetDbgDeviceInfo, NULL, NULL},
68 {L"NumberDbgDeviceInfo", 4, 40, L"%d", NULL,
69 (VOID**)&NumberDbgDeviceInfo, NULL, NULL}
70 };
71
72 /// An ACPI_PARSER array describing the debug device information.
73 STATIC CONST ACPI_PARSER DbgDevInfoParser[] = {
74 {L"Revision", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
75 {L"Length", 2, 1, L"%d", NULL, (VOID**)&DbgDevInfoLen, NULL, NULL},
76
77 {L"Generic Address Registers Count", 1, 3, L"0x%x", NULL,
78 (VOID**)&GasCount, NULL, NULL},
79 {L"NameSpace String Length", 2, 4, L"%d", NULL,
80 (VOID**)&NameSpaceStringLength, ValidateNameSpaceStrLen, NULL},
81 {L"NameSpace String Offset", 2, 6, L"0x%x", NULL,
82 (VOID**)&NameSpaceStringOffset, NULL, NULL},
83 {L"OEM Data Length", 2, 8, L"%d", NULL, (VOID**)&OEMDataLength,
84 NULL, NULL},
85 {L"OEM Data Offset", 2, 10, L"0x%x", NULL, (VOID**)&OEMDataOffset,
86 NULL, NULL},
87
88 {L"Port Type", 2, 12, L"0x%x", NULL, NULL, NULL, NULL},
89 {L"Port SubType", 2, 14, L"0x%x", NULL, NULL, NULL, NULL},
90 {L"Reserved", 2, 16, L"%x", NULL, NULL, NULL, NULL},
91
92 {L"Base Address Register Offset", 2, 18, L"0x%x", NULL,
93 (VOID**)&BaseAddrRegOffset, NULL, NULL},
94 {L"Address Size Offset", 2, 20, L"0x%x", NULL,
95 (VOID**)&AddrSizeOffset, NULL, NULL}
96 };
97
98 /** This function validates the NameSpace string length.
99
100 @param [in] Ptr Pointer to the start of the buffer.
101 @param [in] Context Pointer to context specific information e.g. this
102 could be a pointer to the ACPI table header.
103 */
104 STATIC
105 VOID
106 EFIAPI
107 ValidateNameSpaceStrLen (
108 IN UINT8* Ptr,
109 IN VOID* Context
110 )
111 {
112 UINT16 NameSpaceStrLen = *(UINT16*)Ptr;
113 if (NameSpaceStrLen < 2) {
114 IncrementErrorCount ();
115 Print (
116 L"\nERROR: NamespaceString Length = %d. If no Namespace device exists,\n"
117 " then NamespaceString[] must contain a period '.'",
118 NameSpaceStrLen
119 );
120 }
121 }
122
123 /** This function parses the debug device information structure.
124
125 @param [in] Ptr Pointer to the start of the buffer.
126 @param [out] Ptr Pointer in which the length of the debug
127 device information is returned.
128 */
129 STATIC
130 VOID
131 EFIAPI
132 DumpDbgDeviceInfo (
133 IN UINT8* Ptr,
134 OUT UINT32* Length
135 )
136 {
137 UINT16 Index;
138 UINT8* DataPtr;
139 UINT32* AddrSize;
140
141 // Parse the debug device info to get the Length
142 ParseAcpi (
143 FALSE,
144 0,
145 "Debug Device Info",
146 Ptr,
147 3, // Length is 2 bytes starting at offset 1
148 PARSER_PARAMS (DbgDevInfoParser)
149 );
150
151 ParseAcpi (
152 TRUE,
153 2,
154 "Debug Device Info",
155 Ptr,
156 *DbgDevInfoLen,
157 PARSER_PARAMS (DbgDevInfoParser)
158 );
159
160 // GAS and Address Size
161 Index = 0;
162 DataPtr = Ptr + (*BaseAddrRegOffset);
163 AddrSize = (UINT32*)(Ptr + (*AddrSizeOffset));
164 while (Index < (*GasCount)) {
165 PrintFieldName (4, L"BaseAddressRegister");
166 DumpGasStruct (DataPtr, 4);
167 PrintFieldName (4, L"Address Size");
168 Print (L"0x%x\n", AddrSize[Index]);
169 DataPtr += GAS_LENGTH;
170 Index++;
171 }
172
173 // NameSpace String
174 Index = 0;
175 DataPtr = Ptr + (*NameSpaceStringOffset);
176 PrintFieldName (4, L"NameSpace String");
177 while (Index < (*NameSpaceStringLength)) {
178 Print (L"%c", DataPtr[Index++]);
179 }
180 Print (L"\n");
181
182 // OEM Data
183 Index = 0;
184 DataPtr = Ptr + (*OEMDataOffset);
185 PrintFieldName (4, L"OEM Data");
186 while (Index < (*OEMDataLength)) {
187 Print (L"%x ", DataPtr[Index++]);
188 if ((Index & 7) == 0) {
189 Print (L"\n%-*s ", OUTPUT_FIELD_COLUMN_WIDTH, L"");
190 }
191 }
192
193 *Length = *DbgDevInfoLen;
194 }
195
196 /** This function parses the ACPI DBG2 table.
197 When trace is enabled this function parses the DBG2 table and
198 traces the ACPI table fields.
199
200 This function also performs validation of the ACPI table fields.
201
202 @param [in] Trace If TRUE, trace the ACPI fields.
203 @param [in] Ptr Pointer to the start of the buffer.
204 @param [in] AcpiTableLength Length of the ACPI table.
205 @param [in] AcpiTableRevision Revision of the ACPI table.
206 */
207 VOID
208 EFIAPI
209 ParseAcpiDbg2 (
210 IN BOOLEAN Trace,
211 IN UINT8* Ptr,
212 IN UINT32 AcpiTableLength,
213 IN UINT8 AcpiTableRevision
214 )
215 {
216 UINT32 Offset;
217 UINT32 DbgDeviceInfoLength;
218 UINT8* DevInfoPtr;
219
220 if (!Trace) {
221 return;
222 }
223
224 Offset = ParseAcpi (
225 TRUE,
226 0,
227 "DBG2",
228 Ptr,
229 AcpiTableLength,
230 PARSER_PARAMS (Dbg2Parser)
231 );
232 DevInfoPtr = Ptr + Offset;
233
234 while (Offset < AcpiTableLength) {
235 DumpDbgDeviceInfo (
236 DevInfoPtr,
237 &DbgDeviceInfoLength
238 );
239 Offset += DbgDeviceInfoLength;
240 DevInfoPtr += DbgDeviceInfoLength;
241 }
242 }