1 /******************************************************************************
3 * Module Name: psargs - Parse AML opcode arguments
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, 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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acparser.h>
49 #include <contrib/dev/acpica/include/amlcode.h>
50 #include <contrib/dev/acpica/include/acnamesp.h>
51 #include <contrib/dev/acpica/include/acdispat.h>
53 #define _COMPONENT ACPI_PARSER
54 ACPI_MODULE_NAME ("psargs")
56 /* Local prototypes */
59 AcpiPsGetNextPackageLength (
60 ACPI_PARSE_STATE *ParserState);
62 static ACPI_PARSE_OBJECT *
64 ACPI_PARSE_STATE *ParserState);
67 /*******************************************************************************
69 * FUNCTION: AcpiPsGetNextPackageLength
71 * PARAMETERS: ParserState - Current parser state object
73 * RETURN: Decoded package length. On completion, the AML pointer points
74 * past the length byte or bytes.
76 * DESCRIPTION: Decode and return a package length field.
77 * Note: Largest package length is 28 bits, from ACPI specification
79 ******************************************************************************/
82 AcpiPsGetNextPackageLength (
83 ACPI_PARSE_STATE *ParserState)
85 UINT8 *Aml = ParserState->Aml;
86 UINT32 PackageLength = 0;
88 UINT8 ByteZeroMask = 0x3F; /* Default [0:5] */
91 ACPI_FUNCTION_TRACE (PsGetNextPackageLength);
95 * Byte 0 bits [6:7] contain the number of additional bytes
96 * used to encode the package length, either 0,1,2, or 3
98 ByteCount = (Aml[0] >> 6);
99 ParserState->Aml += ((ACPI_SIZE) ByteCount + 1);
101 /* Get bytes 3, 2, 1 as needed */
106 * Final bit positions for the package length bytes:
112 PackageLength |= (Aml[ByteCount] << ((ByteCount << 3) - 4));
114 ByteZeroMask = 0x0F; /* Use bits [0:3] of byte 0 */
118 /* Byte 0 is a special case, either bits [0:3] or [0:5] are used */
120 PackageLength |= (Aml[0] & ByteZeroMask);
121 return_UINT32 (PackageLength);
125 /*******************************************************************************
127 * FUNCTION: AcpiPsGetNextPackageEnd
129 * PARAMETERS: ParserState - Current parser state object
131 * RETURN: Pointer to end-of-package +1
133 * DESCRIPTION: Get next package length and return a pointer past the end of
134 * the package. Consumes the package length field
136 ******************************************************************************/
139 AcpiPsGetNextPackageEnd (
140 ACPI_PARSE_STATE *ParserState)
142 UINT8 *Start = ParserState->Aml;
143 UINT32 PackageLength;
146 ACPI_FUNCTION_TRACE (PsGetNextPackageEnd);
149 /* Function below updates ParserState->Aml */
151 PackageLength = AcpiPsGetNextPackageLength (ParserState);
153 return_PTR (Start + PackageLength); /* end of package */
157 /*******************************************************************************
159 * FUNCTION: AcpiPsGetNextNamestring
161 * PARAMETERS: ParserState - Current parser state object
163 * RETURN: Pointer to the start of the name string (pointer points into
166 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
167 * prefix characters. Set parser state to point past the string.
168 * (Name is consumed from the AML.)
170 ******************************************************************************/
173 AcpiPsGetNextNamestring (
174 ACPI_PARSE_STATE *ParserState)
176 UINT8 *Start = ParserState->Aml;
177 UINT8 *End = ParserState->Aml;
180 ACPI_FUNCTION_TRACE (PsGetNextNamestring);
183 /* Point past any namestring prefix characters (backslash or carat) */
185 while (AcpiPsIsPrefixChar (*End))
190 /* Decode the path prefix character */
205 case AML_DUAL_NAME_PREFIX:
207 /* Two name segments */
209 End += 1 + (2 * ACPI_NAME_SIZE);
212 case AML_MULTI_NAME_PREFIX_OP:
214 /* Multiple name segments, 4 chars each, count in next byte */
216 End += 2 + (*(End + 1) * ACPI_NAME_SIZE);
221 /* Single name segment */
223 End += ACPI_NAME_SIZE;
227 ParserState->Aml = End;
228 return_PTR ((char *) Start);
232 /*******************************************************************************
234 * FUNCTION: AcpiPsGetNextNamepath
236 * PARAMETERS: ParserState - Current parser state object
237 * Arg - Where the namepath will be stored
238 * ArgCount - If the namepath points to a control method
239 * the method's argument is returned here.
240 * PossibleMethodCall - Whether the namepath can possibly be the
241 * start of a method call
245 * DESCRIPTION: Get next name (if method call, return # of required args).
246 * Names are looked up in the internal namespace to determine
247 * if the name represents a control method. If a method
248 * is found, the number of arguments to the method is returned.
249 * This information is critical for parsing to continue correctly.
251 ******************************************************************************/
254 AcpiPsGetNextNamepath (
255 ACPI_WALK_STATE *WalkState,
256 ACPI_PARSE_STATE *ParserState,
257 ACPI_PARSE_OBJECT *Arg,
258 BOOLEAN PossibleMethodCall)
262 ACPI_PARSE_OBJECT *NameOp;
263 ACPI_OPERAND_OBJECT *MethodDesc;
264 ACPI_NAMESPACE_NODE *Node;
265 UINT8 *Start = ParserState->Aml;
268 ACPI_FUNCTION_TRACE (PsGetNextNamepath);
271 Path = AcpiPsGetNextNamestring (ParserState);
272 AcpiPsInitOp (Arg, AML_INT_NAMEPATH_OP);
274 /* Null path case is allowed, just exit */
278 Arg->Common.Value.Name = Path;
279 return_ACPI_STATUS (AE_OK);
283 * Lookup the name in the internal namespace, starting with the current
284 * scope. We don't want to add anything new to the namespace here,
285 * however, so we use MODE_EXECUTE.
286 * Allow searching of the parent tree, but don't open a new scope -
287 * we just want to lookup the object (must be mode EXECUTE to perform
290 Status = AcpiNsLookup (WalkState->ScopeInfo, Path,
291 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
292 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE, NULL, &Node);
295 * If this name is a control method invocation, we must
296 * setup the method call
298 if (ACPI_SUCCESS (Status) &&
299 PossibleMethodCall &&
300 (Node->Type == ACPI_TYPE_METHOD))
302 if (WalkState->Opcode == AML_UNLOAD_OP)
305 * AcpiPsGetNextNamestring has increased the AML pointer,
306 * so we need to restore the saved AML pointer for method call.
308 WalkState->ParserState.Aml = Start;
309 WalkState->ArgCount = 1;
310 AcpiPsInitOp (Arg, AML_INT_METHODCALL_OP);
311 return_ACPI_STATUS (AE_OK);
314 /* This name is actually a control method invocation */
316 MethodDesc = AcpiNsGetAttachedObject (Node);
317 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
318 "Control Method - %p Desc %p Path=%p\n", Node, MethodDesc, Path));
320 NameOp = AcpiPsAllocOp (AML_INT_NAMEPATH_OP);
323 return_ACPI_STATUS (AE_NO_MEMORY);
326 /* Change Arg into a METHOD CALL and attach name to it */
328 AcpiPsInitOp (Arg, AML_INT_METHODCALL_OP);
329 NameOp->Common.Value.Name = Path;
331 /* Point METHODCALL/NAME to the METHOD Node */
333 NameOp->Common.Node = Node;
334 AcpiPsAppendArg (Arg, NameOp);
338 ACPI_ERROR ((AE_INFO,
339 "Control Method %p has no attached object",
341 return_ACPI_STATUS (AE_AML_INTERNAL);
344 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
345 "Control Method - %p Args %X\n",
346 Node, MethodDesc->Method.ParamCount));
348 /* Get the number of arguments to expect */
350 WalkState->ArgCount = MethodDesc->Method.ParamCount;
351 return_ACPI_STATUS (AE_OK);
355 * Special handling if the name was not found during the lookup -
356 * some NotFound cases are allowed
358 if (Status == AE_NOT_FOUND)
360 /* 1) NotFound is ok during load pass 1/2 (allow forward references) */
362 if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) !=
368 /* 2) NotFound during a CondRefOf(x) is ok by definition */
370 else if (WalkState->Op->Common.AmlOpcode == AML_COND_REF_OF_OP)
376 * 3) NotFound while building a Package is ok at this point, we
377 * may flag as an error later if slack mode is not enabled.
378 * (Some ASL code depends on allowing this behavior)
380 else if ((Arg->Common.Parent) &&
381 ((Arg->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
382 (Arg->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP)))
388 /* Final exception check (may have been changed from code above) */
390 if (ACPI_FAILURE (Status))
392 ACPI_ERROR_NAMESPACE (Path, Status);
394 if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) ==
397 /* Report a control method execution error */
399 Status = AcpiDsMethodError (Status, WalkState);
403 /* Save the namepath */
405 Arg->Common.Value.Name = Path;
406 return_ACPI_STATUS (Status);
410 /*******************************************************************************
412 * FUNCTION: AcpiPsGetNextSimpleArg
414 * PARAMETERS: ParserState - Current parser state object
415 * ArgType - The argument type (AML_*_ARG)
416 * Arg - Where the argument is returned
420 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
422 ******************************************************************************/
425 AcpiPsGetNextSimpleArg (
426 ACPI_PARSE_STATE *ParserState,
428 ACPI_PARSE_OBJECT *Arg)
432 UINT8 *Aml = ParserState->Aml;
435 ACPI_FUNCTION_TRACE_U32 (PsGetNextSimpleArg, ArgType);
442 /* Get 1 byte from the AML stream */
444 Opcode = AML_BYTE_OP;
445 Arg->Common.Value.Integer = (UINT64) *Aml;
452 /* Get 2 bytes from the AML stream */
454 Opcode = AML_WORD_OP;
455 ACPI_MOVE_16_TO_64 (&Arg->Common.Value.Integer, Aml);
462 /* Get 4 bytes from the AML stream */
464 Opcode = AML_DWORD_OP;
465 ACPI_MOVE_32_TO_64 (&Arg->Common.Value.Integer, Aml);
472 /* Get 8 bytes from the AML stream */
474 Opcode = AML_QWORD_OP;
475 ACPI_MOVE_64_TO_64 (&Arg->Common.Value.Integer, Aml);
482 /* Get a pointer to the string, point past the string */
484 Opcode = AML_STRING_OP;
485 Arg->Common.Value.String = ACPI_CAST_PTR (char, Aml);
487 /* Find the null terminator */
499 case ARGP_NAMESTRING:
501 AcpiPsInitOp (Arg, AML_INT_NAMEPATH_OP);
502 Arg->Common.Value.Name = AcpiPsGetNextNamestring (ParserState);
508 ACPI_ERROR ((AE_INFO, "Invalid ArgType 0x%X", ArgType));
512 AcpiPsInitOp (Arg, Opcode);
513 ParserState->Aml += Length;
518 /*******************************************************************************
520 * FUNCTION: AcpiPsGetNextField
522 * PARAMETERS: ParserState - Current parser state object
524 * RETURN: A newly allocated FIELD op
526 * DESCRIPTION: Get next field (NamedField, ReservedField, or AccessField)
528 ******************************************************************************/
530 static ACPI_PARSE_OBJECT *
532 ACPI_PARSE_STATE *ParserState)
534 UINT32 AmlOffset = (UINT32)
535 ACPI_PTR_DIFF (ParserState->Aml,
536 ParserState->AmlStart);
537 ACPI_PARSE_OBJECT *Field;
542 ACPI_FUNCTION_TRACE (PsGetNextField);
545 /* Determine field type */
547 switch (ACPI_GET8 (ParserState->Aml))
551 Opcode = AML_INT_NAMEDFIELD_OP;
556 Opcode = AML_INT_RESERVEDFIELD_OP;
562 Opcode = AML_INT_ACCESSFIELD_OP;
567 /* Allocate a new field op */
569 Field = AcpiPsAllocOp (Opcode);
575 Field->Common.AmlOffset = AmlOffset;
577 /* Decode the field type */
581 case AML_INT_NAMEDFIELD_OP:
583 /* Get the 4-character name */
585 ACPI_MOVE_32_TO_32 (&Name, ParserState->Aml);
586 AcpiPsSetName (Field, Name);
587 ParserState->Aml += ACPI_NAME_SIZE;
589 /* Get the length which is encoded as a package length */
591 Field->Common.Value.Size = AcpiPsGetNextPackageLength (ParserState);
595 case AML_INT_RESERVEDFIELD_OP:
597 /* Get the length which is encoded as a package length */
599 Field->Common.Value.Size = AcpiPsGetNextPackageLength (ParserState);
603 case AML_INT_ACCESSFIELD_OP:
606 * Get AccessType and AccessAttrib and merge into the field Op
607 * AccessType is first operand, AccessAttribute is second
609 Field->Common.Value.Integer = (((UINT32) ACPI_GET8 (ParserState->Aml) << 8));
611 Field->Common.Value.Integer |= ACPI_GET8 (ParserState->Aml);
617 /* Opcode was set in previous switch */
625 /*******************************************************************************
627 * FUNCTION: AcpiPsGetNextArg
629 * PARAMETERS: WalkState - Current state
630 * ParserState - Current parser state object
631 * ArgType - The argument type (AML_*_ARG)
632 * ReturnArg - Where the next arg is returned
634 * RETURN: Status, and an op object containing the next argument.
636 * DESCRIPTION: Get next argument (including complex list arguments that require
637 * pushing the parser stack)
639 ******************************************************************************/
643 ACPI_WALK_STATE *WalkState,
644 ACPI_PARSE_STATE *ParserState,
646 ACPI_PARSE_OBJECT **ReturnArg)
648 ACPI_PARSE_OBJECT *Arg = NULL;
649 ACPI_PARSE_OBJECT *Prev = NULL;
650 ACPI_PARSE_OBJECT *Field;
652 ACPI_STATUS Status = AE_OK;
655 ACPI_FUNCTION_TRACE_PTR (PsGetNextArg, ParserState);
665 case ARGP_NAMESTRING:
667 /* Constants, strings, and namestrings are all the same size */
669 Arg = AcpiPsAllocOp (AML_BYTE_OP);
672 return_ACPI_STATUS (AE_NO_MEMORY);
674 AcpiPsGetNextSimpleArg (ParserState, ArgType, Arg);
680 /* Package length, nothing returned */
682 ParserState->PkgEnd = AcpiPsGetNextPackageEnd (ParserState);
688 if (ParserState->Aml < ParserState->PkgEnd)
692 while (ParserState->Aml < ParserState->PkgEnd)
694 Field = AcpiPsGetNextField (ParserState);
697 return_ACPI_STATUS (AE_NO_MEMORY);
702 Prev->Common.Next = Field;
711 /* Skip to End of byte data */
713 ParserState->Aml = ParserState->PkgEnd;
720 if (ParserState->Aml < ParserState->PkgEnd)
724 Arg = AcpiPsAllocOp (AML_INT_BYTELIST_OP);
727 return_ACPI_STATUS (AE_NO_MEMORY);
730 /* Fill in bytelist data */
732 Arg->Common.Value.Size = (UINT32)
733 ACPI_PTR_DIFF (ParserState->PkgEnd, ParserState->Aml);
734 Arg->Named.Data = ParserState->Aml;
736 /* Skip to End of byte data */
738 ParserState->Aml = ParserState->PkgEnd;
745 case ARGP_SIMPLENAME:
747 Subop = AcpiPsPeekOpcode (ParserState);
749 AcpiPsIsLeadingChar (Subop) ||
750 AcpiPsIsPrefixChar (Subop))
752 /* NullName or NameString */
754 Arg = AcpiPsAllocOp (AML_INT_NAMEPATH_OP);
757 return_ACPI_STATUS (AE_NO_MEMORY);
760 /* To support SuperName arg of Unload */
762 if (WalkState->Opcode == AML_UNLOAD_OP)
764 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Arg, 1);
767 * If the SuperName arg of Unload is a method call,
768 * we have restored the AML pointer, just free this Arg
770 if (Arg->Common.AmlOpcode == AML_INT_METHODCALL_OP)
778 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Arg, 0);
783 /* Single complex argument, nothing returned */
785 WalkState->ArgCount = 1;
793 /* Single complex argument, nothing returned */
795 WalkState->ArgCount = 1;
799 case ARGP_DATAOBJLIST:
803 if (ParserState->Aml < ParserState->PkgEnd)
805 /* Non-empty list of variable arguments, nothing returned */
807 WalkState->ArgCount = ACPI_VAR_ARGS;
814 ACPI_ERROR ((AE_INFO, "Invalid ArgType: 0x%X", ArgType));
815 Status = AE_AML_OPERAND_TYPE;
820 return_ACPI_STATUS (Status);