nssearch.c revision 91116
1180811Sstas/*******************************************************************************
2180811Sstas *
3181602Sraj * Module Name: nssearch - Namespace search
4181602Sraj *              $Revision: 82 $
5180811Sstas *
6180811Sstas ******************************************************************************/
7180811Sstas
8180811Sstas/******************************************************************************
9180811Sstas *
10180811Sstas * 1. Copyright Notice
11180811Sstas *
12180811Sstas * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp.
13180811Sstas * All rights reserved.
14180811Sstas *
15180811Sstas * 2. License
16180811Sstas *
17180811Sstas * 2.1. This is your license from Intel Corp. under its intellectual property
18180811Sstas * rights.  You may have additional license terms from the party that provided
19180811Sstas * you this software, covering your right to use that party's intellectual
20180811Sstas * property rights.
21180811Sstas *
22180811Sstas * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23180811Sstas * copy of the source code appearing in this file ("Covered Code") an
24180811Sstas * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25180811Sstas * base code distributed originally by Intel ("Original Intel Code") to copy,
26180811Sstas * make derivatives, distribute, use and display any portion of the Covered
27180811Sstas * Code in any form, with the right to sublicense such rights; and
28180811Sstas *
29180811Sstas * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30180811Sstas * license (with the right to sublicense), under only those claims of Intel
31181602Sraj * patents that are infringed by the Original Intel Code, to make, use, sell,
32180811Sstas * offer to sell, and import the Covered Code and derivative works thereof
33180811Sstas * solely to the minimum extent necessary to exercise the above copyright
34180811Sstas * license, and in no event shall the patent license extend to any additions
35180811Sstas * to or modifications of the Original Intel Code.  No other license or right
36180811Sstas * is granted directly or by implication, estoppel or otherwise;
37180811Sstas *
38180811Sstas * The above copyright and patent license is granted only if the following
39180811Sstas * conditions are met:
40180811Sstas *
41180811Sstas * 3. Conditions
42180811Sstas *
43180811Sstas * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44180811Sstas * Redistribution of source code of any substantial portion of the Covered
45180811Sstas * Code or modification with rights to further distribute source must include
46180811Sstas * the above Copyright Notice, the above License, this list of Conditions,
47180811Sstas * and the following Disclaimer and Export Compliance provision.  In addition,
48180811Sstas * Licensee must cause all Covered Code to which Licensee contributes to
49180811Sstas * contain a file documenting the changes Licensee made to create that Covered
50180811Sstas * Code and the date of any change.  Licensee must include in that file the
51180811Sstas * documentation of any changes made by any predecessor Licensee.  Licensee
52180811Sstas * must include a prominent statement that the modification is derived,
53180811Sstas * directly or indirectly, from Original Intel Code.
54180811Sstas *
55180811Sstas * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56181602Sraj * Redistribution of source code of any substantial portion of the Covered
57181602Sraj * Code or modification without rights to further distribute source must
58181602Sraj * include the following Disclaimer and Export Compliance provision in the
59181602Sraj * documentation and/or other materials provided with distribution.  In
60181602Sraj * addition, Licensee may not authorize further sublicense of source of any
61180811Sstas * portion of the Covered Code, and must include terms to the effect that the
62181602Sraj * license from Licensee to its licensee is limited to the intellectual
63181602Sraj * property embodied in the software Licensee provides to its licensee, and
64181602Sraj * not to intellectual property embodied in modifications its licensee may
65181602Sraj * make.
66180811Sstas *
67181602Sraj * 3.3. Redistribution of Executable. Redistribution in executable form of any
68181602Sraj * substantial portion of the Covered Code or modification must reproduce the
69181602Sraj * above Copyright Notice, and the following Disclaimer and Export Compliance
70181602Sraj * provision in the documentation and/or other materials provided with the
71180811Sstas * distribution.
72181602Sraj *
73181602Sraj * 3.4. Intel retains all right, title, and interest in and to the Original
74181602Sraj * Intel Code.
75181602Sraj *
76181602Sraj * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77181602Sraj * Intel shall be used in advertising or otherwise to promote the sale, use or
78181602Sraj * other dealings in products derived from or relating to the Covered Code
79181602Sraj * without prior written authorization from Intel.
80181602Sraj *
81181602Sraj * 4. Disclaimer and Export Compliance
82181602Sraj *
83181602Sraj * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84181602Sraj * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85181602Sraj * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86180811Sstas * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87180811Sstas * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88181602Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89181602Sraj * PARTICULAR PURPOSE.
90181602Sraj *
91181602Sraj * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92181602Sraj * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93180811Sstas * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94181602Sraj * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95180811Sstas * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96181602Sraj * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97181602Sraj * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98181602Sraj * LIMITED REMEDY.
99181602Sraj *
100180811Sstas * 4.3. Licensee shall not export, either directly or indirectly, any of this
101181602Sraj * software or system incorporating such software without first obtaining any
102181602Sraj * required license or other approval from the U. S. Department of Commerce or
103181602Sraj * any other agency or department of the United States Government.  In the
104181602Sraj * event Licensee exports any such software from the United States or
105181602Sraj * re-exports any such software from a foreign destination, Licensee shall
106181602Sraj * ensure that the distribution and export/re-export of the software is in
107181602Sraj * compliance with all laws, regulations, orders, or other restrictions of the
108181602Sraj * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109181602Sraj * any of its subsidiaries will export/re-export any technical data, process,
110181602Sraj * software, or service, directly or indirectly, to any country for which the
111181602Sraj * United States government or any agency thereof requires an export license,
112181602Sraj * other governmental approval, or letter of assurance, without first obtaining
113181602Sraj * such license, approval or letter.
114181602Sraj *
115181602Sraj *****************************************************************************/
116181602Sraj
117181602Sraj#define __NSSEARCH_C__
118181602Sraj
119181602Sraj#include "acpi.h"
120181602Sraj#include "amlcode.h"
121181602Sraj#include "acinterp.h"
122181602Sraj#include "acnamesp.h"
123181602Sraj
124181602Sraj
125181602Sraj#define _COMPONENT          ACPI_NAMESPACE
126181602Sraj        ACPI_MODULE_NAME    ("nssearch")
127181602Sraj
128181602Sraj
129181602Sraj/*******************************************************************************
130181602Sraj *
131181602Sraj * FUNCTION:    AcpiNsSearchNode
132181602Sraj *
133181602Sraj * PARAMETERS:  *TargetName         - Ascii ACPI name to search for
134181602Sraj *              *Node               - Starting table where search will begin
135181602Sraj *              Type                - Object type to match
136181602Sraj *              **ReturnNode        - Where the matched Named obj is returned
137181602Sraj *
138181602Sraj * RETURN:      Status
139181602Sraj *
140181602Sraj * DESCRIPTION: Search a single namespace table.  Performs a simple search,
141181602Sraj *              does not add entries or search parents.
142181602Sraj *
143181602Sraj *
144181602Sraj *      Named object lists are built (and subsequently dumped) in the
145181602Sraj *      order in which the names are encountered during the namespace load;
146181602Sraj *
147181602Sraj *      All namespace searching is linear in this implementation, but
148181602Sraj *      could be easily modified to support any improved search
149181602Sraj *      algorithm.  However, the linear search was chosen for simplicity
150181602Sraj *      and because the trees are small and the other interpreter
151181602Sraj *      execution overhead is relatively high.
152181602Sraj *
153181602Sraj ******************************************************************************/
154180811Sstas
155181602SrajACPI_STATUS
156181602SrajAcpiNsSearchNode (
157181602Sraj    UINT32                  TargetName,
158181602Sraj    ACPI_NAMESPACE_NODE     *Node,
159181602Sraj    ACPI_OBJECT_TYPE        Type,
160181602Sraj    ACPI_NAMESPACE_NODE     **ReturnNode)
161181602Sraj{
162181602Sraj    ACPI_NAMESPACE_NODE     *NextNode;
163181602Sraj
164181602Sraj
165181602Sraj    ACPI_FUNCTION_TRACE ("NsSearchNode");
166181602Sraj
167181602Sraj
168181602Sraj#ifdef ACPI_DEBUG
169181602Sraj    if (ACPI_LV_NAMES & AcpiDbgLevel)
170181602Sraj    {
171181602Sraj        NATIVE_CHAR         *ScopeName;
172181602Sraj
173181602Sraj        ScopeName = AcpiNsGetExternalPathname (Node);
174181602Sraj        if (ScopeName)
175181602Sraj        {
176181602Sraj            ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Searching %s [%p] For %4.4s (type %s)\n",
177181602Sraj                ScopeName, Node, (char *) &TargetName, AcpiUtGetTypeName (Type)));
178181602Sraj
179181602Sraj            ACPI_MEM_FREE (ScopeName);
180181602Sraj        }
181181602Sraj    }
182181602Sraj#endif
183181602Sraj
184181602Sraj    /*
185181602Sraj     * Search for name in this table, which is to say that we must search
186181602Sraj     * for the name among the children of this object
187180811Sstas     */
188180811Sstas    NextNode = Node->Child;
189180811Sstas    while (NextNode)
190180811Sstas    {
191181602Sraj        /* Check for match against the name */
192181602Sraj
193180811Sstas        if (NextNode->Name == TargetName)
194180811Sstas        {
195181602Sraj            /*
196180811Sstas             * Found matching entry.  Capture the type if appropriate, before
197181602Sraj             * returning the entry.
198181602Sraj             *
199180811Sstas             * The DefFieldDefn and BankFieldDefn cases are actually looking up
200181602Sraj             * the Region in which the field will be defined
201181602Sraj             */
202181602Sraj            if ((INTERNAL_TYPE_FIELD_DEFN == Type) ||
203181602Sraj                (INTERNAL_TYPE_BANK_FIELD_DEFN == Type))
204181602Sraj            {
205180811Sstas                Type = ACPI_TYPE_REGION;
206181602Sraj            }
207181602Sraj
208181602Sraj            /*
209181602Sraj             * Scope, DefAny, and IndexFieldDefn are bogus "types" which do not
210181602Sraj             * actually have anything to do with the type of the name being
211181602Sraj             * looked up.  For any other value of Type, if the type stored in
212181602Sraj             * the entry is Any (i.e. unknown), save the actual type.
213181602Sraj             */
214181602Sraj            if (Type != INTERNAL_TYPE_SCOPE &&
215180811Sstas                Type != INTERNAL_TYPE_DEF_ANY &&
216180811Sstas                Type != INTERNAL_TYPE_INDEX_FIELD_DEFN &&
217181602Sraj                NextNode->Type == ACPI_TYPE_ANY)
218181602Sraj            {
219180811Sstas                NextNode->Type = (UINT8) Type;
220181602Sraj            }
221181602Sraj
222181602Sraj            ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
223181602Sraj                "Name %4.4s (actual type %X) found at %p\n",
224181602Sraj                (char *) &TargetName, NextNode->Type, NextNode));
225180811Sstas
226181602Sraj            *ReturnNode = NextNode;
227180811Sstas            return_ACPI_STATUS (AE_OK);
228181602Sraj        }
229181602Sraj
230181602Sraj        /*
231181602Sraj         * The last entry in the list points back to the parent,
232181602Sraj         * so a flag is used to indicate the end-of-list
233181602Sraj         */
234181602Sraj        if (NextNode->Flags & ANOBJ_END_OF_PEER_LIST)
235181602Sraj        {
236181602Sraj            /* Searched entire list, we are done */
237181602Sraj
238181602Sraj            break;
239181602Sraj        }
240181602Sraj
241181602Sraj        /* Didn't match name, move on to the next peer object */
242181602Sraj
243180811Sstas        NextNode = NextNode->Peer;
244180811Sstas    }
245181602Sraj
246181602Sraj    /* Searched entire table, not found */
247180811Sstas
248181602Sraj    ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Name %4.4s (type %X) not found at %p\n",
249181602Sraj        (char *) &TargetName, Type, NextNode));
250181602Sraj
251181602Sraj    return_ACPI_STATUS (AE_NOT_FOUND);
252181602Sraj}
253181602Sraj
254181602Sraj
255181602Sraj/*******************************************************************************
256181602Sraj *
257181602Sraj * FUNCTION:    AcpiNsSearchParentTree
258181602Sraj *
259181602Sraj * PARAMETERS:  *TargetName         - Ascii ACPI name to search for
260181602Sraj *              *Node               - Starting table where search will begin
261181602Sraj *              Type                - Object type to match
262180811Sstas *              **ReturnNode        - Where the matched Named Obj is returned
263181602Sraj *
264180811Sstas * RETURN:      Status
265180811Sstas *
266180811Sstas * DESCRIPTION: Called when a name has not been found in the current namespace
267180811Sstas *              table.  Before adding it or giving up, ACPI scope rules require
268181602Sraj *              searching enclosing scopes in cases identified by AcpiNsLocal().
269180811Sstas *
270180811Sstas *              "A name is located by finding the matching name in the current
271180811Sstas *              name space, and then in the parent name space. If the parent
272181602Sraj *              name space does not contain the name, the search continues
273180811Sstas *              recursively until either the name is found or the name space
274181602Sraj *              does not have a parent (the root of the name space).  This
275180811Sstas *              indicates that the name is not found" (From ACPI Specification,
276180811Sstas *              section 5.3)
277180811Sstas *
278180811Sstas ******************************************************************************/
279180811Sstas
280180811Sstasstatic ACPI_STATUS
281180811SstasAcpiNsSearchParentTree (
282180811Sstas    UINT32                  TargetName,
283181602Sraj    ACPI_NAMESPACE_NODE     *Node,
284180811Sstas    ACPI_OBJECT_TYPE        Type,
285181602Sraj    ACPI_NAMESPACE_NODE     **ReturnNode)
286181602Sraj{
287180811Sstas    ACPI_STATUS             Status;
288180811Sstas    ACPI_NAMESPACE_NODE     *ParentNode;
289180811Sstas
290181602Sraj
291180811Sstas    ACPI_FUNCTION_TRACE ("NsSearchParentTree");
292180811Sstas
293180811Sstas
294180811Sstas    ParentNode = AcpiNsGetParentNode (Node);
295181602Sraj
296180811Sstas    /*
297180811Sstas     * If there is no parent (at the root) or type is "local", we won't be
298180811Sstas     * searching the parent tree.
299180811Sstas     */
300181602Sraj    if ((AcpiNsLocal (Type)) ||
301181602Sraj        (!ParentNode))
302181602Sraj    {
303181602Sraj        if (!ParentNode)
304181602Sraj        {
305181602Sraj            ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "[%4.4s] has no parent\n",
306180811Sstas                (char *) &TargetName));
307180811Sstas        }
308180811Sstas
309181602Sraj        if (AcpiNsLocal (Type))
310180811Sstas        {
311180811Sstas            ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "[%4.4s] type %X is local(no search)\n",
312180811Sstas                (char *) &TargetName, Type));
313181602Sraj        }
314180811Sstas
315181602Sraj        return_ACPI_STATUS (AE_NOT_FOUND);
316180811Sstas    }
317181602Sraj
318180811Sstas    /* Search the parent tree */
319180811Sstas
320180811Sstas    ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Searching parent for %4.4s\n", (char *) &TargetName));
321181602Sraj
322181602Sraj    /*
323181602Sraj     * Search parents until found the target or we have backed up to
324181602Sraj     * the root
325181602Sraj     */
326181602Sraj    while (ParentNode)
327181602Sraj    {
328181602Sraj        /*
329181602Sraj         * Search parent scope.  Use TYPE_ANY because we don't care about the
330181602Sraj         * object type at this point, we only care about the existence of
331181602Sraj         * the actual name we are searching for.  Typechecking comes later.
332180811Sstas         */
333181602Sraj        Status = AcpiNsSearchNode (TargetName, ParentNode,
334180811Sstas                                        ACPI_TYPE_ANY, ReturnNode);
335181602Sraj        if (ACPI_SUCCESS (Status))
336180811Sstas        {
337180811Sstas            return_ACPI_STATUS (Status);
338181602Sraj        }
339181602Sraj
340181602Sraj        /*
341180811Sstas         * Not found here, go up another level
342181602Sraj         * (until we reach the root)
343181602Sraj         */
344180811Sstas        ParentNode = AcpiNsGetParentNode (ParentNode);
345180811Sstas    }
346180811Sstas
347180811Sstas    /* Not found in parent tree */
348181602Sraj
349181602Sraj    return_ACPI_STATUS (AE_NOT_FOUND);
350181602Sraj}
351181602Sraj
352180811Sstas
353180811Sstas/*******************************************************************************
354181602Sraj *
355181602Sraj * FUNCTION:    AcpiNsSearchAndEnter
356181602Sraj *
357181602Sraj * PARAMETERS:  TargetName          - Ascii ACPI name to search for (4 chars)
358181602Sraj *              WalkState           - Current state of the walk
359 *              *Node               - Starting table where search will begin
360 *              InterpreterMode     - Add names only in MODE_LoadPassX.
361 *                                    Otherwise,search only.
362 *              Type                - Object type to match
363 *              Flags               - Flags describing the search restrictions
364 *              **ReturnNode        - Where the Node is returned
365 *
366 * RETURN:      Status
367 *
368 * DESCRIPTION: Search for a name segment in a single name table,
369 *              optionally adding it if it is not found.  If the passed
370 *              Type is not Any and the type previously stored in the
371 *              entry was Any (i.e. unknown), update the stored type.
372 *
373 *              In IMODE_EXECUTE, search only.
374 *              In other modes, search and add if not found.
375 *
376 ******************************************************************************/
377
378ACPI_STATUS
379AcpiNsSearchAndEnter (
380    UINT32                  TargetName,
381    ACPI_WALK_STATE         *WalkState,
382    ACPI_NAMESPACE_NODE     *Node,
383    ACPI_INTERPRETER_MODE   InterpreterMode,
384    ACPI_OBJECT_TYPE        Type,
385    UINT32                  Flags,
386    ACPI_NAMESPACE_NODE     **ReturnNode)
387{
388    ACPI_STATUS             Status;
389    ACPI_NAMESPACE_NODE     *NewNode;
390
391
392    ACPI_FUNCTION_TRACE ("NsSearchAndEnter");
393
394
395    /* Parameter validation */
396
397    if (!Node || !TargetName || !ReturnNode)
398    {
399        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null param-  Table %p Name %X Return %p\n",
400            Node, TargetName, ReturnNode));
401
402        ACPI_REPORT_ERROR (("NsSearchAndEnter: bad (null) parameter\n"));
403        return_ACPI_STATUS (AE_BAD_PARAMETER);
404    }
405
406    /* Name must consist of printable characters */
407
408    if (!AcpiUtValidAcpiName (TargetName))
409    {
410        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "*** Bad character in name: %08x *** \n",
411            TargetName));
412
413        ACPI_REPORT_ERROR (("NsSearchAndEnter: Bad character in ACPI Name\n"));
414        return_ACPI_STATUS (AE_BAD_CHARACTER);
415    }
416
417    /* Try to find the name in the table specified by the caller */
418
419    *ReturnNode = ACPI_ENTRY_NOT_FOUND;
420    Status = AcpiNsSearchNode (TargetName, Node, Type, ReturnNode);
421    if (Status != AE_NOT_FOUND)
422    {
423        /*
424         * If we found it AND the request specifies that a find is an error,
425         * return the error
426         */
427        if ((Status == AE_OK) &&
428            (Flags & ACPI_NS_ERROR_IF_FOUND))
429        {
430            Status = AE_ALREADY_EXISTS;
431        }
432
433        /*
434         * Either found it or there was an error
435         * -- finished either way
436         */
437        return_ACPI_STATUS (Status);
438    }
439
440    /*
441     * The name was not found.  If we are NOT performing the
442     * first pass (name entry) of loading the namespace, search
443     * the parent tree (all the way to the root if necessary.)
444     * We don't want to perform the parent search when the
445     * namespace is actually being loaded.  We want to perform
446     * the search when namespace references are being resolved
447     * (load pass 2) and during the execution phase.
448     */
449    if ((InterpreterMode != ACPI_IMODE_LOAD_PASS1) &&
450        (Flags & ACPI_NS_SEARCH_PARENT))
451    {
452        /*
453         * Not found in table - search parent tree according
454         * to ACPI specification
455         */
456        Status = AcpiNsSearchParentTree (TargetName, Node,
457                                            Type, ReturnNode);
458        if (ACPI_SUCCESS (Status))
459        {
460            return_ACPI_STATUS (Status);
461        }
462    }
463
464    /*
465     * In execute mode, just search, never add names.  Exit now.
466     */
467    if (InterpreterMode == ACPI_IMODE_EXECUTE)
468    {
469        ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%4.4s Not found in %p [Not adding]\n",
470            (char *) &TargetName, Node));
471
472        return_ACPI_STATUS (AE_NOT_FOUND);
473    }
474
475    /* Create the new named object */
476
477    NewNode = AcpiNsCreateNode (TargetName);
478    if (!NewNode)
479    {
480        return_ACPI_STATUS (AE_NO_MEMORY);
481    }
482
483    /* Install the new object into the parent's list of children */
484
485    AcpiNsInstallNode (WalkState, Node, NewNode, Type);
486    *ReturnNode = NewNode;
487
488    return_ACPI_STATUS (AE_OK);
489}
490
491