1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: nsnames - Name manipulation and search
6 ******************************************************************************/
13 #define _COMPONENT ACPI_NAMESPACE
14 ACPI_MODULE_NAME("nsnames")
16 /* Local Prototypes */
17 static void acpi_ns_normalize_pathname(char *original_path
);
19 /*******************************************************************************
21 * FUNCTION: acpi_ns_get_external_pathname
23 * PARAMETERS: node - Namespace node whose pathname is needed
25 * RETURN: Pointer to storage containing the fully qualified name of
26 * the node, In external format (name segments separated by path
29 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
30 * for error and debug statements.
32 ******************************************************************************/
34 char *acpi_ns_get_external_pathname(struct acpi_namespace_node
*node
)
38 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname
, node
);
40 name_buffer
= acpi_ns_get_normalized_pathname(node
, FALSE
);
41 return_PTR(name_buffer
);
44 /*******************************************************************************
46 * FUNCTION: acpi_ns_get_pathname_length
48 * PARAMETERS: node - Namespace node
50 * RETURN: Length of path, including prefix
52 * DESCRIPTION: Get the length of the pathname string for this node
54 ******************************************************************************/
56 acpi_size
acpi_ns_get_pathname_length(struct acpi_namespace_node
*node
)
60 /* Validate the Node */
62 if (ACPI_GET_DESCRIPTOR_TYPE(node
) != ACPI_DESC_TYPE_NAMED
) {
64 "Invalid/cached reference target node: %p, descriptor type %d",
65 node
, ACPI_GET_DESCRIPTOR_TYPE(node
)));
69 size
= acpi_ns_build_normalized_path(node
, NULL
, 0, FALSE
);
73 /*******************************************************************************
75 * FUNCTION: acpi_ns_handle_to_name
77 * PARAMETERS: target_handle - Handle of named object whose name is
79 * buffer - Where the name is returned
81 * RETURN: Status, Buffer is filled with name if status is AE_OK
83 * DESCRIPTION: Build and return a full namespace name
85 ******************************************************************************/
88 acpi_ns_handle_to_name(acpi_handle target_handle
, struct acpi_buffer
*buffer
)
91 struct acpi_namespace_node
*node
;
92 const char *node_name
;
94 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name
, target_handle
);
96 node
= acpi_ns_validate_handle(target_handle
);
98 return_ACPI_STATUS(AE_BAD_PARAMETER
);
101 /* Validate/Allocate/Clear caller buffer */
103 status
= acpi_ut_initialize_buffer(buffer
, ACPI_PATH_SEGMENT_LENGTH
);
104 if (ACPI_FAILURE(status
)) {
105 return_ACPI_STATUS(status
);
108 /* Just copy the ACPI name from the Node and zero terminate it */
110 node_name
= acpi_ut_get_node_name(node
);
111 ACPI_MOVE_NAME(buffer
->pointer
, node_name
);
112 ((char *)buffer
->pointer
)[ACPI_NAME_SIZE
] = 0;
114 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "%4.4s\n", (char *)buffer
->pointer
));
115 return_ACPI_STATUS(AE_OK
);
118 /*******************************************************************************
120 * FUNCTION: acpi_ns_handle_to_pathname
122 * PARAMETERS: target_handle - Handle of named object whose name is
124 * buffer - Where the pathname is returned
125 * no_trailing - Remove trailing '_' for each name
128 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
130 * DESCRIPTION: Build and return a full namespace pathname
132 ******************************************************************************/
135 acpi_ns_handle_to_pathname(acpi_handle target_handle
,
136 struct acpi_buffer
*buffer
, u8 no_trailing
)
139 struct acpi_namespace_node
*node
;
140 acpi_size required_size
;
142 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname
, target_handle
);
144 node
= acpi_ns_validate_handle(target_handle
);
146 return_ACPI_STATUS(AE_BAD_PARAMETER
);
149 /* Determine size required for the caller buffer */
152 acpi_ns_build_normalized_path(node
, NULL
, 0, no_trailing
);
153 if (!required_size
) {
154 return_ACPI_STATUS(AE_BAD_PARAMETER
);
157 /* Validate/Allocate/Clear caller buffer */
159 status
= acpi_ut_initialize_buffer(buffer
, required_size
);
160 if (ACPI_FAILURE(status
)) {
161 return_ACPI_STATUS(status
);
164 /* Build the path in the caller buffer */
166 (void)acpi_ns_build_normalized_path(node
, buffer
->pointer
,
167 required_size
, no_trailing
);
169 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "%s [%X]\n",
170 (char *)buffer
->pointer
, (u32
) required_size
));
171 return_ACPI_STATUS(AE_OK
);
174 /*******************************************************************************
176 * FUNCTION: acpi_ns_build_normalized_path
178 * PARAMETERS: node - Namespace node
179 * full_path - Where the path name is returned
180 * path_size - Size of returned path name buffer
181 * no_trailing - Remove trailing '_' from each name segment
183 * RETURN: Return 1 if the AML path is empty, otherwise returning (length
184 * of pathname + 1) which means the 'FullPath' contains a trailing
187 * DESCRIPTION: Build and return a full namespace pathname.
188 * Note that if the size of 'FullPath' isn't large enough to
189 * contain the namespace node's path name, the actual required
190 * buffer length is returned, and it should be greater than
191 * 'PathSize'. So callers are able to check the returning value
192 * to determine the buffer size of 'FullPath'.
194 ******************************************************************************/
197 acpi_ns_build_normalized_path(struct acpi_namespace_node
*node
,
198 char *full_path
, u32 path_size
, u8 no_trailing
)
201 char name
[ACPI_NAME_SIZE
];
203 char c
, *left
, *right
;
204 struct acpi_namespace_node
*next_node
;
206 ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path
, node
);
208 #define ACPI_PATH_PUT8(path, size, byte, length) \
210 if ((length) < (size)) \
212 (path)[(length)] = (byte); \
218 * Make sure the path_size is correct, so that we don't need to
219 * validate both full_path and path_size.
226 goto build_trailing_null
;
230 while (next_node
&& next_node
!= acpi_gbl_root_node
) {
231 if (next_node
!= node
) {
232 ACPI_PATH_PUT8(full_path
, path_size
,
233 AML_DUAL_NAME_PREFIX
, length
);
236 ACPI_MOVE_32_TO_32(name
, &next_node
->name
);
237 do_no_trailing
= no_trailing
;
238 for (i
= 0; i
< 4; i
++) {
240 if (do_no_trailing
&& c
!= '_') {
241 do_no_trailing
= FALSE
;
243 if (!do_no_trailing
) {
244 ACPI_PATH_PUT8(full_path
, path_size
, c
, length
);
248 next_node
= next_node
->parent
;
251 ACPI_PATH_PUT8(full_path
, path_size
, AML_ROOT_PREFIX
, length
);
253 /* Reverse the path string */
255 if (length
<= path_size
) {
257 right
= full_path
+ length
- 1;
259 while (left
< right
) {
266 /* Append the trailing null */
269 ACPI_PATH_PUT8(full_path
, path_size
, '\0', length
);
271 #undef ACPI_PATH_PUT8
273 return_UINT32(length
);
276 /*******************************************************************************
278 * FUNCTION: acpi_ns_get_normalized_pathname
280 * PARAMETERS: node - Namespace node whose pathname is needed
281 * no_trailing - Remove trailing '_' from each name segment
283 * RETURN: Pointer to storage containing the fully qualified name of
284 * the node, In external format (name segments separated by path
287 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
288 * for error and debug statements. All trailing '_' will be
289 * removed from the full pathname if 'NoTrailing' is specified..
291 ******************************************************************************/
293 char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node
*node
,
299 ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname
, node
);
301 /* Calculate required buffer size based on depth below root */
303 size
= acpi_ns_build_normalized_path(node
, NULL
, 0, no_trailing
);
308 /* Allocate a buffer to be returned to caller */
310 name_buffer
= ACPI_ALLOCATE_ZEROED(size
);
312 ACPI_ERROR((AE_INFO
, "Could not allocate %u bytes", (u32
)size
));
316 /* Build the path in the allocated buffer */
318 (void)acpi_ns_build_normalized_path(node
, name_buffer
, size
,
321 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES
, "%s: Path \"%s\"\n",
322 ACPI_GET_FUNCTION_NAME
, name_buffer
));
324 return_PTR(name_buffer
);
327 /*******************************************************************************
329 * FUNCTION: acpi_ns_build_prefixed_pathname
331 * PARAMETERS: prefix_scope - Scope/Path that prefixes the internal path
332 * internal_path - Name or path of the namespace node
336 * DESCRIPTION: Construct a fully qualified pathname from a concatenation of:
337 * 1) Path associated with the prefix_scope namespace node
338 * 2) External path representation of the Internal path
340 ******************************************************************************/
342 char *acpi_ns_build_prefixed_pathname(union acpi_generic_state
*prefix_scope
,
343 const char *internal_path
)
346 char *full_path
= NULL
;
347 char *external_path
= NULL
;
348 char *prefix_path
= NULL
;
349 u32 prefix_path_length
= 0;
351 /* If there is a prefix, get the pathname to it */
353 if (prefix_scope
&& prefix_scope
->scope
.node
) {
355 acpi_ns_get_normalized_pathname(prefix_scope
->scope
.node
,
358 prefix_path_length
= strlen(prefix_path
);
362 status
= acpi_ns_externalize_name(ACPI_UINT32_MAX
, internal_path
,
363 NULL
, &external_path
);
364 if (ACPI_FAILURE(status
)) {
368 /* Merge the prefix path and the path. 2 is for one dot and trailing null */
371 ACPI_ALLOCATE_ZEROED(prefix_path_length
+ strlen(external_path
) +
377 /* Don't merge if the External path is already fully qualified */
379 if (prefix_path
&& (*external_path
!= '\\') && (*external_path
!= '^')) {
380 strcat(full_path
, prefix_path
);
381 if (prefix_path
[1]) {
382 strcat(full_path
, ".");
386 acpi_ns_normalize_pathname(external_path
);
387 strcat(full_path
, external_path
);
391 ACPI_FREE(prefix_path
);
394 ACPI_FREE(external_path
);
400 /*******************************************************************************
402 * FUNCTION: acpi_ns_normalize_pathname
404 * PARAMETERS: original_path - Path to be normalized, in External format
406 * RETURN: The original path is processed in-place
408 * DESCRIPTION: Remove trailing underscores from each element of a path.
410 * For example: \A___.B___.C___ becomes \A.B.C
412 ******************************************************************************/
414 static void acpi_ns_normalize_pathname(char *original_path
)
416 char *input_path
= original_path
;
417 char *new_path_buffer
;
421 /* Allocate a temp buffer in which to construct the new path */
423 new_path_buffer
= ACPI_ALLOCATE_ZEROED(strlen(input_path
) + 1);
424 new_path
= new_path_buffer
;
425 if (!new_path_buffer
) {
429 /* Special characters may appear at the beginning of the path */
431 if (*input_path
== '\\') {
432 *new_path
= *input_path
;
437 while (*input_path
== '^') {
438 *new_path
= *input_path
;
443 /* Remainder of the path */
445 while (*input_path
) {
447 /* Do one nameseg at a time */
449 for (i
= 0; (i
< ACPI_NAME_SIZE
) && *input_path
; i
++) {
450 if ((i
== 0) || (*input_path
!= '_')) { /* First char is allowed to be underscore */
451 *new_path
= *input_path
;
458 /* Dot means that there are more namesegs to come */
460 if (*input_path
== '.') {
461 *new_path
= *input_path
;
468 strcpy(original_path
, new_path_buffer
);
469 ACPI_FREE(new_path_buffer
);