1 /******************************************************************************
3 * Module Name: asllength - Tree walk to determine package and opcode lengths
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("asllength")
52 /* Local prototypes */
55 CgGetPackageLenByteCount (
56 ACPI_PARSE_OBJECT *Op,
57 UINT32 PackageLength);
60 CgGenerateAmlOpcodeLength (
61 ACPI_PARSE_OBJECT *Op);
64 #ifdef ACPI_OBSOLETE_FUNCTIONS
66 LnAdjustLengthToRoot (
67 ACPI_PARSE_OBJECT *Op,
72 /*******************************************************************************
74 * FUNCTION: LnInitLengthsWalk
76 * PARAMETERS: ASL_WALK_CALLBACK
80 * DESCRIPTION: Walk callback to initialize (and re-initialize) the node
81 * subtree length(s) to zero. The Subtree lengths are bubbled
82 * up to the root node in order to get a total AML length.
84 ******************************************************************************/
88 ACPI_PARSE_OBJECT *Op,
93 Op->Asl.AmlSubtreeLength = 0;
98 /*******************************************************************************
100 * FUNCTION: LnPackageLengthWalk
102 * PARAMETERS: ASL_WALK_CALLBACK
106 * DESCRIPTION: Walk callback to calculate the total AML length.
107 * 1) Calculate the AML lengths (opcode, package length, etc.) for
109 * 2) Bubbble up all of these lengths to the parent node by summing
110 * them all into the parent subtree length.
112 * Note: The SubtreeLength represents the total AML length of all child nodes
113 * in all subtrees under a given node. Therefore, once this walk is
114 * complete, the Root Node subtree length is the AML length of the entire
115 * tree (and thus, the entire ACPI table)
117 ******************************************************************************/
120 LnPackageLengthWalk (
121 ACPI_PARSE_OBJECT *Op,
126 /* Generate the AML lengths for this node */
128 CgGenerateAmlLengths (Op);
130 /* Bubble up all lengths (this node and all below it) to the parent */
132 if ((Op->Asl.Parent) &&
133 (Op->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG))
135 Op->Asl.Parent->Asl.AmlSubtreeLength += (Op->Asl.AmlLength +
136 Op->Asl.AmlOpcodeLength +
137 Op->Asl.AmlPkgLenBytes +
138 Op->Asl.AmlSubtreeLength);
144 /*******************************************************************************
146 * FUNCTION: CgGetPackageLenByteCount
148 * PARAMETERS: Op - Parse node
149 * PackageLength - Length to be encoded
151 * RETURN: Required length of the package length encoding
153 * DESCRIPTION: Calculate the number of bytes required to encode the given
156 ******************************************************************************/
159 CgGetPackageLenByteCount (
160 ACPI_PARSE_OBJECT *Op,
161 UINT32 PackageLength)
165 * Determine the number of bytes required to encode the package length
166 * Note: the package length includes the number of bytes used to encode
167 * the package length, so we must account for this also.
169 if (PackageLength <= (0x0000003F - 1))
173 else if (PackageLength <= (0x00000FFF - 2))
177 else if (PackageLength <= (0x000FFFFF - 3))
181 else if (PackageLength <= (0x0FFFFFFF - 4))
187 /* Fatal error - the package length is too large to encode */
189 AslError (ASL_ERROR, ASL_MSG_ENCODING_LENGTH, Op, NULL);
196 /*******************************************************************************
198 * FUNCTION: CgGenerateAmlOpcodeLength
200 * PARAMETERS: Op - Parse node whose AML opcode lengths will be
205 * DESCRIPTION: Calculate the AmlOpcodeLength, AmlPkgLenBytes, and AmlLength
206 * fields for this node.
208 ******************************************************************************/
211 CgGenerateAmlOpcodeLength (
212 ACPI_PARSE_OBJECT *Op)
215 /* Check for two-byte opcode */
217 if (Op->Asl.AmlOpcode > 0x00FF)
219 Op->Asl.AmlOpcodeLength = 2;
223 Op->Asl.AmlOpcodeLength = 1;
226 /* Does this opcode have an associated "PackageLength" field? */
228 Op->Asl.AmlPkgLenBytes = 0;
229 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
231 Op->Asl.AmlPkgLenBytes = CgGetPackageLenByteCount (
232 Op, Op->Asl.AmlSubtreeLength);
235 /* Data opcode lengths are easy */
237 switch (Op->Asl.AmlOpcode)
241 Op->Asl.AmlLength = 1;
246 Op->Asl.AmlLength = 2;
251 Op->Asl.AmlLength = 4;
256 Op->Asl.AmlLength = 8;
261 /* All data opcodes must be above */
267 /*******************************************************************************
269 * FUNCTION: CgGenerateAmlLengths
271 * PARAMETERS: Op - Parse node
275 * DESCRIPTION: Generate internal length fields based on the AML opcode or
278 ******************************************************************************/
281 CgGenerateAmlLengths (
282 ACPI_PARSE_OBJECT *Op)
288 switch (Op->Asl.AmlOpcode)
290 case AML_RAW_DATA_BYTE:
292 Op->Asl.AmlOpcodeLength = 0;
293 Op->Asl.AmlLength = 1;
296 case AML_RAW_DATA_WORD:
298 Op->Asl.AmlOpcodeLength = 0;
299 Op->Asl.AmlLength = 2;
302 case AML_RAW_DATA_DWORD:
304 Op->Asl.AmlOpcodeLength = 0;
305 Op->Asl.AmlLength = 4;
308 case AML_RAW_DATA_QWORD:
310 Op->Asl.AmlOpcodeLength = 0;
311 Op->Asl.AmlLength = 8;
314 case AML_RAW_DATA_BUFFER:
316 /* Aml length is/was set by creator */
318 Op->Asl.AmlOpcodeLength = 0;
321 case AML_RAW_DATA_CHAIN:
323 /* Aml length is/was set by creator */
325 Op->Asl.AmlOpcodeLength = 0;
333 switch (Op->Asl.ParseOpcode)
335 case PARSEOP_DEFINITIONBLOCK:
337 Gbl_TableLength = sizeof (ACPI_TABLE_HEADER) +
338 Op->Asl.AmlSubtreeLength;
341 case PARSEOP_NAMESEG:
343 Op->Asl.AmlOpcodeLength = 0;
344 Op->Asl.AmlLength = 4;
345 Op->Asl.ExternalName = Op->Asl.Value.String;
348 case PARSEOP_NAMESTRING:
349 case PARSEOP_METHODCALL:
351 if (Op->Asl.CompileFlags & NODE_NAME_INTERNALIZED)
356 Op->Asl.AmlOpcodeLength = 0;
357 Status = UtInternalizeName (Op->Asl.Value.String, &Buffer);
358 if (ACPI_FAILURE (Status))
360 DbgPrint (ASL_DEBUG_OUTPUT,
361 "Failure from internalize name %X\n", Status);
365 Op->Asl.ExternalName = Op->Asl.Value.String;
366 Op->Asl.Value.String = Buffer;
367 Op->Asl.CompileFlags |= NODE_NAME_INTERNALIZED;
369 Op->Asl.AmlLength = strlen (Buffer);
372 * Check for single backslash reference to root,
373 * make it a null terminated string in the AML
375 if (Op->Asl.AmlLength == 1)
377 Op->Asl.AmlLength = 2;
381 case PARSEOP_STRING_LITERAL:
383 Op->Asl.AmlOpcodeLength = 1;
385 /* Get null terminator */
387 Op->Asl.AmlLength = strlen (Op->Asl.Value.String) + 1;
390 case PARSEOP_PACKAGE_LENGTH:
392 Op->Asl.AmlOpcodeLength = 0;
393 Op->Asl.AmlPkgLenBytes = CgGetPackageLenByteCount (Op,
394 (UINT32) Op->Asl.Value.Integer);
397 case PARSEOP_RAW_DATA:
399 Op->Asl.AmlOpcodeLength = 0;
402 case PARSEOP_DEFAULT_ARG:
403 case PARSEOP_EXTERNAL:
404 case PARSEOP_INCLUDE:
405 case PARSEOP_INCLUDE_END:
407 /* Ignore the "default arg" nodes, they are extraneous at this point */
413 CgGenerateAmlOpcodeLength (Op);
419 #ifdef ACPI_OBSOLETE_FUNCTIONS
420 /*******************************************************************************
422 * FUNCTION: LnAdjustLengthToRoot
424 * PARAMETERS: Op - Node whose Length was changed
428 * DESCRIPTION: Change the Subtree length of the given node, and bubble the
429 * change all the way up to the root node. This allows for
430 * last second changes to a package length (for example, if the
431 * package length encoding gets shorter or longer.)
433 ******************************************************************************/
436 LnAdjustLengthToRoot (
437 ACPI_PARSE_OBJECT *SubtreeOp,
440 ACPI_PARSE_OBJECT *Op;
443 /* Adjust all subtree lengths up to the root */
445 Op = SubtreeOp->Asl.Parent;
448 Op->Asl.AmlSubtreeLength -= LengthDelta;
452 /* Adjust the global table length */
454 Gbl_TableLength -= LengthDelta;