Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / contrib / dev / acpica-unix-20031203 / namespace / nsutils.c
1 /******************************************************************************
2  *
3  * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4  *                        parents and siblings and Scope manipulation
5  *              $Revision: 131 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2003, 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 "acpi.h"
121 #include "acnamesp.h"
122 #include "amlcode.h"
123 #include "actables.h"
124
125 #define _COMPONENT          ACPI_NAMESPACE
126         ACPI_MODULE_NAME    ("nsutils")
127
128
129 /*******************************************************************************
130  *
131  * FUNCTION:    AcpiNsReportError
132  *
133  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
134  *              LineNumber          - Caller's line number (for error output)
135  *              ComponentId         - Caller's component ID (for error output)
136  *              Message             - Error message to use on failure
137  *
138  * RETURN:      None
139  *
140  * DESCRIPTION: Print warning message with full pathname
141  *
142  ******************************************************************************/
143
144 void
145 AcpiNsReportError (
146     char                    *ModuleName,
147     UINT32                  LineNumber,
148     UINT32                  ComponentId,
149     char                    *InternalName,
150     ACPI_STATUS             LookupStatus)
151 {
152     ACPI_STATUS             Status;
153     char                    *Name = NULL;
154
155
156     AcpiOsPrintf ("%8s-%04d: *** Error: Looking up ",
157         ModuleName, LineNumber);
158
159     if (LookupStatus == AE_BAD_CHARACTER)
160     {
161         /* There is a non-ascii character in the name */
162
163         AcpiOsPrintf ("[0x%4.4X] (NON-ASCII)\n", *(ACPI_CAST_PTR (UINT32, InternalName)));
164     }
165     else
166     {
167         /* Convert path to external format */
168
169         Status = AcpiNsExternalizeName (ACPI_UINT32_MAX, InternalName, NULL, &Name);
170
171         /* Print target name */
172
173         if (ACPI_SUCCESS (Status))
174         {
175             AcpiOsPrintf ("[%s]", Name);
176         }
177         else
178         {
179             AcpiOsPrintf ("[COULD NOT EXTERNALIZE NAME]");
180         }
181
182         if (Name)
183         {
184             ACPI_MEM_FREE (Name);
185         }
186     }
187
188     AcpiOsPrintf (" in namespace, %s\n",
189         AcpiFormatException (LookupStatus));
190 }
191
192
193 /*******************************************************************************
194  *
195  * FUNCTION:    AcpiNsReportMethodError
196  *
197  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
198  *              LineNumber          - Caller's line number (for error output)
199  *              ComponentId         - Caller's component ID (for error output)
200  *              Message             - Error message to use on failure
201  *
202  * RETURN:      None
203  *
204  * DESCRIPTION: Print warning message with full pathname
205  *
206  ******************************************************************************/
207
208 void
209 AcpiNsReportMethodError (
210     char                    *ModuleName,
211     UINT32                  LineNumber,
212     UINT32                  ComponentId,
213     char                    *Message,
214     ACPI_NAMESPACE_NODE     *PrefixNode,
215     char                    *Path,
216     ACPI_STATUS             MethodStatus)
217 {
218     ACPI_STATUS             Status;
219     ACPI_NAMESPACE_NODE     *Node = PrefixNode;
220
221
222     if (Path)
223     {
224         Status = AcpiNsGetNodeByPath (Path, PrefixNode, ACPI_NS_NO_UPSEARCH, &Node);
225         if (ACPI_FAILURE (Status))
226         {
227             AcpiOsPrintf ("ReportMethodError: Could not get node\n");
228             return;
229         }
230     }
231
232     AcpiOsPrintf ("%8s-%04d: *** Error: ", ModuleName, LineNumber);
233     AcpiNsPrintNodePathname (Node, Message);
234     AcpiOsPrintf (", %s\n", AcpiFormatException (MethodStatus));
235 }
236
237
238 /*******************************************************************************
239  *
240  * FUNCTION:    AcpiNsPrintNodePathname
241  *
242  * PARAMETERS:  Node                - Object
243  *              Msg                 - Prefix message
244  *
245  * DESCRIPTION: Print an object's full namespace pathname
246  *              Manages allocation/freeing of a pathname buffer
247  *
248  ******************************************************************************/
249
250 void
251 AcpiNsPrintNodePathname (
252     ACPI_NAMESPACE_NODE     *Node,
253     char                    *Msg)
254 {
255     ACPI_BUFFER             Buffer;
256     ACPI_STATUS             Status;
257
258
259     if (!Node)
260     {
261         AcpiOsPrintf ("[NULL NAME]");
262         return;
263     }
264
265     /* Convert handle to a full pathname and print it (with supplied message) */
266
267     Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
268
269     Status = AcpiNsHandleToPathname (Node, &Buffer);
270     if (ACPI_SUCCESS (Status))
271     {
272         if (Msg)
273         {
274             AcpiOsPrintf ("%s ", Msg);
275         }
276
277         AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer.Pointer, Node);
278         ACPI_MEM_FREE (Buffer.Pointer);
279     }
280 }
281
282
283 /*******************************************************************************
284  *
285  * FUNCTION:    AcpiNsValidRootPrefix
286  *
287  * PARAMETERS:  Prefix          - Character to be checked
288  *
289  * RETURN:      TRUE if a valid prefix
290  *
291  * DESCRIPTION: Check if a character is a valid ACPI Root prefix
292  *
293  ******************************************************************************/
294
295 BOOLEAN
296 AcpiNsValidRootPrefix (
297     char                    Prefix)
298 {
299
300     return ((BOOLEAN) (Prefix == '\\'));
301 }
302
303
304 /*******************************************************************************
305  *
306  * FUNCTION:    AcpiNsValidPathSeparator
307  *
308  * PARAMETERS:  Sep              - Character to be checked
309  *
310  * RETURN:      TRUE if a valid path separator
311  *
312  * DESCRIPTION: Check if a character is a valid ACPI path separator
313  *
314  ******************************************************************************/
315
316 BOOLEAN
317 AcpiNsValidPathSeparator (
318     char                    Sep)
319 {
320
321     return ((BOOLEAN) (Sep == '.'));
322 }
323
324
325 /*******************************************************************************
326  *
327  * FUNCTION:    AcpiNsGetType
328  *
329  * PARAMETERS:  Handle              - Parent Node to be examined
330  *
331  * RETURN:      Type field from Node whose handle is passed
332  *
333  ******************************************************************************/
334
335 ACPI_OBJECT_TYPE
336 AcpiNsGetType (
337     ACPI_NAMESPACE_NODE     *Node)
338 {
339     ACPI_FUNCTION_TRACE ("NsGetType");
340
341
342     if (!Node)
343     {
344         ACPI_REPORT_WARNING (("NsGetType: Null Node ptr"));
345         return_VALUE (ACPI_TYPE_ANY);
346     }
347
348     return_VALUE ((ACPI_OBJECT_TYPE) Node->Type);
349 }
350
351
352 /*******************************************************************************
353  *
354  * FUNCTION:    AcpiNsLocal
355  *
356  * PARAMETERS:  Type            - A namespace object type
357  *
358  * RETURN:      LOCAL if names must be found locally in objects of the
359  *              passed type, 0 if enclosing scopes should be searched
360  *
361  ******************************************************************************/
362
363 UINT32
364 AcpiNsLocal (
365     ACPI_OBJECT_TYPE        Type)
366 {
367     ACPI_FUNCTION_TRACE ("NsLocal");
368
369
370     if (!AcpiUtValidObjectType (Type))
371     {
372         /* Type code out of range  */
373
374         ACPI_REPORT_WARNING (("NsLocal: Invalid Object Type\n"));
375         return_VALUE (ACPI_NS_NORMAL);
376     }
377
378     return_VALUE ((UINT32) AcpiGbl_NsProperties[Type] & ACPI_NS_LOCAL);
379 }
380
381
382 /*******************************************************************************
383  *
384  * FUNCTION:    AcpiNsGetInternalNameLength
385  *
386  * PARAMETERS:  Info            - Info struct initialized with the
387  *                                external name pointer.
388  *
389  * RETURN:      Status
390  *
391  * DESCRIPTION: Calculate the length of the internal (AML) namestring
392  *              corresponding to the external (ASL) namestring.
393  *
394  ******************************************************************************/
395
396 void
397 AcpiNsGetInternalNameLength (
398     ACPI_NAMESTRING_INFO    *Info)
399 {
400     char                    *NextExternalChar;
401     UINT32                  i;
402
403
404     ACPI_FUNCTION_ENTRY ();
405
406
407     NextExternalChar = Info->ExternalName;
408     Info->NumCarats = 0;
409     Info->NumSegments = 0;
410     Info->FullyQualified = FALSE;
411
412     /*
413      * For the internal name, the required length is 4 bytes
414      * per segment, plus 1 each for RootPrefix, MultiNamePrefixOp,
415      * segment count, trailing null (which is not really needed,
416      * but no there's harm in putting it there)
417      *
418      * strlen() + 1 covers the first NameSeg, which has no
419      * path separator
420      */
421     if (AcpiNsValidRootPrefix (NextExternalChar[0]))
422     {
423         Info->FullyQualified = TRUE;
424         NextExternalChar++;
425     }
426     else
427     {
428         /*
429          * Handle Carat prefixes
430          */
431         while (*NextExternalChar == '^')
432         {
433             Info->NumCarats++;
434             NextExternalChar++;
435         }
436     }
437
438     /*
439      * Determine the number of ACPI name "segments" by counting
440      * the number of path separators within the string.  Start
441      * with one segment since the segment count is (# separators)
442      * + 1, and zero separators is ok.
443      */
444     if (*NextExternalChar)
445     {
446         Info->NumSegments = 1;
447         for (i = 0; NextExternalChar[i]; i++)
448         {
449             if (AcpiNsValidPathSeparator (NextExternalChar[i]))
450             {
451                 Info->NumSegments++;
452             }
453         }
454     }
455
456     Info->Length = (ACPI_NAME_SIZE * Info->NumSegments) +
457                     4 + Info->NumCarats;
458
459     Info->NextExternalChar = NextExternalChar;
460 }
461
462
463 /*******************************************************************************
464  *
465  * FUNCTION:    AcpiNsBuildInternalName
466  *
467  * PARAMETERS:  Info            - Info struct fully initialized
468  *
469  * RETURN:      Status
470  *
471  * DESCRIPTION: Construct the internal (AML) namestring
472  *              corresponding to the external (ASL) namestring.
473  *
474  ******************************************************************************/
475
476 ACPI_STATUS
477 AcpiNsBuildInternalName (
478     ACPI_NAMESTRING_INFO    *Info)
479 {
480     UINT32                  NumSegments = Info->NumSegments;
481     char                    *InternalName = Info->InternalName;
482     char                    *ExternalName = Info->NextExternalChar;
483     char                    *Result = NULL;
484     ACPI_NATIVE_UINT        i;
485
486
487     ACPI_FUNCTION_TRACE ("NsBuildInternalName");
488
489
490     /* Setup the correct prefixes, counts, and pointers */
491
492     if (Info->FullyQualified)
493     {
494         InternalName[0] = '\\';
495
496         if (NumSegments <= 1)
497         {
498             Result = &InternalName[1];
499         }
500         else if (NumSegments == 2)
501         {
502             InternalName[1] = AML_DUAL_NAME_PREFIX;
503             Result = &InternalName[2];
504         }
505         else
506         {
507             InternalName[1] = AML_MULTI_NAME_PREFIX_OP;
508             InternalName[2] = (char) NumSegments;
509             Result = &InternalName[3];
510         }
511     }
512     else
513     {
514         /*
515          * Not fully qualified.
516          * Handle Carats first, then append the name segments
517          */
518         i = 0;
519         if (Info->NumCarats)
520         {
521             for (i = 0; i < Info->NumCarats; i++)
522             {
523                 InternalName[i] = '^';
524             }
525         }
526
527         if (NumSegments <= 1)
528         {
529             Result = &InternalName[i];
530         }
531         else if (NumSegments == 2)
532         {
533             InternalName[i] = AML_DUAL_NAME_PREFIX;
534             Result = &InternalName[(ACPI_NATIVE_UINT) (i+1)];
535         }
536         else
537         {
538             InternalName[i] = AML_MULTI_NAME_PREFIX_OP;
539             InternalName[(ACPI_NATIVE_UINT) (i+1)] = (char) NumSegments;
540             Result = &InternalName[(ACPI_NATIVE_UINT) (i+2)];
541         }
542     }
543
544     /* Build the name (minus path separators) */
545
546     for (; NumSegments; NumSegments--)
547     {
548         for (i = 0; i < ACPI_NAME_SIZE; i++)
549         {
550             if (AcpiNsValidPathSeparator (*ExternalName) ||
551                (*ExternalName == 0))
552             {
553                 /* Pad the segment with underscore(s) if segment is short */
554
555                 Result[i] = '_';
556             }
557             else
558             {
559                 /* Convert the character to uppercase and save it */
560
561                 Result[i] = (char) ACPI_TOUPPER ((int) *ExternalName);
562                 ExternalName++;
563             }
564         }
565
566         /* Now we must have a path separator, or the pathname is bad */
567
568         if (!AcpiNsValidPathSeparator (*ExternalName) &&
569             (*ExternalName != 0))
570         {
571             return_ACPI_STATUS (AE_BAD_PARAMETER);
572         }
573
574         /* Move on the next segment */
575
576         ExternalName++;
577         Result += ACPI_NAME_SIZE;
578     }
579
580     /* Terminate the string */
581
582     *Result = 0;
583
584     if (Info->FullyQualified)
585     {
586         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (abs) \"\\%s\"\n",
587             InternalName, InternalName));
588     }
589     else
590     {
591         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
592             InternalName, InternalName));
593     }
594
595     return_ACPI_STATUS (AE_OK);
596 }
597
598
599 /*******************************************************************************
600  *
601  * FUNCTION:    AcpiNsInternalizeName
602  *
603  * PARAMETERS:  *ExternalName           - External representation of name
604  *              **Converted Name        - Where to return the resulting
605  *                                        internal represention of the name
606  *
607  * RETURN:      Status
608  *
609  * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
610  *              to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
611  *
612  *******************************************************************************/
613
614 ACPI_STATUS
615 AcpiNsInternalizeName (
616     char                    *ExternalName,
617     char                    **ConvertedName)
618 {
619     char                    *InternalName;
620     ACPI_NAMESTRING_INFO    Info;
621     ACPI_STATUS             Status;
622
623
624     ACPI_FUNCTION_TRACE ("NsInternalizeName");
625
626
627     if ((!ExternalName)      ||
628         (*ExternalName == 0) ||
629         (!ConvertedName))
630     {
631         return_ACPI_STATUS (AE_BAD_PARAMETER);
632     }
633
634     /* Get the length of the new internal name */
635
636     Info.ExternalName = ExternalName;
637     AcpiNsGetInternalNameLength (&Info);
638
639     /* We need a segment to store the internal  name */
640
641     InternalName = ACPI_MEM_CALLOCATE (Info.Length);
642     if (!InternalName)
643     {
644         return_ACPI_STATUS (AE_NO_MEMORY);
645     }
646
647     /* Build the name */
648
649     Info.InternalName = InternalName;
650     Status = AcpiNsBuildInternalName (&Info);
651     if (ACPI_FAILURE (Status))
652     {
653         ACPI_MEM_FREE (InternalName);
654         return_ACPI_STATUS (Status);
655     }
656
657     *ConvertedName = InternalName;
658     return_ACPI_STATUS (AE_OK);
659 }
660
661
662 /*******************************************************************************
663  *
664  * FUNCTION:    AcpiNsExternalizeName
665  *
666  * PARAMETERS:  *InternalName          - Internal representation of name
667  *              **ConvertedName        - Where to return the resulting
668  *                                       external representation of name
669  *
670  * RETURN:      Status
671  *
672  * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
673  *              to its external form (e.g. "\_PR_.CPU0")
674  *
675  ******************************************************************************/
676
677 ACPI_STATUS
678 AcpiNsExternalizeName (
679     UINT32                  InternalNameLength,
680     char                    *InternalName,
681     UINT32                  *ConvertedNameLength,
682     char                    **ConvertedName)
683 {
684     ACPI_NATIVE_UINT        NamesIndex = 0;
685     ACPI_NATIVE_UINT        NumSegments = 0;
686     ACPI_NATIVE_UINT        RequiredLength;
687     ACPI_NATIVE_UINT        PrefixLength = 0;
688     ACPI_NATIVE_UINT        i = 0;
689     ACPI_NATIVE_UINT        j = 0;
690
691
692     ACPI_FUNCTION_TRACE ("NsExternalizeName");
693
694
695     if (!InternalNameLength     ||
696         !InternalName           ||
697         !ConvertedName)
698     {
699         return_ACPI_STATUS (AE_BAD_PARAMETER);
700     }
701
702     /*
703      * Check for a prefix (one '\' | one or more '^').
704      */
705     switch (InternalName[0])
706     {
707     case '\\':
708         PrefixLength = 1;
709         break;
710
711     case '^':
712         for (i = 0; i < InternalNameLength; i++)
713         {
714             if (InternalName[i] == '^')
715             {
716                 PrefixLength = i + 1;
717             }
718             else
719             {
720                 break;
721             }
722         }
723
724         if (i == InternalNameLength)
725         {
726             PrefixLength = i;
727         }
728
729         break;
730
731     default:
732         break;
733     }
734
735     /*
736      * Check for object names.  Note that there could be 0-255 of these
737      * 4-byte elements.
738      */
739     if (PrefixLength < InternalNameLength)
740     {
741         switch (InternalName[PrefixLength])
742         {
743         case AML_MULTI_NAME_PREFIX_OP:
744
745             /* <count> 4-byte names */
746
747             NamesIndex = PrefixLength + 2;
748             NumSegments = (ACPI_NATIVE_UINT) (UINT8) InternalName[(ACPI_NATIVE_UINT) (PrefixLength + 1)];
749             break;
750
751         case AML_DUAL_NAME_PREFIX:
752
753             /* Two 4-byte names */
754
755             NamesIndex = PrefixLength + 1;
756             NumSegments = 2;
757             break;
758
759         case 0:
760
761             /* NullName */
762
763             NamesIndex = 0;
764             NumSegments = 0;
765             break;
766
767         default:
768
769             /* one 4-byte name */
770
771             NamesIndex = PrefixLength;
772             NumSegments = 1;
773             break;
774         }
775     }
776
777     /*
778      * Calculate the length of ConvertedName, which equals the length
779      * of the prefix, length of all object names, length of any required
780      * punctuation ('.') between object names, plus the NULL terminator.
781      */
782     RequiredLength = PrefixLength + (4 * NumSegments) +
783                         ((NumSegments > 0) ? (NumSegments - 1) : 0) + 1;
784
785     /*
786      * Check to see if we're still in bounds.  If not, there's a problem
787      * with InternalName (invalid format).
788      */
789     if (RequiredLength > InternalNameLength)
790     {
791         ACPI_REPORT_ERROR (("NsExternalizeName: Invalid internal name\n"));
792         return_ACPI_STATUS (AE_BAD_PATHNAME);
793     }
794
795     /*
796      * Build ConvertedName...
797      */
798     *ConvertedName = ACPI_MEM_CALLOCATE (RequiredLength);
799     if (!(*ConvertedName))
800     {
801         return_ACPI_STATUS (AE_NO_MEMORY);
802     }
803
804     j = 0;
805
806     for (i = 0; i < PrefixLength; i++)
807     {
808         (*ConvertedName)[j++] = InternalName[i];
809     }
810
811     if (NumSegments > 0)
812     {
813         for (i = 0; i < NumSegments; i++)
814         {
815             if (i > 0)
816             {
817                 (*ConvertedName)[j++] = '.';
818             }
819
820             (*ConvertedName)[j++] = InternalName[NamesIndex++];
821             (*ConvertedName)[j++] = InternalName[NamesIndex++];
822             (*ConvertedName)[j++] = InternalName[NamesIndex++];
823             (*ConvertedName)[j++] = InternalName[NamesIndex++];
824         }
825     }
826
827     if (ConvertedNameLength)
828     {
829         *ConvertedNameLength = (UINT32) RequiredLength;
830     }
831
832     return_ACPI_STATUS (AE_OK);
833 }
834
835
836 /*******************************************************************************
837  *
838  * FUNCTION:    AcpiNsMapHandleToNode
839  *
840  * PARAMETERS:  Handle          - Handle to be converted to an Node
841  *
842  * RETURN:      A Name table entry pointer
843  *
844  * DESCRIPTION: Convert a namespace handle to a real Node
845  *
846  * Note: Real integer handles allow for more verification
847  *       and keep all pointers within this subsystem.
848  *
849  ******************************************************************************/
850
851 ACPI_NAMESPACE_NODE *
852 AcpiNsMapHandleToNode (
853     ACPI_HANDLE             Handle)
854 {
855
856     ACPI_FUNCTION_ENTRY ();
857
858
859     /*
860      * Simple implementation.
861      */
862     if (!Handle)
863     {
864         return (NULL);
865     }
866
867     if (Handle == ACPI_ROOT_OBJECT)
868     {
869         return (AcpiGbl_RootNode);
870     }
871
872     /* We can at least attempt to verify the handle */
873
874     if (ACPI_GET_DESCRIPTOR_TYPE (Handle) != ACPI_DESC_TYPE_NAMED)
875     {
876         return (NULL);
877     }
878
879     return ((ACPI_NAMESPACE_NODE *) Handle);
880 }
881
882
883 /*******************************************************************************
884  *
885  * FUNCTION:    AcpiNsConvertEntryToHandle
886  *
887  * PARAMETERS:  Node          - Node to be converted to a Handle
888  *
889  * RETURN:      An USER ACPI_HANDLE
890  *
891  * DESCRIPTION: Convert a real Node to a namespace handle
892  *
893  ******************************************************************************/
894
895 ACPI_HANDLE
896 AcpiNsConvertEntryToHandle (
897     ACPI_NAMESPACE_NODE         *Node)
898 {
899
900
901     /*
902      * Simple implementation for now;
903      */
904     return ((ACPI_HANDLE) Node);
905
906
907 /* ---------------------------------------------------
908
909     if (!Node)
910     {
911         return (NULL);
912     }
913
914     if (Node == AcpiGbl_RootNode)
915     {
916         return (ACPI_ROOT_OBJECT);
917     }
918
919
920     return ((ACPI_HANDLE) Node);
921 ------------------------------------------------------*/
922 }
923
924
925 /*******************************************************************************
926  *
927  * FUNCTION:    AcpiNsTerminate
928  *
929  * PARAMETERS:  none
930  *
931  * RETURN:      none
932  *
933  * DESCRIPTION: free memory allocated for table storage.
934  *
935  ******************************************************************************/
936
937 void
938 AcpiNsTerminate (void)
939 {
940     ACPI_OPERAND_OBJECT     *ObjDesc;
941
942
943     ACPI_FUNCTION_TRACE ("NsTerminate");
944
945
946     /*
947      * 1) Free the entire namespace -- all nodes and objects
948      *
949      * Delete all object descriptors attached to namepsace nodes
950      */
951     AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
952
953     /* Detach any objects attached to the root */
954
955     ObjDesc = AcpiNsGetAttachedObject (AcpiGbl_RootNode);
956     if (ObjDesc)
957     {
958         AcpiNsDetachObject (AcpiGbl_RootNode);
959     }
960
961     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Namespace freed\n"));
962
963     /*
964      * 2) Now we can delete the ACPI tables
965      */
966     AcpiTbDeleteAllTables ();
967     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ACPI Tables freed\n"));
968
969     return_VOID;
970 }
971
972
973 /*******************************************************************************
974  *
975  * FUNCTION:    AcpiNsOpensScope
976  *
977  * PARAMETERS:  Type        - A valid namespace type
978  *
979  * RETURN:      NEWSCOPE if the passed type "opens a name scope" according
980  *              to the ACPI specification, else 0
981  *
982  ******************************************************************************/
983
984 UINT32
985 AcpiNsOpensScope (
986     ACPI_OBJECT_TYPE        Type)
987 {
988     ACPI_FUNCTION_TRACE_STR ("NsOpensScope", AcpiUtGetTypeName (Type));
989
990
991     if (!AcpiUtValidObjectType (Type))
992     {
993         /* type code out of range  */
994
995         ACPI_REPORT_WARNING (("NsOpensScope: Invalid Object Type %X\n", Type));
996         return_VALUE (ACPI_NS_NORMAL);
997     }
998
999     return_VALUE (((UINT32) AcpiGbl_NsProperties[Type]) & ACPI_NS_NEWSCOPE);
1000 }
1001
1002
1003 /*******************************************************************************
1004  *
1005  * FUNCTION:    AcpiNsGetNodeByPath
1006  *
1007  * PARAMETERS:  *Pathname   - Name to be found, in external (ASL) format. The
1008  *                            \ (backslash) and ^ (carat) prefixes, and the
1009  *                            . (period) to separate segments are supported.
1010  *              StartNode   - Root of subtree to be searched, or NS_ALL for the
1011  *                            root of the name space.  If Name is fully
1012  *                            qualified (first INT8 is '\'), the passed value
1013  *                            of Scope will not be accessed.
1014  *              Flags       - Used to indicate whether to perform upsearch or
1015  *                            not.
1016  *              ReturnNode  - Where the Node is returned
1017  *
1018  * DESCRIPTION: Look up a name relative to a given scope and return the
1019  *              corresponding Node.  NOTE: Scope can be null.
1020  *
1021  * MUTEX:       Locks namespace
1022  *
1023  ******************************************************************************/
1024
1025 ACPI_STATUS
1026 AcpiNsGetNodeByPath (
1027     char                    *Pathname,
1028     ACPI_NAMESPACE_NODE     *StartNode,
1029     UINT32                  Flags,
1030     ACPI_NAMESPACE_NODE     **ReturnNode)
1031 {
1032     ACPI_GENERIC_STATE      ScopeInfo;
1033     ACPI_STATUS             Status;
1034     char                    *InternalPath = NULL;
1035
1036
1037     ACPI_FUNCTION_TRACE_PTR ("NsGetNodeByPath", Pathname);
1038
1039
1040     if (Pathname)
1041     {
1042         /* Convert path to internal representation */
1043
1044         Status = AcpiNsInternalizeName (Pathname, &InternalPath);
1045         if (ACPI_FAILURE (Status))
1046         {
1047             return_ACPI_STATUS (Status);
1048         }
1049     }
1050
1051     /* Must lock namespace during lookup */
1052
1053     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1054     if (ACPI_FAILURE (Status))
1055     {
1056         return_ACPI_STATUS (Status);
1057     }
1058
1059     /* Setup lookup scope (search starting point) */
1060
1061     ScopeInfo.Scope.Node = StartNode;
1062
1063     /* Lookup the name in the namespace */
1064
1065     Status = AcpiNsLookup (&ScopeInfo, InternalPath,
1066                             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
1067                             (Flags | ACPI_NS_DONT_OPEN_SCOPE),
1068                             NULL, ReturnNode);
1069     if (ACPI_FAILURE (Status))
1070     {
1071         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s, %s\n",
1072                 InternalPath, AcpiFormatException (Status)));
1073     }
1074
1075     /* Cleanup */
1076
1077     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1078
1079     if (InternalPath)
1080     {
1081         ACPI_MEM_FREE (InternalPath);
1082     }
1083     return_ACPI_STATUS (Status);
1084 }
1085
1086
1087 /*******************************************************************************
1088  *
1089  * FUNCTION:    AcpiNsFindParentName
1090  *
1091  * PARAMETERS:  *ChildNode             - Named Obj whose name is to be found
1092  *
1093  * RETURN:      The ACPI name
1094  *
1095  * DESCRIPTION: Search for the given obj in its parent scope and return the
1096  *              name segment, or "????" if the parent name can't be found
1097  *              (which "should not happen").
1098  *
1099  ******************************************************************************/
1100
1101 ACPI_NAME
1102 AcpiNsFindParentName (
1103     ACPI_NAMESPACE_NODE     *ChildNode)
1104 {
1105     ACPI_NAMESPACE_NODE     *ParentNode;
1106
1107
1108     ACPI_FUNCTION_TRACE ("NsFindParentName");
1109
1110
1111     if (ChildNode)
1112     {
1113         /* Valid entry.  Get the parent Node */
1114
1115         ParentNode = AcpiNsGetParentNode (ChildNode);
1116         if (ParentNode)
1117         {
1118             ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Parent of %p [%4.4s] is %p [%4.4s]\n",
1119                 ChildNode,  AcpiUtGetNodeName (ChildNode),
1120                 ParentNode, AcpiUtGetNodeName (ParentNode)));
1121
1122             if (ParentNode->Name.Integer)
1123             {
1124                 return_VALUE ((ACPI_NAME) ParentNode->Name.Integer);
1125             }
1126         }
1127
1128         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "unable to find parent of %p (%4.4s)\n",
1129             ChildNode, AcpiUtGetNodeName (ChildNode)));
1130     }
1131
1132     return_VALUE (ACPI_UNKNOWN_NAME);
1133 }
1134
1135
1136 /*******************************************************************************
1137  *
1138  * FUNCTION:    AcpiNsGetParentNode
1139  *
1140  * PARAMETERS:  Node       - Current table entry
1141  *
1142  * RETURN:      Parent entry of the given entry
1143  *
1144  * DESCRIPTION: Obtain the parent entry for a given entry in the namespace.
1145  *
1146  ******************************************************************************/
1147
1148
1149 ACPI_NAMESPACE_NODE *
1150 AcpiNsGetParentNode (
1151     ACPI_NAMESPACE_NODE     *Node)
1152 {
1153     ACPI_FUNCTION_ENTRY ();
1154
1155
1156     if (!Node)
1157     {
1158         return (NULL);
1159     }
1160
1161     /*
1162      * Walk to the end of this peer list.
1163      * The last entry is marked with a flag and the peer
1164      * pointer is really a pointer back to the parent.
1165      * This saves putting a parent back pointer in each and
1166      * every named object!
1167      */
1168     while (!(Node->Flags & ANOBJ_END_OF_PEER_LIST))
1169     {
1170         Node = Node->Peer;
1171     }
1172
1173
1174     return (Node->Peer);
1175 }
1176
1177
1178 /*******************************************************************************
1179  *
1180  * FUNCTION:    AcpiNsGetNextValidNode
1181  *
1182  * PARAMETERS:  Node       - Current table entry
1183  *
1184  * RETURN:      Next valid Node in the linked node list.  NULL if no more valid
1185  *              nodess
1186  *
1187  * DESCRIPTION: Find the next valid node within a name table.
1188  *              Useful for implementing NULL-end-of-list loops.
1189  *
1190  ******************************************************************************/
1191
1192
1193 ACPI_NAMESPACE_NODE *
1194 AcpiNsGetNextValidNode (
1195     ACPI_NAMESPACE_NODE     *Node)
1196 {
1197
1198     /* If we are at the end of this peer list, return NULL */
1199
1200     if (Node->Flags & ANOBJ_END_OF_PEER_LIST)
1201     {
1202         return NULL;
1203     }
1204
1205     /* Otherwise just return the next peer */
1206
1207     return (Node->Peer);
1208 }
1209
1210