1 /******************************************************************************
3 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4 * parents and siblings and Scope manipulation
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2016, 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.
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("nsutils")
53 /* Local prototypes */
55 #ifdef ACPI_OBSOLETE_FUNCTIONS
57 AcpiNsFindParentName (
58 ACPI_NAMESPACE_NODE *NodeToSearch);
62 /*******************************************************************************
64 * FUNCTION: AcpiNsPrintNodePathname
66 * PARAMETERS: Node - Object
67 * Message - Prefix message
69 * DESCRIPTION: Print an object's full namespace pathname
70 * Manages allocation/freeing of a pathname buffer
72 ******************************************************************************/
75 AcpiNsPrintNodePathname (
76 ACPI_NAMESPACE_NODE *Node,
85 AcpiOsPrintf ("[NULL NAME]");
89 /* Convert handle to full pathname and print it (with supplied message) */
91 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
93 Status = AcpiNsHandleToPathname (Node, &Buffer, TRUE);
94 if (ACPI_SUCCESS (Status))
98 AcpiOsPrintf ("%s ", Message);
101 AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer.Pointer, Node);
102 ACPI_FREE (Buffer.Pointer);
107 /*******************************************************************************
109 * FUNCTION: AcpiNsGetType
111 * PARAMETERS: Node - Parent Node to be examined
113 * RETURN: Type field from Node whose handle is passed
115 * DESCRIPTION: Return the type of a Namespace node
117 ******************************************************************************/
121 ACPI_NAMESPACE_NODE *Node)
123 ACPI_FUNCTION_TRACE (NsGetType);
128 ACPI_WARNING ((AE_INFO, "Null Node parameter"));
129 return_UINT8 (ACPI_TYPE_ANY);
132 return_UINT8 (Node->Type);
136 /*******************************************************************************
138 * FUNCTION: AcpiNsLocal
140 * PARAMETERS: Type - A namespace object type
142 * RETURN: LOCAL if names must be found locally in objects of the
143 * passed type, 0 if enclosing scopes should be searched
145 * DESCRIPTION: Returns scope rule for the given object type.
147 ******************************************************************************/
151 ACPI_OBJECT_TYPE Type)
153 ACPI_FUNCTION_TRACE (NsLocal);
156 if (!AcpiUtValidObjectType (Type))
158 /* Type code out of range */
160 ACPI_WARNING ((AE_INFO, "Invalid Object Type 0x%X", Type));
161 return_UINT32 (ACPI_NS_NORMAL);
164 return_UINT32 (AcpiGbl_NsProperties[Type] & ACPI_NS_LOCAL);
168 /*******************************************************************************
170 * FUNCTION: AcpiNsGetInternalNameLength
172 * PARAMETERS: Info - Info struct initialized with the
173 * external name pointer.
177 * DESCRIPTION: Calculate the length of the internal (AML) namestring
178 * corresponding to the external (ASL) namestring.
180 ******************************************************************************/
183 AcpiNsGetInternalNameLength (
184 ACPI_NAMESTRING_INFO *Info)
186 const char *NextExternalChar;
190 ACPI_FUNCTION_ENTRY ();
193 NextExternalChar = Info->ExternalName;
195 Info->NumSegments = 0;
196 Info->FullyQualified = FALSE;
199 * For the internal name, the required length is 4 bytes per segment,
200 * plus 1 each for RootPrefix, MultiNamePrefixOp, segment count,
201 * trailing null (which is not really needed, but no there's harm in
204 * strlen() + 1 covers the first NameSeg, which has no path separator
206 if (ACPI_IS_ROOT_PREFIX (*NextExternalChar))
208 Info->FullyQualified = TRUE;
211 /* Skip redundant RootPrefix, like \\_SB.PCI0.SBRG.EC0 */
213 while (ACPI_IS_ROOT_PREFIX (*NextExternalChar))
220 /* Handle Carat prefixes */
222 while (ACPI_IS_PARENT_PREFIX (*NextExternalChar))
230 * Determine the number of ACPI name "segments" by counting the number of
231 * path separators within the string. Start with one segment since the
232 * segment count is [(# separators) + 1], and zero separators is ok.
234 if (*NextExternalChar)
236 Info->NumSegments = 1;
237 for (i = 0; NextExternalChar[i]; i++)
239 if (ACPI_IS_PATH_SEPARATOR (NextExternalChar[i]))
246 Info->Length = (ACPI_NAME_SIZE * Info->NumSegments) +
249 Info->NextExternalChar = NextExternalChar;
253 /*******************************************************************************
255 * FUNCTION: AcpiNsBuildInternalName
257 * PARAMETERS: Info - Info struct fully initialized
261 * DESCRIPTION: Construct the internal (AML) namestring
262 * corresponding to the external (ASL) namestring.
264 ******************************************************************************/
267 AcpiNsBuildInternalName (
268 ACPI_NAMESTRING_INFO *Info)
270 UINT32 NumSegments = Info->NumSegments;
271 char *InternalName = Info->InternalName;
272 const char *ExternalName = Info->NextExternalChar;
277 ACPI_FUNCTION_TRACE (NsBuildInternalName);
280 /* Setup the correct prefixes, counts, and pointers */
282 if (Info->FullyQualified)
284 InternalName[0] = AML_ROOT_PREFIX;
286 if (NumSegments <= 1)
288 Result = &InternalName[1];
290 else if (NumSegments == 2)
292 InternalName[1] = AML_DUAL_NAME_PREFIX;
293 Result = &InternalName[2];
297 InternalName[1] = AML_MULTI_NAME_PREFIX_OP;
298 InternalName[2] = (char) NumSegments;
299 Result = &InternalName[3];
305 * Not fully qualified.
306 * Handle Carats first, then append the name segments
311 for (i = 0; i < Info->NumCarats; i++)
313 InternalName[i] = AML_PARENT_PREFIX;
317 if (NumSegments <= 1)
319 Result = &InternalName[i];
321 else if (NumSegments == 2)
323 InternalName[i] = AML_DUAL_NAME_PREFIX;
324 Result = &InternalName[(ACPI_SIZE) i+1];
328 InternalName[i] = AML_MULTI_NAME_PREFIX_OP;
329 InternalName[(ACPI_SIZE) i+1] = (char) NumSegments;
330 Result = &InternalName[(ACPI_SIZE) i+2];
334 /* Build the name (minus path separators) */
336 for (; NumSegments; NumSegments--)
338 for (i = 0; i < ACPI_NAME_SIZE; i++)
340 if (ACPI_IS_PATH_SEPARATOR (*ExternalName) ||
341 (*ExternalName == 0))
343 /* Pad the segment with underscore(s) if segment is short */
349 /* Convert the character to uppercase and save it */
351 Result[i] = (char) toupper ((int) *ExternalName);
356 /* Now we must have a path separator, or the pathname is bad */
358 if (!ACPI_IS_PATH_SEPARATOR (*ExternalName) &&
359 (*ExternalName != 0))
361 return_ACPI_STATUS (AE_BAD_PATHNAME);
364 /* Move on the next segment */
367 Result += ACPI_NAME_SIZE;
370 /* Terminate the string */
374 if (Info->FullyQualified)
376 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (abs) \"\\%s\"\n",
377 InternalName, InternalName));
381 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
382 InternalName, InternalName));
385 return_ACPI_STATUS (AE_OK);
389 /*******************************************************************************
391 * FUNCTION: AcpiNsInternalizeName
393 * PARAMETERS: *ExternalName - External representation of name
394 * **Converted Name - Where to return the resulting
395 * internal represention of the name
399 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
400 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
402 *******************************************************************************/
405 AcpiNsInternalizeName (
406 const char *ExternalName,
407 char **ConvertedName)
410 ACPI_NAMESTRING_INFO Info;
414 ACPI_FUNCTION_TRACE (NsInternalizeName);
417 if ((!ExternalName) ||
418 (*ExternalName == 0) ||
421 return_ACPI_STATUS (AE_BAD_PARAMETER);
424 /* Get the length of the new internal name */
426 Info.ExternalName = ExternalName;
427 AcpiNsGetInternalNameLength (&Info);
429 /* We need a segment to store the internal name */
431 InternalName = ACPI_ALLOCATE_ZEROED (Info.Length);
434 return_ACPI_STATUS (AE_NO_MEMORY);
439 Info.InternalName = InternalName;
440 Status = AcpiNsBuildInternalName (&Info);
441 if (ACPI_FAILURE (Status))
443 ACPI_FREE (InternalName);
444 return_ACPI_STATUS (Status);
447 *ConvertedName = InternalName;
448 return_ACPI_STATUS (AE_OK);
452 /*******************************************************************************
454 * FUNCTION: AcpiNsExternalizeName
456 * PARAMETERS: InternalNameLength - Lenth of the internal name below
457 * InternalName - Internal representation of name
458 * ConvertedNameLength - Where the length is returned
459 * ConvertedName - Where the resulting external name
464 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
465 * to its external (printable) form (e.g. "\_PR_.CPU0")
467 ******************************************************************************/
470 AcpiNsExternalizeName (
471 UINT32 InternalNameLength,
472 const char *InternalName,
473 UINT32 *ConvertedNameLength,
474 char **ConvertedName)
476 UINT32 NamesIndex = 0;
477 UINT32 NumSegments = 0;
478 UINT32 RequiredLength;
479 UINT32 PrefixLength = 0;
484 ACPI_FUNCTION_TRACE (NsExternalizeName);
487 if (!InternalNameLength ||
491 return_ACPI_STATUS (AE_BAD_PARAMETER);
494 /* Check for a prefix (one '\' | one or more '^') */
496 switch (InternalName[0])
498 case AML_ROOT_PREFIX:
503 case AML_PARENT_PREFIX:
505 for (i = 0; i < InternalNameLength; i++)
507 if (ACPI_IS_PARENT_PREFIX (InternalName[i]))
509 PrefixLength = i + 1;
517 if (i == InternalNameLength)
530 * Check for object names. Note that there could be 0-255 of these
533 if (PrefixLength < InternalNameLength)
535 switch (InternalName[PrefixLength])
537 case AML_MULTI_NAME_PREFIX_OP:
539 /* <count> 4-byte names */
541 NamesIndex = PrefixLength + 2;
542 NumSegments = (UINT8)
543 InternalName[(ACPI_SIZE) PrefixLength + 1];
546 case AML_DUAL_NAME_PREFIX:
548 /* Two 4-byte names */
550 NamesIndex = PrefixLength + 1;
564 /* one 4-byte name */
566 NamesIndex = PrefixLength;
573 * Calculate the length of ConvertedName, which equals the length
574 * of the prefix, length of all object names, length of any required
575 * punctuation ('.') between object names, plus the NULL terminator.
577 RequiredLength = PrefixLength + (4 * NumSegments) +
578 ((NumSegments > 0) ? (NumSegments - 1) : 0) + 1;
581 * Check to see if we're still in bounds. If not, there's a problem
582 * with InternalName (invalid format).
584 if (RequiredLength > InternalNameLength)
586 ACPI_ERROR ((AE_INFO, "Invalid internal name"));
587 return_ACPI_STATUS (AE_BAD_PATHNAME);
590 /* Build the ConvertedName */
592 *ConvertedName = ACPI_ALLOCATE_ZEROED (RequiredLength);
593 if (!(*ConvertedName))
595 return_ACPI_STATUS (AE_NO_MEMORY);
600 for (i = 0; i < PrefixLength; i++)
602 (*ConvertedName)[j++] = InternalName[i];
607 for (i = 0; i < NumSegments; i++)
611 (*ConvertedName)[j++] = '.';
614 /* Copy and validate the 4-char name segment */
616 ACPI_MOVE_NAME (&(*ConvertedName)[j],
617 &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);
704 ACPI_OPERAND_OBJECT *Prev;
705 ACPI_OPERAND_OBJECT *Next;
707 /* Delete any module-level code blocks */
709 Next = AcpiGbl_ModuleCodeList;
713 Next = Next->Method.Mutex;
714 Prev->Method.Mutex = NULL; /* Clear the Mutex (cheated) field */
715 AcpiUtRemoveReference (Prev);
721 * Free the entire namespace -- all nodes and all objects
722 * attached to the nodes
724 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
726 /* Delete any objects attached to the root node */
728 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
729 if (ACPI_FAILURE (Status))
734 AcpiNsDeleteNode (AcpiGbl_RootNode);
735 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
737 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Namespace freed\n"));
742 /*******************************************************************************
744 * FUNCTION: AcpiNsOpensScope
746 * PARAMETERS: Type - A valid namespace type
748 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
749 * to the ACPI specification, else 0
751 ******************************************************************************/
755 ACPI_OBJECT_TYPE Type)
757 ACPI_FUNCTION_ENTRY ();
760 if (Type > ACPI_TYPE_LOCAL_MAX)
762 /* type code out of range */
764 ACPI_WARNING ((AE_INFO, "Invalid Object Type 0x%X", Type));
765 return (ACPI_NS_NORMAL);
768 return (((UINT32) AcpiGbl_NsProperties[Type]) & ACPI_NS_NEWSCOPE);
772 /*******************************************************************************
774 * FUNCTION: AcpiNsGetNode
776 * PARAMETERS: *Pathname - Name to be found, in external (ASL) format. The
777 * \ (backslash) and ^ (carat) prefixes, and the
778 * . (period) to separate segments are supported.
779 * PrefixNode - Root of subtree to be searched, or NS_ALL for the
780 * root of the name space. If Name is fully
781 * qualified (first INT8 is '\'), the passed value
782 * of Scope will not be accessed.
783 * Flags - Used to indicate whether to perform upsearch or
785 * ReturnNode - Where the Node is returned
787 * DESCRIPTION: Look up a name relative to a given scope and return the
788 * corresponding Node. NOTE: Scope can be null.
790 * MUTEX: Locks namespace
792 ******************************************************************************/
796 ACPI_NAMESPACE_NODE *PrefixNode,
797 const char *Pathname,
799 ACPI_NAMESPACE_NODE **ReturnNode)
801 ACPI_GENERIC_STATE ScopeInfo;
806 ACPI_FUNCTION_TRACE_PTR (NsGetNode, ACPI_CAST_PTR (char, Pathname));
809 /* Simplest case is a null pathname */
813 *ReturnNode = PrefixNode;
816 *ReturnNode = AcpiGbl_RootNode;
819 return_ACPI_STATUS (AE_OK);
822 /* Quick check for a reference to the root */
824 if (ACPI_IS_ROOT_PREFIX (Pathname[0]) && (!Pathname[1]))
826 *ReturnNode = AcpiGbl_RootNode;
827 return_ACPI_STATUS (AE_OK);
830 /* Convert path to internal representation */
832 Status = AcpiNsInternalizeName (Pathname, &InternalPath);
833 if (ACPI_FAILURE (Status))
835 return_ACPI_STATUS (Status);
838 /* Must lock namespace during lookup */
840 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
841 if (ACPI_FAILURE (Status))
846 /* Setup lookup scope (search starting point) */
848 ScopeInfo.Scope.Node = PrefixNode;
850 /* Lookup the name in the namespace */
852 Status = AcpiNsLookup (&ScopeInfo, InternalPath, ACPI_TYPE_ANY,
853 ACPI_IMODE_EXECUTE, (Flags | ACPI_NS_DONT_OPEN_SCOPE),
855 if (ACPI_FAILURE (Status))
857 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s, %s\n",
858 Pathname, AcpiFormatException (Status)));
861 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
864 ACPI_FREE (InternalPath);
865 return_ACPI_STATUS (Status);