]> git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Common/AmlLib/Api/AmlApiHelper.c
DynamicTablesPkg: Apply uncrustify changes
[mirror_edk2.git] / DynamicTablesPkg / Library / Common / AmlLib / Api / AmlApiHelper.c
1 /** @file
2 AML Helper.
3
4 Copyright (c) 2020, Arm Limited. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 /* Even though this file has access to the internal Node definition,
10 i.e. AML_ROOT_NODE, AML_OBJECT_NODE, etc. Only the external node
11 handle types should be used, i.e. AML_NODE_HANDLE, AML_ROOT_NODE_HANDLE,
12 etc.
13 Indeed, the functions in the "Api" folder should be implemented only
14 using the "safe" functions available in the "Include" folder. This
15 makes the functions available in the "Api" folder easy to export.
16 */
17 #include <Api/AmlApiHelper.h>
18
19 #include <AmlCoreInterface.h>
20 #include <AmlInclude.h>
21 #include <String/AmlString.h>
22
23 /** Compare the NameString defined by the "Name ()" ASL function,
24 and stored in the NameOpNode, with the input NameString.
25
26 An ASL NameString is expected to be NULL terminated, and can be composed
27 of NameSegs that have less that 4 chars, like "DEV". "DEV" will be expanded
28 as "DEV_".
29
30 An AML NameString is not NULL terminated and is is only composed of
31 4 chars long NameSegs.
32
33 @param [in] NameOpNode NameOp object node defining a variable.
34 Must have an AML_NAME_OP/0 OpCode/SubOpCode.
35 NameOp object nodes are defined in ASL
36 using the "Name ()" function.
37 @param [in] AslName ASL NameString to compare the NameOp's name with.
38 Must be NULL terminated.
39
40 @retval TRUE If the AslName and the AmlName defined by the NameOp node
41 are similar.
42 @retval FALSE Otherwise.
43 **/
44 BOOLEAN
45 EFIAPI
46 AmlNameOpCompareName (
47 IN AML_OBJECT_NODE_HANDLE NameOpNode,
48 IN CHAR8 *AslName
49 )
50 {
51 EFI_STATUS Status;
52 AML_DATA_NODE_HANDLE NameDataNode;
53
54 CHAR8 *AmlName;
55 UINT32 AmlNameSize;
56
57 BOOLEAN RetVal;
58
59 if ((NameOpNode == NULL) ||
60 (AmlGetNodeType ((AML_NODE_HANDLE)NameOpNode) != EAmlNodeObject) ||
61 (!AmlNodeHasOpCode (NameOpNode, AML_NAME_OP, 0)) ||
62 (AslName == NULL))
63 {
64 ASSERT (0);
65 return FALSE;
66 }
67
68 // Get the NameOp name, being in a data node
69 // which is the first fixed argument (i.e. index 0).
70 NameDataNode = (AML_DATA_NODE_HANDLE)AmlGetFixedArgument (
71 NameOpNode,
72 EAmlParseIndexTerm0
73 );
74 if ((NameDataNode == NULL) ||
75 (AmlGetNodeType ((AML_NODE_HANDLE)NameDataNode) != EAmlNodeData) ||
76 (!AmlNodeHasDataType (NameDataNode, EAmlNodeDataTypeNameString)))
77 {
78 ASSERT (0);
79 return FALSE;
80 }
81
82 // Get the size of the name.
83 Status = AmlGetDataNodeBuffer (NameDataNode, NULL, &AmlNameSize);
84 if (EFI_ERROR (Status)) {
85 ASSERT (0);
86 return FALSE;
87 }
88
89 // Allocate memory to fetch the name.
90 AmlName = AllocateZeroPool (AmlNameSize);
91 if (AmlName == NULL) {
92 ASSERT (0);
93 return FALSE;
94 }
95
96 // Fetch the name.
97 Status = AmlGetDataNodeBuffer (NameDataNode, (UINT8 *)AmlName, &AmlNameSize);
98 if (EFI_ERROR (Status)) {
99 FreePool (AmlName);
100 ASSERT (0);
101 return FALSE;
102 }
103
104 // Compare the input AslName and the AmlName stored in the NameOp node.
105 RetVal = CompareAmlWithAslNameString (AmlName, AslName);
106
107 // Free the string buffer.
108 FreePool (AmlName);
109 return RetVal;
110 }
111
112 /** Check whether ObjectNode has the input OpCode/SubOpcode couple.
113
114 @param [in] ObjectNode Pointer to an object node.
115 @param [in] OpCode OpCode to check
116 @param [in] SubOpCode SubOpCode to check
117
118 @retval TRUE The node is an object node and
119 the Opcode and SubOpCode match.
120 @retval FALSE Otherwise.
121 **/
122 BOOLEAN
123 EFIAPI
124 AmlNodeHasOpCode (
125 IN AML_OBJECT_NODE_HANDLE ObjectNode,
126 IN UINT8 OpCode,
127 IN UINT8 SubOpCode
128 )
129 {
130 EFI_STATUS Status;
131 UINT8 NodeOpCode;
132 UINT8 NodeSubOpCode;
133
134 // Get the Node information.
135 Status = AmlGetObjectNodeInfo (
136 ObjectNode,
137 &NodeOpCode,
138 &NodeSubOpCode,
139 NULL,
140 NULL
141 );
142 if (EFI_ERROR (Status)) {
143 ASSERT (0);
144 return FALSE;
145 }
146
147 // Check the OpCode and SubOpCode.
148 if ((OpCode != NodeOpCode) ||
149 (SubOpCode != NodeSubOpCode))
150 {
151 return FALSE;
152 }
153
154 return TRUE;
155 }
156
157 /** Check whether DataNode has the input DataType.
158
159 @param [in] DataNode Pointer to a data node.
160 @param [in] DataType DataType to check.
161
162 @retval TRUE The node is a data node and
163 the DataType match.
164 @retval FALSE Otherwise.
165 **/
166 BOOLEAN
167 EFIAPI
168 AmlNodeHasDataType (
169 IN AML_DATA_NODE_HANDLE DataNode,
170 IN EAML_NODE_DATA_TYPE DataType
171 )
172 {
173 EFI_STATUS Status;
174 EAML_NODE_DATA_TYPE NodeDataType;
175
176 // Get the data type.
177 Status = AmlGetNodeDataType (DataNode, &NodeDataType);
178 if (EFI_ERROR (Status)) {
179 ASSERT (0);
180 return FALSE;
181 }
182
183 // Check the data type.
184 if (NodeDataType != DataType) {
185 return FALSE;
186 }
187
188 return TRUE;
189 }
190
191 /** Check whether RdNode has the input RdDataType.
192
193 @param [in] RdNode Pointer to a data node.
194 @param [in] RdDataType DataType to check.
195
196 @retval TRUE The node is a Resource Data node and
197 the RdDataType match.
198 @retval FALSE Otherwise.
199 **/
200 BOOLEAN
201 EFIAPI
202 AmlNodeHasRdDataType (
203 IN AML_DATA_NODE_HANDLE RdNode,
204 IN AML_RD_HEADER RdDataType
205 )
206 {
207 EFI_STATUS Status;
208 AML_RD_HEADER NodeRdDataType;
209
210 // Get the resource data type.
211 Status = AmlGetResourceDataType (
212 RdNode,
213 &NodeRdDataType
214 );
215 if (EFI_ERROR (Status)) {
216 ASSERT (0);
217 return FALSE;
218 }
219
220 // Check the RdDataType.
221 return AmlRdCompareDescId (&NodeRdDataType, RdDataType);
222 }