checkdirs() was being passed the wrong mount point, resulting in a panic
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050309 / namespace / nsutils.c
1 /******************************************************************************
2  *
3  * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4  *                        parents and siblings and Scope manipulation
5  *              $Revision: 137 $
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 "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",
164             *(ACPI_CAST_PTR (UINT32, InternalName)));
165     }
166     else
167     {
168         /* Convert path to external format */
169
170         Status = AcpiNsExternalizeName (ACPI_UINT32_MAX,
171                     InternalName, NULL, &Name);
172
173         /* Print target name */
174
175         if (ACPI_SUCCESS (Status))
176         {
177             AcpiOsPrintf ("[%s]", Name);
178         }
179         else
180         {
181             AcpiOsPrintf ("[COULD NOT EXTERNALIZE NAME]");
182         }
183
184         if (Name)
185         {
186             ACPI_MEM_FREE (Name);
187         }
188     }
189
190     AcpiOsPrintf (" in namespace, %s\n",
191         AcpiFormatException (LookupStatus));
192 }
193
194
195 /*******************************************************************************
196  *
197  * FUNCTION:    AcpiNsReportMethodError
198  *
199  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
200  *              LineNumber          - Caller's line number (for error output)
201  *              ComponentId         - Caller's component ID (for error output)
202  *              Message             - Error message to use on failure
203  *
204  * RETURN:      None
205  *
206  * DESCRIPTION: Print warning message with full pathname
207  *
208  ******************************************************************************/
209
210 void
211 AcpiNsReportMethodError (
212     char                    *ModuleName,
213     UINT32                  LineNumber,
214     UINT32                  ComponentId,
215     char                    *Message,
216     ACPI_NAMESPACE_NODE     *PrefixNode,
217     char                    *Path,
218     ACPI_STATUS             MethodStatus)
219 {
220     ACPI_STATUS             Status;
221     ACPI_NAMESPACE_NODE     *Node = PrefixNode;
222
223
224     if (Path)
225     {
226         Status = AcpiNsGetNodeByPath (Path, PrefixNode,
227                     ACPI_NS_NO_UPSEARCH, &Node);
228         if (ACPI_FAILURE (Status))
229         {
230             AcpiOsPrintf ("ReportMethodError: Could not get node\n");
231             return;
232         }
233     }
234
235     AcpiOsPrintf ("%8s-%04d: *** Error: ", ModuleName, LineNumber);
236     AcpiNsPrintNodePathname (Node, Message);
237     AcpiOsPrintf (", %s\n", AcpiFormatException (MethodStatus));
238 }
239
240
241 /*******************************************************************************
242  *
243  * FUNCTION:    AcpiNsPrintNodePathname
244  *
245  * PARAMETERS:  Node                - Object
246  *              Msg                 - Prefix message
247  *
248  * DESCRIPTION: Print an object's full namespace pathname
249  *              Manages allocation/freeing of a pathname buffer
250  *
251  ******************************************************************************/
252
253 void
254 AcpiNsPrintNodePathname (
255     ACPI_NAMESPACE_NODE     *Node,
256     char                    *Msg)
257 {
258     ACPI_BUFFER             Buffer;
259     ACPI_STATUS             Status;
260
261
262     if (!Node)
263     {
264         AcpiOsPrintf ("[NULL NAME]");
265         return;
266     }
267
268     /* Convert handle to full pathname and print it (with supplied message) */
269
270     Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
271
272     Status = AcpiNsHandleToPathname (Node, &Buffer);
273     if (ACPI_SUCCESS (Status))
274     {
275         if (Msg)
276         {
277             AcpiOsPrintf ("%s ", Msg);
278         }
279
280         AcpiOsPrintf ("[%s] (Node %p)", (char *) Buffer.Pointer, Node);
281         ACPI_MEM_FREE (Buffer.Pointer);
282     }
283 }
284
285
286 /*******************************************************************************
287  *
288  * FUNCTION:    AcpiNsValidRootPrefix
289  *
290  * PARAMETERS:  Prefix          - Character to be checked
291  *
292  * RETURN:      TRUE if a valid prefix
293  *
294  * DESCRIPTION: Check if a character is a valid ACPI Root prefix
295  *
296  ******************************************************************************/
297
298 BOOLEAN
299 AcpiNsValidRootPrefix (
300     char                    Prefix)
301 {
302
303     return ((BOOLEAN) (Prefix == '\\'));
304 }
305
306
307 /*******************************************************************************
308  *
309  * FUNCTION:    AcpiNsValidPathSeparator
310  *
311  * PARAMETERS:  Sep              - Character to be checked
312  *
313  * RETURN:      TRUE if a valid path separator
314  *
315  * DESCRIPTION: Check if a character is a valid ACPI path separator
316  *
317  ******************************************************************************/
318
319 BOOLEAN
320 AcpiNsValidPathSeparator (
321     char                    Sep)
322 {
323
324     return ((BOOLEAN) (Sep == '.'));
325 }
326
327
328 /*******************************************************************************
329  *
330  * FUNCTION:    AcpiNsGetType
331  *
332  * PARAMETERS:  Handle              - Parent Node to be examined
333  *
334  * RETURN:      Type field from Node whose handle is passed
335  *
336  ******************************************************************************/
337
338 ACPI_OBJECT_TYPE
339 AcpiNsGetType (
340     ACPI_NAMESPACE_NODE     *Node)
341 {
342     ACPI_FUNCTION_TRACE ("NsGetType");
343
344
345     if (!Node)
346     {
347         ACPI_REPORT_WARNING (("NsGetType: Null Node input pointer\n"));
348         return_VALUE (ACPI_TYPE_ANY);
349     }
350
351     return_VALUE ((ACPI_OBJECT_TYPE) Node->Type);
352 }
353
354
355 /*******************************************************************************
356  *
357  * FUNCTION:    AcpiNsLocal
358  *
359  * PARAMETERS:  Type            - A namespace object type
360  *
361  * RETURN:      LOCAL if names must be found locally in objects of the
362  *              passed type, 0 if enclosing scopes should be searched
363  *
364  ******************************************************************************/
365
366 UINT32
367 AcpiNsLocal (
368     ACPI_OBJECT_TYPE        Type)
369 {
370     ACPI_FUNCTION_TRACE ("NsLocal");
371
372
373     if (!AcpiUtValidObjectType (Type))
374     {
375         /* Type code out of range  */
376
377         ACPI_REPORT_WARNING (("NsLocal: Invalid Object Type\n"));
378         return_VALUE (ACPI_NS_NORMAL);
379     }
380
381     return_VALUE ((UINT32) AcpiGbl_NsProperties[Type] & ACPI_NS_LOCAL);
382 }
383
384
385 /*******************************************************************************
386  *
387  * FUNCTION:    AcpiNsGetInternalNameLength
388  *
389  * PARAMETERS:  Info            - Info struct initialized with the
390  *                                external name pointer.
391  *
392  * RETURN:      Status
393  *
394  * DESCRIPTION: Calculate the length of the internal (AML) namestring
395  *              corresponding to the external (ASL) namestring.
396  *
397  ******************************************************************************/
398
399 void
400 AcpiNsGetInternalNameLength (
401     ACPI_NAMESTRING_INFO    *Info)
402 {
403     char                    *NextExternalChar;
404     UINT32                  i;
405
406
407     ACPI_FUNCTION_ENTRY ();
408
409
410     NextExternalChar = Info->ExternalName;
411     Info->NumCarats = 0;
412     Info->NumSegments = 0;
413     Info->FullyQualified = FALSE;
414
415     /*
416      * For the internal name, the required length is 4 bytes per segment, plus
417      * 1 each for RootPrefix, MultiNamePrefixOp, segment count, trailing null
418      * (which is not really needed, but no there's harm in putting it there)
419      *
420      * strlen() + 1 covers the first NameSeg, which has no path separator
421      */
422     if (AcpiNsValidRootPrefix (NextExternalChar[0]))
423     {
424         Info->FullyQualified = TRUE;
425         NextExternalChar++;
426     }
427     else
428     {
429         /*
430          * Handle Carat prefixes
431          */
432         while (*NextExternalChar == '^')
433         {
434             Info->NumCarats++;
435             NextExternalChar++;
436         }
437     }
438
439     /*
440      * Determine the number of ACPI name "segments" by counting the number of
441      * path separators within the string. Start with one segment since the
442      * segment count is [(# separators) + 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)
749                           InternalName[(ACPI_NATIVE_UINT) (PrefixLength + 1)];
750             break;
751
752         case AML_DUAL_NAME_PREFIX:
753
754             /* Two 4-byte names */
755
756             NamesIndex = PrefixLength + 1;
757             NumSegments = 2;
758             break;
759
760         case 0:
761
762             /* NullName */
763
764             NamesIndex = 0;
765             NumSegments = 0;
766             break;
767
768         default:
769
770             /* one 4-byte name */
771
772             NamesIndex = PrefixLength;
773             NumSegments = 1;
774             break;
775         }
776     }
777
778     /*
779      * Calculate the length of ConvertedName, which equals the length
780      * of the prefix, length of all object names, length of any required
781      * punctuation ('.') between object names, plus the NULL terminator.
782      */
783     RequiredLength = PrefixLength + (4 * NumSegments) +
784                         ((NumSegments > 0) ? (NumSegments - 1) : 0) + 1;
785
786     /*
787      * Check to see if we're still in bounds.  If not, there's a problem
788      * with InternalName (invalid format).
789      */
790     if (RequiredLength > InternalNameLength)
791     {
792         ACPI_REPORT_ERROR (("NsExternalizeName: Invalid internal name\n"));
793         return_ACPI_STATUS (AE_BAD_PATHNAME);
794     }
795
796     /*
797      * Build ConvertedName
798      */
799     *ConvertedName = ACPI_MEM_CALLOCATE (RequiredLength);
800     if (!(*ConvertedName))
801     {
802         return_ACPI_STATUS (AE_NO_MEMORY);
803     }
804
805     j = 0;
806
807     for (i = 0; i < PrefixLength; i++)
808     {
809         (*ConvertedName)[j++] = InternalName[i];
810     }
811
812     if (NumSegments > 0)
813     {
814         for (i = 0; i < NumSegments; i++)
815         {
816             if (i > 0)
817             {
818                 (*ConvertedName)[j++] = '.';
819             }
820
821             (*ConvertedName)[j++] = InternalName[NamesIndex++];
822             (*ConvertedName)[j++] = InternalName[NamesIndex++];
823             (*ConvertedName)[j++] = InternalName[NamesIndex++];
824             (*ConvertedName)[j++] = InternalName[NamesIndex++];
825         }
826     }
827
828     if (ConvertedNameLength)
829     {
830         *ConvertedNameLength = (UINT32) RequiredLength;
831     }
832
833     return_ACPI_STATUS (AE_OK);
834 }
835
836
837 /*******************************************************************************
838  *
839  * FUNCTION:    AcpiNsMapHandleToNode
840  *
841  * PARAMETERS:  Handle          - Handle to be converted to an Node
842  *
843  * RETURN:      A Name table entry pointer
844  *
845  * DESCRIPTION: Convert a namespace handle to a real Node
846  *
847  * Note: Real integer handles allow for more verification
848  *       and keep all pointers within this subsystem.
849  *
850  ******************************************************************************/
851
852 ACPI_NAMESPACE_NODE *
853 AcpiNsMapHandleToNode (
854     ACPI_HANDLE             Handle)
855 {
856
857     ACPI_FUNCTION_ENTRY ();
858
859
860     /*
861      * Simple implementation.
862      */
863     if (!Handle)
864     {
865         return (NULL);
866     }
867
868     if (Handle == ACPI_ROOT_OBJECT)
869     {
870         return (AcpiGbl_RootNode);
871     }
872
873     /* We can at least attempt to verify the handle */
874
875     if (ACPI_GET_DESCRIPTOR_TYPE (Handle) != ACPI_DESC_TYPE_NAMED)
876     {
877         return (NULL);
878     }
879
880     return ((ACPI_NAMESPACE_NODE *) Handle);
881 }
882
883
884 /*******************************************************************************
885  *
886  * FUNCTION:    AcpiNsConvertEntryToHandle
887  *
888  * PARAMETERS:  Node          - Node to be converted to a Handle
889  *
890  * RETURN:      A user handle
891  *
892  * DESCRIPTION: Convert a real Node to a namespace handle
893  *
894  ******************************************************************************/
895
896 ACPI_HANDLE
897 AcpiNsConvertEntryToHandle (
898     ACPI_NAMESPACE_NODE         *Node)
899 {
900
901
902     /*
903      * Simple implementation for now;
904      */
905     return ((ACPI_HANDLE) Node);
906
907
908 /* ---------------------------------------------------
909
910     if (!Node)
911     {
912         return (NULL);
913     }
914
915     if (Node == AcpiGbl_RootNode)
916     {
917         return (ACPI_ROOT_OBJECT);
918     }
919
920
921     return ((ACPI_HANDLE) Node);
922 ------------------------------------------------------*/
923 }
924
925
926 /*******************************************************************************
927  *
928  * FUNCTION:    AcpiNsTerminate
929  *
930  * PARAMETERS:  none
931  *
932  * RETURN:      none
933  *
934  * DESCRIPTION: free memory allocated for table storage.
935  *
936  ******************************************************************************/
937
938 void
939 AcpiNsTerminate (void)
940 {
941     ACPI_OPERAND_OBJECT     *ObjDesc;
942
943
944     ACPI_FUNCTION_TRACE ("NsTerminate");
945
946
947     /*
948      * 1) Free the entire namespace -- all nodes and objects
949      *
950      * Delete all object descriptors attached to namepsace nodes
951      */
952     AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
953
954     /* Detach any objects attached to the root */
955
956     ObjDesc = AcpiNsGetAttachedObject (AcpiGbl_RootNode);
957     if (ObjDesc)
958     {
959         AcpiNsDetachObject (AcpiGbl_RootNode);
960     }
961
962     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Namespace freed\n"));
963
964     /*
965      * 2) Now we can delete the ACPI tables
966      */
967     AcpiTbDeleteAllTables ();
968     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ACPI Tables freed\n"));
969
970     return_VOID;
971 }
972
973
974 /*******************************************************************************
975  *
976  * FUNCTION:    AcpiNsOpensScope
977  *
978  * PARAMETERS:  Type        - A valid namespace type
979  *
980  * RETURN:      NEWSCOPE if the passed type "opens a name scope" according
981  *              to the ACPI specification, else 0
982  *
983  ******************************************************************************/
984
985 UINT32
986 AcpiNsOpensScope (
987     ACPI_OBJECT_TYPE        Type)
988 {
989     ACPI_FUNCTION_TRACE_STR ("NsOpensScope", AcpiUtGetTypeName (Type));
990
991
992     if (!AcpiUtValidObjectType (Type))
993     {
994         /* type code out of range  */
995
996         ACPI_REPORT_WARNING (("NsOpensScope: Invalid Object Type %X\n", Type));
997         return_VALUE (ACPI_NS_NORMAL);
998     }
999
1000     return_VALUE (((UINT32) AcpiGbl_NsProperties[Type]) & ACPI_NS_NEWSCOPE);
1001 }
1002
1003
1004 /*******************************************************************************
1005  *
1006  * FUNCTION:    AcpiNsGetNodeByPath
1007  *
1008  * PARAMETERS:  *Pathname   - Name to be found, in external (ASL) format. The
1009  *                            \ (backslash) and ^ (carat) prefixes, and the
1010  *                            . (period) to separate segments are supported.
1011  *              StartNode   - Root of subtree to be searched, or NS_ALL for the
1012  *                            root of the name space.  If Name is fully
1013  *                            qualified (first INT8 is '\'), the passed value
1014  *                            of Scope will not be accessed.
1015  *              Flags       - Used to indicate whether to perform upsearch or
1016  *                            not.
1017  *              ReturnNode  - Where the Node is returned
1018  *
1019  * DESCRIPTION: Look up a name relative to a given scope and return the
1020  *              corresponding Node.  NOTE: Scope can be null.
1021  *
1022  * MUTEX:       Locks namespace
1023  *
1024  ******************************************************************************/
1025
1026 ACPI_STATUS
1027 AcpiNsGetNodeByPath (
1028     char                    *Pathname,
1029     ACPI_NAMESPACE_NODE     *StartNode,
1030     UINT32                  Flags,
1031     ACPI_NAMESPACE_NODE     **ReturnNode)
1032 {
1033     ACPI_GENERIC_STATE      ScopeInfo;
1034     ACPI_STATUS             Status;
1035     char                    *InternalPath = NULL;
1036
1037
1038     ACPI_FUNCTION_TRACE_PTR ("NsGetNodeByPath", Pathname);
1039
1040
1041     if (Pathname)
1042     {
1043         /* Convert path to internal representation */
1044
1045         Status = AcpiNsInternalizeName (Pathname, &InternalPath);
1046         if (ACPI_FAILURE (Status))
1047         {
1048             return_ACPI_STATUS (Status);
1049         }
1050     }
1051
1052     /* Must lock namespace during lookup */
1053
1054     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1055     if (ACPI_FAILURE (Status))
1056     {
1057         goto Cleanup;
1058     }
1059
1060     /* Setup lookup scope (search starting point) */
1061
1062     ScopeInfo.Scope.Node = StartNode;
1063
1064     /* Lookup the name in the namespace */
1065
1066     Status = AcpiNsLookup (&ScopeInfo, InternalPath,
1067                             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
1068                             (Flags | ACPI_NS_DONT_OPEN_SCOPE),
1069                             NULL, ReturnNode);
1070     if (ACPI_FAILURE (Status))
1071     {
1072         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s, %s\n",
1073                 InternalPath, AcpiFormatException (Status)));
1074     }
1075
1076     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1077
1078 Cleanup:
1079     /* Cleanup */
1080     if (InternalPath)
1081     {
1082         ACPI_MEM_FREE (InternalPath);
1083     }
1084     return_ACPI_STATUS (Status);
1085 }
1086
1087
1088 /*******************************************************************************
1089  *
1090  * FUNCTION:    AcpiNsFindParentName
1091  *
1092  * PARAMETERS:  *ChildNode             - Named Obj whose name is to be found
1093  *
1094  * RETURN:      The ACPI name
1095  *
1096  * DESCRIPTION: Search for the given obj in its parent scope and return the
1097  *              name segment, or "????" if the parent name can't be found
1098  *              (which "should not happen").
1099  *
1100  ******************************************************************************/
1101
1102 ACPI_NAME
1103 AcpiNsFindParentName (
1104     ACPI_NAMESPACE_NODE     *ChildNode)
1105 {
1106     ACPI_NAMESPACE_NODE     *ParentNode;
1107
1108
1109     ACPI_FUNCTION_TRACE ("NsFindParentName");
1110
1111
1112     if (ChildNode)
1113     {
1114         /* Valid entry.  Get the parent Node */
1115
1116         ParentNode = AcpiNsGetParentNode (ChildNode);
1117         if (ParentNode)
1118         {
1119             ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
1120                 "Parent of %p [%4.4s] is %p [%4.4s]\n",
1121                 ChildNode,  AcpiUtGetNodeName (ChildNode),
1122                 ParentNode, AcpiUtGetNodeName (ParentNode)));
1123
1124             if (ParentNode->Name.Integer)
1125             {
1126                 return_VALUE ((ACPI_NAME) ParentNode->Name.Integer);
1127             }
1128         }
1129
1130         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
1131             "Unable to find parent of %p (%4.4s)\n",
1132             ChildNode, AcpiUtGetNodeName (ChildNode)));
1133     }
1134
1135     return_VALUE (ACPI_UNKNOWN_NAME);
1136 }
1137
1138
1139 /*******************************************************************************
1140  *
1141  * FUNCTION:    AcpiNsGetParentNode
1142  *
1143  * PARAMETERS:  Node       - Current table entry
1144  *
1145  * RETURN:      Parent entry of the given entry
1146  *
1147  * DESCRIPTION: Obtain the parent entry for a given entry in the namespace.
1148  *
1149  ******************************************************************************/
1150
1151
1152 ACPI_NAMESPACE_NODE *
1153 AcpiNsGetParentNode (
1154     ACPI_NAMESPACE_NODE     *Node)
1155 {
1156     ACPI_FUNCTION_ENTRY ();
1157
1158
1159     if (!Node)
1160     {
1161         return (NULL);
1162     }
1163
1164     /*
1165      * Walk to the end of this peer list. The last entry is marked with a flag
1166      * and the peer pointer is really a pointer back to the parent. This saves
1167      * putting a parent back pointer in each and every named object!
1168      */
1169     while (!(Node->Flags & ANOBJ_END_OF_PEER_LIST))
1170     {
1171         Node = Node->Peer;
1172     }
1173
1174
1175     return (Node->Peer);
1176 }
1177
1178
1179 /*******************************************************************************
1180  *
1181  * FUNCTION:    AcpiNsGetNextValidNode
1182  *
1183  * PARAMETERS:  Node       - Current table entry
1184  *
1185  * RETURN:      Next valid Node in the linked node list. NULL if no more valid
1186  *              nodes.
1187  *
1188  * DESCRIPTION: Find the next valid node within a name table.
1189  *              Useful for implementing NULL-end-of-list loops.
1190  *
1191  ******************************************************************************/
1192
1193
1194 ACPI_NAMESPACE_NODE *
1195 AcpiNsGetNextValidNode (
1196     ACPI_NAMESPACE_NODE     *Node)
1197 {
1198
1199     /* If we are at the end of this peer list, return NULL */
1200
1201     if (Node->Flags & ANOBJ_END_OF_PEER_LIST)
1202     {
1203         return NULL;
1204     }
1205
1206     /* Otherwise just return the next peer */
1207
1208     return (Node->Peer);
1209 }
1210
1211