1 /******************************************************************************
3 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4 * parents and siblings and Scope manipulation
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2013, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsutils")
55 /* Local prototypes */
57 #ifdef ACPI_OBSOLETE_FUNCTIONS
59 AcpiNsFindParentName (
60 ACPI_NAMESPACE_NODE
*NodeToSearch
);
64 /*******************************************************************************
66 * FUNCTION: AcpiNsPrintNodePathname
68 * PARAMETERS: Node - Object
69 * Message - Prefix message
71 * DESCRIPTION: Print an object's full namespace pathname
72 * Manages allocation/freeing of a pathname buffer
74 ******************************************************************************/
77 AcpiNsPrintNodePathname (
78 ACPI_NAMESPACE_NODE
*Node
,
87 AcpiOsPrintf ("[NULL NAME]");
91 /* Convert handle to full pathname and print it (with supplied message) */
93 Buffer
.Length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
95 Status
= AcpiNsHandleToPathname (Node
, &Buffer
);
96 if (ACPI_SUCCESS (Status
))
100 AcpiOsPrintf ("%s ", Message
);
103 AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer
.Pointer
, Node
);
104 ACPI_FREE (Buffer
.Pointer
);
109 /*******************************************************************************
111 * FUNCTION: AcpiNsGetType
113 * PARAMETERS: Node - Parent Node to be examined
115 * RETURN: Type field from Node whose handle is passed
117 * DESCRIPTION: Return the type of a Namespace node
119 ******************************************************************************/
123 ACPI_NAMESPACE_NODE
*Node
)
125 ACPI_FUNCTION_TRACE (NsGetType
);
130 ACPI_WARNING ((AE_INFO
, "Null Node parameter"));
131 return_UINT8 (ACPI_TYPE_ANY
);
134 return_UINT8 (Node
->Type
);
138 /*******************************************************************************
140 * FUNCTION: AcpiNsLocal
142 * PARAMETERS: Type - A namespace object type
144 * RETURN: LOCAL if names must be found locally in objects of the
145 * passed type, 0 if enclosing scopes should be searched
147 * DESCRIPTION: Returns scope rule for the given object type.
149 ******************************************************************************/
153 ACPI_OBJECT_TYPE Type
)
155 ACPI_FUNCTION_TRACE (NsLocal
);
158 if (!AcpiUtValidObjectType (Type
))
160 /* Type code out of range */
162 ACPI_WARNING ((AE_INFO
, "Invalid Object Type 0x%X", Type
));
163 return_UINT32 (ACPI_NS_NORMAL
);
166 return_UINT32 (AcpiGbl_NsProperties
[Type
] & ACPI_NS_LOCAL
);
170 /*******************************************************************************
172 * FUNCTION: AcpiNsGetInternalNameLength
174 * PARAMETERS: Info - Info struct initialized with the
175 * external name pointer.
179 * DESCRIPTION: Calculate the length of the internal (AML) namestring
180 * corresponding to the external (ASL) namestring.
182 ******************************************************************************/
185 AcpiNsGetInternalNameLength (
186 ACPI_NAMESTRING_INFO
*Info
)
188 const char *NextExternalChar
;
192 ACPI_FUNCTION_ENTRY ();
195 NextExternalChar
= Info
->ExternalName
;
197 Info
->NumSegments
= 0;
198 Info
->FullyQualified
= FALSE
;
201 * For the internal name, the required length is 4 bytes per segment, plus
202 * 1 each for RootPrefix, MultiNamePrefixOp, segment count, trailing null
203 * (which is not really needed, but no there's harm in putting it there)
205 * strlen() + 1 covers the first NameSeg, which has no path separator
207 if (ACPI_IS_ROOT_PREFIX (*NextExternalChar
))
209 Info
->FullyQualified
= TRUE
;
212 /* Skip redundant RootPrefix, like \\_SB.PCI0.SBRG.EC0 */
214 while (ACPI_IS_ROOT_PREFIX (*NextExternalChar
))
221 /* Handle Carat prefixes */
223 while (ACPI_IS_PARENT_PREFIX (*NextExternalChar
))
231 * Determine the number of ACPI name "segments" by counting the number of
232 * path separators within the string. Start with one segment since the
233 * segment count is [(# separators) + 1], and zero separators is ok.
235 if (*NextExternalChar
)
237 Info
->NumSegments
= 1;
238 for (i
= 0; NextExternalChar
[i
]; i
++)
240 if (ACPI_IS_PATH_SEPARATOR (NextExternalChar
[i
]))
247 Info
->Length
= (ACPI_NAME_SIZE
* Info
->NumSegments
) +
250 Info
->NextExternalChar
= NextExternalChar
;
254 /*******************************************************************************
256 * FUNCTION: AcpiNsBuildInternalName
258 * PARAMETERS: Info - Info struct fully initialized
262 * DESCRIPTION: Construct the internal (AML) namestring
263 * corresponding to the external (ASL) namestring.
265 ******************************************************************************/
268 AcpiNsBuildInternalName (
269 ACPI_NAMESTRING_INFO
*Info
)
271 UINT32 NumSegments
= Info
->NumSegments
;
272 char *InternalName
= Info
->InternalName
;
273 const char *ExternalName
= Info
->NextExternalChar
;
278 ACPI_FUNCTION_TRACE (NsBuildInternalName
);
281 /* Setup the correct prefixes, counts, and pointers */
283 if (Info
->FullyQualified
)
285 InternalName
[0] = AML_ROOT_PREFIX
;
287 if (NumSegments
<= 1)
289 Result
= &InternalName
[1];
291 else if (NumSegments
== 2)
293 InternalName
[1] = AML_DUAL_NAME_PREFIX
;
294 Result
= &InternalName
[2];
298 InternalName
[1] = AML_MULTI_NAME_PREFIX_OP
;
299 InternalName
[2] = (char) NumSegments
;
300 Result
= &InternalName
[3];
306 * Not fully qualified.
307 * Handle Carats first, then append the name segments
312 for (i
= 0; i
< Info
->NumCarats
; i
++)
314 InternalName
[i
] = AML_PARENT_PREFIX
;
318 if (NumSegments
<= 1)
320 Result
= &InternalName
[i
];
322 else if (NumSegments
== 2)
324 InternalName
[i
] = AML_DUAL_NAME_PREFIX
;
325 Result
= &InternalName
[(ACPI_SIZE
) i
+1];
329 InternalName
[i
] = AML_MULTI_NAME_PREFIX_OP
;
330 InternalName
[(ACPI_SIZE
) i
+1] = (char) NumSegments
;
331 Result
= &InternalName
[(ACPI_SIZE
) i
+2];
335 /* Build the name (minus path separators) */
337 for (; NumSegments
; NumSegments
--)
339 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++)
341 if (ACPI_IS_PATH_SEPARATOR (*ExternalName
) ||
342 (*ExternalName
== 0))
344 /* Pad the segment with underscore(s) if segment is short */
350 /* Convert the character to uppercase and save it */
352 Result
[i
] = (char) ACPI_TOUPPER ((int) *ExternalName
);
357 /* Now we must have a path separator, or the pathname is bad */
359 if (!ACPI_IS_PATH_SEPARATOR (*ExternalName
) &&
360 (*ExternalName
!= 0))
362 return_ACPI_STATUS (AE_BAD_PATHNAME
);
365 /* Move on the next segment */
368 Result
+= ACPI_NAME_SIZE
;
371 /* Terminate the string */
375 if (Info
->FullyQualified
)
377 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Returning [%p] (abs) \"\\%s\"\n",
378 InternalName
, InternalName
));
382 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Returning [%p] (rel) \"%s\"\n",
383 InternalName
, InternalName
));
386 return_ACPI_STATUS (AE_OK
);
390 /*******************************************************************************
392 * FUNCTION: AcpiNsInternalizeName
394 * PARAMETERS: *ExternalName - External representation of name
395 * **Converted Name - Where to return the resulting
396 * internal represention of the name
400 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
401 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
403 *******************************************************************************/
406 AcpiNsInternalizeName (
407 const char *ExternalName
,
408 char **ConvertedName
)
411 ACPI_NAMESTRING_INFO Info
;
415 ACPI_FUNCTION_TRACE (NsInternalizeName
);
418 if ((!ExternalName
) ||
419 (*ExternalName
== 0) ||
422 return_ACPI_STATUS (AE_BAD_PARAMETER
);
425 /* Get the length of the new internal name */
427 Info
.ExternalName
= ExternalName
;
428 AcpiNsGetInternalNameLength (&Info
);
430 /* We need a segment to store the internal name */
432 InternalName
= ACPI_ALLOCATE_ZEROED (Info
.Length
);
435 return_ACPI_STATUS (AE_NO_MEMORY
);
440 Info
.InternalName
= InternalName
;
441 Status
= AcpiNsBuildInternalName (&Info
);
442 if (ACPI_FAILURE (Status
))
444 ACPI_FREE (InternalName
);
445 return_ACPI_STATUS (Status
);
448 *ConvertedName
= InternalName
;
449 return_ACPI_STATUS (AE_OK
);
453 /*******************************************************************************
455 * FUNCTION: AcpiNsExternalizeName
457 * PARAMETERS: InternalNameLength - Lenth of the internal name below
458 * InternalName - Internal representation of name
459 * ConvertedNameLength - Where the length is returned
460 * ConvertedName - Where the resulting external name
465 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
466 * to its external (printable) form (e.g. "\_PR_.CPU0")
468 ******************************************************************************/
471 AcpiNsExternalizeName (
472 UINT32 InternalNameLength
,
473 const char *InternalName
,
474 UINT32
*ConvertedNameLength
,
475 char **ConvertedName
)
477 UINT32 NamesIndex
= 0;
478 UINT32 NumSegments
= 0;
479 UINT32 RequiredLength
;
480 UINT32 PrefixLength
= 0;
485 ACPI_FUNCTION_TRACE (NsExternalizeName
);
488 if (!InternalNameLength
||
492 return_ACPI_STATUS (AE_BAD_PARAMETER
);
495 /* Check for a prefix (one '\' | one or more '^') */
497 switch (InternalName
[0])
499 case AML_ROOT_PREFIX
:
504 case AML_PARENT_PREFIX
:
506 for (i
= 0; i
< InternalNameLength
; i
++)
508 if (ACPI_IS_PARENT_PREFIX (InternalName
[i
]))
510 PrefixLength
= i
+ 1;
518 if (i
== InternalNameLength
)
531 * Check for object names. Note that there could be 0-255 of these
534 if (PrefixLength
< InternalNameLength
)
536 switch (InternalName
[PrefixLength
])
538 case AML_MULTI_NAME_PREFIX_OP
:
540 /* <count> 4-byte names */
542 NamesIndex
= PrefixLength
+ 2;
543 NumSegments
= (UINT8
)
544 InternalName
[(ACPI_SIZE
) PrefixLength
+ 1];
547 case AML_DUAL_NAME_PREFIX
:
549 /* Two 4-byte names */
551 NamesIndex
= PrefixLength
+ 1;
565 /* one 4-byte name */
567 NamesIndex
= PrefixLength
;
574 * Calculate the length of ConvertedName, which equals the length
575 * of the prefix, length of all object names, length of any required
576 * punctuation ('.') between object names, plus the NULL terminator.
578 RequiredLength
= PrefixLength
+ (4 * NumSegments
) +
579 ((NumSegments
> 0) ? (NumSegments
- 1) : 0) + 1;
582 * Check to see if we're still in bounds. If not, there's a problem
583 * with InternalName (invalid format).
585 if (RequiredLength
> InternalNameLength
)
587 ACPI_ERROR ((AE_INFO
, "Invalid internal name"));
588 return_ACPI_STATUS (AE_BAD_PATHNAME
);
591 /* Build the ConvertedName */
593 *ConvertedName
= ACPI_ALLOCATE_ZEROED (RequiredLength
);
594 if (!(*ConvertedName
))
596 return_ACPI_STATUS (AE_NO_MEMORY
);
601 for (i
= 0; i
< PrefixLength
; i
++)
603 (*ConvertedName
)[j
++] = InternalName
[i
];
608 for (i
= 0; i
< NumSegments
; i
++)
612 (*ConvertedName
)[j
++] = '.';
615 /* Copy and validate the 4-char name segment */
617 ACPI_MOVE_NAME (&(*ConvertedName
)[j
], &InternalName
[NamesIndex
]);
618 AcpiUtRepairName (&(*ConvertedName
)[j
]);
621 NamesIndex
+= ACPI_NAME_SIZE
;
625 if (ConvertedNameLength
)
627 *ConvertedNameLength
= (UINT32
) RequiredLength
;
630 return_ACPI_STATUS (AE_OK
);
634 /*******************************************************************************
636 * FUNCTION: AcpiNsValidateHandle
638 * PARAMETERS: Handle - Handle to be validated and typecast to a
641 * RETURN: A pointer to a namespace node
643 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
644 * cases for the root node.
646 * NOTE: Real integer handles would allow for more verification
647 * and keep all pointers within this subsystem - however this introduces
648 * more overhead and has not been necessary to this point. Drivers
649 * holding handles are typically notified before a node becomes invalid
650 * due to a table unload.
652 ******************************************************************************/
654 ACPI_NAMESPACE_NODE
*
655 AcpiNsValidateHandle (
659 ACPI_FUNCTION_ENTRY ();
662 /* Parameter validation */
664 if ((!Handle
) || (Handle
== ACPI_ROOT_OBJECT
))
666 return (AcpiGbl_RootNode
);
669 /* We can at least attempt to verify the handle */
671 if (ACPI_GET_DESCRIPTOR_TYPE (Handle
) != ACPI_DESC_TYPE_NAMED
)
676 return (ACPI_CAST_PTR (ACPI_NAMESPACE_NODE
, Handle
));
680 /*******************************************************************************
682 * FUNCTION: AcpiNsTerminate
688 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
690 ******************************************************************************/
699 ACPI_FUNCTION_TRACE (NsTerminate
);
703 * Free the entire namespace -- all nodes and all objects
704 * attached to the nodes
706 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode
);
708 /* Delete any objects attached to the root node */
710 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
711 if (ACPI_FAILURE (Status
))
716 AcpiNsDeleteNode (AcpiGbl_RootNode
);
717 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
719 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "Namespace freed\n"));
724 /*******************************************************************************
726 * FUNCTION: AcpiNsOpensScope
728 * PARAMETERS: Type - A valid namespace type
730 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
731 * to the ACPI specification, else 0
733 ******************************************************************************/
737 ACPI_OBJECT_TYPE Type
)
739 ACPI_FUNCTION_ENTRY ();
742 if (Type
> ACPI_TYPE_LOCAL_MAX
)
744 /* type code out of range */
746 ACPI_WARNING ((AE_INFO
, "Invalid Object Type 0x%X", Type
));
747 return (ACPI_NS_NORMAL
);
750 return (((UINT32
) AcpiGbl_NsProperties
[Type
]) & ACPI_NS_NEWSCOPE
);
754 /*******************************************************************************
756 * FUNCTION: AcpiNsGetNode
758 * PARAMETERS: *Pathname - Name to be found, in external (ASL) format. The
759 * \ (backslash) and ^ (carat) prefixes, and the
760 * . (period) to separate segments are supported.
761 * PrefixNode - Root of subtree to be searched, or NS_ALL for the
762 * root of the name space. If Name is fully
763 * qualified (first INT8 is '\'), the passed value
764 * of Scope will not be accessed.
765 * Flags - Used to indicate whether to perform upsearch or
767 * ReturnNode - Where the Node is returned
769 * DESCRIPTION: Look up a name relative to a given scope and return the
770 * corresponding Node. NOTE: Scope can be null.
772 * MUTEX: Locks namespace
774 ******************************************************************************/
778 ACPI_NAMESPACE_NODE
*PrefixNode
,
779 const char *Pathname
,
781 ACPI_NAMESPACE_NODE
**ReturnNode
)
783 ACPI_GENERIC_STATE ScopeInfo
;
788 ACPI_FUNCTION_TRACE_PTR (NsGetNode
, ACPI_CAST_PTR (char, Pathname
));
791 /* Simplest case is a null pathname */
795 *ReturnNode
= PrefixNode
;
798 *ReturnNode
= AcpiGbl_RootNode
;
800 return_ACPI_STATUS (AE_OK
);
803 /* Quick check for a reference to the root */
805 if (ACPI_IS_ROOT_PREFIX (Pathname
[0]) && (!Pathname
[1]))
807 *ReturnNode
= AcpiGbl_RootNode
;
808 return_ACPI_STATUS (AE_OK
);
811 /* Convert path to internal representation */
813 Status
= AcpiNsInternalizeName (Pathname
, &InternalPath
);
814 if (ACPI_FAILURE (Status
))
816 return_ACPI_STATUS (Status
);
819 /* Must lock namespace during lookup */
821 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
822 if (ACPI_FAILURE (Status
))
827 /* Setup lookup scope (search starting point) */
829 ScopeInfo
.Scope
.Node
= PrefixNode
;
831 /* Lookup the name in the namespace */
833 Status
= AcpiNsLookup (&ScopeInfo
, InternalPath
, ACPI_TYPE_ANY
,
834 ACPI_IMODE_EXECUTE
, (Flags
| ACPI_NS_DONT_OPEN_SCOPE
),
836 if (ACPI_FAILURE (Status
))
838 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "%s, %s\n",
839 Pathname
, AcpiFormatException (Status
)));
842 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
845 ACPI_FREE (InternalPath
);
846 return_ACPI_STATUS (Status
);