xref: /openbmc/linux/drivers/acpi/acpica/nsnames.c (revision 9a1ae804)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /*******************************************************************************
395b482a8SLen Brown  *
495b482a8SLen Brown  * Module Name: nsnames - Name manipulation and search
595b482a8SLen Brown  *
695b482a8SLen Brown  ******************************************************************************/
795b482a8SLen Brown 
895b482a8SLen Brown #include <acpi/acpi.h>
9e2f7a777SLen Brown #include "accommon.h"
10e2f7a777SLen Brown #include "amlcode.h"
11e2f7a777SLen Brown #include "acnamesp.h"
1295b482a8SLen Brown 
1395b482a8SLen Brown #define _COMPONENT          ACPI_NAMESPACE
1495b482a8SLen Brown ACPI_MODULE_NAME("nsnames")
1595b482a8SLen Brown 
1695b482a8SLen Brown /*******************************************************************************
1795b482a8SLen Brown  *
1895b482a8SLen Brown  * FUNCTION:    acpi_ns_get_external_pathname
1995b482a8SLen Brown  *
20ba494beeSBob Moore  * PARAMETERS:  node            - Namespace node whose pathname is needed
2195b482a8SLen Brown  *
2295b482a8SLen Brown  * RETURN:      Pointer to storage containing the fully qualified name of
2395b482a8SLen Brown  *              the node, In external format (name segments separated by path
2495b482a8SLen Brown  *              separators.)
2595b482a8SLen Brown  *
2675c8044fSLv Zheng  * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
2775c8044fSLv Zheng  *              for error and debug statements.
2895b482a8SLen Brown  *
2995b482a8SLen Brown  ******************************************************************************/
acpi_ns_get_external_pathname(struct acpi_namespace_node * node)3095b482a8SLen Brown char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
3195b482a8SLen Brown {
3295b482a8SLen Brown 	char *name_buffer;
3395b482a8SLen Brown 
3495b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
3595b482a8SLen Brown 
36d1e7ffe5SLv Zheng 	name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
3795b482a8SLen Brown 	return_PTR(name_buffer);
3895b482a8SLen Brown }
3995b482a8SLen Brown 
4095b482a8SLen Brown /*******************************************************************************
4195b482a8SLen Brown  *
4295b482a8SLen Brown  * FUNCTION:    acpi_ns_get_pathname_length
4395b482a8SLen Brown  *
44ba494beeSBob Moore  * PARAMETERS:  node        - Namespace node
4595b482a8SLen Brown  *
4695b482a8SLen Brown  * RETURN:      Length of path, including prefix
4795b482a8SLen Brown  *
4895b482a8SLen Brown  * DESCRIPTION: Get the length of the pathname string for this node
4995b482a8SLen Brown  *
5095b482a8SLen Brown  ******************************************************************************/
5195b482a8SLen Brown 
acpi_ns_get_pathname_length(struct acpi_namespace_node * node)5295b482a8SLen Brown acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
5395b482a8SLen Brown {
5495b482a8SLen Brown 	acpi_size size;
5595b482a8SLen Brown 
56a62a7117SBob Moore 	/* Validate the Node */
57a62a7117SBob Moore 
58a62a7117SBob Moore 	if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
59a62a7117SBob Moore 		ACPI_ERROR((AE_INFO,
60a62a7117SBob Moore 			    "Invalid/cached reference target node: %p, descriptor type %d",
61a62a7117SBob Moore 			    node, ACPI_GET_DESCRIPTOR_TYPE(node)));
62a62a7117SBob Moore 		return (0);
63a62a7117SBob Moore 	}
6495b482a8SLen Brown 
65d1e7ffe5SLv Zheng 	size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
66d1e7ffe5SLv Zheng 	return (size);
6795b482a8SLen Brown }
6895b482a8SLen Brown 
6995b482a8SLen Brown /*******************************************************************************
7095b482a8SLen Brown  *
71523db19bSLv Zheng  * FUNCTION:    acpi_ns_handle_to_name
72523db19bSLv Zheng  *
73523db19bSLv Zheng  * PARAMETERS:  target_handle           - Handle of named object whose name is
74523db19bSLv Zheng  *                                        to be found
75523db19bSLv Zheng  *              buffer                  - Where the name is returned
76523db19bSLv Zheng  *
77523db19bSLv Zheng  * RETURN:      Status, Buffer is filled with name if status is AE_OK
78523db19bSLv Zheng  *
79523db19bSLv Zheng  * DESCRIPTION: Build and return a full namespace name
80523db19bSLv Zheng  *
81523db19bSLv Zheng  ******************************************************************************/
82523db19bSLv Zheng 
83523db19bSLv Zheng acpi_status
acpi_ns_handle_to_name(acpi_handle target_handle,struct acpi_buffer * buffer)84523db19bSLv Zheng acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
85523db19bSLv Zheng {
86523db19bSLv Zheng 	acpi_status status;
87523db19bSLv Zheng 	struct acpi_namespace_node *node;
88523db19bSLv Zheng 	const char *node_name;
89523db19bSLv Zheng 
90523db19bSLv Zheng 	ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
91523db19bSLv Zheng 
92523db19bSLv Zheng 	node = acpi_ns_validate_handle(target_handle);
93523db19bSLv Zheng 	if (!node) {
94523db19bSLv Zheng 		return_ACPI_STATUS(AE_BAD_PARAMETER);
95523db19bSLv Zheng 	}
96523db19bSLv Zheng 
97523db19bSLv Zheng 	/* Validate/Allocate/Clear caller buffer */
98523db19bSLv Zheng 
99523db19bSLv Zheng 	status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
100523db19bSLv Zheng 	if (ACPI_FAILURE(status)) {
101523db19bSLv Zheng 		return_ACPI_STATUS(status);
102523db19bSLv Zheng 	}
103523db19bSLv Zheng 
104523db19bSLv Zheng 	/* Just copy the ACPI name from the Node and zero terminate it */
105523db19bSLv Zheng 
106523db19bSLv Zheng 	node_name = acpi_ut_get_node_name(node);
107a3ce7a8eSBob Moore 	ACPI_COPY_NAMESEG(buffer->pointer, node_name);
10832786755SBob Moore 	((char *)buffer->pointer)[ACPI_NAMESEG_SIZE] = 0;
109523db19bSLv Zheng 
110523db19bSLv Zheng 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
111523db19bSLv Zheng 	return_ACPI_STATUS(AE_OK);
112523db19bSLv Zheng }
113523db19bSLv Zheng 
114523db19bSLv Zheng /*******************************************************************************
115523db19bSLv Zheng  *
11695b482a8SLen Brown  * FUNCTION:    acpi_ns_handle_to_pathname
11795b482a8SLen Brown  *
11895b482a8SLen Brown  * PARAMETERS:  target_handle           - Handle of named object whose name is
11995b482a8SLen Brown  *                                        to be found
120ba494beeSBob Moore  *              buffer                  - Where the pathname is returned
121d1e7ffe5SLv Zheng  *              no_trailing             - Remove trailing '_' for each name
122d1e7ffe5SLv Zheng  *                                        segment
12395b482a8SLen Brown  *
12495b482a8SLen Brown  * RETURN:      Status, Buffer is filled with pathname if status is AE_OK
12595b482a8SLen Brown  *
12695b482a8SLen Brown  * DESCRIPTION: Build and return a full namespace pathname
12795b482a8SLen Brown  *
12895b482a8SLen Brown  ******************************************************************************/
12995b482a8SLen Brown 
13095b482a8SLen Brown acpi_status
acpi_ns_handle_to_pathname(acpi_handle target_handle,struct acpi_buffer * buffer,u8 no_trailing)13195b482a8SLen Brown acpi_ns_handle_to_pathname(acpi_handle target_handle,
132d1e7ffe5SLv Zheng 			   struct acpi_buffer *buffer, u8 no_trailing)
13395b482a8SLen Brown {
13495b482a8SLen Brown 	acpi_status status;
13595b482a8SLen Brown 	struct acpi_namespace_node *node;
13695b482a8SLen Brown 	acpi_size required_size;
13795b482a8SLen Brown 
13895b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
13995b482a8SLen Brown 
140f24b664dSBob Moore 	node = acpi_ns_validate_handle(target_handle);
14195b482a8SLen Brown 	if (!node) {
14295b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
14395b482a8SLen Brown 	}
14495b482a8SLen Brown 
14595b482a8SLen Brown 	/* Determine size required for the caller buffer */
14695b482a8SLen Brown 
147d1e7ffe5SLv Zheng 	required_size =
148d1e7ffe5SLv Zheng 	    acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
14995b482a8SLen Brown 	if (!required_size) {
15095b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
15195b482a8SLen Brown 	}
15295b482a8SLen Brown 
15395b482a8SLen Brown 	/* Validate/Allocate/Clear caller buffer */
15495b482a8SLen Brown 
15595b482a8SLen Brown 	status = acpi_ut_initialize_buffer(buffer, required_size);
15695b482a8SLen Brown 	if (ACPI_FAILURE(status)) {
15795b482a8SLen Brown 		return_ACPI_STATUS(status);
15895b482a8SLen Brown 	}
15995b482a8SLen Brown 
16095b482a8SLen Brown 	/* Build the path in the caller buffer */
16195b482a8SLen Brown 
162d1e7ffe5SLv Zheng 	(void)acpi_ns_build_normalized_path(node, buffer->pointer,
1638dbb75ceSSven Barth 					    (u32)required_size, no_trailing);
16495b482a8SLen Brown 
16595b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
16695b482a8SLen Brown 			  (char *)buffer->pointer, (u32) required_size));
16795b482a8SLen Brown 	return_ACPI_STATUS(AE_OK);
16895b482a8SLen Brown }
169d1e7ffe5SLv Zheng 
170d1e7ffe5SLv Zheng /*******************************************************************************
171d1e7ffe5SLv Zheng  *
172d1e7ffe5SLv Zheng  * FUNCTION:    acpi_ns_build_normalized_path
173d1e7ffe5SLv Zheng  *
174d1e7ffe5SLv Zheng  * PARAMETERS:  node        - Namespace node
175d1e7ffe5SLv Zheng  *              full_path   - Where the path name is returned
176d1e7ffe5SLv Zheng  *              path_size   - Size of returned path name buffer
177d1e7ffe5SLv Zheng  *              no_trailing - Remove trailing '_' from each name segment
178d1e7ffe5SLv Zheng  *
179d1e7ffe5SLv Zheng  * RETURN:      Return 1 if the AML path is empty, otherwise returning (length
180d1e7ffe5SLv Zheng  *              of pathname + 1) which means the 'FullPath' contains a trailing
181d1e7ffe5SLv Zheng  *              null.
182d1e7ffe5SLv Zheng  *
183d1e7ffe5SLv Zheng  * DESCRIPTION: Build and return a full namespace pathname.
184d1e7ffe5SLv Zheng  *              Note that if the size of 'FullPath' isn't large enough to
185d1e7ffe5SLv Zheng  *              contain the namespace node's path name, the actual required
186d1e7ffe5SLv Zheng  *              buffer length is returned, and it should be greater than
187d1e7ffe5SLv Zheng  *              'PathSize'. So callers are able to check the returning value
188d1e7ffe5SLv Zheng  *              to determine the buffer size of 'FullPath'.
189d1e7ffe5SLv Zheng  *
190d1e7ffe5SLv Zheng  ******************************************************************************/
191d1e7ffe5SLv Zheng 
192d1e7ffe5SLv Zheng u32
acpi_ns_build_normalized_path(struct acpi_namespace_node * node,char * full_path,u32 path_size,u8 no_trailing)193d1e7ffe5SLv Zheng acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
194d1e7ffe5SLv Zheng 			      char *full_path, u32 path_size, u8 no_trailing)
195d1e7ffe5SLv Zheng {
196d1e7ffe5SLv Zheng 	u32 length = 0, i;
19732786755SBob Moore 	char name[ACPI_NAMESEG_SIZE];
198d1e7ffe5SLv Zheng 	u8 do_no_trailing;
199d1e7ffe5SLv Zheng 	char c, *left, *right;
200d1e7ffe5SLv Zheng 	struct acpi_namespace_node *next_node;
201d1e7ffe5SLv Zheng 
202d1e7ffe5SLv Zheng 	ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
203d1e7ffe5SLv Zheng 
204d1e7ffe5SLv Zheng #define ACPI_PATH_PUT8(path, size, byte, length)    \
205d1e7ffe5SLv Zheng 	do {                                            \
206d1e7ffe5SLv Zheng 		if ((length) < (size))                      \
207d1e7ffe5SLv Zheng 		{                                           \
208d1e7ffe5SLv Zheng 			(path)[(length)] = (byte);              \
209d1e7ffe5SLv Zheng 		}                                           \
210d1e7ffe5SLv Zheng 		(length)++;                                 \
211d1e7ffe5SLv Zheng 	} while (0)
212d1e7ffe5SLv Zheng 
213d1e7ffe5SLv Zheng 	/*
214d1e7ffe5SLv Zheng 	 * Make sure the path_size is correct, so that we don't need to
215d1e7ffe5SLv Zheng 	 * validate both full_path and path_size.
216d1e7ffe5SLv Zheng 	 */
217d1e7ffe5SLv Zheng 	if (!full_path) {
218d1e7ffe5SLv Zheng 		path_size = 0;
219d1e7ffe5SLv Zheng 	}
220d1e7ffe5SLv Zheng 
221d1e7ffe5SLv Zheng 	if (!node) {
222d1e7ffe5SLv Zheng 		goto build_trailing_null;
223d1e7ffe5SLv Zheng 	}
224d1e7ffe5SLv Zheng 
225d1e7ffe5SLv Zheng 	next_node = node;
226d1e7ffe5SLv Zheng 	while (next_node && next_node != acpi_gbl_root_node) {
227d1e7ffe5SLv Zheng 		if (next_node != node) {
228d1e7ffe5SLv Zheng 			ACPI_PATH_PUT8(full_path, path_size,
229d1e7ffe5SLv Zheng 				       AML_DUAL_NAME_PREFIX, length);
230d1e7ffe5SLv Zheng 		}
2311fad8738SBob Moore 
232d1e7ffe5SLv Zheng 		ACPI_MOVE_32_TO_32(name, &next_node->name);
233d1e7ffe5SLv Zheng 		do_no_trailing = no_trailing;
234d1e7ffe5SLv Zheng 		for (i = 0; i < 4; i++) {
235d1e7ffe5SLv Zheng 			c = name[4 - i - 1];
236d1e7ffe5SLv Zheng 			if (do_no_trailing && c != '_') {
237d1e7ffe5SLv Zheng 				do_no_trailing = FALSE;
238d1e7ffe5SLv Zheng 			}
239d1e7ffe5SLv Zheng 			if (!do_no_trailing) {
240d1e7ffe5SLv Zheng 				ACPI_PATH_PUT8(full_path, path_size, c, length);
241d1e7ffe5SLv Zheng 			}
242d1e7ffe5SLv Zheng 		}
2431fad8738SBob Moore 
244d1e7ffe5SLv Zheng 		next_node = next_node->parent;
245d1e7ffe5SLv Zheng 	}
2461fad8738SBob Moore 
247d1e7ffe5SLv Zheng 	ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
248d1e7ffe5SLv Zheng 
249d1e7ffe5SLv Zheng 	/* Reverse the path string */
250d1e7ffe5SLv Zheng 
251d1e7ffe5SLv Zheng 	if (length <= path_size) {
252d1e7ffe5SLv Zheng 		left = full_path;
253d1e7ffe5SLv Zheng 		right = full_path + length - 1;
2541fad8738SBob Moore 
255d1e7ffe5SLv Zheng 		while (left < right) {
256d1e7ffe5SLv Zheng 			c = *left;
257d1e7ffe5SLv Zheng 			*left++ = *right;
258d1e7ffe5SLv Zheng 			*right-- = c;
259d1e7ffe5SLv Zheng 		}
260d1e7ffe5SLv Zheng 	}
261d1e7ffe5SLv Zheng 
262d1e7ffe5SLv Zheng 	/* Append the trailing null */
263d1e7ffe5SLv Zheng 
264d1e7ffe5SLv Zheng build_trailing_null:
265d1e7ffe5SLv Zheng 	ACPI_PATH_PUT8(full_path, path_size, '\0', length);
266d1e7ffe5SLv Zheng 
267d1e7ffe5SLv Zheng #undef ACPI_PATH_PUT8
268d1e7ffe5SLv Zheng 
269d1e7ffe5SLv Zheng 	return_UINT32(length);
270d1e7ffe5SLv Zheng }
271d1e7ffe5SLv Zheng 
272d1e7ffe5SLv Zheng /*******************************************************************************
273d1e7ffe5SLv Zheng  *
274d1e7ffe5SLv Zheng  * FUNCTION:    acpi_ns_get_normalized_pathname
275d1e7ffe5SLv Zheng  *
276d1e7ffe5SLv Zheng  * PARAMETERS:  node            - Namespace node whose pathname is needed
277d1e7ffe5SLv Zheng  *              no_trailing     - Remove trailing '_' from each name segment
278d1e7ffe5SLv Zheng  *
279d1e7ffe5SLv Zheng  * RETURN:      Pointer to storage containing the fully qualified name of
280d1e7ffe5SLv Zheng  *              the node, In external format (name segments separated by path
281d1e7ffe5SLv Zheng  *              separators.)
282d1e7ffe5SLv Zheng  *
283d1e7ffe5SLv Zheng  * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
284d1e7ffe5SLv Zheng  *              for error and debug statements. All trailing '_' will be
285d1e7ffe5SLv Zheng  *              removed from the full pathname if 'NoTrailing' is specified..
286d1e7ffe5SLv Zheng  *
287d1e7ffe5SLv Zheng  ******************************************************************************/
288d1e7ffe5SLv Zheng 
acpi_ns_get_normalized_pathname(struct acpi_namespace_node * node,u8 no_trailing)289d1e7ffe5SLv Zheng char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
290d1e7ffe5SLv Zheng 				      u8 no_trailing)
291d1e7ffe5SLv Zheng {
292d1e7ffe5SLv Zheng 	char *name_buffer;
293d1e7ffe5SLv Zheng 	acpi_size size;
294d1e7ffe5SLv Zheng 
295d1e7ffe5SLv Zheng 	ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
296d1e7ffe5SLv Zheng 
297d1e7ffe5SLv Zheng 	/* Calculate required buffer size based on depth below root */
298d1e7ffe5SLv Zheng 
299d1e7ffe5SLv Zheng 	size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
300d1e7ffe5SLv Zheng 	if (!size) {
301d1e7ffe5SLv Zheng 		return_PTR(NULL);
302d1e7ffe5SLv Zheng 	}
303d1e7ffe5SLv Zheng 
304d1e7ffe5SLv Zheng 	/* Allocate a buffer to be returned to caller */
305d1e7ffe5SLv Zheng 
306d1e7ffe5SLv Zheng 	name_buffer = ACPI_ALLOCATE_ZEROED(size);
307d1e7ffe5SLv Zheng 	if (!name_buffer) {
308d1e7ffe5SLv Zheng 		ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
309d1e7ffe5SLv Zheng 		return_PTR(NULL);
310d1e7ffe5SLv Zheng 	}
311d1e7ffe5SLv Zheng 
312d1e7ffe5SLv Zheng 	/* Build the path in the allocated buffer */
313d1e7ffe5SLv Zheng 
3148dbb75ceSSven Barth 	(void)acpi_ns_build_normalized_path(node, name_buffer, (u32)size,
315d1e7ffe5SLv Zheng 					    no_trailing);
316d1e7ffe5SLv Zheng 
3171ef63231SBob Moore 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES, "%s: Path \"%s\"\n",
3181ef63231SBob Moore 			      ACPI_GET_FUNCTION_NAME, name_buffer));
3191ef63231SBob Moore 
320d1e7ffe5SLv Zheng 	return_PTR(name_buffer);
321d1e7ffe5SLv Zheng }
32216ccf829SBob Moore 
32316ccf829SBob Moore /*******************************************************************************
32416ccf829SBob Moore  *
32516ccf829SBob Moore  * FUNCTION:    acpi_ns_build_prefixed_pathname
32616ccf829SBob Moore  *
32716ccf829SBob Moore  * PARAMETERS:  prefix_scope        - Scope/Path that prefixes the internal path
32816ccf829SBob Moore  *              internal_path       - Name or path of the namespace node
32916ccf829SBob Moore  *
33016ccf829SBob Moore  * RETURN:      None
33116ccf829SBob Moore  *
33216ccf829SBob Moore  * DESCRIPTION: Construct a fully qualified pathname from a concatenation of:
33316ccf829SBob Moore  *              1) Path associated with the prefix_scope namespace node
33416ccf829SBob Moore  *              2) External path representation of the Internal path
33516ccf829SBob Moore  *
33616ccf829SBob Moore  ******************************************************************************/
33716ccf829SBob Moore 
acpi_ns_build_prefixed_pathname(union acpi_generic_state * prefix_scope,const char * internal_path)33816ccf829SBob Moore char *acpi_ns_build_prefixed_pathname(union acpi_generic_state *prefix_scope,
33916ccf829SBob Moore 				      const char *internal_path)
34016ccf829SBob Moore {
34116ccf829SBob Moore 	acpi_status status;
34216ccf829SBob Moore 	char *full_path = NULL;
343dea4e833SErik Schmauss 	char *external_path = NULL;
34416ccf829SBob Moore 	char *prefix_path = NULL;
345a34991fdSSven Barth 	acpi_size prefix_path_length = 0;
34616ccf829SBob Moore 
34716ccf829SBob Moore 	/* If there is a prefix, get the pathname to it */
34816ccf829SBob Moore 
34916ccf829SBob Moore 	if (prefix_scope && prefix_scope->scope.node) {
35016ccf829SBob Moore 		prefix_path =
35116ccf829SBob Moore 		    acpi_ns_get_normalized_pathname(prefix_scope->scope.node,
35216ccf829SBob Moore 						    TRUE);
35316ccf829SBob Moore 		if (prefix_path) {
35416ccf829SBob Moore 			prefix_path_length = strlen(prefix_path);
35516ccf829SBob Moore 		}
35616ccf829SBob Moore 	}
35716ccf829SBob Moore 
35816ccf829SBob Moore 	status = acpi_ns_externalize_name(ACPI_UINT32_MAX, internal_path,
35916ccf829SBob Moore 					  NULL, &external_path);
36016ccf829SBob Moore 	if (ACPI_FAILURE(status)) {
36116ccf829SBob Moore 		goto cleanup;
36216ccf829SBob Moore 	}
36316ccf829SBob Moore 
36416ccf829SBob Moore 	/* Merge the prefix path and the path. 2 is for one dot and trailing null */
36516ccf829SBob Moore 
36616ccf829SBob Moore 	full_path =
36716ccf829SBob Moore 	    ACPI_ALLOCATE_ZEROED(prefix_path_length + strlen(external_path) +
36816ccf829SBob Moore 				 2);
36916ccf829SBob Moore 	if (!full_path) {
37016ccf829SBob Moore 		goto cleanup;
37116ccf829SBob Moore 	}
37216ccf829SBob Moore 
37316ccf829SBob Moore 	/* Don't merge if the External path is already fully qualified */
37416ccf829SBob Moore 
37516ccf829SBob Moore 	if (prefix_path && (*external_path != '\\') && (*external_path != '^')) {
37616ccf829SBob Moore 		strcat(full_path, prefix_path);
37716ccf829SBob Moore 		if (prefix_path[1]) {
37816ccf829SBob Moore 			strcat(full_path, ".");
37916ccf829SBob Moore 		}
38016ccf829SBob Moore 	}
38116ccf829SBob Moore 
38216ccf829SBob Moore 	acpi_ns_normalize_pathname(external_path);
38316ccf829SBob Moore 	strcat(full_path, external_path);
38416ccf829SBob Moore 
38516ccf829SBob Moore cleanup:
38616ccf829SBob Moore 	if (prefix_path) {
38716ccf829SBob Moore 		ACPI_FREE(prefix_path);
38816ccf829SBob Moore 	}
389dea4e833SErik Schmauss 	if (external_path) {
390dea4e833SErik Schmauss 		ACPI_FREE(external_path);
391dea4e833SErik Schmauss 	}
39216ccf829SBob Moore 
39316ccf829SBob Moore 	return (full_path);
39416ccf829SBob Moore }
39516ccf829SBob Moore 
39616ccf829SBob Moore /*******************************************************************************
39716ccf829SBob Moore  *
39816ccf829SBob Moore  * FUNCTION:    acpi_ns_normalize_pathname
39916ccf829SBob Moore  *
40016ccf829SBob Moore  * PARAMETERS:  original_path       - Path to be normalized, in External format
40116ccf829SBob Moore  *
40216ccf829SBob Moore  * RETURN:      The original path is processed in-place
40316ccf829SBob Moore  *
40416ccf829SBob Moore  * DESCRIPTION: Remove trailing underscores from each element of a path.
40516ccf829SBob Moore  *
40616ccf829SBob Moore  *              For example:  \A___.B___.C___ becomes \A.B.C
40716ccf829SBob Moore  *
40816ccf829SBob Moore  ******************************************************************************/
40916ccf829SBob Moore 
acpi_ns_normalize_pathname(char * original_path)4109a1ae804SBob Moore void acpi_ns_normalize_pathname(char *original_path)
41116ccf829SBob Moore {
41216ccf829SBob Moore 	char *input_path = original_path;
41316ccf829SBob Moore 	char *new_path_buffer;
41416ccf829SBob Moore 	char *new_path;
41516ccf829SBob Moore 	u32 i;
41616ccf829SBob Moore 
41716ccf829SBob Moore 	/* Allocate a temp buffer in which to construct the new path */
41816ccf829SBob Moore 
41916ccf829SBob Moore 	new_path_buffer = ACPI_ALLOCATE_ZEROED(strlen(input_path) + 1);
42016ccf829SBob Moore 	new_path = new_path_buffer;
42116ccf829SBob Moore 	if (!new_path_buffer) {
42216ccf829SBob Moore 		return;
42316ccf829SBob Moore 	}
42416ccf829SBob Moore 
42516ccf829SBob Moore 	/* Special characters may appear at the beginning of the path */
42616ccf829SBob Moore 
42716ccf829SBob Moore 	if (*input_path == '\\') {
42816ccf829SBob Moore 		*new_path = *input_path;
42916ccf829SBob Moore 		new_path++;
43016ccf829SBob Moore 		input_path++;
43116ccf829SBob Moore 	}
43216ccf829SBob Moore 
43316ccf829SBob Moore 	while (*input_path == '^') {
43416ccf829SBob Moore 		*new_path = *input_path;
43516ccf829SBob Moore 		new_path++;
43616ccf829SBob Moore 		input_path++;
43716ccf829SBob Moore 	}
43816ccf829SBob Moore 
43916ccf829SBob Moore 	/* Remainder of the path */
44016ccf829SBob Moore 
44116ccf829SBob Moore 	while (*input_path) {
44216ccf829SBob Moore 
44316ccf829SBob Moore 		/* Do one nameseg at a time */
44416ccf829SBob Moore 
44532786755SBob Moore 		for (i = 0; (i < ACPI_NAMESEG_SIZE) && *input_path; i++) {
44616ccf829SBob Moore 			if ((i == 0) || (*input_path != '_')) {	/* First char is allowed to be underscore */
44716ccf829SBob Moore 				*new_path = *input_path;
44816ccf829SBob Moore 				new_path++;
44916ccf829SBob Moore 			}
45016ccf829SBob Moore 
45116ccf829SBob Moore 			input_path++;
45216ccf829SBob Moore 		}
45316ccf829SBob Moore 
45416ccf829SBob Moore 		/* Dot means that there are more namesegs to come */
45516ccf829SBob Moore 
45616ccf829SBob Moore 		if (*input_path == '.') {
45716ccf829SBob Moore 			*new_path = *input_path;
45816ccf829SBob Moore 			new_path++;
45916ccf829SBob Moore 			input_path++;
46016ccf829SBob Moore 		}
46116ccf829SBob Moore 	}
46216ccf829SBob Moore 
46316ccf829SBob Moore 	*new_path = 0;
46416ccf829SBob Moore 	strcpy(original_path, new_path_buffer);
46516ccf829SBob Moore 	ACPI_FREE(new_path_buffer);
46616ccf829SBob Moore }
467