1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: nsparse - namespace interface to AML parser
4433d6423SLionel Sambuc *
5433d6423SLionel Sambuc *****************************************************************************/
6433d6423SLionel Sambuc
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc * All rights reserved.
10433d6423SLionel Sambuc *
11*29492bb7SDavid van Moolenbroek * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek * are met:
14*29492bb7SDavid van Moolenbroek * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek * notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek * without modification.
17*29492bb7SDavid van Moolenbroek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek * substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek * ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek * including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek * binary redistribution.
22*29492bb7SDavid van Moolenbroek * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek * of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek * from this software without specific prior written permission.
25433d6423SLionel Sambuc *
26*29492bb7SDavid van Moolenbroek * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek * Software Foundation.
29433d6423SLionel Sambuc *
30*29492bb7SDavid van Moolenbroek * NO WARRANTY
31*29492bb7SDavid van Moolenbroek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acnamesp.h"
47433d6423SLionel Sambuc #include "acparser.h"
48433d6423SLionel Sambuc #include "acdispat.h"
49433d6423SLionel Sambuc #include "actables.h"
50433d6423SLionel Sambuc
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc #define _COMPONENT ACPI_NAMESPACE
53433d6423SLionel Sambuc ACPI_MODULE_NAME ("nsparse")
54433d6423SLionel Sambuc
55433d6423SLionel Sambuc
56433d6423SLionel Sambuc /*******************************************************************************
57433d6423SLionel Sambuc *
58433d6423SLionel Sambuc * FUNCTION: NsOneCompleteParse
59433d6423SLionel Sambuc *
60433d6423SLionel Sambuc * PARAMETERS: PassNumber - 1 or 2
61433d6423SLionel Sambuc * TableDesc - The table to be parsed.
62433d6423SLionel Sambuc *
63433d6423SLionel Sambuc * RETURN: Status
64433d6423SLionel Sambuc *
65433d6423SLionel Sambuc * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
66433d6423SLionel Sambuc *
67433d6423SLionel Sambuc ******************************************************************************/
68433d6423SLionel Sambuc
69433d6423SLionel Sambuc ACPI_STATUS
AcpiNsOneCompleteParse(UINT32 PassNumber,UINT32 TableIndex,ACPI_NAMESPACE_NODE * StartNode)70433d6423SLionel Sambuc AcpiNsOneCompleteParse (
71433d6423SLionel Sambuc UINT32 PassNumber,
72433d6423SLionel Sambuc UINT32 TableIndex,
73433d6423SLionel Sambuc ACPI_NAMESPACE_NODE *StartNode)
74433d6423SLionel Sambuc {
75433d6423SLionel Sambuc ACPI_PARSE_OBJECT *ParseRoot;
76433d6423SLionel Sambuc ACPI_STATUS Status;
77433d6423SLionel Sambuc UINT32 AmlLength;
78433d6423SLionel Sambuc UINT8 *AmlStart;
79433d6423SLionel Sambuc ACPI_WALK_STATE *WalkState;
80433d6423SLionel Sambuc ACPI_TABLE_HEADER *Table;
81433d6423SLionel Sambuc ACPI_OWNER_ID OwnerId;
82433d6423SLionel Sambuc
83433d6423SLionel Sambuc
84433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (NsOneCompleteParse);
85433d6423SLionel Sambuc
86433d6423SLionel Sambuc
87433d6423SLionel Sambuc Status = AcpiTbGetOwnerId (TableIndex, &OwnerId);
88433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
89433d6423SLionel Sambuc {
90433d6423SLionel Sambuc return_ACPI_STATUS (Status);
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc
93433d6423SLionel Sambuc /* Create and init a Root Node */
94433d6423SLionel Sambuc
95433d6423SLionel Sambuc ParseRoot = AcpiPsCreateScopeOp ();
96433d6423SLionel Sambuc if (!ParseRoot)
97433d6423SLionel Sambuc {
98433d6423SLionel Sambuc return_ACPI_STATUS (AE_NO_MEMORY);
99433d6423SLionel Sambuc }
100433d6423SLionel Sambuc
101433d6423SLionel Sambuc /* Create and initialize a new walk state */
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc WalkState = AcpiDsCreateWalkState (OwnerId, NULL, NULL, NULL);
104433d6423SLionel Sambuc if (!WalkState)
105433d6423SLionel Sambuc {
106433d6423SLionel Sambuc AcpiPsFreeOp (ParseRoot);
107433d6423SLionel Sambuc return_ACPI_STATUS (AE_NO_MEMORY);
108433d6423SLionel Sambuc }
109433d6423SLionel Sambuc
110433d6423SLionel Sambuc Status = AcpiGetTableByIndex (TableIndex, &Table);
111433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
112433d6423SLionel Sambuc {
113433d6423SLionel Sambuc AcpiDsDeleteWalkState (WalkState);
114433d6423SLionel Sambuc AcpiPsFreeOp (ParseRoot);
115433d6423SLionel Sambuc return_ACPI_STATUS (Status);
116433d6423SLionel Sambuc }
117433d6423SLionel Sambuc
118433d6423SLionel Sambuc /* Table must consist of at least a complete header */
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc if (Table->Length < sizeof (ACPI_TABLE_HEADER))
121433d6423SLionel Sambuc {
122433d6423SLionel Sambuc Status = AE_BAD_HEADER;
123433d6423SLionel Sambuc }
124433d6423SLionel Sambuc else
125433d6423SLionel Sambuc {
126433d6423SLionel Sambuc AmlStart = (UINT8 *) Table + sizeof (ACPI_TABLE_HEADER);
127433d6423SLionel Sambuc AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
128433d6423SLionel Sambuc Status = AcpiDsInitAmlWalk (WalkState, ParseRoot, NULL,
129433d6423SLionel Sambuc AmlStart, AmlLength, NULL, (UINT8) PassNumber);
130433d6423SLionel Sambuc }
131433d6423SLionel Sambuc
132433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
133433d6423SLionel Sambuc {
134433d6423SLionel Sambuc AcpiDsDeleteWalkState (WalkState);
135433d6423SLionel Sambuc goto Cleanup;
136433d6423SLionel Sambuc }
137433d6423SLionel Sambuc
138433d6423SLionel Sambuc /* StartNode is the default location to load the table */
139433d6423SLionel Sambuc
140433d6423SLionel Sambuc if (StartNode && StartNode != AcpiGbl_RootNode)
141433d6423SLionel Sambuc {
142433d6423SLionel Sambuc Status = AcpiDsScopeStackPush (StartNode, ACPI_TYPE_METHOD, WalkState);
143433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
144433d6423SLionel Sambuc {
145433d6423SLionel Sambuc AcpiDsDeleteWalkState (WalkState);
146433d6423SLionel Sambuc goto Cleanup;
147433d6423SLionel Sambuc }
148433d6423SLionel Sambuc }
149433d6423SLionel Sambuc
150433d6423SLionel Sambuc /* Parse the AML */
151433d6423SLionel Sambuc
152433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "*PARSE* pass %u parse\n", PassNumber));
153433d6423SLionel Sambuc Status = AcpiPsParseAml (WalkState);
154433d6423SLionel Sambuc
155433d6423SLionel Sambuc Cleanup:
156433d6423SLionel Sambuc AcpiPsDeleteParseTree (ParseRoot);
157433d6423SLionel Sambuc return_ACPI_STATUS (Status);
158433d6423SLionel Sambuc }
159433d6423SLionel Sambuc
160433d6423SLionel Sambuc
161433d6423SLionel Sambuc /*******************************************************************************
162433d6423SLionel Sambuc *
163433d6423SLionel Sambuc * FUNCTION: AcpiNsParseTable
164433d6423SLionel Sambuc *
165433d6423SLionel Sambuc * PARAMETERS: TableDesc - An ACPI table descriptor for table to parse
166433d6423SLionel Sambuc * StartNode - Where to enter the table into the namespace
167433d6423SLionel Sambuc *
168433d6423SLionel Sambuc * RETURN: Status
169433d6423SLionel Sambuc *
170433d6423SLionel Sambuc * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
171433d6423SLionel Sambuc *
172433d6423SLionel Sambuc ******************************************************************************/
173433d6423SLionel Sambuc
174433d6423SLionel Sambuc ACPI_STATUS
AcpiNsParseTable(UINT32 TableIndex,ACPI_NAMESPACE_NODE * StartNode)175433d6423SLionel Sambuc AcpiNsParseTable (
176433d6423SLionel Sambuc UINT32 TableIndex,
177433d6423SLionel Sambuc ACPI_NAMESPACE_NODE *StartNode)
178433d6423SLionel Sambuc {
179433d6423SLionel Sambuc ACPI_STATUS Status;
180433d6423SLionel Sambuc
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (NsParseTable);
183433d6423SLionel Sambuc
184433d6423SLionel Sambuc
185433d6423SLionel Sambuc /*
186433d6423SLionel Sambuc * AML Parse, pass 1
187433d6423SLionel Sambuc *
188433d6423SLionel Sambuc * In this pass, we load most of the namespace. Control methods
189433d6423SLionel Sambuc * are not parsed until later. A parse tree is not created. Instead,
190433d6423SLionel Sambuc * each Parser Op subtree is deleted when it is finished. This saves
191433d6423SLionel Sambuc * a great deal of memory, and allows a small cache of parse objects
192433d6423SLionel Sambuc * to service the entire parse. The second pass of the parse then
193433d6423SLionel Sambuc * performs another complete parse of the AML.
194433d6423SLionel Sambuc */
195433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 1\n"));
196433d6423SLionel Sambuc Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS1,
197433d6423SLionel Sambuc TableIndex, StartNode);
198433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
199433d6423SLionel Sambuc {
200433d6423SLionel Sambuc return_ACPI_STATUS (Status);
201433d6423SLionel Sambuc }
202433d6423SLionel Sambuc
203433d6423SLionel Sambuc /*
204433d6423SLionel Sambuc * AML Parse, pass 2
205433d6423SLionel Sambuc *
206433d6423SLionel Sambuc * In this pass, we resolve forward references and other things
207433d6423SLionel Sambuc * that could not be completed during the first pass.
208433d6423SLionel Sambuc * Another complete parse of the AML is performed, but the
209433d6423SLionel Sambuc * overhead of this is compensated for by the fact that the
210433d6423SLionel Sambuc * parse objects are all cached.
211433d6423SLionel Sambuc */
212433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 2\n"));
213433d6423SLionel Sambuc Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2,
214433d6423SLionel Sambuc TableIndex, StartNode);
215433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
216433d6423SLionel Sambuc {
217433d6423SLionel Sambuc return_ACPI_STATUS (Status);
218433d6423SLionel Sambuc }
219433d6423SLionel Sambuc
220433d6423SLionel Sambuc return_ACPI_STATUS (Status);
221433d6423SLionel Sambuc }
222