1 /******************************************************************************
3 * Module Name: psargs - Parse AML opcode arguments
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
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 (ACPI_IS_ROOT_PREFIX (*End) ||
186 ACPI_IS_PARENT_PREFIX (*End))
191 /* Decode the path prefix character */
206 case AML_DUAL_NAME_PREFIX:
208 /* Two name segments */
210 End += 1 + (2 * ACPI_NAME_SIZE);
213 case AML_MULTI_NAME_PREFIX_OP:
215 /* Multiple name segments, 4 chars each, count in next byte */
217 End += 2 + (*(End + 1) * ACPI_NAME_SIZE);
222 /* Single name segment */
224 End += ACPI_NAME_SIZE;
228 ParserState->Aml = End;
229 return_PTR ((char *) Start);
233 /*******************************************************************************
235 * FUNCTION: AcpiPsGetNextNamepath
237 * PARAMETERS: ParserState - Current parser state object
238 * Arg - Where the namepath will be stored
239 * ArgCount - If the namepath points to a control method
240 * the method's argument is returned here.
241 * PossibleMethodCall - Whether the namepath can possibly be the
242 * start of a method call
246 * DESCRIPTION: Get next name (if method call, return # of required args).
247 * Names are looked up in the internal namespace to determine
248 * if the name represents a control method. If a method
249 * is found, the number of arguments to the method is returned.
250 * This information is critical for parsing to continue correctly.
252 ******************************************************************************/
255 AcpiPsGetNextNamepath (
256 ACPI_WALK_STATE *WalkState,
257 ACPI_PARSE_STATE *ParserState,
258 ACPI_PARSE_OBJECT *Arg,
259 BOOLEAN PossibleMethodCall)
263 ACPI_PARSE_OBJECT *NameOp;
264 ACPI_OPERAND_OBJECT *MethodDesc;
265 ACPI_NAMESPACE_NODE *Node;
266 UINT8 *Start = ParserState->Aml;
269 ACPI_FUNCTION_TRACE (PsGetNextNamepath);
272 Path = AcpiPsGetNextNamestring (ParserState);
273 AcpiPsInitOp (Arg, AML_INT_NAMEPATH_OP);
275 /* Null path case is allowed, just exit */
279 Arg->Common.Value.Name = Path;
280 return_ACPI_STATUS (AE_OK);
284 * Lookup the name in the internal namespace, starting with the current
285 * scope. We don't want to add anything new to the namespace here,
286 * however, so we use MODE_EXECUTE.
287 * Allow searching of the parent tree, but don't open a new scope -
288 * we just want to lookup the object (must be mode EXECUTE to perform
291 Status = AcpiNsLookup (WalkState->ScopeInfo, Path,
292 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
293 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE, NULL, &Node);
296 * If this name is a control method invocation, we must
297 * setup the method call
299 if (ACPI_SUCCESS (Status) &&
300 PossibleMethodCall &&
301 (Node->Type == ACPI_TYPE_METHOD))
303 if (WalkState->Opcode == AML_UNLOAD_OP)
306 * AcpiPsGetNextNamestring has increased the AML pointer,
307 * so we need to restore the saved AML pointer for method call.
309 WalkState->ParserState.Aml = Start;
310 WalkState->ArgCount = 1;
311 AcpiPsInitOp (Arg, AML_INT_METHODCALL_OP);
312 return_ACPI_STATUS (AE_OK);
315 /* This name is actually a control method invocation */
317 MethodDesc = AcpiNsGetAttachedObject (Node);
318 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
319 "Control Method - %p Desc %p Path=%p\n", Node, MethodDesc, Path));
321 NameOp = AcpiPsAllocOp (AML_INT_NAMEPATH_OP);
324 return_ACPI_STATUS (AE_NO_MEMORY);
327 /* Change Arg into a METHOD CALL and attach name to it */
329 AcpiPsInitOp (Arg, AML_INT_METHODCALL_OP);
330 NameOp->Common.Value.Name = Path;
332 /* Point METHODCALL/NAME to the METHOD Node */
334 NameOp->Common.Node = Node;
335 AcpiPsAppendArg (Arg, NameOp);
339 ACPI_ERROR ((AE_INFO,
340 "Control Method %p has no attached object",
342 return_ACPI_STATUS (AE_AML_INTERNAL);
345 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
346 "Control Method - %p Args %X\n",
347 Node, MethodDesc->Method.ParamCount));
349 /* Get the number of arguments to expect */
351 WalkState->ArgCount = MethodDesc->Method.ParamCount;
352 return_ACPI_STATUS (AE_OK);
356 * Special handling if the name was not found during the lookup -
357 * some NotFound cases are allowed
359 if (Status == AE_NOT_FOUND)
361 /* 1) NotFound is ok during load pass 1/2 (allow forward references) */
363 if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) !=
369 /* 2) NotFound during a CondRefOf(x) is ok by definition */
371 else if (WalkState->Op->Common.AmlOpcode == AML_COND_REF_OF_OP)
377 * 3) NotFound while building a Package is ok at this point, we
378 * may flag as an error later if slack mode is not enabled.
379 * (Some ASL code depends on allowing this behavior)
381 else if ((Arg->Common.Parent) &&
382 ((Arg->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
383 (Arg->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP)))
389 /* Final exception check (may have been changed from code above) */
391 if (ACPI_FAILURE (Status))
393 ACPI_ERROR_NAMESPACE (Path, Status);
395 if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) ==
398 /* Report a control method execution error */
400 Status = AcpiDsMethodError (Status, WalkState);
404 /* Save the namepath */
406 Arg->Common.Value.Name = Path;
407 return_ACPI_STATUS (Status);
411 /*******************************************************************************
413 * FUNCTION: AcpiPsGetNextSimpleArg
415 * PARAMETERS: ParserState - Current parser state object
416 * ArgType - The argument type (AML_*_ARG)
417 * Arg - Where the argument is returned
421 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
423 ******************************************************************************/
426 AcpiPsGetNextSimpleArg (
427 ACPI_PARSE_STATE *ParserState,
429 ACPI_PARSE_OBJECT *Arg)
433 UINT8 *Aml = ParserState->Aml;
436 ACPI_FUNCTION_TRACE_U32 (PsGetNextSimpleArg, ArgType);
443 /* Get 1 byte from the AML stream */
445 Opcode = AML_BYTE_OP;
446 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);
461 /* Get 4 bytes from the AML stream */
463 Opcode = AML_DWORD_OP;
464 ACPI_MOVE_32_TO_64 (&Arg->Common.Value.Integer, Aml);
470 /* Get 8 bytes from the AML stream */
472 Opcode = AML_QWORD_OP;
473 ACPI_MOVE_64_TO_64 (&Arg->Common.Value.Integer, Aml);
479 /* Get a pointer to the string, point past the string */
481 Opcode = AML_STRING_OP;
482 Arg->Common.Value.String = ACPI_CAST_PTR (char, Aml);
484 /* Find the null terminator */
495 case ARGP_NAMESTRING:
497 AcpiPsInitOp (Arg, AML_INT_NAMEPATH_OP);
498 Arg->Common.Value.Name = AcpiPsGetNextNamestring (ParserState);
503 ACPI_ERROR ((AE_INFO, "Invalid ArgType 0x%X", ArgType));
507 AcpiPsInitOp (Arg, Opcode);
508 ParserState->Aml += Length;
513 /*******************************************************************************
515 * FUNCTION: AcpiPsGetNextField
517 * PARAMETERS: ParserState - Current parser state object
519 * RETURN: A newly allocated FIELD op
521 * DESCRIPTION: Get next field (NamedField, ReservedField, or AccessField)
523 ******************************************************************************/
525 static ACPI_PARSE_OBJECT *
527 ACPI_PARSE_STATE *ParserState)
530 ACPI_PARSE_OBJECT *Field;
531 ACPI_PARSE_OBJECT *Arg = NULL;
535 UINT8 AccessAttribute;
542 ACPI_FUNCTION_TRACE (PsGetNextField);
545 AmlOffset = (UINT32) ACPI_PTR_DIFF (
546 ParserState->Aml, ParserState->AmlStart);
548 /* Determine field type */
550 switch (ACPI_GET8 (ParserState->Aml))
552 case AML_FIELD_OFFSET_OP:
554 Opcode = AML_INT_RESERVEDFIELD_OP;
558 case AML_FIELD_ACCESS_OP:
560 Opcode = AML_INT_ACCESSFIELD_OP;
564 case AML_FIELD_CONNECTION_OP:
566 Opcode = AML_INT_CONNECTION_OP;
570 case AML_FIELD_EXT_ACCESS_OP:
572 Opcode = AML_INT_EXTACCESSFIELD_OP;
578 Opcode = AML_INT_NAMEDFIELD_OP;
582 /* Allocate a new field op */
584 Field = AcpiPsAllocOp (Opcode);
590 Field->Common.AmlOffset = AmlOffset;
592 /* Decode the field type */
596 case AML_INT_NAMEDFIELD_OP:
598 /* Get the 4-character name */
600 ACPI_MOVE_32_TO_32 (&Name, ParserState->Aml);
601 AcpiPsSetName (Field, Name);
602 ParserState->Aml += ACPI_NAME_SIZE;
604 /* Get the length which is encoded as a package length */
606 Field->Common.Value.Size = AcpiPsGetNextPackageLength (ParserState);
610 case AML_INT_RESERVEDFIELD_OP:
612 /* Get the length which is encoded as a package length */
614 Field->Common.Value.Size = AcpiPsGetNextPackageLength (ParserState);
618 case AML_INT_ACCESSFIELD_OP:
619 case AML_INT_EXTACCESSFIELD_OP:
622 * Get AccessType and AccessAttrib and merge into the field Op
623 * AccessType is first operand, AccessAttribute is second. stuff
624 * these bytes into the node integer value for convenience.
627 /* Get the two bytes (Type/Attribute) */
629 AccessType = ACPI_GET8 (ParserState->Aml);
631 AccessAttribute = ACPI_GET8 (ParserState->Aml);
634 Field->Common.Value.Integer = (UINT8) AccessType;
635 Field->Common.Value.Integer |= (UINT16) (AccessAttribute << 8);
637 /* This opcode has a third byte, AccessLength */
639 if (Opcode == AML_INT_EXTACCESSFIELD_OP)
641 AccessLength = ACPI_GET8 (ParserState->Aml);
644 Field->Common.Value.Integer |= (UINT32) (AccessLength << 16);
649 case AML_INT_CONNECTION_OP:
652 * Argument for Connection operator can be either a Buffer
653 * (resource descriptor), or a NameString.
655 if (ACPI_GET8 (ParserState->Aml) == AML_BUFFER_OP)
659 PkgEnd = ParserState->Aml;
660 PkgLength = AcpiPsGetNextPackageLength (ParserState);
663 if (ParserState->Aml < PkgEnd)
667 Arg = AcpiPsAllocOp (AML_INT_BYTELIST_OP);
670 AcpiPsFreeOp (Field);
674 /* Get the actual buffer length argument */
676 Opcode = ACPI_GET8 (ParserState->Aml);
681 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
683 BufferLength = ACPI_GET8 (ParserState->Aml);
684 ParserState->Aml += 1;
687 case AML_WORD_OP: /* AML_WORDDATA_ARG */
689 BufferLength = ACPI_GET16 (ParserState->Aml);
690 ParserState->Aml += 2;
693 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
695 BufferLength = ACPI_GET32 (ParserState->Aml);
696 ParserState->Aml += 4;
705 /* Fill in bytelist data */
707 Arg->Named.Value.Size = BufferLength;
708 Arg->Named.Data = ParserState->Aml;
711 /* Skip to End of byte data */
713 ParserState->Aml = PkgEnd;
717 Arg = AcpiPsAllocOp (AML_INT_NAMEPATH_OP);
720 AcpiPsFreeOp (Field);
724 /* Get the Namestring argument */
726 Arg->Common.Value.Name = AcpiPsGetNextNamestring (ParserState);
729 /* Link the buffer/namestring to parent (CONNECTION_OP) */
731 AcpiPsAppendArg (Field, Arg);
737 /* Opcode was set in previous switch */
745 /*******************************************************************************
747 * FUNCTION: AcpiPsGetNextArg
749 * PARAMETERS: WalkState - Current state
750 * ParserState - Current parser state object
751 * ArgType - The argument type (AML_*_ARG)
752 * ReturnArg - Where the next arg is returned
754 * RETURN: Status, and an op object containing the next argument.
756 * DESCRIPTION: Get next argument (including complex list arguments that require
757 * pushing the parser stack)
759 ******************************************************************************/
763 ACPI_WALK_STATE *WalkState,
764 ACPI_PARSE_STATE *ParserState,
766 ACPI_PARSE_OBJECT **ReturnArg)
768 ACPI_PARSE_OBJECT *Arg = NULL;
769 ACPI_PARSE_OBJECT *Prev = NULL;
770 ACPI_PARSE_OBJECT *Field;
772 ACPI_STATUS Status = AE_OK;
775 ACPI_FUNCTION_TRACE_PTR (PsGetNextArg, ParserState);
785 case ARGP_NAMESTRING:
787 /* Constants, strings, and namestrings are all the same size */
789 Arg = AcpiPsAllocOp (AML_BYTE_OP);
792 return_ACPI_STATUS (AE_NO_MEMORY);
794 AcpiPsGetNextSimpleArg (ParserState, ArgType, Arg);
799 /* Package length, nothing returned */
801 ParserState->PkgEnd = AcpiPsGetNextPackageEnd (ParserState);
806 if (ParserState->Aml < ParserState->PkgEnd)
810 while (ParserState->Aml < ParserState->PkgEnd)
812 Field = AcpiPsGetNextField (ParserState);
815 return_ACPI_STATUS (AE_NO_MEMORY);
820 Prev->Common.Next = Field;
829 /* Skip to End of byte data */
831 ParserState->Aml = ParserState->PkgEnd;
837 if (ParserState->Aml < ParserState->PkgEnd)
841 Arg = AcpiPsAllocOp (AML_INT_BYTELIST_OP);
844 return_ACPI_STATUS (AE_NO_MEMORY);
847 /* Fill in bytelist data */
849 Arg->Common.Value.Size = (UINT32)
850 ACPI_PTR_DIFF (ParserState->PkgEnd, ParserState->Aml);
851 Arg->Named.Data = ParserState->Aml;
853 /* Skip to End of byte data */
855 ParserState->Aml = ParserState->PkgEnd;
861 case ARGP_SIMPLENAME:
863 Subop = AcpiPsPeekOpcode (ParserState);
865 AcpiPsIsLeadingChar (Subop) ||
866 ACPI_IS_ROOT_PREFIX (Subop) ||
867 ACPI_IS_PARENT_PREFIX (Subop))
869 /* NullName or NameString */
871 Arg = AcpiPsAllocOp (AML_INT_NAMEPATH_OP);
874 return_ACPI_STATUS (AE_NO_MEMORY);
877 /* To support SuperName arg of Unload */
879 if (WalkState->Opcode == AML_UNLOAD_OP)
881 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Arg, 1);
884 * If the SuperName arg of Unload is a method call,
885 * we have restored the AML pointer, just free this Arg
887 if (Arg->Common.AmlOpcode == AML_INT_METHODCALL_OP)
895 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Arg, 0);
900 /* Single complex argument, nothing returned */
902 WalkState->ArgCount = 1;
909 /* Single complex argument, nothing returned */
911 WalkState->ArgCount = 1;
914 case ARGP_DATAOBJLIST:
918 if (ParserState->Aml < ParserState->PkgEnd)
920 /* Non-empty list of variable arguments, nothing returned */
922 WalkState->ArgCount = ACPI_VAR_ARGS;
928 ACPI_ERROR ((AE_INFO, "Invalid ArgType: 0x%X", ArgType));
929 Status = AE_AML_OPERAND_TYPE;
934 return_ACPI_STATUS (Status);