1 /******************************************************************************
3 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4 * parents and siblings and Scope manipulation
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, 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.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsutils")
55 /* Local prototypes */
58 AcpiNsValidPathSeparator (
61 #ifdef ACPI_OBSOLETE_FUNCTIONS
63 AcpiNsFindParentName (
64 ACPI_NAMESPACE_NODE *NodeToSearch);
68 /*******************************************************************************
70 * FUNCTION: AcpiNsPrintNodePathname
72 * PARAMETERS: Node - Object
73 * Message - Prefix message
75 * DESCRIPTION: Print an object's full namespace pathname
76 * Manages allocation/freeing of a pathname buffer
78 ******************************************************************************/
81 AcpiNsPrintNodePathname (
82 ACPI_NAMESPACE_NODE *Node,
91 AcpiOsPrintf ("[NULL NAME]");
95 /* Convert handle to full pathname and print it (with supplied message) */
97 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
99 Status = AcpiNsHandleToPathname (Node, &Buffer);
100 if (ACPI_SUCCESS (Status))
104 AcpiOsPrintf ("%s ", Message);
107 AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer.Pointer, Node);
108 ACPI_FREE (Buffer.Pointer);
113 /*******************************************************************************
115 * FUNCTION: AcpiNsValidRootPrefix
117 * PARAMETERS: Prefix - Character to be checked
119 * RETURN: TRUE if a valid prefix
121 * DESCRIPTION: Check if a character is a valid ACPI Root prefix
123 ******************************************************************************/
126 AcpiNsValidRootPrefix (
130 return ((BOOLEAN) (Prefix == '\\'));
134 /*******************************************************************************
136 * FUNCTION: AcpiNsValidPathSeparator
138 * PARAMETERS: Sep - Character to be checked
140 * RETURN: TRUE if a valid path separator
142 * DESCRIPTION: Check if a character is a valid ACPI path separator
144 ******************************************************************************/
147 AcpiNsValidPathSeparator (
151 return ((BOOLEAN) (Sep == '.'));
155 /*******************************************************************************
157 * FUNCTION: AcpiNsGetType
159 * PARAMETERS: Node - Parent Node to be examined
161 * RETURN: Type field from Node whose handle is passed
163 * DESCRIPTION: Return the type of a Namespace node
165 ******************************************************************************/
169 ACPI_NAMESPACE_NODE *Node)
171 ACPI_FUNCTION_TRACE (NsGetType);
176 ACPI_WARNING ((AE_INFO, "Null Node parameter"));
177 return_UINT32 (ACPI_TYPE_ANY);
180 return_UINT32 ((ACPI_OBJECT_TYPE) Node->Type);
184 /*******************************************************************************
186 * FUNCTION: AcpiNsLocal
188 * PARAMETERS: Type - A namespace object type
190 * RETURN: LOCAL if names must be found locally in objects of the
191 * passed type, 0 if enclosing scopes should be searched
193 * DESCRIPTION: Returns scope rule for the given object type.
195 ******************************************************************************/
199 ACPI_OBJECT_TYPE Type)
201 ACPI_FUNCTION_TRACE (NsLocal);
204 if (!AcpiUtValidObjectType (Type))
206 /* Type code out of range */
208 ACPI_WARNING ((AE_INFO, "Invalid Object Type 0x%X", Type));
209 return_UINT32 (ACPI_NS_NORMAL);
212 return_UINT32 ((UINT32) AcpiGbl_NsProperties[Type] & ACPI_NS_LOCAL);
216 /*******************************************************************************
218 * FUNCTION: AcpiNsGetInternalNameLength
220 * PARAMETERS: Info - Info struct initialized with the
221 * external name pointer.
225 * DESCRIPTION: Calculate the length of the internal (AML) namestring
226 * corresponding to the external (ASL) namestring.
228 ******************************************************************************/
231 AcpiNsGetInternalNameLength (
232 ACPI_NAMESTRING_INFO *Info)
234 const char *NextExternalChar;
238 ACPI_FUNCTION_ENTRY ();
241 NextExternalChar = Info->ExternalName;
243 Info->NumSegments = 0;
244 Info->FullyQualified = FALSE;
247 * For the internal name, the required length is 4 bytes per segment, plus
248 * 1 each for RootPrefix, MultiNamePrefixOp, segment count, trailing null
249 * (which is not really needed, but no there's harm in putting it there)
251 * strlen() + 1 covers the first NameSeg, which has no path separator
253 if (AcpiNsValidRootPrefix (*NextExternalChar))
255 Info->FullyQualified = TRUE;
258 /* Skip redundant RootPrefix, like \\_SB.PCI0.SBRG.EC0 */
260 while (AcpiNsValidRootPrefix (*NextExternalChar))
267 /* Handle Carat prefixes */
269 while (*NextExternalChar == '^')
277 * Determine the number of ACPI name "segments" by counting the number of
278 * path separators within the string. Start with one segment since the
279 * segment count is [(# separators) + 1], and zero separators is ok.
281 if (*NextExternalChar)
283 Info->NumSegments = 1;
284 for (i = 0; NextExternalChar[i]; i++)
286 if (AcpiNsValidPathSeparator (NextExternalChar[i]))
293 Info->Length = (ACPI_NAME_SIZE * Info->NumSegments) +
296 Info->NextExternalChar = NextExternalChar;
300 /*******************************************************************************
302 * FUNCTION: AcpiNsBuildInternalName
304 * PARAMETERS: Info - Info struct fully initialized
308 * DESCRIPTION: Construct the internal (AML) namestring
309 * corresponding to the external (ASL) namestring.
311 ******************************************************************************/
314 AcpiNsBuildInternalName (
315 ACPI_NAMESTRING_INFO *Info)
317 UINT32 NumSegments = Info->NumSegments;
318 char *InternalName = Info->InternalName;
319 const char *ExternalName = Info->NextExternalChar;
324 ACPI_FUNCTION_TRACE (NsBuildInternalName);
327 /* Setup the correct prefixes, counts, and pointers */
329 if (Info->FullyQualified)
331 InternalName[0] = '\\';
333 if (NumSegments <= 1)
335 Result = &InternalName[1];
337 else if (NumSegments == 2)
339 InternalName[1] = AML_DUAL_NAME_PREFIX;
340 Result = &InternalName[2];
344 InternalName[1] = AML_MULTI_NAME_PREFIX_OP;
345 InternalName[2] = (char) NumSegments;
346 Result = &InternalName[3];
352 * Not fully qualified.
353 * Handle Carats first, then append the name segments
358 for (i = 0; i < Info->NumCarats; i++)
360 InternalName[i] = '^';
364 if (NumSegments <= 1)
366 Result = &InternalName[i];
368 else if (NumSegments == 2)
370 InternalName[i] = AML_DUAL_NAME_PREFIX;
371 Result = &InternalName[(ACPI_SIZE) i+1];
375 InternalName[i] = AML_MULTI_NAME_PREFIX_OP;
376 InternalName[(ACPI_SIZE) i+1] = (char) NumSegments;
377 Result = &InternalName[(ACPI_SIZE) i+2];
381 /* Build the name (minus path separators) */
383 for (; NumSegments; NumSegments--)
385 for (i = 0; i < ACPI_NAME_SIZE; i++)
387 if (AcpiNsValidPathSeparator (*ExternalName) ||
388 (*ExternalName == 0))
390 /* Pad the segment with underscore(s) if segment is short */
396 /* Convert the character to uppercase and save it */
398 Result[i] = (char) ACPI_TOUPPER ((int) *ExternalName);
403 /* Now we must have a path separator, or the pathname is bad */
405 if (!AcpiNsValidPathSeparator (*ExternalName) &&
406 (*ExternalName != 0))
408 return_ACPI_STATUS (AE_BAD_PARAMETER);
411 /* Move on the next segment */
414 Result += ACPI_NAME_SIZE;
417 /* Terminate the string */
421 if (Info->FullyQualified)
423 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (abs) \"\\%s\"\n",
424 InternalName, InternalName));
428 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
429 InternalName, InternalName));
432 return_ACPI_STATUS (AE_OK);
436 /*******************************************************************************
438 * FUNCTION: AcpiNsInternalizeName
440 * PARAMETERS: *ExternalName - External representation of name
441 * **Converted Name - Where to return the resulting
442 * internal represention of the name
446 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
447 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
449 *******************************************************************************/
452 AcpiNsInternalizeName (
453 const char *ExternalName,
454 char **ConvertedName)
457 ACPI_NAMESTRING_INFO Info;
461 ACPI_FUNCTION_TRACE (NsInternalizeName);
464 if ((!ExternalName) ||
465 (*ExternalName == 0) ||
468 return_ACPI_STATUS (AE_BAD_PARAMETER);
471 /* Get the length of the new internal name */
473 Info.ExternalName = ExternalName;
474 AcpiNsGetInternalNameLength (&Info);
476 /* We need a segment to store the internal name */
478 InternalName = ACPI_ALLOCATE_ZEROED (Info.Length);
481 return_ACPI_STATUS (AE_NO_MEMORY);
486 Info.InternalName = InternalName;
487 Status = AcpiNsBuildInternalName (&Info);
488 if (ACPI_FAILURE (Status))
490 ACPI_FREE (InternalName);
491 return_ACPI_STATUS (Status);
494 *ConvertedName = InternalName;
495 return_ACPI_STATUS (AE_OK);
499 /*******************************************************************************
501 * FUNCTION: AcpiNsExternalizeName
503 * PARAMETERS: InternalNameLength - Lenth of the internal name below
504 * InternalName - Internal representation of name
505 * ConvertedNameLength - Where the length is returned
506 * ConvertedName - Where the resulting external name
511 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
512 * to its external (printable) form (e.g. "\_PR_.CPU0")
514 ******************************************************************************/
517 AcpiNsExternalizeName (
518 UINT32 InternalNameLength,
519 const char *InternalName,
520 UINT32 *ConvertedNameLength,
521 char **ConvertedName)
523 UINT32 NamesIndex = 0;
524 UINT32 NumSegments = 0;
525 UINT32 RequiredLength;
526 UINT32 PrefixLength = 0;
531 ACPI_FUNCTION_TRACE (NsExternalizeName);
534 if (!InternalNameLength ||
538 return_ACPI_STATUS (AE_BAD_PARAMETER);
541 /* Check for a prefix (one '\' | one or more '^') */
543 switch (InternalName[0])
550 for (i = 0; i < InternalNameLength; i++)
552 if (InternalName[i] == '^')
554 PrefixLength = i + 1;
562 if (i == InternalNameLength)
574 * Check for object names. Note that there could be 0-255 of these
577 if (PrefixLength < InternalNameLength)
579 switch (InternalName[PrefixLength])
581 case AML_MULTI_NAME_PREFIX_OP:
583 /* <count> 4-byte names */
585 NamesIndex = PrefixLength + 2;
586 NumSegments = (UINT8)
587 InternalName[(ACPI_SIZE) PrefixLength + 1];
590 case AML_DUAL_NAME_PREFIX:
592 /* Two 4-byte names */
594 NamesIndex = PrefixLength + 1;
608 /* one 4-byte name */
610 NamesIndex = PrefixLength;
617 * Calculate the length of ConvertedName, which equals the length
618 * of the prefix, length of all object names, length of any required
619 * punctuation ('.') between object names, plus the NULL terminator.
621 RequiredLength = PrefixLength + (4 * NumSegments) +
622 ((NumSegments > 0) ? (NumSegments - 1) : 0) + 1;
625 * Check to see if we're still in bounds. If not, there's a problem
626 * with InternalName (invalid format).
628 if (RequiredLength > InternalNameLength)
630 ACPI_ERROR ((AE_INFO, "Invalid internal name"));
631 return_ACPI_STATUS (AE_BAD_PATHNAME);
634 /* Build the ConvertedName */
636 *ConvertedName = ACPI_ALLOCATE_ZEROED (RequiredLength);
637 if (!(*ConvertedName))
639 return_ACPI_STATUS (AE_NO_MEMORY);
644 for (i = 0; i < PrefixLength; i++)
646 (*ConvertedName)[j++] = InternalName[i];
651 for (i = 0; i < NumSegments; i++)
655 (*ConvertedName)[j++] = '.';
658 (*ConvertedName)[j++] = InternalName[NamesIndex++];
659 (*ConvertedName)[j++] = InternalName[NamesIndex++];
660 (*ConvertedName)[j++] = InternalName[NamesIndex++];
661 (*ConvertedName)[j++] = InternalName[NamesIndex++];
665 if (ConvertedNameLength)
667 *ConvertedNameLength = (UINT32) RequiredLength;
670 return_ACPI_STATUS (AE_OK);
674 /*******************************************************************************
676 * FUNCTION: AcpiNsValidateHandle
678 * PARAMETERS: Handle - Handle to be validated and typecast to a
681 * RETURN: A pointer to a namespace node
683 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
684 * cases for the root node.
686 * NOTE: Real integer handles would allow for more verification
687 * and keep all pointers within this subsystem - however this introduces
688 * more overhead and has not been necessary to this point. Drivers
689 * holding handles are typically notified before a node becomes invalid
690 * due to a table unload.
692 ******************************************************************************/
694 ACPI_NAMESPACE_NODE *
695 AcpiNsValidateHandle (
699 ACPI_FUNCTION_ENTRY ();
702 /* Parameter validation */
704 if ((!Handle) || (Handle == ACPI_ROOT_OBJECT))
706 return (AcpiGbl_RootNode);
709 /* We can at least attempt to verify the handle */
711 if (ACPI_GET_DESCRIPTOR_TYPE (Handle) != ACPI_DESC_TYPE_NAMED)
716 return (ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Handle));
720 /*******************************************************************************
722 * FUNCTION: AcpiNsTerminate
728 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
730 ******************************************************************************/
736 ACPI_OPERAND_OBJECT *ObjDesc;
739 ACPI_FUNCTION_TRACE (NsTerminate);
743 * 1) Free the entire namespace -- all nodes and objects
745 * Delete all object descriptors attached to namepsace nodes
747 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
749 /* Detach any objects attached to the root */
751 ObjDesc = AcpiNsGetAttachedObject (AcpiGbl_RootNode);
754 AcpiNsDetachObject (AcpiGbl_RootNode);
757 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Namespace freed\n"));
762 /*******************************************************************************
764 * FUNCTION: AcpiNsOpensScope
766 * PARAMETERS: Type - A valid namespace type
768 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
769 * to the ACPI specification, else 0
771 ******************************************************************************/
775 ACPI_OBJECT_TYPE Type)
777 ACPI_FUNCTION_TRACE_STR (NsOpensScope, AcpiUtGetTypeName (Type));
780 if (!AcpiUtValidObjectType (Type))
782 /* type code out of range */
784 ACPI_WARNING ((AE_INFO, "Invalid Object Type 0x%X", Type));
785 return_UINT32 (ACPI_NS_NORMAL);
788 return_UINT32 (((UINT32) AcpiGbl_NsProperties[Type]) & ACPI_NS_NEWSCOPE);
792 /*******************************************************************************
794 * FUNCTION: AcpiNsGetNode
796 * PARAMETERS: *Pathname - Name to be found, in external (ASL) format. The
797 * \ (backslash) and ^ (carat) prefixes, and the
798 * . (period) to separate segments are supported.
799 * PrefixNode - Root of subtree to be searched, or NS_ALL for the
800 * root of the name space. If Name is fully
801 * qualified (first INT8 is '\'), the passed value
802 * of Scope will not be accessed.
803 * Flags - Used to indicate whether to perform upsearch or
805 * ReturnNode - Where the Node is returned
807 * DESCRIPTION: Look up a name relative to a given scope and return the
808 * corresponding Node. NOTE: Scope can be null.
810 * MUTEX: Locks namespace
812 ******************************************************************************/
816 ACPI_NAMESPACE_NODE *PrefixNode,
817 const char *Pathname,
819 ACPI_NAMESPACE_NODE **ReturnNode)
821 ACPI_GENERIC_STATE ScopeInfo;
826 ACPI_FUNCTION_TRACE_PTR (NsGetNode, ACPI_CAST_PTR (char, Pathname));
831 *ReturnNode = PrefixNode;
834 *ReturnNode = AcpiGbl_RootNode;
836 return_ACPI_STATUS (AE_OK);
839 /* Convert path to internal representation */
841 Status = AcpiNsInternalizeName (Pathname, &InternalPath);
842 if (ACPI_FAILURE (Status))
844 return_ACPI_STATUS (Status);
847 /* Must lock namespace during lookup */
849 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
850 if (ACPI_FAILURE (Status))
855 /* Setup lookup scope (search starting point) */
857 ScopeInfo.Scope.Node = PrefixNode;
859 /* Lookup the name in the namespace */
861 Status = AcpiNsLookup (&ScopeInfo, InternalPath, ACPI_TYPE_ANY,
862 ACPI_IMODE_EXECUTE, (Flags | ACPI_NS_DONT_OPEN_SCOPE),
864 if (ACPI_FAILURE (Status))
866 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s, %s\n",
867 Pathname, AcpiFormatException (Status)));
870 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
873 ACPI_FREE (InternalPath);
874 return_ACPI_STATUS (Status);