]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/nsutils.c
This commit was generated by cvs2svn to compensate for changes in r162911,
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / nsutils.c
1 /******************************************************************************
2  *
3  * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4  *                        parents and siblings and Scope manipulation
5  *              $Revision: 1.141 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
18  * 2.1. This is your license from Intel Corp. under its intellectual property
19  * rights.  You may have additional license terms from the party that provided
20  * you this software, covering your right to use that party's intellectual
21  * property rights.
22  *
23  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24  * copy of the source code appearing in this file ("Covered Code") an
25  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26  * base code distributed originally by Intel ("Original Intel Code") to copy,
27  * make derivatives, distribute, use and display any portion of the Covered
28  * Code in any form, with the right to sublicense such rights; and
29  *
30  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31  * license (with the right to sublicense), under only those claims of Intel
32  * patents that are infringed by the Original Intel Code, to make, use, sell,
33  * offer to sell, and import the Covered Code and derivative works thereof
34  * solely to the minimum extent necessary to exercise the above copyright
35  * license, and in no event shall the patent license extend to any additions
36  * to or modifications of the Original Intel Code.  No other license or right
37  * is granted directly or by implication, estoppel or otherwise;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
44  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45  * Redistribution of source code of any substantial portion of the Covered
46  * Code or modification with rights to further distribute source must include
47  * the above Copyright Notice, the above License, this list of Conditions,
48  * and the following Disclaimer and Export Compliance provision.  In addition,
49  * Licensee must cause all Covered Code to which Licensee contributes to
50  * contain a file documenting the changes Licensee made to create that Covered
51  * Code and the date of any change.  Licensee must include in that file the
52  * documentation of any changes made by any predecessor Licensee.  Licensee
53  * must include a prominent statement that the modification is derived,
54  * directly or indirectly, from Original Intel Code.
55  *
56  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57  * Redistribution of source code of any substantial portion of the Covered
58  * Code or modification without rights to further distribute source must
59  * include the following Disclaimer and Export Compliance provision in the
60  * documentation and/or other materials provided with distribution.  In
61  * addition, Licensee may not authorize further sublicense of source of any
62  * portion of the Covered Code, and must include terms to the effect that the
63  * license from Licensee to its licensee is limited to the intellectual
64  * property embodied in the software Licensee provides to its licensee, and
65  * not to intellectual property embodied in modifications its licensee may
66  * make.
67  *
68  * 3.3. Redistribution of Executable. Redistribution in executable form of any
69  * substantial portion of the Covered Code or modification must reproduce the
70  * above Copyright Notice, and the following Disclaimer and Export Compliance
71  * provision in the documentation and/or other materials provided with the
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
77  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78  * Intel shall be used in advertising or otherwise to promote the sale, use or
79  * other dealings in products derived from or relating to the Covered Code
80  * without prior written authorization from Intel.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
84  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90  * PARTICULAR PURPOSE.
91  *
92  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99  * LIMITED REMEDY.
100  *
101  * 4.3. Licensee shall not export, either directly or indirectly, any of this
102  * software or system incorporating such software without first obtaining any
103  * required license or other approval from the U. S. Department of Commerce or
104  * any other agency or department of the United States Government.  In the
105  * event Licensee exports any such software from the United States or
106  * re-exports any such software from a foreign destination, Licensee shall
107  * ensure that the distribution and export/re-export of the software is in
108  * compliance with all laws, regulations, orders, or other restrictions of the
109  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110  * any of its subsidiaries will export/re-export any technical data, process,
111  * software, or service, directly or indirectly, to any country for which the
112  * United States government or any agency thereof requires an export license,
113  * other governmental approval, or letter of assurance, without first obtaining
114  * such license, approval or letter.
115  *
116  *****************************************************************************/
117
118 #define __NSUTILS_C__
119
120 #include <contrib/dev/acpica/acpi.h>
121 #include <contrib/dev/acpica/acnamesp.h>
122 #include <contrib/dev/acpica/amlcode.h>
123 #include <contrib/dev/acpica/actables.h>
124
125 #define _COMPONENT          ACPI_NAMESPACE
126         ACPI_MODULE_NAME    ("nsutils")
127
128 /* Local prototypes */
129
130 static BOOLEAN
131 AcpiNsValidPathSeparator (
132     char                    Sep);
133
134 #ifdef ACPI_OBSOLETE_FUNCTIONS
135 ACPI_NAME
136 AcpiNsFindParentName (
137     ACPI_NAMESPACE_NODE     *NodeToSearch);
138 #endif
139
140
141 /*******************************************************************************
142  *
143  * FUNCTION:    AcpiNsReportError
144  *
145  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
146  *              LineNumber          - Caller's line number (for error output)
147  *              ComponentId         - Caller's component ID (for error output)
148  *              InternalName        - Name or path of the namespace node
149  *              LookupStatus        - Exception code from NS lookup
150  *
151  * RETURN:      None
152  *
153  * DESCRIPTION: Print warning message with full pathname
154  *
155  ******************************************************************************/
156
157 void
158 AcpiNsReportError (
159     char                    *ModuleName,
160     UINT32                  LineNumber,
161     UINT32                  ComponentId,
162     char                    *InternalName,
163     ACPI_STATUS             LookupStatus)
164 {
165     ACPI_STATUS             Status;
166     char                    *Name = NULL;
167
168
169     AcpiOsPrintf ("%8s-%04d: *** Error: Looking up ",
170         ModuleName, LineNumber);
171
172     if (LookupStatus == AE_BAD_CHARACTER)
173     {
174         /* There is a non-ascii character in the name */
175
176         AcpiOsPrintf ("[0x%4.4X] (NON-ASCII)\n",
177             *(ACPI_CAST_PTR (UINT32, InternalName)));
178     }
179     else
180     {
181         /* Convert path to external format */
182
183         Status = AcpiNsExternalizeName (ACPI_UINT32_MAX,
184                     InternalName, NULL, &Name);
185
186         /* Print target name */
187
188         if (ACPI_SUCCESS (Status))
189         {
190             AcpiOsPrintf ("[%s]", Name);
191         }
192         else
193         {
194             AcpiOsPrintf ("[COULD NOT EXTERNALIZE NAME]");
195         }
196
197         if (Name)
198         {
199             ACPI_MEM_FREE (Name);
200         }
201     }
202
203     AcpiOsPrintf (" in namespace, %s\n",
204         AcpiFormatException (LookupStatus));
205 }
206
207
208 /*******************************************************************************
209  *
210  * FUNCTION:    AcpiNsReportMethodError
211  *
212  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
213  *              LineNumber          - Caller's line number (for error output)
214  *              ComponentId         - Caller's component ID (for error output)
215  *              Message             - Error message to use on failure
216  *              PrefixNode          - Prefix relative to the path
217  *              Path                - Path to the node
218  *              MethodStatus        - Execution status
219  *
220  * RETURN:      None
221  *
222  * DESCRIPTION: Print warning message with full pathname
223  *
224  ******************************************************************************/
225
226 void
227 AcpiNsReportMethodError (
228     char                    *ModuleName,
229     UINT32                  LineNumber,
230     UINT32                  ComponentId,
231     char                    *Message,
232     ACPI_NAMESPACE_NODE     *PrefixNode,
233     char                    *Path,
234     ACPI_STATUS             MethodStatus)
235 {
236     ACPI_STATUS             Status;
237     ACPI_NAMESPACE_NODE     *Node = PrefixNode;
238
239
240     if (Path)
241     {
242         Status = AcpiNsGetNodeByPath (Path, PrefixNode,
243                     ACPI_NS_NO_UPSEARCH, &Node);
244         if (ACPI_FAILURE (Status))
245         {
246             AcpiOsPrintf ("ReportMethodError: Could not get node\n");
247             return;
248         }
249     }
250
251     AcpiOsPrintf ("%8s-%04d: *** Error: ", ModuleName, LineNumber);
252     AcpiNsPrintNodePathname (Node, Message);
253     AcpiOsPrintf (", %s\n", AcpiFormatException (MethodStatus));
254 }
255
256
257 /*******************************************************************************
258  *
259  * FUNCTION:    AcpiNsPrintNodePathname
260  *
261  * PARAMETERS:  Node            - Object
262  *              Message         - Prefix message
263  *
264  * DESCRIPTION: Print an object's full namespace pathname
265  *              Manages allocation/freeing of a pathname buffer
266  *
267  ******************************************************************************/
268
269 void
270 AcpiNsPrintNodePathname (
271     ACPI_NAMESPACE_NODE     *Node,
272     char                    *Message)
273 {
274     ACPI_BUFFER             Buffer;
275     ACPI_STATUS             Status;
276
277
278     if (!Node)
279     {
280         AcpiOsPrintf ("[NULL NAME]");
281         return;
282     }
283
284     /* Convert handle to full pathname and print it (with supplied message) */
285
286     Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
287
288     Status = AcpiNsHandleToPathname (Node, &Buffer);
289     if (ACPI_SUCCESS (Status))
290     {
291         if (Message)
292         {
293             AcpiOsPrintf ("%s ", Message);
294         }
295
296         AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer.Pointer, Node);
297         ACPI_MEM_FREE (Buffer.Pointer);
298     }
299 }
300
301
302 /*******************************************************************************
303  *
304  * FUNCTION:    AcpiNsValidRootPrefix
305  *
306  * PARAMETERS:  Prefix          - Character to be checked
307  *
308  * RETURN:      TRUE if a valid prefix
309  *
310  * DESCRIPTION: Check if a character is a valid ACPI Root prefix
311  *
312  ******************************************************************************/
313
314 BOOLEAN
315 AcpiNsValidRootPrefix (
316     char                    Prefix)
317 {
318
319     return ((BOOLEAN) (Prefix == '\\'));
320 }
321
322
323 /*******************************************************************************
324  *
325  * FUNCTION:    AcpiNsValidPathSeparator
326  *
327  * PARAMETERS:  Sep         - Character to be checked
328  *
329  * RETURN:      TRUE if a valid path separator
330  *
331  * DESCRIPTION: Check if a character is a valid ACPI path separator
332  *
333  ******************************************************************************/
334
335 static BOOLEAN
336 AcpiNsValidPathSeparator (
337     char                    Sep)
338 {
339
340     return ((BOOLEAN) (Sep == '.'));
341 }
342
343
344 /*******************************************************************************
345  *
346  * FUNCTION:    AcpiNsGetType
347  *
348  * PARAMETERS:  Node        - Parent Node to be examined
349  *
350  * RETURN:      Type field from Node whose handle is passed
351  *
352  * DESCRIPTION: Return the type of a Namespace node
353  *
354  ******************************************************************************/
355
356 ACPI_OBJECT_TYPE
357 AcpiNsGetType (
358     ACPI_NAMESPACE_NODE     *Node)
359 {
360     ACPI_FUNCTION_TRACE ("NsGetType");
361
362
363     if (!Node)
364     {
365         ACPI_REPORT_WARNING (("NsGetType: Null Node input pointer\n"));
366         return_UINT32 (ACPI_TYPE_ANY);
367     }
368
369     return_UINT32 ((ACPI_OBJECT_TYPE) Node->Type);
370 }
371
372
373 /*******************************************************************************
374  *
375  * FUNCTION:    AcpiNsLocal
376  *
377  * PARAMETERS:  Type        - A namespace object type
378  *
379  * RETURN:      LOCAL if names must be found locally in objects of the
380  *              passed type, 0 if enclosing scopes should be searched
381  *
382  * DESCRIPTION: Returns scope rule for the given object type.
383  *
384  ******************************************************************************/
385
386 UINT32
387 AcpiNsLocal (
388     ACPI_OBJECT_TYPE        Type)
389 {
390     ACPI_FUNCTION_TRACE ("NsLocal");
391
392
393     if (!AcpiUtValidObjectType (Type))
394     {
395         /* Type code out of range  */
396
397         ACPI_REPORT_WARNING (("NsLocal: Invalid Object Type\n"));
398         return_UINT32 (ACPI_NS_NORMAL);
399     }
400
401     return_UINT32 ((UINT32) AcpiGbl_NsProperties[Type] & ACPI_NS_LOCAL);
402 }
403
404
405 /*******************************************************************************
406  *
407  * FUNCTION:    AcpiNsGetInternalNameLength
408  *
409  * PARAMETERS:  Info            - Info struct initialized with the
410  *                                external name pointer.
411  *
412  * RETURN:      None
413  *
414  * DESCRIPTION: Calculate the length of the internal (AML) namestring
415  *              corresponding to the external (ASL) namestring.
416  *
417  ******************************************************************************/
418
419 void
420 AcpiNsGetInternalNameLength (
421     ACPI_NAMESTRING_INFO    *Info)
422 {
423     char                    *NextExternalChar;
424     UINT32                  i;
425
426
427     ACPI_FUNCTION_ENTRY ();
428
429
430     NextExternalChar = Info->ExternalName;
431     Info->NumCarats = 0;
432     Info->NumSegments = 0;
433     Info->FullyQualified = FALSE;
434
435     /*
436      * For the internal name, the required length is 4 bytes per segment, plus
437      * 1 each for RootPrefix, MultiNamePrefixOp, segment count, trailing null
438      * (which is not really needed, but no there's harm in putting it there)
439      *
440      * strlen() + 1 covers the first NameSeg, which has no path separator
441      */
442     if (AcpiNsValidRootPrefix (NextExternalChar[0]))
443     {
444         Info->FullyQualified = TRUE;
445         NextExternalChar++;
446     }
447     else
448     {
449         /*
450          * Handle Carat prefixes
451          */
452         while (*NextExternalChar == '^')
453         {
454             Info->NumCarats++;
455             NextExternalChar++;
456         }
457     }
458
459     /*
460      * Determine the number of ACPI name "segments" by counting the number of
461      * path separators within the string. Start with one segment since the
462      * segment count is [(# separators) + 1], and zero separators is ok.
463      */
464     if (*NextExternalChar)
465     {
466         Info->NumSegments = 1;
467         for (i = 0; NextExternalChar[i]; i++)
468         {
469             if (AcpiNsValidPathSeparator (NextExternalChar[i]))
470             {
471                 Info->NumSegments++;
472             }
473         }
474     }
475
476     Info->Length = (ACPI_NAME_SIZE * Info->NumSegments) +
477                     4 + Info->NumCarats;
478
479     Info->NextExternalChar = NextExternalChar;
480 }
481
482
483 /*******************************************************************************
484  *
485  * FUNCTION:    AcpiNsBuildInternalName
486  *
487  * PARAMETERS:  Info            - Info struct fully initialized
488  *
489  * RETURN:      Status
490  *
491  * DESCRIPTION: Construct the internal (AML) namestring
492  *              corresponding to the external (ASL) namestring.
493  *
494  ******************************************************************************/
495
496 ACPI_STATUS
497 AcpiNsBuildInternalName (
498     ACPI_NAMESTRING_INFO    *Info)
499 {
500     UINT32                  NumSegments = Info->NumSegments;
501     char                    *InternalName = Info->InternalName;
502     char                    *ExternalName = Info->NextExternalChar;
503     char                    *Result = NULL;
504     ACPI_NATIVE_UINT        i;
505
506
507     ACPI_FUNCTION_TRACE ("NsBuildInternalName");
508
509
510     /* Setup the correct prefixes, counts, and pointers */
511
512     if (Info->FullyQualified)
513     {
514         InternalName[0] = '\\';
515
516         if (NumSegments <= 1)
517         {
518             Result = &InternalName[1];
519         }
520         else if (NumSegments == 2)
521         {
522             InternalName[1] = AML_DUAL_NAME_PREFIX;
523             Result = &InternalName[2];
524         }
525         else
526         {
527             InternalName[1] = AML_MULTI_NAME_PREFIX_OP;
528             InternalName[2] = (char) NumSegments;
529             Result = &InternalName[3];
530         }
531     }
532     else
533     {
534         /*
535          * Not fully qualified.
536          * Handle Carats first, then append the name segments
537          */
538         i = 0;
539         if (Info->NumCarats)
540         {
541             for (i = 0; i < Info->NumCarats; i++)
542             {
543                 InternalName[i] = '^';
544             }
545         }
546
547         if (NumSegments <= 1)
548         {
549             Result = &InternalName[i];
550         }
551         else if (NumSegments == 2)
552         {
553             InternalName[i] = AML_DUAL_NAME_PREFIX;
554             Result = &InternalName[(ACPI_NATIVE_UINT) (i+1)];
555         }
556         else
557         {
558             InternalName[i] = AML_MULTI_NAME_PREFIX_OP;
559             InternalName[(ACPI_NATIVE_UINT) (i+1)] = (char) NumSegments;
560             Result = &InternalName[(ACPI_NATIVE_UINT) (i+2)];
561         }
562     }
563
564     /* Build the name (minus path separators) */
565
566     for (; NumSegments; NumSegments--)
567     {
568         for (i = 0; i < ACPI_NAME_SIZE; i++)
569         {
570             if (AcpiNsValidPathSeparator (*ExternalName) ||
571                (*ExternalName == 0))
572             {
573                 /* Pad the segment with underscore(s) if segment is short */
574
575                 Result[i] = '_';
576             }
577             else
578             {
579                 /* Convert the character to uppercase and save it */
580
581                 Result[i] = (char) ACPI_TOUPPER ((int) *ExternalName);
582                 ExternalName++;
583             }
584         }
585
586         /* Now we must have a path separator, or the pathname is bad */
587
588         if (!AcpiNsValidPathSeparator (*ExternalName) &&
589             (*ExternalName != 0))
590         {
591             return_ACPI_STATUS (AE_BAD_PARAMETER);
592         }
593
594         /* Move on the next segment */
595
596         ExternalName++;
597         Result += ACPI_NAME_SIZE;
598     }
599
600     /* Terminate the string */
601
602     *Result = 0;
603
604     if (Info->FullyQualified)
605     {
606         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (abs) \"\\%s\"\n",
607             InternalName, InternalName));
608     }
609     else
610     {
611         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
612             InternalName, InternalName));
613     }
614
615     return_ACPI_STATUS (AE_OK);
616 }
617
618
619 /*******************************************************************************
620  *
621  * FUNCTION:    AcpiNsInternalizeName
622  *
623  * PARAMETERS:  *ExternalName           - External representation of name
624  *              **Converted Name        - Where to return the resulting
625  *                                        internal represention of the name
626  *
627  * RETURN:      Status
628  *
629  * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
630  *              to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
631  *
632  *******************************************************************************/
633
634 ACPI_STATUS
635 AcpiNsInternalizeName (
636     char                    *ExternalName,
637     char                    **ConvertedName)
638 {
639     char                    *InternalName;
640     ACPI_NAMESTRING_INFO    Info;
641     ACPI_STATUS             Status;
642
643
644     ACPI_FUNCTION_TRACE ("NsInternalizeName");
645
646
647     if ((!ExternalName)      ||
648         (*ExternalName == 0) ||
649         (!ConvertedName))
650     {
651         return_ACPI_STATUS (AE_BAD_PARAMETER);
652     }
653
654     /* Get the length of the new internal name */
655
656     Info.ExternalName = ExternalName;
657     AcpiNsGetInternalNameLength (&Info);
658
659     /* We need a segment to store the internal  name */
660
661     InternalName = ACPI_MEM_CALLOCATE (Info.Length);
662     if (!InternalName)
663     {
664         return_ACPI_STATUS (AE_NO_MEMORY);
665     }
666
667     /* Build the name */
668
669     Info.InternalName = InternalName;
670     Status = AcpiNsBuildInternalName (&Info);
671     if (ACPI_FAILURE (Status))
672     {
673         ACPI_MEM_FREE (InternalName);
674         return_ACPI_STATUS (Status);
675     }
676
677     *ConvertedName = InternalName;
678     return_ACPI_STATUS (AE_OK);
679 }
680
681
682 /*******************************************************************************
683  *
684  * FUNCTION:    AcpiNsExternalizeName
685  *
686  * PARAMETERS:  InternalNameLength  - Lenth of the internal name below
687  *              InternalName        - Internal representation of name
688  *              ConvertedNameLength - Where the length is returned
689  *              ConvertedName       - Where the resulting external name
690  *                                    is returned
691  *
692  * RETURN:      Status
693  *
694  * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
695  *              to its external (printable) form (e.g. "\_PR_.CPU0")
696  *
697  ******************************************************************************/
698
699 ACPI_STATUS
700 AcpiNsExternalizeName (
701     UINT32                  InternalNameLength,
702     char                    *InternalName,
703     UINT32                  *ConvertedNameLength,
704     char                    **ConvertedName)
705 {
706     ACPI_NATIVE_UINT        NamesIndex = 0;
707     ACPI_NATIVE_UINT        NumSegments = 0;
708     ACPI_NATIVE_UINT        RequiredLength;
709     ACPI_NATIVE_UINT        PrefixLength = 0;
710     ACPI_NATIVE_UINT        i = 0;
711     ACPI_NATIVE_UINT        j = 0;
712
713
714     ACPI_FUNCTION_TRACE ("NsExternalizeName");
715
716
717     if (!InternalNameLength     ||
718         !InternalName           ||
719         !ConvertedName)
720     {
721         return_ACPI_STATUS (AE_BAD_PARAMETER);
722     }
723
724     /*
725      * Check for a prefix (one '\' | one or more '^').
726      */
727     switch (InternalName[0])
728     {
729     case '\\':
730         PrefixLength = 1;
731         break;
732
733     case '^':
734         for (i = 0; i < InternalNameLength; i++)
735         {
736             if (InternalName[i] == '^')
737             {
738                 PrefixLength = i + 1;
739             }
740             else
741             {
742                 break;
743             }
744         }
745
746         if (i == InternalNameLength)
747         {
748             PrefixLength = i;
749         }
750
751         break;
752
753     default:
754         break;
755     }
756
757     /*
758      * Check for object names.  Note that there could be 0-255 of these
759      * 4-byte elements.
760      */
761     if (PrefixLength < InternalNameLength)
762     {
763         switch (InternalName[PrefixLength])
764         {
765         case AML_MULTI_NAME_PREFIX_OP:
766
767             /* <count> 4-byte names */
768
769             NamesIndex = PrefixLength + 2;
770             NumSegments = (ACPI_NATIVE_UINT) (UINT8)
771                           InternalName[(ACPI_NATIVE_UINT) (PrefixLength + 1)];
772             break;
773
774         case AML_DUAL_NAME_PREFIX:
775
776             /* Two 4-byte names */
777
778             NamesIndex = PrefixLength + 1;
779             NumSegments = 2;
780             break;
781
782         case 0:
783
784             /* NullName */
785
786             NamesIndex = 0;
787             NumSegments = 0;
788             break;
789
790         default:
791
792             /* one 4-byte name */
793
794             NamesIndex = PrefixLength;
795             NumSegments = 1;
796             break;
797         }
798     }
799
800     /*
801      * Calculate the length of ConvertedName, which equals the length
802      * of the prefix, length of all object names, length of any required
803      * punctuation ('.') between object names, plus the NULL terminator.
804      */
805     RequiredLength = PrefixLength + (4 * NumSegments) +
806                         ((NumSegments > 0) ? (NumSegments - 1) : 0) + 1;
807
808     /*
809      * Check to see if we're still in bounds.  If not, there's a problem
810      * with InternalName (invalid format).
811      */
812     if (RequiredLength > InternalNameLength)
813     {
814         ACPI_REPORT_ERROR (("NsExternalizeName: Invalid internal name\n"));
815         return_ACPI_STATUS (AE_BAD_PATHNAME);
816     }
817
818     /*
819      * Build ConvertedName
820      */
821     *ConvertedName = ACPI_MEM_CALLOCATE (RequiredLength);
822     if (!(*ConvertedName))
823     {
824         return_ACPI_STATUS (AE_NO_MEMORY);
825     }
826
827     j = 0;
828
829     for (i = 0; i < PrefixLength; i++)
830     {
831         (*ConvertedName)[j++] = InternalName[i];
832     }
833
834     if (NumSegments > 0)
835     {
836         for (i = 0; i < NumSegments; i++)
837         {
838             if (i > 0)
839             {
840                 (*ConvertedName)[j++] = '.';
841             }
842
843             (*ConvertedName)[j++] = InternalName[NamesIndex++];
844             (*ConvertedName)[j++] = InternalName[NamesIndex++];
845             (*ConvertedName)[j++] = InternalName[NamesIndex++];
846             (*ConvertedName)[j++] = InternalName[NamesIndex++];
847         }
848     }
849
850     if (ConvertedNameLength)
851     {
852         *ConvertedNameLength = (UINT32) RequiredLength;
853     }
854
855     return_ACPI_STATUS (AE_OK);
856 }
857
858
859 /*******************************************************************************
860  *
861  * FUNCTION:    AcpiNsMapHandleToNode
862  *
863  * PARAMETERS:  Handle          - Handle to be converted to an Node
864  *
865  * RETURN:      A Name table entry pointer
866  *
867  * DESCRIPTION: Convert a namespace handle to a real Node
868  *
869  * Note: Real integer handles would allow for more verification
870  *       and keep all pointers within this subsystem - however this introduces
871  *       more (and perhaps unnecessary) overhead.
872  *
873  ******************************************************************************/
874
875 ACPI_NAMESPACE_NODE *
876 AcpiNsMapHandleToNode (
877     ACPI_HANDLE             Handle)
878 {
879
880     ACPI_FUNCTION_ENTRY ();
881
882
883     /*
884      * Simple implementation.
885      */
886     if (!Handle)
887     {
888         return (NULL);
889     }
890
891     if (Handle == ACPI_ROOT_OBJECT)
892     {
893         return (AcpiGbl_RootNode);
894     }
895
896     /* We can at least attempt to verify the handle */
897
898     if (ACPI_GET_DESCRIPTOR_TYPE (Handle) != ACPI_DESC_TYPE_NAMED)
899     {
900         return (NULL);
901     }
902
903     return ((ACPI_NAMESPACE_NODE *) Handle);
904 }
905
906
907 /*******************************************************************************
908  *
909  * FUNCTION:    AcpiNsConvertEntryToHandle
910  *
911  * PARAMETERS:  Node          - Node to be converted to a Handle
912  *
913  * RETURN:      A user handle
914  *
915  * DESCRIPTION: Convert a real Node to a namespace handle
916  *
917  ******************************************************************************/
918
919 ACPI_HANDLE
920 AcpiNsConvertEntryToHandle (
921     ACPI_NAMESPACE_NODE         *Node)
922 {
923
924
925     /*
926      * Simple implementation for now;
927      */
928     return ((ACPI_HANDLE) Node);
929
930
931 /* Example future implementation ---------------------
932
933     if (!Node)
934     {
935         return (NULL);
936     }
937
938     if (Node == AcpiGbl_RootNode)
939     {
940         return (ACPI_ROOT_OBJECT);
941     }
942
943
944     return ((ACPI_HANDLE) Node);
945 ------------------------------------------------------*/
946 }
947
948
949 /*******************************************************************************
950  *
951  * FUNCTION:    AcpiNsTerminate
952  *
953  * PARAMETERS:  none
954  *
955  * RETURN:      none
956  *
957  * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
958  *
959  ******************************************************************************/
960
961 void
962 AcpiNsTerminate (
963     void)
964 {
965     ACPI_OPERAND_OBJECT     *ObjDesc;
966
967
968     ACPI_FUNCTION_TRACE ("NsTerminate");
969
970
971     /*
972      * 1) Free the entire namespace -- all nodes and objects
973      *
974      * Delete all object descriptors attached to namepsace nodes
975      */
976     AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
977
978     /* Detach any objects attached to the root */
979
980     ObjDesc = AcpiNsGetAttachedObject (AcpiGbl_RootNode);
981     if (ObjDesc)
982     {
983         AcpiNsDetachObject (AcpiGbl_RootNode);
984     }
985
986     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Namespace freed\n"));
987
988     /*
989      * 2) Now we can delete the ACPI tables
990      */
991     AcpiTbDeleteAllTables ();
992     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ACPI Tables freed\n"));
993
994     return_VOID;
995 }
996
997
998 /*******************************************************************************
999  *
1000  * FUNCTION:    AcpiNsOpensScope
1001  *
1002  * PARAMETERS:  Type        - A valid namespace type
1003  *
1004  * RETURN:      NEWSCOPE if the passed type "opens a name scope" according
1005  *              to the ACPI specification, else 0
1006  *
1007  ******************************************************************************/
1008
1009 UINT32
1010 AcpiNsOpensScope (
1011     ACPI_OBJECT_TYPE        Type)
1012 {
1013     ACPI_FUNCTION_TRACE_STR ("NsOpensScope", AcpiUtGetTypeName (Type));
1014
1015
1016     if (!AcpiUtValidObjectType (Type))
1017     {
1018         /* type code out of range  */
1019
1020         ACPI_REPORT_WARNING (("NsOpensScope: Invalid Object Type %X\n", Type));
1021         return_UINT32 (ACPI_NS_NORMAL);
1022     }
1023
1024     return_UINT32 (((UINT32) AcpiGbl_NsProperties[Type]) & ACPI_NS_NEWSCOPE);
1025 }
1026
1027
1028 /*******************************************************************************
1029  *
1030  * FUNCTION:    AcpiNsGetNodeByPath
1031  *
1032  * PARAMETERS:  *Pathname   - Name to be found, in external (ASL) format. The
1033  *                            \ (backslash) and ^ (carat) prefixes, and the
1034  *                            . (period) to separate segments are supported.
1035  *              StartNode   - Root of subtree to be searched, or NS_ALL for the
1036  *                            root of the name space.  If Name is fully
1037  *                            qualified (first INT8 is '\'), the passed value
1038  *                            of Scope will not be accessed.
1039  *              Flags       - Used to indicate whether to perform upsearch or
1040  *                            not.
1041  *              ReturnNode  - Where the Node is returned
1042  *
1043  * DESCRIPTION: Look up a name relative to a given scope and return the
1044  *              corresponding Node.  NOTE: Scope can be null.
1045  *
1046  * MUTEX:       Locks namespace
1047  *
1048  ******************************************************************************/
1049
1050 ACPI_STATUS
1051 AcpiNsGetNodeByPath (
1052     char                    *Pathname,
1053     ACPI_NAMESPACE_NODE     *StartNode,
1054     UINT32                  Flags,
1055     ACPI_NAMESPACE_NODE     **ReturnNode)
1056 {
1057     ACPI_GENERIC_STATE      ScopeInfo;
1058     ACPI_STATUS             Status;
1059     char                    *InternalPath = NULL;
1060
1061
1062     ACPI_FUNCTION_TRACE_PTR ("NsGetNodeByPath", Pathname);
1063
1064
1065     if (Pathname)
1066     {
1067         /* Convert path to internal representation */
1068
1069         Status = AcpiNsInternalizeName (Pathname, &InternalPath);
1070         if (ACPI_FAILURE (Status))
1071         {
1072             return_ACPI_STATUS (Status);
1073         }
1074     }
1075
1076     /* Must lock namespace during lookup */
1077
1078     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1079     if (ACPI_FAILURE (Status))
1080     {
1081         goto Cleanup;
1082     }
1083
1084     /* Setup lookup scope (search starting point) */
1085
1086     ScopeInfo.Scope.Node = StartNode;
1087
1088     /* Lookup the name in the namespace */
1089
1090     Status = AcpiNsLookup (&ScopeInfo, InternalPath,
1091                             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
1092                             (Flags | ACPI_NS_DONT_OPEN_SCOPE),
1093                             NULL, ReturnNode);
1094     if (ACPI_FAILURE (Status))
1095     {
1096         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s, %s\n",
1097                 InternalPath, AcpiFormatException (Status)));
1098     }
1099
1100     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1101
1102 Cleanup:
1103     if (InternalPath)
1104     {
1105         ACPI_MEM_FREE (InternalPath);
1106     }
1107     return_ACPI_STATUS (Status);
1108 }
1109
1110
1111 /*******************************************************************************
1112  *
1113  * FUNCTION:    AcpiNsGetParentNode
1114  *
1115  * PARAMETERS:  Node       - Current table entry
1116  *
1117  * RETURN:      Parent entry of the given entry
1118  *
1119  * DESCRIPTION: Obtain the parent entry for a given entry in the namespace.
1120  *
1121  ******************************************************************************/
1122
1123 ACPI_NAMESPACE_NODE *
1124 AcpiNsGetParentNode (
1125     ACPI_NAMESPACE_NODE     *Node)
1126 {
1127     ACPI_FUNCTION_ENTRY ();
1128
1129
1130     if (!Node)
1131     {
1132         return (NULL);
1133     }
1134
1135     /*
1136      * Walk to the end of this peer list. The last entry is marked with a flag
1137      * and the peer pointer is really a pointer back to the parent. This saves
1138      * putting a parent back pointer in each and every named object!
1139      */
1140     while (!(Node->Flags & ANOBJ_END_OF_PEER_LIST))
1141     {
1142         Node = Node->Peer;
1143     }
1144
1145     return (Node->Peer);
1146 }
1147
1148
1149 /*******************************************************************************
1150  *
1151  * FUNCTION:    AcpiNsGetNextValidNode
1152  *
1153  * PARAMETERS:  Node       - Current table entry
1154  *
1155  * RETURN:      Next valid Node in the linked node list. NULL if no more valid
1156  *              nodes.
1157  *
1158  * DESCRIPTION: Find the next valid node within a name table.
1159  *              Useful for implementing NULL-end-of-list loops.
1160  *
1161  ******************************************************************************/
1162
1163 ACPI_NAMESPACE_NODE *
1164 AcpiNsGetNextValidNode (
1165     ACPI_NAMESPACE_NODE     *Node)
1166 {
1167
1168     /* If we are at the end of this peer list, return NULL */
1169
1170     if (Node->Flags & ANOBJ_END_OF_PEER_LIST)
1171     {
1172         return NULL;
1173     }
1174
1175     /* Otherwise just return the next peer */
1176
1177     return (Node->Peer);
1178 }
1179
1180
1181 #ifdef ACPI_OBSOLETE_FUNCTIONS
1182 /*******************************************************************************
1183  *
1184  * FUNCTION:    AcpiNsFindParentName
1185  *
1186  * PARAMETERS:  *ChildNode             - Named Obj whose name is to be found
1187  *
1188  * RETURN:      The ACPI name
1189  *
1190  * DESCRIPTION: Search for the given obj in its parent scope and return the
1191  *              name segment, or "????" if the parent name can't be found
1192  *              (which "should not happen").
1193  *
1194  ******************************************************************************/
1195
1196 ACPI_NAME
1197 AcpiNsFindParentName (
1198     ACPI_NAMESPACE_NODE     *ChildNode)
1199 {
1200     ACPI_NAMESPACE_NODE     *ParentNode;
1201
1202
1203     ACPI_FUNCTION_TRACE ("NsFindParentName");
1204
1205
1206     if (ChildNode)
1207     {
1208         /* Valid entry.  Get the parent Node */
1209
1210         ParentNode = AcpiNsGetParentNode (ChildNode);
1211         if (ParentNode)
1212         {
1213             ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
1214                 "Parent of %p [%4.4s] is %p [%4.4s]\n",
1215                 ChildNode,  AcpiUtGetNodeName (ChildNode),
1216                 ParentNode, AcpiUtGetNodeName (ParentNode)));
1217
1218             if (ParentNode->Name.Integer)
1219             {
1220                 return_VALUE ((ACPI_NAME) ParentNode->Name.Integer);
1221             }
1222         }
1223
1224         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
1225             "Unable to find parent of %p (%4.4s)\n",
1226             ChildNode, AcpiUtGetNodeName (ChildNode)));
1227     }
1228
1229     return_VALUE (ACPI_UNKNOWN_NAME);
1230 }
1231 #endif
1232
1233