1 /******************************************************************************
3 * Module Name: psargs - Parse AML opcode arguments
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
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>
45 #include <acpi/acparser.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_PARSER
50 ACPI_MODULE_NAME("psargs")
52 /* Local prototypes */
54 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state);
56 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
59 /*******************************************************************************
61 * FUNCTION: acpi_ps_get_next_package_length
63 * PARAMETERS: parser_state - Current parser state object
65 * RETURN: Decoded package length. On completion, the AML pointer points
66 * past the length byte or bytes.
68 * DESCRIPTION: Decode and return a package length field.
69 * Note: Largest package length is 28 bits, from ACPI specification
71 ******************************************************************************/
74 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
76 u8 *aml = parser_state->aml;
77 u32 package_length = 0;
78 acpi_native_uint byte_count;
79 u8 byte_zero_mask = 0x3F; /* Default [0:5] */
81 ACPI_FUNCTION_TRACE("ps_get_next_package_length");
84 * Byte 0 bits [6:7] contain the number of additional bytes
85 * used to encode the package length, either 0,1,2, or 3
87 byte_count = (aml[0] >> 6);
88 parser_state->aml += (byte_count + 1);
90 /* Get bytes 3, 2, 1 as needed */
94 * Final bit positions for the package length bytes:
100 package_length |= (aml[byte_count] << ((byte_count << 3) - 4));
102 byte_zero_mask = 0x0F; /* Use bits [0:3] of byte 0 */
106 /* Byte 0 is a special case, either bits [0:3] or [0:5] are used */
108 package_length |= (aml[0] & byte_zero_mask);
109 return_UINT32(package_length);
112 /*******************************************************************************
114 * FUNCTION: acpi_ps_get_next_package_end
116 * PARAMETERS: parser_state - Current parser state object
118 * RETURN: Pointer to end-of-package +1
120 * DESCRIPTION: Get next package length and return a pointer past the end of
121 * the package. Consumes the package length field
123 ******************************************************************************/
125 u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
127 u8 *start = parser_state->aml;
130 ACPI_FUNCTION_TRACE("ps_get_next_package_end");
132 /* Function below updates parser_state->Aml */
134 package_length = acpi_ps_get_next_package_length(parser_state);
136 return_PTR(start + package_length); /* end of package */
139 /*******************************************************************************
141 * FUNCTION: acpi_ps_get_next_namestring
143 * PARAMETERS: parser_state - Current parser state object
145 * RETURN: Pointer to the start of the name string (pointer points into
148 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
149 * prefix characters. Set parser state to point past the string.
150 * (Name is consumed from the AML.)
152 ******************************************************************************/
154 char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
156 u8 *start = parser_state->aml;
157 u8 *end = parser_state->aml;
159 ACPI_FUNCTION_TRACE("ps_get_next_namestring");
161 /* Point past any namestring prefix characters (backslash or carat) */
163 while (acpi_ps_is_prefix_char(*end)) {
167 /* Decode the path prefix character */
180 case AML_DUAL_NAME_PREFIX:
182 /* Two name segments */
184 end += 1 + (2 * ACPI_NAME_SIZE);
187 case AML_MULTI_NAME_PREFIX_OP:
189 /* Multiple name segments, 4 chars each, count in next byte */
191 end += 2 + (*(end + 1) * ACPI_NAME_SIZE);
196 /* Single name segment */
198 end += ACPI_NAME_SIZE;
202 parser_state->aml = end;
203 return_PTR((char *)start);
206 /*******************************************************************************
208 * FUNCTION: acpi_ps_get_next_namepath
210 * PARAMETERS: parser_state - Current parser state object
211 * Arg - Where the namepath will be stored
212 * arg_count - If the namepath points to a control method
213 * the method's argument is returned here.
214 * method_call - Whether the namepath can possibly be the
215 * start of a method call
219 * DESCRIPTION: Get next name (if method call, return # of required args).
220 * Names are looked up in the internal namespace to determine
221 * if the name represents a control method. If a method
222 * is found, the number of arguments to the method is returned.
223 * This information is critical for parsing to continue correctly.
225 ******************************************************************************/
228 acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
229 struct acpi_parse_state *parser_state,
230 union acpi_parse_object *arg, u8 method_call)
233 union acpi_parse_object *name_op;
234 acpi_status status = AE_OK;
235 union acpi_operand_object *method_desc;
236 struct acpi_namespace_node *node;
237 union acpi_generic_state scope_info;
239 ACPI_FUNCTION_TRACE("ps_get_next_namepath");
241 path = acpi_ps_get_next_namestring(parser_state);
243 /* Null path case is allowed */
247 * Lookup the name in the internal namespace
249 scope_info.scope.node = NULL;
250 node = parser_state->start_node;
252 scope_info.scope.node = node;
256 * Lookup object. We don't want to add anything new to the namespace
257 * here, however. So we use MODE_EXECUTE. Allow searching of the
258 * parent tree, but don't open a new scope -- we just want to lookup the
259 * object (MUST BE mode EXECUTE to perform upsearch)
261 status = acpi_ns_lookup(&scope_info, path, ACPI_TYPE_ANY,
263 ACPI_NS_SEARCH_PARENT |
264 ACPI_NS_DONT_OPEN_SCOPE, NULL, &node);
265 if (ACPI_SUCCESS(status) && method_call) {
266 if (node->type == ACPI_TYPE_METHOD) {
267 /* This name is actually a control method invocation */
269 method_desc = acpi_ns_get_attached_object(node);
270 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
271 "Control Method - %p Desc %p Path=%p\n",
272 node, method_desc, path));
274 name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
276 return_ACPI_STATUS(AE_NO_MEMORY);
279 /* Change arg into a METHOD CALL and attach name to it */
281 acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
282 name_op->common.value.name = path;
284 /* Point METHODCALL/NAME to the METHOD Node */
286 name_op->common.node = node;
287 acpi_ps_append_arg(arg, name_op);
290 ACPI_REPORT_ERROR(("ps_get_next_namepath: Control Method %p has no attached object\n", node));
291 return_ACPI_STATUS(AE_AML_INTERNAL);
294 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
295 "Control Method - %p Args %X\n",
300 /* Get the number of arguments to expect */
302 walk_state->arg_count =
303 method_desc->method.param_count;
304 return_ACPI_STATUS(AE_OK);
308 * Else this is normal named object reference.
309 * Just init the NAMEPATH object with the pathname.
314 if (ACPI_FAILURE(status)) {
316 * 1) Any error other than NOT_FOUND is always severe
317 * 2) NOT_FOUND is only important if we are executing a method.
318 * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
321 parse_flags & ACPI_PARSE_MODE_MASK) ==
322 ACPI_PARSE_EXECUTE) && (status == AE_NOT_FOUND)
323 && (walk_state->op->common.aml_opcode !=
325 || (status != AE_NOT_FOUND)) {
326 ACPI_REPORT_NSERROR(path, status);
329 ("search_node %p start_node %p return_node %p\n",
330 scope_info.scope.node,
331 parser_state->start_node, node);
334 * We got a NOT_FOUND during table load or we encountered
335 * a cond_ref_of(x) where the target does not exist.
344 * Regardless of success/failure above,
345 * Just initialize the Op with the pathname.
347 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
348 arg->common.value.name = path;
350 return_ACPI_STATUS(status);
353 /*******************************************************************************
355 * FUNCTION: acpi_ps_get_next_simple_arg
357 * PARAMETERS: parser_state - Current parser state object
358 * arg_type - The argument type (AML_*_ARG)
359 * Arg - Where the argument is returned
363 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
365 ******************************************************************************/
368 acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
369 u32 arg_type, union acpi_parse_object *arg)
373 u8 *aml = parser_state->aml;
375 ACPI_FUNCTION_TRACE_U32("ps_get_next_simple_arg", arg_type);
380 /* Get 1 byte from the AML stream */
382 opcode = AML_BYTE_OP;
383 arg->common.value.integer = (acpi_integer) * aml;
389 /* Get 2 bytes from the AML stream */
391 opcode = AML_WORD_OP;
392 ACPI_MOVE_16_TO_64(&arg->common.value.integer, aml);
398 /* Get 4 bytes from the AML stream */
400 opcode = AML_DWORD_OP;
401 ACPI_MOVE_32_TO_64(&arg->common.value.integer, aml);
407 /* Get 8 bytes from the AML stream */
409 opcode = AML_QWORD_OP;
410 ACPI_MOVE_64_TO_64(&arg->common.value.integer, aml);
416 /* Get a pointer to the string, point past the string */
418 opcode = AML_STRING_OP;
419 arg->common.value.string = ACPI_CAST_PTR(char, aml);
421 /* Find the null terminator */
424 while (aml[length]) {
431 case ARGP_NAMESTRING:
433 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
434 arg->common.value.name =
435 acpi_ps_get_next_namestring(parser_state);
440 ACPI_REPORT_ERROR(("Invalid arg_type %X\n", arg_type));
444 acpi_ps_init_op(arg, opcode);
445 parser_state->aml += length;
449 /*******************************************************************************
451 * FUNCTION: acpi_ps_get_next_field
453 * PARAMETERS: parser_state - Current parser state object
455 * RETURN: A newly allocated FIELD op
457 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
459 ******************************************************************************/
461 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
464 u32 aml_offset = (u32)
465 ACPI_PTR_DIFF(parser_state->aml,
466 parser_state->aml_start);
467 union acpi_parse_object *field;
471 ACPI_FUNCTION_TRACE("ps_get_next_field");
473 /* Determine field type */
475 switch (ACPI_GET8(parser_state->aml)) {
478 opcode = AML_INT_NAMEDFIELD_OP;
483 opcode = AML_INT_RESERVEDFIELD_OP;
489 opcode = AML_INT_ACCESSFIELD_OP;
494 /* Allocate a new field op */
496 field = acpi_ps_alloc_op(opcode);
501 field->common.aml_offset = aml_offset;
503 /* Decode the field type */
506 case AML_INT_NAMEDFIELD_OP:
508 /* Get the 4-character name */
510 ACPI_MOVE_32_TO_32(&name, parser_state->aml);
511 acpi_ps_set_name(field, name);
512 parser_state->aml += ACPI_NAME_SIZE;
514 /* Get the length which is encoded as a package length */
516 field->common.value.size =
517 acpi_ps_get_next_package_length(parser_state);
520 case AML_INT_RESERVEDFIELD_OP:
522 /* Get the length which is encoded as a package length */
524 field->common.value.size =
525 acpi_ps_get_next_package_length(parser_state);
528 case AML_INT_ACCESSFIELD_OP:
531 * Get access_type and access_attrib and merge into the field Op
532 * access_type is first operand, access_attribute is second
534 field->common.value.integer =
535 (((u32) ACPI_GET8(parser_state->aml) << 8));
537 field->common.value.integer |= ACPI_GET8(parser_state->aml);
543 /* Opcode was set in previous switch */
550 /*******************************************************************************
552 * FUNCTION: acpi_ps_get_next_arg
554 * PARAMETERS: walk_state - Current state
555 * parser_state - Current parser state object
556 * arg_type - The argument type (AML_*_ARG)
557 * return_arg - Where the next arg is returned
559 * RETURN: Status, and an op object containing the next argument.
561 * DESCRIPTION: Get next argument (including complex list arguments that require
562 * pushing the parser stack)
564 ******************************************************************************/
567 acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
568 struct acpi_parse_state *parser_state,
569 u32 arg_type, union acpi_parse_object **return_arg)
571 union acpi_parse_object *arg = NULL;
572 union acpi_parse_object *prev = NULL;
573 union acpi_parse_object *field;
575 acpi_status status = AE_OK;
577 ACPI_FUNCTION_TRACE_PTR("ps_get_next_arg", parser_state);
585 case ARGP_NAMESTRING:
587 /* Constants, strings, and namestrings are all the same size */
589 arg = acpi_ps_alloc_op(AML_BYTE_OP);
591 return_ACPI_STATUS(AE_NO_MEMORY);
593 acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
598 /* Package length, nothing returned */
600 parser_state->pkg_end =
601 acpi_ps_get_next_package_end(parser_state);
606 if (parser_state->aml < parser_state->pkg_end) {
609 while (parser_state->aml < parser_state->pkg_end) {
610 field = acpi_ps_get_next_field(parser_state);
612 return_ACPI_STATUS(AE_NO_MEMORY);
616 prev->common.next = field;
623 /* Skip to End of byte data */
625 parser_state->aml = parser_state->pkg_end;
631 if (parser_state->aml < parser_state->pkg_end) {
634 arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP);
636 return_ACPI_STATUS(AE_NO_MEMORY);
639 /* Fill in bytelist data */
641 arg->common.value.size = (u32)
642 ACPI_PTR_DIFF(parser_state->pkg_end,
644 arg->named.data = parser_state->aml;
646 /* Skip to End of byte data */
648 parser_state->aml = parser_state->pkg_end;
654 case ARGP_SIMPLENAME:
656 subop = acpi_ps_peek_opcode(parser_state);
658 acpi_ps_is_leading_char(subop) ||
659 acpi_ps_is_prefix_char(subop)) {
660 /* null_name or name_string */
662 arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
664 return_ACPI_STATUS(AE_NO_MEMORY);
668 acpi_ps_get_next_namepath(walk_state, parser_state,
671 /* Single complex argument, nothing returned */
673 walk_state->arg_count = 1;
680 /* Single complex argument, nothing returned */
682 walk_state->arg_count = 1;
685 case ARGP_DATAOBJLIST:
689 if (parser_state->aml < parser_state->pkg_end) {
690 /* Non-empty list of variable arguments, nothing returned */
692 walk_state->arg_count = ACPI_VAR_ARGS;
698 ACPI_REPORT_ERROR(("Invalid arg_type: %X\n", arg_type));
699 status = AE_AML_OPERAND_TYPE;
704 return_ACPI_STATUS(status);