1 /******************************************************************************
3 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4 * parents and siblings and Scope manipulation
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2014, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
117 #define __NSUTILS_C__
120 #include "accommon.h"
121 #include "acnamesp.h"
124 #define _COMPONENT ACPI_NAMESPACE
125 ACPI_MODULE_NAME ("nsutils")
127 /* Local prototypes */
129 #ifdef ACPI_OBSOLETE_FUNCTIONS
131 AcpiNsFindParentName (
132 ACPI_NAMESPACE_NODE
*NodeToSearch
);
136 /*******************************************************************************
138 * FUNCTION: AcpiNsPrintNodePathname
140 * PARAMETERS: Node - Object
141 * Message - Prefix message
143 * DESCRIPTION: Print an object's full namespace pathname
144 * Manages allocation/freeing of a pathname buffer
146 ******************************************************************************/
149 AcpiNsPrintNodePathname (
150 ACPI_NAMESPACE_NODE
*Node
,
159 AcpiOsPrintf ("[NULL NAME]");
163 /* Convert handle to full pathname and print it (with supplied message) */
165 Buffer
.Length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
167 Status
= AcpiNsHandleToPathname (Node
, &Buffer
);
168 if (ACPI_SUCCESS (Status
))
172 AcpiOsPrintf ("%s ", Message
);
175 AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer
.Pointer
, Node
);
176 ACPI_FREE (Buffer
.Pointer
);
181 /*******************************************************************************
183 * FUNCTION: AcpiNsGetType
185 * PARAMETERS: Node - Parent Node to be examined
187 * RETURN: Type field from Node whose handle is passed
189 * DESCRIPTION: Return the type of a Namespace node
191 ******************************************************************************/
195 ACPI_NAMESPACE_NODE
*Node
)
197 ACPI_FUNCTION_TRACE (NsGetType
);
202 ACPI_WARNING ((AE_INFO
, "Null Node parameter"));
203 return_UINT8 (ACPI_TYPE_ANY
);
206 return_UINT8 (Node
->Type
);
210 /*******************************************************************************
212 * FUNCTION: AcpiNsLocal
214 * PARAMETERS: Type - A namespace object type
216 * RETURN: LOCAL if names must be found locally in objects of the
217 * passed type, 0 if enclosing scopes should be searched
219 * DESCRIPTION: Returns scope rule for the given object type.
221 ******************************************************************************/
225 ACPI_OBJECT_TYPE Type
)
227 ACPI_FUNCTION_TRACE (NsLocal
);
230 if (!AcpiUtValidObjectType (Type
))
232 /* Type code out of range */
234 ACPI_WARNING ((AE_INFO
, "Invalid Object Type 0x%X", Type
));
235 return_UINT32 (ACPI_NS_NORMAL
);
238 return_UINT32 (AcpiGbl_NsProperties
[Type
] & ACPI_NS_LOCAL
);
242 /*******************************************************************************
244 * FUNCTION: AcpiNsGetInternalNameLength
246 * PARAMETERS: Info - Info struct initialized with the
247 * external name pointer.
251 * DESCRIPTION: Calculate the length of the internal (AML) namestring
252 * corresponding to the external (ASL) namestring.
254 ******************************************************************************/
257 AcpiNsGetInternalNameLength (
258 ACPI_NAMESTRING_INFO
*Info
)
260 const char *NextExternalChar
;
264 ACPI_FUNCTION_ENTRY ();
267 NextExternalChar
= Info
->ExternalName
;
269 Info
->NumSegments
= 0;
270 Info
->FullyQualified
= FALSE
;
273 * For the internal name, the required length is 4 bytes per segment, plus
274 * 1 each for RootPrefix, MultiNamePrefixOp, segment count, trailing null
275 * (which is not really needed, but no there's harm in putting it there)
277 * strlen() + 1 covers the first NameSeg, which has no path separator
279 if (ACPI_IS_ROOT_PREFIX (*NextExternalChar
))
281 Info
->FullyQualified
= TRUE
;
284 /* Skip redundant RootPrefix, like \\_SB.PCI0.SBRG.EC0 */
286 while (ACPI_IS_ROOT_PREFIX (*NextExternalChar
))
293 /* Handle Carat prefixes */
295 while (ACPI_IS_PARENT_PREFIX (*NextExternalChar
))
303 * Determine the number of ACPI name "segments" by counting the number of
304 * path separators within the string. Start with one segment since the
305 * segment count is [(# separators) + 1], and zero separators is ok.
307 if (*NextExternalChar
)
309 Info
->NumSegments
= 1;
310 for (i
= 0; NextExternalChar
[i
]; i
++)
312 if (ACPI_IS_PATH_SEPARATOR (NextExternalChar
[i
]))
319 Info
->Length
= (ACPI_NAME_SIZE
* Info
->NumSegments
) +
322 Info
->NextExternalChar
= NextExternalChar
;
326 /*******************************************************************************
328 * FUNCTION: AcpiNsBuildInternalName
330 * PARAMETERS: Info - Info struct fully initialized
334 * DESCRIPTION: Construct the internal (AML) namestring
335 * corresponding to the external (ASL) namestring.
337 ******************************************************************************/
340 AcpiNsBuildInternalName (
341 ACPI_NAMESTRING_INFO
*Info
)
343 UINT32 NumSegments
= Info
->NumSegments
;
344 char *InternalName
= Info
->InternalName
;
345 const char *ExternalName
= Info
->NextExternalChar
;
350 ACPI_FUNCTION_TRACE (NsBuildInternalName
);
353 /* Setup the correct prefixes, counts, and pointers */
355 if (Info
->FullyQualified
)
357 InternalName
[0] = AML_ROOT_PREFIX
;
359 if (NumSegments
<= 1)
361 Result
= &InternalName
[1];
363 else if (NumSegments
== 2)
365 InternalName
[1] = AML_DUAL_NAME_PREFIX
;
366 Result
= &InternalName
[2];
370 InternalName
[1] = AML_MULTI_NAME_PREFIX_OP
;
371 InternalName
[2] = (char) NumSegments
;
372 Result
= &InternalName
[3];
378 * Not fully qualified.
379 * Handle Carats first, then append the name segments
384 for (i
= 0; i
< Info
->NumCarats
; i
++)
386 InternalName
[i
] = AML_PARENT_PREFIX
;
390 if (NumSegments
<= 1)
392 Result
= &InternalName
[i
];
394 else if (NumSegments
== 2)
396 InternalName
[i
] = AML_DUAL_NAME_PREFIX
;
397 Result
= &InternalName
[(ACPI_SIZE
) i
+1];
401 InternalName
[i
] = AML_MULTI_NAME_PREFIX_OP
;
402 InternalName
[(ACPI_SIZE
) i
+1] = (char) NumSegments
;
403 Result
= &InternalName
[(ACPI_SIZE
) i
+2];
407 /* Build the name (minus path separators) */
409 for (; NumSegments
; NumSegments
--)
411 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++)
413 if (ACPI_IS_PATH_SEPARATOR (*ExternalName
) ||
414 (*ExternalName
== 0))
416 /* Pad the segment with underscore(s) if segment is short */
422 /* Convert the character to uppercase and save it */
424 Result
[i
] = (char) ACPI_TOUPPER ((int) *ExternalName
);
429 /* Now we must have a path separator, or the pathname is bad */
431 if (!ACPI_IS_PATH_SEPARATOR (*ExternalName
) &&
432 (*ExternalName
!= 0))
434 return_ACPI_STATUS (AE_BAD_PATHNAME
);
437 /* Move on the next segment */
440 Result
+= ACPI_NAME_SIZE
;
443 /* Terminate the string */
447 if (Info
->FullyQualified
)
449 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Returning [%p] (abs) \"\\%s\"\n",
450 InternalName
, InternalName
));
454 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Returning [%p] (rel) \"%s\"\n",
455 InternalName
, InternalName
));
458 return_ACPI_STATUS (AE_OK
);
462 /*******************************************************************************
464 * FUNCTION: AcpiNsInternalizeName
466 * PARAMETERS: *ExternalName - External representation of name
467 * **Converted Name - Where to return the resulting
468 * internal represention of the name
472 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
473 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
475 *******************************************************************************/
478 AcpiNsInternalizeName (
479 const char *ExternalName
,
480 char **ConvertedName
)
483 ACPI_NAMESTRING_INFO Info
;
487 ACPI_FUNCTION_TRACE (NsInternalizeName
);
490 if ((!ExternalName
) ||
491 (*ExternalName
== 0) ||
494 return_ACPI_STATUS (AE_BAD_PARAMETER
);
497 /* Get the length of the new internal name */
499 Info
.ExternalName
= ExternalName
;
500 AcpiNsGetInternalNameLength (&Info
);
502 /* We need a segment to store the internal name */
504 InternalName
= ACPI_ALLOCATE_ZEROED (Info
.Length
);
507 return_ACPI_STATUS (AE_NO_MEMORY
);
512 Info
.InternalName
= InternalName
;
513 Status
= AcpiNsBuildInternalName (&Info
);
514 if (ACPI_FAILURE (Status
))
516 ACPI_FREE (InternalName
);
517 return_ACPI_STATUS (Status
);
520 *ConvertedName
= InternalName
;
521 return_ACPI_STATUS (AE_OK
);
525 /*******************************************************************************
527 * FUNCTION: AcpiNsExternalizeName
529 * PARAMETERS: InternalNameLength - Lenth of the internal name below
530 * InternalName - Internal representation of name
531 * ConvertedNameLength - Where the length is returned
532 * ConvertedName - Where the resulting external name
537 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
538 * to its external (printable) form (e.g. "\_PR_.CPU0")
540 ******************************************************************************/
543 AcpiNsExternalizeName (
544 UINT32 InternalNameLength
,
545 const char *InternalName
,
546 UINT32
*ConvertedNameLength
,
547 char **ConvertedName
)
549 UINT32 NamesIndex
= 0;
550 UINT32 NumSegments
= 0;
551 UINT32 RequiredLength
;
552 UINT32 PrefixLength
= 0;
557 ACPI_FUNCTION_TRACE (NsExternalizeName
);
560 if (!InternalNameLength
||
564 return_ACPI_STATUS (AE_BAD_PARAMETER
);
567 /* Check for a prefix (one '\' | one or more '^') */
569 switch (InternalName
[0])
571 case AML_ROOT_PREFIX
:
576 case AML_PARENT_PREFIX
:
578 for (i
= 0; i
< InternalNameLength
; i
++)
580 if (ACPI_IS_PARENT_PREFIX (InternalName
[i
]))
582 PrefixLength
= i
+ 1;
590 if (i
== InternalNameLength
)
603 * Check for object names. Note that there could be 0-255 of these
606 if (PrefixLength
< InternalNameLength
)
608 switch (InternalName
[PrefixLength
])
610 case AML_MULTI_NAME_PREFIX_OP
:
612 /* <count> 4-byte names */
614 NamesIndex
= PrefixLength
+ 2;
615 NumSegments
= (UINT8
)
616 InternalName
[(ACPI_SIZE
) PrefixLength
+ 1];
619 case AML_DUAL_NAME_PREFIX
:
621 /* Two 4-byte names */
623 NamesIndex
= PrefixLength
+ 1;
637 /* one 4-byte name */
639 NamesIndex
= PrefixLength
;
646 * Calculate the length of ConvertedName, which equals the length
647 * of the prefix, length of all object names, length of any required
648 * punctuation ('.') between object names, plus the NULL terminator.
650 RequiredLength
= PrefixLength
+ (4 * NumSegments
) +
651 ((NumSegments
> 0) ? (NumSegments
- 1) : 0) + 1;
654 * Check to see if we're still in bounds. If not, there's a problem
655 * with InternalName (invalid format).
657 if (RequiredLength
> InternalNameLength
)
659 ACPI_ERROR ((AE_INFO
, "Invalid internal name"));
660 return_ACPI_STATUS (AE_BAD_PATHNAME
);
663 /* Build the ConvertedName */
665 *ConvertedName
= ACPI_ALLOCATE_ZEROED (RequiredLength
);
666 if (!(*ConvertedName
))
668 return_ACPI_STATUS (AE_NO_MEMORY
);
673 for (i
= 0; i
< PrefixLength
; i
++)
675 (*ConvertedName
)[j
++] = InternalName
[i
];
680 for (i
= 0; i
< NumSegments
; i
++)
684 (*ConvertedName
)[j
++] = '.';
687 /* Copy and validate the 4-char name segment */
689 ACPI_MOVE_NAME (&(*ConvertedName
)[j
], &InternalName
[NamesIndex
]);
690 AcpiUtRepairName (&(*ConvertedName
)[j
]);
693 NamesIndex
+= ACPI_NAME_SIZE
;
697 if (ConvertedNameLength
)
699 *ConvertedNameLength
= (UINT32
) RequiredLength
;
702 return_ACPI_STATUS (AE_OK
);
706 /*******************************************************************************
708 * FUNCTION: AcpiNsValidateHandle
710 * PARAMETERS: Handle - Handle to be validated and typecast to a
713 * RETURN: A pointer to a namespace node
715 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
716 * cases for the root node.
718 * NOTE: Real integer handles would allow for more verification
719 * and keep all pointers within this subsystem - however this introduces
720 * more overhead and has not been necessary to this point. Drivers
721 * holding handles are typically notified before a node becomes invalid
722 * due to a table unload.
724 ******************************************************************************/
726 ACPI_NAMESPACE_NODE
*
727 AcpiNsValidateHandle (
731 ACPI_FUNCTION_ENTRY ();
734 /* Parameter validation */
736 if ((!Handle
) || (Handle
== ACPI_ROOT_OBJECT
))
738 return (AcpiGbl_RootNode
);
741 /* We can at least attempt to verify the handle */
743 if (ACPI_GET_DESCRIPTOR_TYPE (Handle
) != ACPI_DESC_TYPE_NAMED
)
748 return (ACPI_CAST_PTR (ACPI_NAMESPACE_NODE
, Handle
));
752 /*******************************************************************************
754 * FUNCTION: AcpiNsTerminate
760 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
762 ******************************************************************************/
771 ACPI_FUNCTION_TRACE (NsTerminate
);
775 * Free the entire namespace -- all nodes and all objects
776 * attached to the nodes
778 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode
);
780 /* Delete any objects attached to the root node */
782 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
783 if (ACPI_FAILURE (Status
))
788 AcpiNsDeleteNode (AcpiGbl_RootNode
);
789 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
791 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "Namespace freed\n"));
796 /*******************************************************************************
798 * FUNCTION: AcpiNsOpensScope
800 * PARAMETERS: Type - A valid namespace type
802 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
803 * to the ACPI specification, else 0
805 ******************************************************************************/
809 ACPI_OBJECT_TYPE Type
)
811 ACPI_FUNCTION_ENTRY ();
814 if (Type
> ACPI_TYPE_LOCAL_MAX
)
816 /* type code out of range */
818 ACPI_WARNING ((AE_INFO
, "Invalid Object Type 0x%X", Type
));
819 return (ACPI_NS_NORMAL
);
822 return (((UINT32
) AcpiGbl_NsProperties
[Type
]) & ACPI_NS_NEWSCOPE
);
826 /*******************************************************************************
828 * FUNCTION: AcpiNsGetNode
830 * PARAMETERS: *Pathname - Name to be found, in external (ASL) format. The
831 * \ (backslash) and ^ (carat) prefixes, and the
832 * . (period) to separate segments are supported.
833 * PrefixNode - Root of subtree to be searched, or NS_ALL for the
834 * root of the name space. If Name is fully
835 * qualified (first INT8 is '\'), the passed value
836 * of Scope will not be accessed.
837 * Flags - Used to indicate whether to perform upsearch or
839 * ReturnNode - Where the Node is returned
841 * DESCRIPTION: Look up a name relative to a given scope and return the
842 * corresponding Node. NOTE: Scope can be null.
844 * MUTEX: Locks namespace
846 ******************************************************************************/
850 ACPI_NAMESPACE_NODE
*PrefixNode
,
851 const char *Pathname
,
853 ACPI_NAMESPACE_NODE
**ReturnNode
)
855 ACPI_GENERIC_STATE ScopeInfo
;
860 ACPI_FUNCTION_TRACE_PTR (NsGetNode
, ACPI_CAST_PTR (char, Pathname
));
863 /* Simplest case is a null pathname */
867 *ReturnNode
= PrefixNode
;
870 *ReturnNode
= AcpiGbl_RootNode
;
872 return_ACPI_STATUS (AE_OK
);
875 /* Quick check for a reference to the root */
877 if (ACPI_IS_ROOT_PREFIX (Pathname
[0]) && (!Pathname
[1]))
879 *ReturnNode
= AcpiGbl_RootNode
;
880 return_ACPI_STATUS (AE_OK
);
883 /* Convert path to internal representation */
885 Status
= AcpiNsInternalizeName (Pathname
, &InternalPath
);
886 if (ACPI_FAILURE (Status
))
888 return_ACPI_STATUS (Status
);
891 /* Must lock namespace during lookup */
893 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
894 if (ACPI_FAILURE (Status
))
899 /* Setup lookup scope (search starting point) */
901 ScopeInfo
.Scope
.Node
= PrefixNode
;
903 /* Lookup the name in the namespace */
905 Status
= AcpiNsLookup (&ScopeInfo
, InternalPath
, ACPI_TYPE_ANY
,
906 ACPI_IMODE_EXECUTE
, (Flags
| ACPI_NS_DONT_OPEN_SCOPE
),
908 if (ACPI_FAILURE (Status
))
910 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "%s, %s\n",
911 Pathname
, AcpiFormatException (Status
)));
914 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
917 ACPI_FREE (InternalPath
);
918 return_ACPI_STATUS (Status
);