1 /******************************************************************************
2  *
3  * Module Name: dmdeferred - Disassembly of deferred AML opcodes
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 "acdispat.h"
47 #include "amlcode.h"
48 #include "acdisasm.h"
49 #include "acparser.h"
50 
51 #define _COMPONENT          ACPI_CA_DISASSEMBLER
52         ACPI_MODULE_NAME    ("dmdeferred")
53 
54 
55 /* Local prototypes */
56 
57 static ACPI_STATUS
58 AcpiDmDeferredParse (
59     ACPI_PARSE_OBJECT       *Op,
60     UINT8                   *Aml,
61     UINT32                  AmlLength);
62 
63 
64 /******************************************************************************
65  *
66  * FUNCTION:    AcpiDmParseDeferredOps
67  *
68  * PARAMETERS:  Root                - Root of the parse tree
69  *
70  * RETURN:      Status
71  *
72  * DESCRIPTION: Parse the deferred opcodes (Methods, regions, etc.)
73  *
74  *****************************************************************************/
75 
76 ACPI_STATUS
77 AcpiDmParseDeferredOps (
78     ACPI_PARSE_OBJECT       *Root)
79 {
80     const ACPI_OPCODE_INFO  *OpInfo;
81     ACPI_PARSE_OBJECT       *Op = Root;
82     ACPI_STATUS             Status;
83 
84 
85     ACPI_FUNCTION_ENTRY ();
86 
87 
88     /* Traverse the entire parse tree */
89 
90     while (Op)
91     {
92         OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
93         if (!(OpInfo->Flags & AML_DEFER))
94         {
95             Op = AcpiPsGetDepthNext (Root, Op);
96             continue;
97         }
98 
99         /* Now we know we have a deferred opcode */
100 
101         switch (Op->Common.AmlOpcode)
102         {
103         case AML_METHOD_OP:
104         case AML_BUFFER_OP:
105         case AML_PACKAGE_OP:
106         case AML_VAR_PACKAGE_OP:
107 
108             Status = AcpiDmDeferredParse (Op, Op->Named.Data, Op->Named.Length);
109             if (ACPI_FAILURE (Status))
110             {
111                 return (Status);
112             }
113             break;
114 
115         /* We don't need to do anything for these deferred opcodes */
116 
117         case AML_REGION_OP:
118         case AML_DATA_REGION_OP:
119         case AML_CREATE_QWORD_FIELD_OP:
120         case AML_CREATE_DWORD_FIELD_OP:
121         case AML_CREATE_WORD_FIELD_OP:
122         case AML_CREATE_BYTE_FIELD_OP:
123         case AML_CREATE_BIT_FIELD_OP:
124         case AML_CREATE_FIELD_OP:
125         case AML_BANK_FIELD_OP:
126 
127             break;
128 
129         default:
130 
131             ACPI_ERROR ((AE_INFO, "Unhandled deferred AML opcode [0x%.4X]",
132                  Op->Common.AmlOpcode));
133             break;
134         }
135 
136         Op = AcpiPsGetDepthNext (Root, Op);
137     }
138 
139     return (AE_OK);
140 }
141 
142 
143 /******************************************************************************
144  *
145  * FUNCTION:    AcpiDmDeferredParse
146  *
147  * PARAMETERS:  Op                  - Root Op of the deferred opcode
148  *              Aml                 - Pointer to the raw AML
149  *              AmlLength           - Length of the AML
150  *
151  * RETURN:      Status
152  *
153  * DESCRIPTION: Parse one deferred opcode
154  *              (Methods, operation regions, etc.)
155  *
156  *****************************************************************************/
157 
158 static ACPI_STATUS
159 AcpiDmDeferredParse (
160     ACPI_PARSE_OBJECT       *Op,
161     UINT8                   *Aml,
162     UINT32                  AmlLength)
163 {
164     ACPI_WALK_STATE         *WalkState;
165     ACPI_STATUS             Status;
166     ACPI_PARSE_OBJECT       *SearchOp;
167     ACPI_PARSE_OBJECT       *StartOp;
168     ACPI_PARSE_OBJECT       *NewRootOp;
169     ACPI_PARSE_OBJECT       *ExtraOp;
170 
171 
172     ACPI_FUNCTION_TRACE (DmDeferredParse);
173 
174 
175     if (!Aml || !AmlLength)
176     {
177         return_ACPI_STATUS (AE_OK);
178     }
179 
180     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Parsing deferred opcode %s [%4.4s]\n",
181         Op->Common.AmlOpName, (char *) &Op->Named.Name));
182 
183     /* Need a new walk state to parse the AML */
184 
185     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
186     if (!WalkState)
187     {
188         return_ACPI_STATUS (AE_NO_MEMORY);
189     }
190 
191     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL, Aml,
192         AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
193     if (ACPI_FAILURE (Status))
194     {
195         return_ACPI_STATUS (Status);
196     }
197 
198     /* Parse the AML for this deferred opcode */
199 
200     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
201     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
202     Status = AcpiPsParseAml (WalkState);
203 
204     StartOp = (Op->Common.Value.Arg)->Common.Next;
205     SearchOp = StartOp;
206     while (SearchOp)
207     {
208         SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);
209     }
210 
211     /*
212      * For Buffer and Package opcodes, link the newly parsed subtree
213      * into the main parse tree
214      */
215     switch (Op->Common.AmlOpcode)
216     {
217     case AML_BUFFER_OP:
218     case AML_PACKAGE_OP:
219     case AML_VAR_PACKAGE_OP:
220 
221         switch (Op->Common.AmlOpcode)
222         {
223         case AML_PACKAGE_OP:
224 
225             ExtraOp = Op->Common.Value.Arg;
226             NewRootOp = ExtraOp->Common.Next;
227             ACPI_FREE (ExtraOp);
228             break;
229 
230         case AML_VAR_PACKAGE_OP:
231         case AML_BUFFER_OP:
232         default:
233 
234             NewRootOp = Op->Common.Value.Arg;
235             break;
236         }
237 
238         Op->Common.Value.Arg = NewRootOp->Common.Value.Arg;
239 
240         /* Must point all parents to the main tree */
241 
242         StartOp = Op;
243         SearchOp = StartOp;
244         while (SearchOp)
245         {
246             if (SearchOp->Common.Parent == NewRootOp)
247             {
248                 SearchOp->Common.Parent = Op;
249             }
250 
251             SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);
252         }
253 
254         ACPI_FREE (NewRootOp);
255         break;
256 
257     default:
258 
259         break;
260     }
261 
262     return_ACPI_STATUS (AE_OK);
263 }
264