1 /******************************************************************************
3 * Module Name: psargs - Parse AML opcode arguments
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 <acpi/acpi.h>
51 #define _COMPONENT ACPI_PARSER
52 ACPI_MODULE_NAME("psargs")
54 /* Local prototypes */
56 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state);
58 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
61 /*******************************************************************************
63 * FUNCTION: acpi_ps_get_next_package_length
65 * PARAMETERS: parser_state - Current parser state object
67 * RETURN: Decoded package length. On completion, the AML pointer points
68 * past the length byte or bytes.
70 * DESCRIPTION: Decode and return a package length field.
71 * Note: Largest package length is 28 bits, from ACPI specification
73 ******************************************************************************/
76 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
78 u8 *aml = parser_state->aml;
79 u32 package_length = 0;
81 u8 byte_zero_mask = 0x3F; /* Default [0:5] */
83 ACPI_FUNCTION_TRACE(ps_get_next_package_length);
86 * Byte 0 bits [6:7] contain the number of additional bytes
87 * used to encode the package length, either 0,1,2, or 3
89 byte_count = (aml[0] >> 6);
90 parser_state->aml += ((acpi_size) byte_count + 1);
92 /* Get bytes 3, 2, 1 as needed */
96 * Final bit positions for the package length bytes:
102 package_length |= (aml[byte_count] << ((byte_count << 3) - 4));
104 byte_zero_mask = 0x0F; /* Use bits [0:3] of byte 0 */
108 /* Byte 0 is a special case, either bits [0:3] or [0:5] are used */
110 package_length |= (aml[0] & byte_zero_mask);
111 return_UINT32(package_length);
114 /*******************************************************************************
116 * FUNCTION: acpi_ps_get_next_package_end
118 * PARAMETERS: parser_state - Current parser state object
120 * RETURN: Pointer to end-of-package +1
122 * DESCRIPTION: Get next package length and return a pointer past the end of
123 * the package. Consumes the package length field
125 ******************************************************************************/
127 u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
129 u8 *start = parser_state->aml;
132 ACPI_FUNCTION_TRACE(ps_get_next_package_end);
134 /* Function below updates parser_state->Aml */
136 package_length = acpi_ps_get_next_package_length(parser_state);
138 return_PTR(start + package_length); /* end of package */
141 /*******************************************************************************
143 * FUNCTION: acpi_ps_get_next_namestring
145 * PARAMETERS: parser_state - Current parser state object
147 * RETURN: Pointer to the start of the name string (pointer points into
150 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
151 * prefix characters. Set parser state to point past the string.
152 * (Name is consumed from the AML.)
154 ******************************************************************************/
156 char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
158 u8 *start = parser_state->aml;
159 u8 *end = parser_state->aml;
161 ACPI_FUNCTION_TRACE(ps_get_next_namestring);
163 /* Point past any namestring prefix characters (backslash or carat) */
165 while (ACPI_IS_ROOT_PREFIX(*end) || ACPI_IS_PARENT_PREFIX(*end)) {
169 /* Decode the path prefix character */
182 case AML_DUAL_NAME_PREFIX:
184 /* Two name segments */
186 end += 1 + (2 * ACPI_NAME_SIZE);
189 case AML_MULTI_NAME_PREFIX_OP:
191 /* Multiple name segments, 4 chars each, count in next byte */
193 end += 2 + (*(end + 1) * ACPI_NAME_SIZE);
198 /* Single name segment */
200 end += ACPI_NAME_SIZE;
204 parser_state->aml = end;
205 return_PTR((char *)start);
208 /*******************************************************************************
210 * FUNCTION: acpi_ps_get_next_namepath
212 * PARAMETERS: parser_state - Current parser state object
213 * arg - Where the namepath will be stored
214 * arg_count - If the namepath points to a control method
215 * the method's argument is returned here.
216 * possible_method_call - Whether the namepath can possibly be the
217 * start of a method call
221 * DESCRIPTION: Get next name (if method call, return # of required args).
222 * Names are looked up in the internal namespace to determine
223 * if the name represents a control method. If a method
224 * is found, the number of arguments to the method is returned.
225 * This information is critical for parsing to continue correctly.
227 ******************************************************************************/
230 acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
231 struct acpi_parse_state *parser_state,
232 union acpi_parse_object *arg, u8 possible_method_call)
236 union acpi_parse_object *name_op;
237 union acpi_operand_object *method_desc;
238 struct acpi_namespace_node *node;
239 u8 *start = parser_state->aml;
241 ACPI_FUNCTION_TRACE(ps_get_next_namepath);
243 path = acpi_ps_get_next_namestring(parser_state);
244 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
246 /* Null path case is allowed, just exit */
249 arg->common.value.name = path;
250 return_ACPI_STATUS(AE_OK);
254 * Lookup the name in the internal namespace, starting with the current
255 * scope. We don't want to add anything new to the namespace here,
256 * however, so we use MODE_EXECUTE.
257 * Allow searching of the parent tree, but don't open a new scope -
258 * we just want to lookup the object (must be mode EXECUTE to perform
261 status = acpi_ns_lookup(walk_state->scope_info, path,
262 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
263 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
267 * If this name is a control method invocation, we must
268 * setup the method call
270 if (ACPI_SUCCESS(status) &&
271 possible_method_call && (node->type == ACPI_TYPE_METHOD)) {
272 if (GET_CURRENT_ARG_TYPE(walk_state->arg_types) ==
275 * acpi_ps_get_next_namestring has increased the AML pointer,
276 * so we need to restore the saved AML pointer for method call.
278 walk_state->parser_state.aml = start;
279 walk_state->arg_count = 1;
280 acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
281 return_ACPI_STATUS(AE_OK);
284 /* This name is actually a control method invocation */
286 method_desc = acpi_ns_get_attached_object(node);
287 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
288 "Control Method - %p Desc %p Path=%p\n", node,
291 name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP, start);
293 return_ACPI_STATUS(AE_NO_MEMORY);
296 /* Change Arg into a METHOD CALL and attach name to it */
298 acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
299 name_op->common.value.name = path;
301 /* Point METHODCALL/NAME to the METHOD Node */
303 name_op->common.node = node;
304 acpi_ps_append_arg(arg, name_op);
308 "Control Method %p has no attached object",
310 return_ACPI_STATUS(AE_AML_INTERNAL);
313 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
314 "Control Method - %p Args %X\n",
315 node, method_desc->method.param_count));
317 /* Get the number of arguments to expect */
319 walk_state->arg_count = method_desc->method.param_count;
320 return_ACPI_STATUS(AE_OK);
324 * Special handling if the name was not found during the lookup -
325 * some not_found cases are allowed
327 if (status == AE_NOT_FOUND) {
329 /* 1) not_found is ok during load pass 1/2 (allow forward references) */
331 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) !=
332 ACPI_PARSE_EXECUTE) {
336 /* 2) not_found during a cond_ref_of(x) is ok by definition */
338 else if (walk_state->op->common.aml_opcode ==
339 AML_COND_REF_OF_OP) {
344 * 3) not_found while building a Package is ok at this point, we
345 * may flag as an error later if slack mode is not enabled.
346 * (Some ASL code depends on allowing this behavior)
348 else if ((arg->common.parent) &&
349 ((arg->common.parent->common.aml_opcode ==
351 || (arg->common.parent->common.aml_opcode ==
352 AML_VAR_PACKAGE_OP))) {
357 /* Final exception check (may have been changed from code above) */
359 if (ACPI_FAILURE(status)) {
360 ACPI_ERROR_NAMESPACE(path, status);
362 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) ==
363 ACPI_PARSE_EXECUTE) {
365 /* Report a control method execution error */
367 status = acpi_ds_method_error(status, walk_state);
371 /* Save the namepath */
373 arg->common.value.name = path;
374 return_ACPI_STATUS(status);
377 /*******************************************************************************
379 * FUNCTION: acpi_ps_get_next_simple_arg
381 * PARAMETERS: parser_state - Current parser state object
382 * arg_type - The argument type (AML_*_ARG)
383 * arg - Where the argument is returned
387 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
389 ******************************************************************************/
392 acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
393 u32 arg_type, union acpi_parse_object *arg)
397 u8 *aml = parser_state->aml;
399 ACPI_FUNCTION_TRACE_U32(ps_get_next_simple_arg, arg_type);
404 /* Get 1 byte from the AML stream */
406 opcode = AML_BYTE_OP;
407 arg->common.value.integer = (u64) *aml;
413 /* Get 2 bytes from the AML stream */
415 opcode = AML_WORD_OP;
416 ACPI_MOVE_16_TO_64(&arg->common.value.integer, aml);
422 /* Get 4 bytes from the AML stream */
424 opcode = AML_DWORD_OP;
425 ACPI_MOVE_32_TO_64(&arg->common.value.integer, aml);
431 /* Get 8 bytes from the AML stream */
433 opcode = AML_QWORD_OP;
434 ACPI_MOVE_64_TO_64(&arg->common.value.integer, aml);
440 /* Get a pointer to the string, point past the string */
442 opcode = AML_STRING_OP;
443 arg->common.value.string = ACPI_CAST_PTR(char, aml);
445 /* Find the null terminator */
448 while (aml[length]) {
455 case ARGP_NAMESTRING:
457 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
458 arg->common.value.name =
459 acpi_ps_get_next_namestring(parser_state);
464 ACPI_ERROR((AE_INFO, "Invalid ArgType 0x%X", arg_type));
468 acpi_ps_init_op(arg, opcode);
469 parser_state->aml += length;
473 /*******************************************************************************
475 * FUNCTION: acpi_ps_get_next_field
477 * PARAMETERS: parser_state - Current parser state object
479 * RETURN: A newly allocated FIELD op
481 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
483 ******************************************************************************/
485 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
489 union acpi_parse_object *field;
490 union acpi_parse_object *arg = NULL;
500 ACPI_FUNCTION_TRACE(ps_get_next_field);
502 aml = parser_state->aml;
504 /* Determine field type */
506 switch (ACPI_GET8(parser_state->aml)) {
507 case AML_FIELD_OFFSET_OP:
509 opcode = AML_INT_RESERVEDFIELD_OP;
513 case AML_FIELD_ACCESS_OP:
515 opcode = AML_INT_ACCESSFIELD_OP;
519 case AML_FIELD_CONNECTION_OP:
521 opcode = AML_INT_CONNECTION_OP;
525 case AML_FIELD_EXT_ACCESS_OP:
527 opcode = AML_INT_EXTACCESSFIELD_OP;
533 opcode = AML_INT_NAMEDFIELD_OP;
537 /* Allocate a new field op */
539 field = acpi_ps_alloc_op(opcode, aml);
544 /* Decode the field type */
547 case AML_INT_NAMEDFIELD_OP:
549 /* Get the 4-character name */
551 ACPI_MOVE_32_TO_32(&name, parser_state->aml);
552 acpi_ps_set_name(field, name);
553 parser_state->aml += ACPI_NAME_SIZE;
555 /* Get the length which is encoded as a package length */
557 field->common.value.size =
558 acpi_ps_get_next_package_length(parser_state);
561 case AML_INT_RESERVEDFIELD_OP:
563 /* Get the length which is encoded as a package length */
565 field->common.value.size =
566 acpi_ps_get_next_package_length(parser_state);
569 case AML_INT_ACCESSFIELD_OP:
570 case AML_INT_EXTACCESSFIELD_OP:
573 * Get access_type and access_attrib and merge into the field Op
574 * access_type is first operand, access_attribute is second. stuff
575 * these bytes into the node integer value for convenience.
578 /* Get the two bytes (Type/Attribute) */
580 access_type = ACPI_GET8(parser_state->aml);
582 access_attribute = ACPI_GET8(parser_state->aml);
585 field->common.value.integer = (u8)access_type;
586 field->common.value.integer |= (u16)(access_attribute << 8);
588 /* This opcode has a third byte, access_length */
590 if (opcode == AML_INT_EXTACCESSFIELD_OP) {
591 access_length = ACPI_GET8(parser_state->aml);
594 field->common.value.integer |=
595 (u32)(access_length << 16);
599 case AML_INT_CONNECTION_OP:
602 * Argument for Connection operator can be either a Buffer
603 * (resource descriptor), or a name_string.
605 aml = parser_state->aml;
606 if (ACPI_GET8(parser_state->aml) == AML_BUFFER_OP) {
609 pkg_end = parser_state->aml;
611 acpi_ps_get_next_package_length(parser_state);
612 pkg_end += pkg_length;
614 if (parser_state->aml < pkg_end) {
619 acpi_ps_alloc_op(AML_INT_BYTELIST_OP, aml);
621 acpi_ps_free_op(field);
625 /* Get the actual buffer length argument */
627 opcode = ACPI_GET8(parser_state->aml);
631 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
634 ACPI_GET8(parser_state->aml);
635 parser_state->aml += 1;
638 case AML_WORD_OP: /* AML_WORDDATA_ARG */
641 ACPI_GET16(parser_state->aml);
642 parser_state->aml += 2;
645 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
648 ACPI_GET32(parser_state->aml);
649 parser_state->aml += 4;
658 /* Fill in bytelist data */
660 arg->named.value.size = buffer_length;
661 arg->named.data = parser_state->aml;
664 /* Skip to End of byte data */
666 parser_state->aml = pkg_end;
668 arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP, aml);
670 acpi_ps_free_op(field);
674 /* Get the Namestring argument */
676 arg->common.value.name =
677 acpi_ps_get_next_namestring(parser_state);
680 /* Link the buffer/namestring to parent (CONNECTION_OP) */
682 acpi_ps_append_arg(field, arg);
687 /* Opcode was set in previous switch */
694 /*******************************************************************************
696 * FUNCTION: acpi_ps_get_next_arg
698 * PARAMETERS: walk_state - Current state
699 * parser_state - Current parser state object
700 * arg_type - The parser argument type (ARGP_*)
701 * return_arg - Where the next arg is returned
703 * RETURN: Status, and an op object containing the next argument.
705 * DESCRIPTION: Get next argument (including complex list arguments that require
706 * pushing the parser stack)
708 ******************************************************************************/
711 acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
712 struct acpi_parse_state *parser_state,
713 u32 arg_type, union acpi_parse_object **return_arg)
715 union acpi_parse_object *arg = NULL;
716 union acpi_parse_object *prev = NULL;
717 union acpi_parse_object *field;
719 acpi_status status = AE_OK;
721 ACPI_FUNCTION_TRACE_PTR(ps_get_next_arg, parser_state);
729 case ARGP_NAMESTRING:
731 /* Constants, strings, and namestrings are all the same size */
733 arg = acpi_ps_alloc_op(AML_BYTE_OP, parser_state->aml);
735 return_ACPI_STATUS(AE_NO_MEMORY);
738 acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
743 /* Package length, nothing returned */
745 parser_state->pkg_end =
746 acpi_ps_get_next_package_end(parser_state);
751 if (parser_state->aml < parser_state->pkg_end) {
755 while (parser_state->aml < parser_state->pkg_end) {
756 field = acpi_ps_get_next_field(parser_state);
758 return_ACPI_STATUS(AE_NO_MEMORY);
762 prev->common.next = field;
769 /* Skip to End of byte data */
771 parser_state->aml = parser_state->pkg_end;
777 if (parser_state->aml < parser_state->pkg_end) {
781 arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP,
784 return_ACPI_STATUS(AE_NO_MEMORY);
787 /* Fill in bytelist data */
789 arg->common.value.size = (u32)
790 ACPI_PTR_DIFF(parser_state->pkg_end,
792 arg->named.data = parser_state->aml;
794 /* Skip to End of byte data */
796 parser_state->aml = parser_state->pkg_end;
802 case ARGP_SIMPLENAME:
804 subop = acpi_ps_peek_opcode(parser_state);
806 acpi_ps_is_leading_char(subop) ||
807 ACPI_IS_ROOT_PREFIX(subop) ||
808 ACPI_IS_PARENT_PREFIX(subop)) {
810 /* null_name or name_string */
813 acpi_ps_alloc_op(AML_INT_NAMEPATH_OP,
816 return_ACPI_STATUS(AE_NO_MEMORY);
819 /* super_name allows argument to be a method call */
821 if (arg_type == ARGP_SUPERNAME) {
823 acpi_ps_get_next_namepath(walk_state,
825 ACPI_POSSIBLE_METHOD_CALL);
828 * If the super_name arg of Unload is a method call,
829 * we have restored the AML pointer, just free this Arg
831 if (arg->common.aml_opcode ==
832 AML_INT_METHODCALL_OP) {
833 acpi_ps_free_op(arg);
838 acpi_ps_get_next_namepath(walk_state,
840 ACPI_NOT_METHOD_CALL);
843 /* Single complex argument, nothing returned */
845 walk_state->arg_count = 1;
852 /* Single complex argument, nothing returned */
854 walk_state->arg_count = 1;
857 case ARGP_DATAOBJLIST:
861 if (parser_state->aml < parser_state->pkg_end) {
863 /* Non-empty list of variable arguments, nothing returned */
865 walk_state->arg_count = ACPI_VAR_ARGS;
871 ACPI_ERROR((AE_INFO, "Invalid ArgType: 0x%X", arg_type));
872 status = AE_AML_OPERAND_TYPE;
877 return_ACPI_STATUS(status);