1 /*******************************************************************************
3 * Module Name: nsnames - Name manipulation and search
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2017, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/amlcode.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("nsnames")
54 /*******************************************************************************
56 * FUNCTION: AcpiNsGetExternalPathname
58 * PARAMETERS: Node - Namespace node whose pathname is needed
60 * RETURN: Pointer to storage containing the fully qualified name of
61 * the node, In external format (name segments separated by path
64 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
65 * for error and debug statements.
67 ******************************************************************************/
70 AcpiNsGetExternalPathname (
71 ACPI_NAMESPACE_NODE *Node)
76 ACPI_FUNCTION_TRACE_PTR (NsGetExternalPathname, Node);
79 NameBuffer = AcpiNsGetNormalizedPathname (Node, FALSE);
80 return_PTR (NameBuffer);
84 /*******************************************************************************
86 * FUNCTION: AcpiNsGetPathnameLength
88 * PARAMETERS: Node - Namespace node
90 * RETURN: Length of path, including prefix
92 * DESCRIPTION: Get the length of the pathname string for this node
94 ******************************************************************************/
97 AcpiNsGetPathnameLength (
98 ACPI_NAMESPACE_NODE *Node)
103 ACPI_FUNCTION_ENTRY ();
106 Size = AcpiNsBuildNormalizedPath (Node, NULL, 0, FALSE);
111 /*******************************************************************************
113 * FUNCTION: AcpiNsHandleToName
115 * PARAMETERS: TargetHandle - Handle of named object whose name is
117 * Buffer - Where the name is returned
119 * RETURN: Status, Buffer is filled with name if status is AE_OK
121 * DESCRIPTION: Build and return a full namespace name
123 ******************************************************************************/
127 ACPI_HANDLE TargetHandle,
131 ACPI_NAMESPACE_NODE *Node;
132 const char *NodeName;
135 ACPI_FUNCTION_TRACE_PTR (NsHandleToName, TargetHandle);
138 Node = AcpiNsValidateHandle (TargetHandle);
141 return_ACPI_STATUS (AE_BAD_PARAMETER);
144 /* Validate/Allocate/Clear caller buffer */
146 Status = AcpiUtInitializeBuffer (Buffer, ACPI_PATH_SEGMENT_LENGTH);
147 if (ACPI_FAILURE (Status))
149 return_ACPI_STATUS (Status);
152 /* Just copy the ACPI name from the Node and zero terminate it */
154 NodeName = AcpiUtGetNodeName (Node);
155 ACPI_MOVE_NAME (Buffer->Pointer, NodeName);
156 ((char *) Buffer->Pointer) [ACPI_NAME_SIZE] = 0;
158 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%4.4s\n", (char *) Buffer->Pointer));
159 return_ACPI_STATUS (AE_OK);
163 /*******************************************************************************
165 * FUNCTION: AcpiNsHandleToPathname
167 * PARAMETERS: TargetHandle - Handle of named object whose name is
169 * Buffer - Where the pathname is returned
170 * NoTrailing - Remove trailing '_' for each name
173 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
175 * DESCRIPTION: Build and return a full namespace pathname
177 ******************************************************************************/
180 AcpiNsHandleToPathname (
181 ACPI_HANDLE TargetHandle,
186 ACPI_NAMESPACE_NODE *Node;
187 ACPI_SIZE RequiredSize;
190 ACPI_FUNCTION_TRACE_PTR (NsHandleToPathname, TargetHandle);
193 Node = AcpiNsValidateHandle (TargetHandle);
196 return_ACPI_STATUS (AE_BAD_PARAMETER);
199 /* Determine size required for the caller buffer */
201 RequiredSize = AcpiNsBuildNormalizedPath (Node, NULL, 0, NoTrailing);
204 return_ACPI_STATUS (AE_BAD_PARAMETER);
207 /* Validate/Allocate/Clear caller buffer */
209 Status = AcpiUtInitializeBuffer (Buffer, RequiredSize);
210 if (ACPI_FAILURE (Status))
212 return_ACPI_STATUS (Status);
215 /* Build the path in the caller buffer */
217 (void) AcpiNsBuildNormalizedPath (Node, Buffer->Pointer,
218 RequiredSize, NoTrailing);
219 if (ACPI_FAILURE (Status))
221 return_ACPI_STATUS (Status);
224 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X]\n",
225 (char *) Buffer->Pointer, (UINT32) RequiredSize));
226 return_ACPI_STATUS (AE_OK);
230 /*******************************************************************************
232 * FUNCTION: AcpiNsBuildNormalizedPath
234 * PARAMETERS: Node - Namespace node
235 * FullPath - Where the path name is returned
236 * PathSize - Size of returned path name buffer
237 * NoTrailing - Remove trailing '_' from each name segment
239 * RETURN: Return 1 if the AML path is empty, otherwise returning (length
240 * of pathname + 1) which means the 'FullPath' contains a trailing
243 * DESCRIPTION: Build and return a full namespace pathname.
244 * Note that if the size of 'FullPath' isn't large enough to
245 * contain the namespace node's path name, the actual required
246 * buffer length is returned, and it should be greater than
247 * 'PathSize'. So callers are able to check the returning value
248 * to determine the buffer size of 'FullPath'.
250 ******************************************************************************/
253 AcpiNsBuildNormalizedPath (
254 ACPI_NAMESPACE_NODE *Node,
259 UINT32 Length = 0, i;
260 char Name[ACPI_NAME_SIZE];
261 BOOLEAN DoNoTrailing;
262 char c, *Left, *Right;
263 ACPI_NAMESPACE_NODE *NextNode;
266 ACPI_FUNCTION_TRACE_PTR (NsBuildNormalizedPath, Node);
269 #define ACPI_PATH_PUT8(Path, Size, Byte, Length) \
271 if ((Length) < (Size)) \
273 (Path)[(Length)] = (Byte); \
279 * Make sure the PathSize is correct, so that we don't need to
280 * validate both FullPath and PathSize.
289 goto BuildTrailingNull;
293 while (NextNode && NextNode != AcpiGbl_RootNode)
295 if (NextNode != Node)
297 ACPI_PATH_PUT8(FullPath, PathSize, AML_DUAL_NAME_PREFIX, Length);
300 ACPI_MOVE_32_TO_32 (Name, &NextNode->Name);
301 DoNoTrailing = NoTrailing;
302 for (i = 0; i < 4; i++)
305 if (DoNoTrailing && c != '_')
307 DoNoTrailing = FALSE;
311 ACPI_PATH_PUT8(FullPath, PathSize, c, Length);
315 NextNode = NextNode->Parent;
318 ACPI_PATH_PUT8(FullPath, PathSize, AML_ROOT_PREFIX, Length);
320 /* Reverse the path string */
322 if (Length <= PathSize)
325 Right = FullPath+Length - 1;
335 /* Append the trailing null */
338 ACPI_PATH_PUT8 (FullPath, PathSize, '\0', Length);
340 #undef ACPI_PATH_PUT8
342 return_UINT32 (Length);
346 /*******************************************************************************
348 * FUNCTION: AcpiNsGetNormalizedPathname
350 * PARAMETERS: Node - Namespace node whose pathname is needed
351 * NoTrailing - Remove trailing '_' from each name segment
353 * RETURN: Pointer to storage containing the fully qualified name of
354 * the node, In external format (name segments separated by path
357 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
358 * for error and debug statements. All trailing '_' will be
359 * removed from the full pathname if 'NoTrailing' is specified..
361 ******************************************************************************/
364 AcpiNsGetNormalizedPathname (
365 ACPI_NAMESPACE_NODE *Node,
372 ACPI_FUNCTION_TRACE_PTR (NsGetNormalizedPathname, Node);
375 /* Calculate required buffer size based on depth below root */
377 Size = AcpiNsBuildNormalizedPath (Node, NULL, 0, NoTrailing);
383 /* Allocate a buffer to be returned to caller */
385 NameBuffer = ACPI_ALLOCATE_ZEROED (Size);
388 ACPI_ERROR ((AE_INFO,
389 "Could not allocate %u bytes", (UINT32) Size));
393 /* Build the path in the allocated buffer */
395 (void) AcpiNsBuildNormalizedPath (Node, NameBuffer, Size, NoTrailing);
397 return_PTR (NameBuffer);