Sync ACPICA with Intel's version 20150717.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / namespace / nsnames.c
1 /*******************************************************************************
2  *
3  * Module Name: nsnames - Name manipulation and search
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, 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
81     return_PTR (NameBuffer);
82 }
83
84
85 /*******************************************************************************
86  *
87  * FUNCTION:    AcpiNsGetPathnameLength
88  *
89  * PARAMETERS:  Node        - Namespace node
90  *
91  * RETURN:      Length of path, including prefix
92  *
93  * DESCRIPTION: Get the length of the pathname string for this node
94  *
95  ******************************************************************************/
96
97 ACPI_SIZE
98 AcpiNsGetPathnameLength (
99     ACPI_NAMESPACE_NODE     *Node)
100 {
101     ACPI_SIZE               Size;
102
103
104     ACPI_FUNCTION_ENTRY ();
105
106
107     Size = AcpiNsBuildNormalizedPath (Node, NULL, 0, FALSE);
108
109     return (Size);
110 }
111
112
113 /*******************************************************************************
114  *
115  * FUNCTION:    AcpiNsHandleToPathname
116  *
117  * PARAMETERS:  TargetHandle            - Handle of named object whose name is
118  *                                        to be found
119  *              Buffer                  - Where the pathname is returned
120  *              NoTrailing              - Remove trailing '_' for each name
121  *                                        segment
122  *
123  * RETURN:      Status, Buffer is filled with pathname if status is AE_OK
124  *
125  * DESCRIPTION: Build and return a full namespace pathname
126  *
127  ******************************************************************************/
128
129 ACPI_STATUS
130 AcpiNsHandleToPathname (
131     ACPI_HANDLE             TargetHandle,
132     ACPI_BUFFER             *Buffer,
133     BOOLEAN                 NoTrailing)
134 {
135     ACPI_STATUS             Status;
136     ACPI_NAMESPACE_NODE     *Node;
137     ACPI_SIZE               RequiredSize;
138
139
140     ACPI_FUNCTION_TRACE_PTR (NsHandleToPathname, TargetHandle);
141
142
143     Node = AcpiNsValidateHandle (TargetHandle);
144     if (!Node)
145     {
146         return_ACPI_STATUS (AE_BAD_PARAMETER);
147     }
148
149     /* Determine size required for the caller buffer */
150
151     RequiredSize = AcpiNsBuildNormalizedPath (Node, NULL, 0, NoTrailing);
152     if (!RequiredSize)
153     {
154         return_ACPI_STATUS (AE_BAD_PARAMETER);
155     }
156
157     /* Validate/Allocate/Clear caller buffer */
158
159     Status = AcpiUtInitializeBuffer (Buffer, RequiredSize);
160     if (ACPI_FAILURE (Status))
161     {
162         return_ACPI_STATUS (Status);
163     }
164
165     /* Build the path in the caller buffer */
166
167     (void) AcpiNsBuildNormalizedPath (Node, Buffer->Pointer,
168             RequiredSize, NoTrailing);
169     if (ACPI_FAILURE (Status))
170     {
171         return_ACPI_STATUS (Status);
172     }
173
174     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X]\n",
175         (char *) Buffer->Pointer, (UINT32) RequiredSize));
176     return_ACPI_STATUS (AE_OK);
177 }
178
179
180 /*******************************************************************************
181  *
182  * FUNCTION:    AcpiNsBuildNormalizedPath
183  *
184  * PARAMETERS:  Node        - Namespace node
185  *              FullPath    - Where the path name is returned
186  *              PathSize    - Size of returned path name buffer
187  *              NoTrailing  - Remove trailing '_' from each name segment
188  *
189  * RETURN:      Return 1 if the AML path is empty, otherwise returning (length
190  *              of pathname + 1) which means the 'FullPath' contains a trailing
191  *              null.
192  *
193  * DESCRIPTION: Build and return a full namespace pathname.
194  *              Note that if the size of 'FullPath' isn't large enough to
195  *              contain the namespace node's path name, the actual required
196  *              buffer length is returned, and it should be greater than
197  *              'PathSize'. So callers are able to check the returning value
198  *              to determine the buffer size of 'FullPath'.
199  *
200  ******************************************************************************/
201
202 UINT32
203 AcpiNsBuildNormalizedPath (
204     ACPI_NAMESPACE_NODE     *Node,
205     char                    *FullPath,
206     UINT32                  PathSize,
207     BOOLEAN                 NoTrailing)
208 {
209     UINT32                  Length = 0, i;
210     char                    Name[ACPI_NAME_SIZE];
211     BOOLEAN                 DoNoTrailing;
212     char                    c, *Left, *Right;
213     ACPI_NAMESPACE_NODE     *NextNode;
214
215
216     ACPI_FUNCTION_TRACE_PTR (NsBuildNormalizedPath, Node);
217
218
219 #define ACPI_PATH_PUT8(Path, Size, Byte, Length)    \
220     do {                                            \
221         if ((Length) < (Size))                      \
222         {                                           \
223             (Path)[(Length)] = (Byte);              \
224         }                                           \
225         (Length)++;                                 \
226     } while (0)
227
228     /*
229      * Make sure the PathSize is correct, so that we don't need to
230      * validate both FullPath and PathSize.
231      */
232     if (!FullPath)
233     {
234         PathSize = 0;
235     }
236
237     if (!Node)
238     {
239         goto BuildTrailingNull;
240     }
241
242     NextNode = Node;
243     while (NextNode && NextNode != AcpiGbl_RootNode)
244     {
245         if (NextNode != Node)
246         {
247             ACPI_PATH_PUT8(FullPath, PathSize, AML_DUAL_NAME_PREFIX, Length);
248         }
249         ACPI_MOVE_32_TO_32 (Name, &NextNode->Name);
250         DoNoTrailing = NoTrailing;
251         for (i = 0; i < 4; i++)
252         {
253             c = Name[4-i-1];
254             if (DoNoTrailing && c != '_')
255             {
256                 DoNoTrailing = FALSE;
257             }
258             if (!DoNoTrailing)
259             {
260                 ACPI_PATH_PUT8(FullPath, PathSize, c, Length);
261             }
262         }
263         NextNode = NextNode->Parent;
264     }
265     ACPI_PATH_PUT8(FullPath, PathSize, AML_ROOT_PREFIX, Length);
266
267     /* Reverse the path string */
268
269     if (Length <= PathSize)
270     {
271         Left = FullPath;
272         Right = FullPath+Length-1;
273         while (Left < Right)
274         {
275             c = *Left;
276             *Left++ = *Right;
277             *Right-- = c;
278         }
279     }
280
281     /* Append the trailing null */
282
283 BuildTrailingNull:
284     ACPI_PATH_PUT8(FullPath, PathSize, '\0', Length);
285
286 #undef ACPI_PATH_PUT8
287
288     return_UINT32 (Length);
289 }
290
291
292 /*******************************************************************************
293  *
294  * FUNCTION:    AcpiNsGetNormalizedPathname
295  *
296  * PARAMETERS:  Node            - Namespace node whose pathname is needed
297  *              NoTrailing      - Remove trailing '_' from each name segment
298  *
299  * RETURN:      Pointer to storage containing the fully qualified name of
300  *              the node, In external format (name segments separated by path
301  *              separators.)
302  *
303  * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
304  *              for error and debug statements. All trailing '_' will be
305  *              removed from the full pathname if 'NoTrailing' is specified..
306  *
307  ******************************************************************************/
308
309 char *
310 AcpiNsGetNormalizedPathname (
311     ACPI_NAMESPACE_NODE     *Node,
312     BOOLEAN                 NoTrailing)
313 {
314     char                    *NameBuffer;
315     ACPI_SIZE               Size;
316
317
318     ACPI_FUNCTION_TRACE_PTR (NsGetNormalizedPathname, Node);
319
320
321     /* Calculate required buffer size based on depth below root */
322
323     Size = AcpiNsBuildNormalizedPath (Node, NULL, 0, NoTrailing);
324     if (!Size)
325     {
326         return_PTR (NULL);
327     }
328
329     /* Allocate a buffer to be returned to caller */
330
331     NameBuffer = ACPI_ALLOCATE_ZEROED (Size);
332     if (!NameBuffer)
333     {
334         ACPI_ERROR ((AE_INFO, "Could not allocate %u bytes", (UINT32) Size));
335         return_PTR (NULL);
336     }
337
338     /* Build the path in the allocated buffer */
339
340     (void) AcpiNsBuildNormalizedPath (Node, NameBuffer, Size, NoTrailing);
341
342     return_PTR (NameBuffer);
343 }