1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
5 * parents and siblings and Scope manipulation
7 * Copyright (C) 2000 - 2018, Intel Corp.
9 *****************************************************************************/
11 #include <acpi/acpi.h>
16 #define _COMPONENT ACPI_NAMESPACE
17 ACPI_MODULE_NAME("nsutils")
19 /* Local prototypes */
20 #ifdef ACPI_OBSOLETE_FUNCTIONS
21 acpi_name
acpi_ns_find_parent_name(struct acpi_namespace_node
*node_to_search
);
24 /*******************************************************************************
26 * FUNCTION: acpi_ns_print_node_pathname
28 * PARAMETERS: node - Object
29 * message - Prefix message
31 * DESCRIPTION: Print an object's full namespace pathname
32 * Manages allocation/freeing of a pathname buffer
34 ******************************************************************************/
37 acpi_ns_print_node_pathname(struct acpi_namespace_node
*node
,
40 struct acpi_buffer buffer
;
44 acpi_os_printf("[NULL NAME]");
48 /* Convert handle to full pathname and print it (with supplied message) */
50 buffer
.length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
52 status
= acpi_ns_handle_to_pathname(node
, &buffer
, TRUE
);
53 if (ACPI_SUCCESS(status
)) {
55 acpi_os_printf("%s ", message
);
58 acpi_os_printf("%s", (char *)buffer
.pointer
);
59 ACPI_FREE(buffer
.pointer
);
63 /*******************************************************************************
65 * FUNCTION: acpi_ns_get_type
67 * PARAMETERS: node - Parent Node to be examined
69 * RETURN: Type field from Node whose handle is passed
71 * DESCRIPTION: Return the type of a Namespace node
73 ******************************************************************************/
75 acpi_object_type
acpi_ns_get_type(struct acpi_namespace_node
* node
)
77 ACPI_FUNCTION_TRACE(ns_get_type
);
80 ACPI_WARNING((AE_INFO
, "Null Node parameter"));
81 return_UINT8(ACPI_TYPE_ANY
);
84 return_UINT8(node
->type
);
87 /*******************************************************************************
89 * FUNCTION: acpi_ns_local
91 * PARAMETERS: type - A namespace object type
93 * RETURN: LOCAL if names must be found locally in objects of the
94 * passed type, 0 if enclosing scopes should be searched
96 * DESCRIPTION: Returns scope rule for the given object type.
98 ******************************************************************************/
100 u32
acpi_ns_local(acpi_object_type type
)
102 ACPI_FUNCTION_TRACE(ns_local
);
104 if (!acpi_ut_valid_object_type(type
)) {
106 /* Type code out of range */
108 ACPI_WARNING((AE_INFO
, "Invalid Object Type 0x%X", type
));
109 return_UINT32(ACPI_NS_NORMAL
);
112 return_UINT32(acpi_gbl_ns_properties
[type
] & ACPI_NS_LOCAL
);
115 /*******************************************************************************
117 * FUNCTION: acpi_ns_get_internal_name_length
119 * PARAMETERS: info - Info struct initialized with the
120 * external name pointer.
124 * DESCRIPTION: Calculate the length of the internal (AML) namestring
125 * corresponding to the external (ASL) namestring.
127 ******************************************************************************/
129 void acpi_ns_get_internal_name_length(struct acpi_namestring_info
*info
)
131 const char *next_external_char
;
134 ACPI_FUNCTION_ENTRY();
136 next_external_char
= info
->external_name
;
137 info
->num_carats
= 0;
138 info
->num_segments
= 0;
139 info
->fully_qualified
= FALSE
;
142 * For the internal name, the required length is 4 bytes per segment,
143 * plus 1 each for root_prefix, multi_name_prefix_op, segment count,
144 * trailing null (which is not really needed, but no there's harm in
147 * strlen() + 1 covers the first name_seg, which has no path separator
149 if (ACPI_IS_ROOT_PREFIX(*next_external_char
)) {
150 info
->fully_qualified
= TRUE
;
151 next_external_char
++;
153 /* Skip redundant root_prefix, like \\_SB.PCI0.SBRG.EC0 */
155 while (ACPI_IS_ROOT_PREFIX(*next_external_char
)) {
156 next_external_char
++;
159 /* Handle Carat prefixes */
161 while (ACPI_IS_PARENT_PREFIX(*next_external_char
)) {
163 next_external_char
++;
168 * Determine the number of ACPI name "segments" by counting the number of
169 * path separators within the string. Start with one segment since the
170 * segment count is [(# separators) + 1], and zero separators is ok.
172 if (*next_external_char
) {
173 info
->num_segments
= 1;
174 for (i
= 0; next_external_char
[i
]; i
++) {
175 if (ACPI_IS_PATH_SEPARATOR(next_external_char
[i
])) {
176 info
->num_segments
++;
181 info
->length
= (ACPI_NAME_SIZE
* info
->num_segments
) +
182 4 + info
->num_carats
;
184 info
->next_external_char
= next_external_char
;
187 /*******************************************************************************
189 * FUNCTION: acpi_ns_build_internal_name
191 * PARAMETERS: info - Info struct fully initialized
195 * DESCRIPTION: Construct the internal (AML) namestring
196 * corresponding to the external (ASL) namestring.
198 ******************************************************************************/
200 acpi_status
acpi_ns_build_internal_name(struct acpi_namestring_info
*info
)
202 u32 num_segments
= info
->num_segments
;
203 char *internal_name
= info
->internal_name
;
204 const char *external_name
= info
->next_external_char
;
208 ACPI_FUNCTION_TRACE(ns_build_internal_name
);
210 /* Setup the correct prefixes, counts, and pointers */
212 if (info
->fully_qualified
) {
213 internal_name
[0] = AML_ROOT_PREFIX
;
215 if (num_segments
<= 1) {
216 result
= &internal_name
[1];
217 } else if (num_segments
== 2) {
218 internal_name
[1] = AML_DUAL_NAME_PREFIX
;
219 result
= &internal_name
[2];
221 internal_name
[1] = AML_MULTI_NAME_PREFIX
;
222 internal_name
[2] = (char)num_segments
;
223 result
= &internal_name
[3];
227 * Not fully qualified.
228 * Handle Carats first, then append the name segments
231 if (info
->num_carats
) {
232 for (i
= 0; i
< info
->num_carats
; i
++) {
233 internal_name
[i
] = AML_PARENT_PREFIX
;
237 if (num_segments
<= 1) {
238 result
= &internal_name
[i
];
239 } else if (num_segments
== 2) {
240 internal_name
[i
] = AML_DUAL_NAME_PREFIX
;
241 result
= &internal_name
[(acpi_size
)i
+ 1];
243 internal_name
[i
] = AML_MULTI_NAME_PREFIX
;
244 internal_name
[(acpi_size
)i
+ 1] = (char)num_segments
;
245 result
= &internal_name
[(acpi_size
)i
+ 2];
249 /* Build the name (minus path separators) */
251 for (; num_segments
; num_segments
--) {
252 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++) {
253 if (ACPI_IS_PATH_SEPARATOR(*external_name
) ||
254 (*external_name
== 0)) {
256 /* Pad the segment with underscore(s) if segment is short */
260 /* Convert the character to uppercase and save it */
262 result
[i
] = (char)toupper((int)*external_name
);
267 /* Now we must have a path separator, or the pathname is bad */
269 if (!ACPI_IS_PATH_SEPARATOR(*external_name
) &&
270 (*external_name
!= 0)) {
271 return_ACPI_STATUS(AE_BAD_PATHNAME
);
274 /* Move on the next segment */
277 result
+= ACPI_NAME_SIZE
;
280 /* Terminate the string */
284 if (info
->fully_qualified
) {
285 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
286 "Returning [%p] (abs) \"\\%s\"\n",
287 internal_name
, internal_name
));
289 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "Returning [%p] (rel) \"%s\"\n",
290 internal_name
, internal_name
));
293 return_ACPI_STATUS(AE_OK
);
296 /*******************************************************************************
298 * FUNCTION: acpi_ns_internalize_name
300 * PARAMETERS: *external_name - External representation of name
301 * **Converted name - Where to return the resulting
302 * internal represention of the name
306 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
307 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
309 *******************************************************************************/
312 acpi_ns_internalize_name(const char *external_name
, char **converted_name
)
315 struct acpi_namestring_info info
;
318 ACPI_FUNCTION_TRACE(ns_internalize_name
);
320 if ((!external_name
) || (*external_name
== 0) || (!converted_name
)) {
321 return_ACPI_STATUS(AE_BAD_PARAMETER
);
324 /* Get the length of the new internal name */
326 info
.external_name
= external_name
;
327 acpi_ns_get_internal_name_length(&info
);
329 /* We need a segment to store the internal name */
331 internal_name
= ACPI_ALLOCATE_ZEROED(info
.length
);
332 if (!internal_name
) {
333 return_ACPI_STATUS(AE_NO_MEMORY
);
338 info
.internal_name
= internal_name
;
339 status
= acpi_ns_build_internal_name(&info
);
340 if (ACPI_FAILURE(status
)) {
341 ACPI_FREE(internal_name
);
342 return_ACPI_STATUS(status
);
345 *converted_name
= internal_name
;
346 return_ACPI_STATUS(AE_OK
);
349 /*******************************************************************************
351 * FUNCTION: acpi_ns_externalize_name
353 * PARAMETERS: internal_name_length - Lenth of the internal name below
354 * internal_name - Internal representation of name
355 * converted_name_length - Where the length is returned
356 * converted_name - Where the resulting external name
361 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
362 * to its external (printable) form (e.g. "\_PR_.CPU0")
364 ******************************************************************************/
367 acpi_ns_externalize_name(u32 internal_name_length
,
368 const char *internal_name
,
369 u32
* converted_name_length
, char **converted_name
)
372 u32 num_segments
= 0;
374 u32 prefix_length
= 0;
378 ACPI_FUNCTION_TRACE(ns_externalize_name
);
380 if (!internal_name_length
|| !internal_name
|| !converted_name
) {
381 return_ACPI_STATUS(AE_BAD_PARAMETER
);
384 /* Check for a prefix (one '\' | one or more '^') */
386 switch (internal_name
[0]) {
387 case AML_ROOT_PREFIX
:
392 case AML_PARENT_PREFIX
:
394 for (i
= 0; i
< internal_name_length
; i
++) {
395 if (ACPI_IS_PARENT_PREFIX(internal_name
[i
])) {
396 prefix_length
= i
+ 1;
402 if (i
== internal_name_length
) {
414 * Check for object names. Note that there could be 0-255 of these
417 if (prefix_length
< internal_name_length
) {
418 switch (internal_name
[prefix_length
]) {
419 case AML_MULTI_NAME_PREFIX
:
421 /* <count> 4-byte names */
423 names_index
= prefix_length
+ 2;
425 internal_name
[(acpi_size
)prefix_length
+ 1];
428 case AML_DUAL_NAME_PREFIX
:
430 /* Two 4-byte names */
432 names_index
= prefix_length
+ 1;
446 /* one 4-byte name */
448 names_index
= prefix_length
;
455 * Calculate the length of converted_name, which equals the length
456 * of the prefix, length of all object names, length of any required
457 * punctuation ('.') between object names, plus the NULL terminator.
459 required_length
= prefix_length
+ (4 * num_segments
) +
460 ((num_segments
> 0) ? (num_segments
- 1) : 0) + 1;
463 * Check to see if we're still in bounds. If not, there's a problem
464 * with internal_name (invalid format).
466 if (required_length
> internal_name_length
) {
467 ACPI_ERROR((AE_INFO
, "Invalid internal name"));
468 return_ACPI_STATUS(AE_BAD_PATHNAME
);
471 /* Build the converted_name */
473 *converted_name
= ACPI_ALLOCATE_ZEROED(required_length
);
474 if (!(*converted_name
)) {
475 return_ACPI_STATUS(AE_NO_MEMORY
);
480 for (i
= 0; i
< prefix_length
; i
++) {
481 (*converted_name
)[j
++] = internal_name
[i
];
484 if (num_segments
> 0) {
485 for (i
= 0; i
< num_segments
; i
++) {
487 (*converted_name
)[j
++] = '.';
490 /* Copy and validate the 4-char name segment */
492 ACPI_MOVE_NAME(&(*converted_name
)[j
],
493 &internal_name
[names_index
]);
494 acpi_ut_repair_name(&(*converted_name
)[j
]);
497 names_index
+= ACPI_NAME_SIZE
;
501 if (converted_name_length
) {
502 *converted_name_length
= (u32
) required_length
;
505 return_ACPI_STATUS(AE_OK
);
508 /*******************************************************************************
510 * FUNCTION: acpi_ns_validate_handle
512 * PARAMETERS: handle - Handle to be validated and typecast to a
515 * RETURN: A pointer to a namespace node
517 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
518 * cases for the root node.
520 * NOTE: Real integer handles would allow for more verification
521 * and keep all pointers within this subsystem - however this introduces
522 * more overhead and has not been necessary to this point. Drivers
523 * holding handles are typically notified before a node becomes invalid
524 * due to a table unload.
526 ******************************************************************************/
528 struct acpi_namespace_node
*acpi_ns_validate_handle(acpi_handle handle
)
531 ACPI_FUNCTION_ENTRY();
533 /* Parameter validation */
535 if ((!handle
) || (handle
== ACPI_ROOT_OBJECT
)) {
536 return (acpi_gbl_root_node
);
539 /* We can at least attempt to verify the handle */
541 if (ACPI_GET_DESCRIPTOR_TYPE(handle
) != ACPI_DESC_TYPE_NAMED
) {
545 return (ACPI_CAST_PTR(struct acpi_namespace_node
, handle
));
548 /*******************************************************************************
550 * FUNCTION: acpi_ns_terminate
556 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
558 ******************************************************************************/
560 void acpi_ns_terminate(void)
563 union acpi_operand_object
*prev
;
564 union acpi_operand_object
*next
;
566 ACPI_FUNCTION_TRACE(ns_terminate
);
568 /* Delete any module-level code blocks */
570 next
= acpi_gbl_module_code_list
;
573 next
= next
->method
.mutex
;
574 prev
->method
.mutex
= NULL
; /* Clear the Mutex (cheated) field */
575 acpi_ut_remove_reference(prev
);
579 * Free the entire namespace -- all nodes and all objects
580 * attached to the nodes
582 acpi_ns_delete_namespace_subtree(acpi_gbl_root_node
);
584 /* Delete any objects attached to the root node */
586 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
587 if (ACPI_FAILURE(status
)) {
591 acpi_ns_delete_node(acpi_gbl_root_node
);
592 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
594 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Namespace freed\n"));
598 /*******************************************************************************
600 * FUNCTION: acpi_ns_opens_scope
602 * PARAMETERS: type - A valid namespace type
604 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
605 * to the ACPI specification, else 0
607 ******************************************************************************/
609 u32
acpi_ns_opens_scope(acpi_object_type type
)
611 ACPI_FUNCTION_ENTRY();
613 if (type
> ACPI_TYPE_LOCAL_MAX
) {
615 /* type code out of range */
617 ACPI_WARNING((AE_INFO
, "Invalid Object Type 0x%X", type
));
618 return (ACPI_NS_NORMAL
);
621 return (((u32
)acpi_gbl_ns_properties
[type
]) & ACPI_NS_NEWSCOPE
);
624 /*******************************************************************************
626 * FUNCTION: acpi_ns_get_node_unlocked
628 * PARAMETERS: *pathname - Name to be found, in external (ASL) format. The
629 * \ (backslash) and ^ (carat) prefixes, and the
630 * . (period) to separate segments are supported.
631 * prefix_node - Root of subtree to be searched, or NS_ALL for the
632 * root of the name space. If Name is fully
633 * qualified (first s8 is '\'), the passed value
634 * of Scope will not be accessed.
635 * flags - Used to indicate whether to perform upsearch or
637 * return_node - Where the Node is returned
639 * DESCRIPTION: Look up a name relative to a given scope and return the
640 * corresponding Node. NOTE: Scope can be null.
642 * MUTEX: Doesn't locks namespace
644 ******************************************************************************/
647 acpi_ns_get_node_unlocked(struct acpi_namespace_node
*prefix_node
,
648 const char *pathname
,
649 u32 flags
, struct acpi_namespace_node
**return_node
)
651 union acpi_generic_state scope_info
;
655 ACPI_FUNCTION_TRACE_PTR(ns_get_node_unlocked
,
656 ACPI_CAST_PTR(char, pathname
));
658 /* Simplest case is a null pathname */
661 *return_node
= prefix_node
;
663 *return_node
= acpi_gbl_root_node
;
666 return_ACPI_STATUS(AE_OK
);
669 /* Quick check for a reference to the root */
671 if (ACPI_IS_ROOT_PREFIX(pathname
[0]) && (!pathname
[1])) {
672 *return_node
= acpi_gbl_root_node
;
673 return_ACPI_STATUS(AE_OK
);
676 /* Convert path to internal representation */
678 status
= acpi_ns_internalize_name(pathname
, &internal_path
);
679 if (ACPI_FAILURE(status
)) {
680 return_ACPI_STATUS(status
);
683 /* Setup lookup scope (search starting point) */
685 scope_info
.scope
.node
= prefix_node
;
687 /* Lookup the name in the namespace */
689 status
= acpi_ns_lookup(&scope_info
, internal_path
, ACPI_TYPE_ANY
,
691 (flags
| ACPI_NS_DONT_OPEN_SCOPE
), NULL
,
693 if (ACPI_FAILURE(status
)) {
694 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "%s, %s\n",
695 pathname
, acpi_format_exception(status
)));
698 ACPI_FREE(internal_path
);
699 return_ACPI_STATUS(status
);
702 /*******************************************************************************
704 * FUNCTION: acpi_ns_get_node
706 * PARAMETERS: *pathname - Name to be found, in external (ASL) format. The
707 * \ (backslash) and ^ (carat) prefixes, and the
708 * . (period) to separate segments are supported.
709 * prefix_node - Root of subtree to be searched, or NS_ALL for the
710 * root of the name space. If Name is fully
711 * qualified (first s8 is '\'), the passed value
712 * of Scope will not be accessed.
713 * flags - Used to indicate whether to perform upsearch or
715 * return_node - Where the Node is returned
717 * DESCRIPTION: Look up a name relative to a given scope and return the
718 * corresponding Node. NOTE: Scope can be null.
720 * MUTEX: Locks namespace
722 ******************************************************************************/
725 acpi_ns_get_node(struct acpi_namespace_node
*prefix_node
,
726 const char *pathname
,
727 u32 flags
, struct acpi_namespace_node
**return_node
)
731 ACPI_FUNCTION_TRACE_PTR(ns_get_node
, ACPI_CAST_PTR(char, pathname
));
733 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
734 if (ACPI_FAILURE(status
)) {
735 return_ACPI_STATUS(status
);
738 status
= acpi_ns_get_node_unlocked(prefix_node
, pathname
,
741 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
742 return_ACPI_STATUS(status
);