1 /******************************************************************************
2  *
3  * Module Name: nsparse - namespace interface to AML parser
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, 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 (
147             StartNode, ACPI_TYPE_METHOD, WalkState);
148         if (ACPI_FAILURE (Status))
149         {
150             AcpiDsDeleteWalkState (WalkState);
151             goto Cleanup;
152         }
153     }
154 
155     /* Parse the AML */
156 
157     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
158         "*PARSE* pass %u parse\n", PassNumber));
159     Status = AcpiPsParseAml (WalkState);
160 
161 Cleanup:
162     AcpiPsDeleteParseTree (ParseRoot);
163     return_ACPI_STATUS (Status);
164 }
165 
166 
167 /*******************************************************************************
168  *
169  * FUNCTION:    AcpiNsParseTable
170  *
171  * PARAMETERS:  TableDesc       - An ACPI table descriptor for table to parse
172  *              StartNode       - Where to enter the table into the namespace
173  *
174  * RETURN:      Status
175  *
176  * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
177  *
178  ******************************************************************************/
179 
180 ACPI_STATUS
181 AcpiNsParseTable (
182     UINT32                  TableIndex,
183     ACPI_NAMESPACE_NODE     *StartNode)
184 {
185     ACPI_STATUS             Status;
186 
187 
188     ACPI_FUNCTION_TRACE (NsParseTable);
189 
190 
191     /*
192      * AML Parse, pass 1
193      *
194      * In this pass, we load most of the namespace. Control methods
195      * are not parsed until later. A parse tree is not created. Instead,
196      * each Parser Op subtree is deleted when it is finished. This saves
197      * a great deal of memory, and allows a small cache of parse objects
198      * to service the entire parse. The second pass of the parse then
199      * performs another complete parse of the AML.
200      */
201     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 1\n"));
202 
203     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS1,
204         TableIndex, StartNode);
205     if (ACPI_FAILURE (Status))
206     {
207         return_ACPI_STATUS (Status);
208     }
209 
210     /*
211      * AML Parse, pass 2
212      *
213      * In this pass, we resolve forward references and other things
214      * that could not be completed during the first pass.
215      * Another complete parse of the AML is performed, but the
216      * overhead of this is compensated for by the fact that the
217      * parse objects are all cached.
218      */
219     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 2\n"));
220     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2,
221         TableIndex, StartNode);
222     if (ACPI_FAILURE (Status))
223     {
224         return_ACPI_STATUS (Status);
225     }
226 
227     return_ACPI_STATUS (Status);
228 }
229