1 /******************************************************************************
2  *
3  * Module Name: nsparse - namespace interface to AML parser
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acparser.h"
48 #include "acdispat.h"
49 #include "actables.h"
50 
51 
52 #define _COMPONENT          ACPI_NAMESPACE
53         ACPI_MODULE_NAME    ("nsparse")
54 
55 
56 /*******************************************************************************
57  *
58  * FUNCTION:    NsOneCompleteParse
59  *
60  * PARAMETERS:  PassNumber              - 1 or 2
61  *              TableDesc               - The table to be parsed.
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
66  *
67  ******************************************************************************/
68 
69 ACPI_STATUS
70 AcpiNsOneCompleteParse (
71     UINT32                  PassNumber,
72     UINT32                  TableIndex,
73     ACPI_NAMESPACE_NODE     *StartNode)
74 {
75     ACPI_PARSE_OBJECT       *ParseRoot;
76     ACPI_STATUS             Status;
77     UINT32                  AmlLength;
78     UINT8                   *AmlStart;
79     ACPI_WALK_STATE         *WalkState;
80     ACPI_TABLE_HEADER       *Table;
81     ACPI_OWNER_ID           OwnerId;
82 
83 
84     ACPI_FUNCTION_TRACE (NsOneCompleteParse);
85 
86 
87     Status = AcpiGetTableByIndex (TableIndex, &Table);
88     if (ACPI_FAILURE (Status))
89     {
90         return_ACPI_STATUS (Status);
91     }
92 
93     /* Table must consist of at least a complete header */
94 
95     if (Table->Length < sizeof (ACPI_TABLE_HEADER))
96     {
97         return_ACPI_STATUS (AE_BAD_HEADER);
98     }
99 
100     AmlStart = (UINT8 *) Table + sizeof (ACPI_TABLE_HEADER);
101     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
102 
103     Status = AcpiTbGetOwnerId (TableIndex, &OwnerId);
104     if (ACPI_FAILURE (Status))
105     {
106         return_ACPI_STATUS (Status);
107     }
108 
109     /* Create and init a Root Node */
110 
111     ParseRoot = AcpiPsCreateScopeOp (AmlStart);
112     if (!ParseRoot)
113     {
114         return_ACPI_STATUS (AE_NO_MEMORY);
115     }
116 
117     /* Create and initialize a new walk state */
118 
119     WalkState = AcpiDsCreateWalkState (OwnerId, NULL, NULL, NULL);
120     if (!WalkState)
121     {
122         AcpiPsFreeOp (ParseRoot);
123         return_ACPI_STATUS (AE_NO_MEMORY);
124     }
125 
126     Status = AcpiDsInitAmlWalk (WalkState, ParseRoot, NULL,
127                 AmlStart, AmlLength, NULL, (UINT8) PassNumber);
128     if (ACPI_FAILURE (Status))
129     {
130         AcpiDsDeleteWalkState (WalkState);
131         goto Cleanup;
132     }
133 
134     /* Found OSDT table, enable the namespace override feature */
135 
136     if (ACPI_COMPARE_NAME(Table->Signature, ACPI_SIG_OSDT) &&
137         PassNumber == ACPI_IMODE_LOAD_PASS1)
138     {
139         WalkState->NamespaceOverride = TRUE;
140     }
141 
142     /* StartNode is the default location to load the table  */
143 
144     if (StartNode && StartNode != AcpiGbl_RootNode)
145     {
146         Status = AcpiDsScopeStackPush (StartNode, ACPI_TYPE_METHOD, WalkState);
147         if (ACPI_FAILURE (Status))
148         {
149             AcpiDsDeleteWalkState (WalkState);
150             goto Cleanup;
151         }
152     }
153 
154     /* Parse the AML */
155 
156     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "*PARSE* pass %u parse\n", PassNumber));
157     Status = AcpiPsParseAml (WalkState);
158 
159 Cleanup:
160     AcpiPsDeleteParseTree (ParseRoot);
161     return_ACPI_STATUS (Status);
162 }
163 
164 
165 /*******************************************************************************
166  *
167  * FUNCTION:    AcpiNsParseTable
168  *
169  * PARAMETERS:  TableDesc       - An ACPI table descriptor for table to parse
170  *              StartNode       - Where to enter the table into the namespace
171  *
172  * RETURN:      Status
173  *
174  * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
175  *
176  ******************************************************************************/
177 
178 ACPI_STATUS
179 AcpiNsParseTable (
180     UINT32                  TableIndex,
181     ACPI_NAMESPACE_NODE     *StartNode)
182 {
183     ACPI_STATUS             Status;
184 
185 
186     ACPI_FUNCTION_TRACE (NsParseTable);
187 
188 
189     /*
190      * AML Parse, pass 1
191      *
192      * In this pass, we load most of the namespace. Control methods
193      * are not parsed until later. A parse tree is not created. Instead,
194      * each Parser Op subtree is deleted when it is finished. This saves
195      * a great deal of memory, and allows a small cache of parse objects
196      * to service the entire parse. The second pass of the parse then
197      * performs another complete parse of the AML.
198      */
199     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 1\n"));
200     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS1,
201                 TableIndex, StartNode);
202     if (ACPI_FAILURE (Status))
203     {
204         return_ACPI_STATUS (Status);
205     }
206 
207     /*
208      * AML Parse, pass 2
209      *
210      * In this pass, we resolve forward references and other things
211      * that could not be completed during the first pass.
212      * Another complete parse of the AML is performed, but the
213      * overhead of this is compensated for by the fact that the
214      * parse objects are all cached.
215      */
216     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 2\n"));
217     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2,
218                 TableIndex, StartNode);
219     if (ACPI_FAILURE (Status))
220     {
221         return_ACPI_STATUS (Status);
222     }
223 
224     return_ACPI_STATUS (Status);
225 }
226