0ca3514b8e51ea1f466df0eb888bcafacd6154b3
[reactos.git] / reactos / drivers / bus / acpi / acpica / namespace / nsnames.c
1 /*******************************************************************************
2 *
3 * Module Name: nsnames - Name manipulation and search
4 *
5 ******************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2017, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "amlcode.h"
47 #include "acnamesp.h"
48
49
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("nsnames")
52
53
54 /*******************************************************************************
55 *
56 * FUNCTION: AcpiNsGetExternalPathname
57 *
58 * PARAMETERS: Node - Namespace node whose pathname is needed
59 *
60 * RETURN: Pointer to storage containing the fully qualified name of
61 * the node, In external format (name segments separated by path
62 * separators.)
63 *
64 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
65 * for error and debug statements.
66 *
67 ******************************************************************************/
68
69 char *
70 AcpiNsGetExternalPathname (
71 ACPI_NAMESPACE_NODE *Node)
72 {
73 char *NameBuffer;
74
75
76 ACPI_FUNCTION_TRACE_PTR (NsGetExternalPathname, Node);
77
78
79 NameBuffer = AcpiNsGetNormalizedPathname (Node, FALSE);
80 return_PTR (NameBuffer);
81 }
82
83
84 /*******************************************************************************
85 *
86 * FUNCTION: AcpiNsGetPathnameLength
87 *
88 * PARAMETERS: Node - Namespace node
89 *
90 * RETURN: Length of path, including prefix
91 *
92 * DESCRIPTION: Get the length of the pathname string for this node
93 *
94 ******************************************************************************/
95
96 ACPI_SIZE
97 AcpiNsGetPathnameLength (
98 ACPI_NAMESPACE_NODE *Node)
99 {
100 ACPI_SIZE Size;
101
102
103 ACPI_FUNCTION_ENTRY ();
104
105
106 Size = AcpiNsBuildNormalizedPath (Node, NULL, 0, FALSE);
107 return (Size);
108 }
109
110
111 /*******************************************************************************
112 *
113 * FUNCTION: AcpiNsHandleToName
114 *
115 * PARAMETERS: TargetHandle - Handle of named object whose name is
116 * to be found
117 * Buffer - Where the name is returned
118 *
119 * RETURN: Status, Buffer is filled with name if status is AE_OK
120 *
121 * DESCRIPTION: Build and return a full namespace name
122 *
123 ******************************************************************************/
124
125 ACPI_STATUS
126 AcpiNsHandleToName (
127 ACPI_HANDLE TargetHandle,
128 ACPI_BUFFER *Buffer)
129 {
130 ACPI_STATUS Status;
131 ACPI_NAMESPACE_NODE *Node;
132 const char *NodeName;
133
134
135 ACPI_FUNCTION_TRACE_PTR (NsHandleToName, TargetHandle);
136
137
138 Node = AcpiNsValidateHandle (TargetHandle);
139 if (!Node)
140 {
141 return_ACPI_STATUS (AE_BAD_PARAMETER);
142 }
143
144 /* Validate/Allocate/Clear caller buffer */
145
146 Status = AcpiUtInitializeBuffer (Buffer, ACPI_PATH_SEGMENT_LENGTH);
147 if (ACPI_FAILURE (Status))
148 {
149 return_ACPI_STATUS (Status);
150 }
151
152 /* Just copy the ACPI name from the Node and zero terminate it */
153
154 NodeName = AcpiUtGetNodeName (Node);
155 ACPI_MOVE_NAME (Buffer->Pointer, NodeName);
156 ((char *) Buffer->Pointer) [ACPI_NAME_SIZE] = 0;
157
158 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%4.4s\n", (char *) Buffer->Pointer));
159 return_ACPI_STATUS (AE_OK);
160 }
161
162
163 /*******************************************************************************
164 *
165 * FUNCTION: AcpiNsHandleToPathname
166 *
167 * PARAMETERS: TargetHandle - Handle of named object whose name is
168 * to be found
169 * Buffer - Where the pathname is returned
170 * NoTrailing - Remove trailing '_' for each name
171 * segment
172 *
173 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
174 *
175 * DESCRIPTION: Build and return a full namespace pathname
176 *
177 ******************************************************************************/
178
179 ACPI_STATUS
180 AcpiNsHandleToPathname (
181 ACPI_HANDLE TargetHandle,
182 ACPI_BUFFER *Buffer,
183 BOOLEAN NoTrailing)
184 {
185 ACPI_STATUS Status;
186 ACPI_NAMESPACE_NODE *Node;
187 ACPI_SIZE RequiredSize;
188
189
190 ACPI_FUNCTION_TRACE_PTR (NsHandleToPathname, TargetHandle);
191
192
193 Node = AcpiNsValidateHandle (TargetHandle);
194 if (!Node)
195 {
196 return_ACPI_STATUS (AE_BAD_PARAMETER);
197 }
198
199 /* Determine size required for the caller buffer */
200
201 RequiredSize = AcpiNsBuildNormalizedPath (Node, NULL, 0, NoTrailing);
202 if (!RequiredSize)
203 {
204 return_ACPI_STATUS (AE_BAD_PARAMETER);
205 }
206
207 /* Validate/Allocate/Clear caller buffer */
208
209 Status = AcpiUtInitializeBuffer (Buffer, RequiredSize);
210 if (ACPI_FAILURE (Status))
211 {
212 return_ACPI_STATUS (Status);
213 }
214
215 /* Build the path in the caller buffer */
216
217 (void) AcpiNsBuildNormalizedPath (Node, Buffer->Pointer,
218 RequiredSize, NoTrailing);
219
220 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X]\n",
221 (char *) Buffer->Pointer, (UINT32) RequiredSize));
222 return_ACPI_STATUS (AE_OK);
223 }
224
225
226 /*******************************************************************************
227 *
228 * FUNCTION: AcpiNsBuildNormalizedPath
229 *
230 * PARAMETERS: Node - Namespace node
231 * FullPath - Where the path name is returned
232 * PathSize - Size of returned path name buffer
233 * NoTrailing - Remove trailing '_' from each name segment
234 *
235 * RETURN: Return 1 if the AML path is empty, otherwise returning (length
236 * of pathname + 1) which means the 'FullPath' contains a trailing
237 * null.
238 *
239 * DESCRIPTION: Build and return a full namespace pathname.
240 * Note that if the size of 'FullPath' isn't large enough to
241 * contain the namespace node's path name, the actual required
242 * buffer length is returned, and it should be greater than
243 * 'PathSize'. So callers are able to check the returning value
244 * to determine the buffer size of 'FullPath'.
245 *
246 ******************************************************************************/
247
248 UINT32
249 AcpiNsBuildNormalizedPath (
250 ACPI_NAMESPACE_NODE *Node,
251 char *FullPath,
252 UINT32 PathSize,
253 BOOLEAN NoTrailing)
254 {
255 UINT32 Length = 0, i;
256 char Name[ACPI_NAME_SIZE];
257 BOOLEAN DoNoTrailing;
258 char c, *Left, *Right;
259 ACPI_NAMESPACE_NODE *NextNode;
260
261
262 ACPI_FUNCTION_TRACE_PTR (NsBuildNormalizedPath, Node);
263
264
265 #define ACPI_PATH_PUT8(Path, Size, Byte, Length) \
266 do { \
267 if ((Length) < (Size)) \
268 { \
269 (Path)[(Length)] = (Byte); \
270 } \
271 (Length)++; \
272 } while (0)
273
274 /*
275 * Make sure the PathSize is correct, so that we don't need to
276 * validate both FullPath and PathSize.
277 */
278 if (!FullPath)
279 {
280 PathSize = 0;
281 }
282
283 if (!Node)
284 {
285 goto BuildTrailingNull;
286 }
287
288 NextNode = Node;
289 while (NextNode && NextNode != AcpiGbl_RootNode)
290 {
291 if (NextNode != Node)
292 {
293 ACPI_PATH_PUT8(FullPath, PathSize, AML_DUAL_NAME_PREFIX, Length);
294 }
295
296 ACPI_MOVE_32_TO_32 (Name, &NextNode->Name);
297 DoNoTrailing = NoTrailing;
298 for (i = 0; i < 4; i++)
299 {
300 c = Name[4-i-1];
301 if (DoNoTrailing && c != '_')
302 {
303 DoNoTrailing = FALSE;
304 }
305 if (!DoNoTrailing)
306 {
307 ACPI_PATH_PUT8(FullPath, PathSize, c, Length);
308 }
309 }
310
311 NextNode = NextNode->Parent;
312 }
313
314 ACPI_PATH_PUT8(FullPath, PathSize, AML_ROOT_PREFIX, Length);
315
316 /* Reverse the path string */
317
318 if (Length <= PathSize)
319 {
320 Left = FullPath;
321 Right = FullPath+Length - 1;
322
323 while (Left < Right)
324 {
325 c = *Left;
326 *Left++ = *Right;
327 *Right-- = c;
328 }
329 }
330
331 /* Append the trailing null */
332
333 BuildTrailingNull:
334 ACPI_PATH_PUT8 (FullPath, PathSize, '\0', Length);
335
336 #undef ACPI_PATH_PUT8
337
338 return_UINT32 (Length);
339 }
340
341
342 /*******************************************************************************
343 *
344 * FUNCTION: AcpiNsGetNormalizedPathname
345 *
346 * PARAMETERS: Node - Namespace node whose pathname is needed
347 * NoTrailing - Remove trailing '_' from each name segment
348 *
349 * RETURN: Pointer to storage containing the fully qualified name of
350 * the node, In external format (name segments separated by path
351 * separators.)
352 *
353 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
354 * for error and debug statements. All trailing '_' will be
355 * removed from the full pathname if 'NoTrailing' is specified..
356 *
357 ******************************************************************************/
358
359 char *
360 AcpiNsGetNormalizedPathname (
361 ACPI_NAMESPACE_NODE *Node,
362 BOOLEAN NoTrailing)
363 {
364 char *NameBuffer;
365 ACPI_SIZE Size;
366
367
368 ACPI_FUNCTION_TRACE_PTR (NsGetNormalizedPathname, Node);
369
370
371 /* Calculate required buffer size based on depth below root */
372
373 Size = AcpiNsBuildNormalizedPath (Node, NULL, 0, NoTrailing);
374 if (!Size)
375 {
376 return_PTR (NULL);
377 }
378
379 /* Allocate a buffer to be returned to caller */
380
381 NameBuffer = ACPI_ALLOCATE_ZEROED (Size);
382 if (!NameBuffer)
383 {
384 ACPI_ERROR ((AE_INFO,
385 "Could not allocate %u bytes", (UINT32) Size));
386 return_PTR (NULL);
387 }
388
389 /* Build the path in the allocated buffer */
390
391 (void) AcpiNsBuildNormalizedPath (Node, NameBuffer, Size, NoTrailing);
392
393 return_PTR (NameBuffer);
394 }