1 /******************************************************************************
3 * Module Name: psargs - Parse AML opcode arguments
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2007, 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>
48 #include <acpi/acdispat.h>
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME("psargs")
53 /* Local prototypes */
55 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state);
57 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
60 /*******************************************************************************
62 * FUNCTION: acpi_ps_get_next_package_length
64 * PARAMETERS: parser_state - Current parser state object
66 * RETURN: Decoded package length. On completion, the AML pointer points
67 * past the length byte or bytes.
69 * DESCRIPTION: Decode and return a package length field.
70 * Note: Largest package length is 28 bits, from ACPI specification
72 ******************************************************************************/
75 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
77 u8 *aml = parser_state->aml;
78 u32 package_length = 0;
79 acpi_native_uint byte_count;
80 u8 byte_zero_mask = 0x3F; /* Default [0:5] */
82 ACPI_FUNCTION_TRACE(ps_get_next_package_length);
85 * Byte 0 bits [6:7] contain the number of additional bytes
86 * used to encode the package length, either 0,1,2, or 3
88 byte_count = (aml[0] >> 6);
89 parser_state->aml += (byte_count + 1);
91 /* Get bytes 3, 2, 1 as needed */
95 * Final bit positions for the package length bytes:
101 package_length |= (aml[byte_count] << ((byte_count << 3) - 4));
103 byte_zero_mask = 0x0F; /* Use bits [0:3] of byte 0 */
107 /* Byte 0 is a special case, either bits [0:3] or [0:5] are used */
109 package_length |= (aml[0] & byte_zero_mask);
110 return_UINT32(package_length);
113 /*******************************************************************************
115 * FUNCTION: acpi_ps_get_next_package_end
117 * PARAMETERS: parser_state - Current parser state object
119 * RETURN: Pointer to end-of-package +1
121 * DESCRIPTION: Get next package length and return a pointer past the end of
122 * the package. Consumes the package length field
124 ******************************************************************************/
126 u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
128 u8 *start = parser_state->aml;
131 ACPI_FUNCTION_TRACE(ps_get_next_package_end);
133 /* Function below updates parser_state->Aml */
135 package_length = acpi_ps_get_next_package_length(parser_state);
137 return_PTR(start + package_length); /* end of package */
140 /*******************************************************************************
142 * FUNCTION: acpi_ps_get_next_namestring
144 * PARAMETERS: parser_state - Current parser state object
146 * RETURN: Pointer to the start of the name string (pointer points into
149 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
150 * prefix characters. Set parser state to point past the string.
151 * (Name is consumed from the AML.)
153 ******************************************************************************/
155 char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
157 u8 *start = parser_state->aml;
158 u8 *end = parser_state->aml;
160 ACPI_FUNCTION_TRACE(ps_get_next_namestring);
162 /* Point past any namestring prefix characters (backslash or carat) */
164 while (acpi_ps_is_prefix_char(*end)) {
168 /* Decode the path prefix character */
181 case AML_DUAL_NAME_PREFIX:
183 /* Two name segments */
185 end += 1 + (2 * ACPI_NAME_SIZE);
188 case AML_MULTI_NAME_PREFIX_OP:
190 /* Multiple name segments, 4 chars each, count in next byte */
192 end += 2 + (*(end + 1) * ACPI_NAME_SIZE);
197 /* Single name segment */
199 end += ACPI_NAME_SIZE;
203 parser_state->aml = end;
204 return_PTR((char *)start);
207 /*******************************************************************************
209 * FUNCTION: acpi_ps_get_next_namepath
211 * PARAMETERS: parser_state - Current parser state object
212 * Arg - Where the namepath will be stored
213 * arg_count - If the namepath points to a control method
214 * the method's argument is returned here.
215 * possible_method_call - Whether the namepath can possibly be the
216 * start of a method call
220 * DESCRIPTION: Get next name (if method call, return # of required args).
221 * Names are looked up in the internal namespace to determine
222 * if the name represents a control method. If a method
223 * is found, the number of arguments to the method is returned.
224 * This information is critical for parsing to continue correctly.
226 ******************************************************************************/
229 acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
230 struct acpi_parse_state *parser_state,
231 union acpi_parse_object *arg, u8 possible_method_call)
235 union acpi_parse_object *name_op;
236 union acpi_operand_object *method_desc;
237 struct acpi_namespace_node *node;
239 ACPI_FUNCTION_TRACE(ps_get_next_namepath);
241 path = acpi_ps_get_next_namestring(parser_state);
242 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
244 /* Null path case is allowed, just exit */
247 arg->common.value.name = path;
248 return_ACPI_STATUS(AE_OK);
252 * Lookup the name in the internal namespace, starting with the current
253 * scope. We don't want to add anything new to the namespace here,
254 * however, so we use MODE_EXECUTE.
255 * Allow searching of the parent tree, but don't open a new scope -
256 * we just want to lookup the object (must be mode EXECUTE to perform
259 status = acpi_ns_lookup(walk_state->scope_info, path,
260 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
261 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
265 * If this name is a control method invocation, we must
266 * setup the method call
268 if (ACPI_SUCCESS(status) &&
269 possible_method_call && (node->type == ACPI_TYPE_METHOD)) {
271 /* This name is actually a control method invocation */
273 method_desc = acpi_ns_get_attached_object(node);
274 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
275 "Control Method - %p Desc %p Path=%p\n", node,
278 name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
280 return_ACPI_STATUS(AE_NO_MEMORY);
283 /* Change Arg into a METHOD CALL and attach name to it */
285 acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
286 name_op->common.value.name = path;
288 /* Point METHODCALL/NAME to the METHOD Node */
290 name_op->common.node = node;
291 acpi_ps_append_arg(arg, name_op);
295 "Control Method %p has no attached object",
297 return_ACPI_STATUS(AE_AML_INTERNAL);
300 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
301 "Control Method - %p Args %X\n",
302 node, method_desc->method.param_count));
304 /* Get the number of arguments to expect */
306 walk_state->arg_count = method_desc->method.param_count;
307 return_ACPI_STATUS(AE_OK);
311 * Special handling if the name was not found during the lookup -
312 * some not_found cases are allowed
314 if (status == AE_NOT_FOUND) {
316 /* 1) not_found is ok during load pass 1/2 (allow forward references) */
318 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) !=
319 ACPI_PARSE_EXECUTE) {
323 /* 2) not_found during a cond_ref_of(x) is ok by definition */
325 else if (walk_state->op->common.aml_opcode ==
326 AML_COND_REF_OF_OP) {
331 * 3) not_found while building a Package is ok at this point, we
332 * may flag as an error later if slack mode is not enabled.
333 * (Some ASL code depends on allowing this behavior)
335 else if ((arg->common.parent) &&
336 ((arg->common.parent->common.aml_opcode ==
338 || (arg->common.parent->common.aml_opcode ==
339 AML_VAR_PACKAGE_OP))) {
344 /* Final exception check (may have been changed from code above) */
346 if (ACPI_FAILURE(status)) {
347 ACPI_ERROR_NAMESPACE(path, status);
349 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) ==
350 ACPI_PARSE_EXECUTE) {
352 /* Report a control method execution error */
354 status = acpi_ds_method_error(status, walk_state);
358 /* Save the namepath */
360 arg->common.value.name = path;
361 return_ACPI_STATUS(status);
364 /*******************************************************************************
366 * FUNCTION: acpi_ps_get_next_simple_arg
368 * PARAMETERS: parser_state - Current parser state object
369 * arg_type - The argument type (AML_*_ARG)
370 * Arg - Where the argument is returned
374 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
376 ******************************************************************************/
379 acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
380 u32 arg_type, union acpi_parse_object *arg)
384 u8 *aml = parser_state->aml;
386 ACPI_FUNCTION_TRACE_U32(ps_get_next_simple_arg, arg_type);
391 /* Get 1 byte from the AML stream */
393 opcode = AML_BYTE_OP;
394 arg->common.value.integer = (acpi_integer) * aml;
400 /* Get 2 bytes from the AML stream */
402 opcode = AML_WORD_OP;
403 ACPI_MOVE_16_TO_64(&arg->common.value.integer, aml);
409 /* Get 4 bytes from the AML stream */
411 opcode = AML_DWORD_OP;
412 ACPI_MOVE_32_TO_64(&arg->common.value.integer, aml);
418 /* Get 8 bytes from the AML stream */
420 opcode = AML_QWORD_OP;
421 ACPI_MOVE_64_TO_64(&arg->common.value.integer, aml);
427 /* Get a pointer to the string, point past the string */
429 opcode = AML_STRING_OP;
430 arg->common.value.string = ACPI_CAST_PTR(char, aml);
432 /* Find the null terminator */
435 while (aml[length]) {
442 case ARGP_NAMESTRING:
444 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
445 arg->common.value.name =
446 acpi_ps_get_next_namestring(parser_state);
451 ACPI_ERROR((AE_INFO, "Invalid ArgType %X", arg_type));
455 acpi_ps_init_op(arg, opcode);
456 parser_state->aml += length;
460 /*******************************************************************************
462 * FUNCTION: acpi_ps_get_next_field
464 * PARAMETERS: parser_state - Current parser state object
466 * RETURN: A newly allocated FIELD op
468 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
470 ******************************************************************************/
472 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
475 u32 aml_offset = (u32)
476 ACPI_PTR_DIFF(parser_state->aml,
477 parser_state->aml_start);
478 union acpi_parse_object *field;
482 ACPI_FUNCTION_TRACE(ps_get_next_field);
484 /* Determine field type */
486 switch (ACPI_GET8(parser_state->aml)) {
489 opcode = AML_INT_NAMEDFIELD_OP;
494 opcode = AML_INT_RESERVEDFIELD_OP;
500 opcode = AML_INT_ACCESSFIELD_OP;
505 /* Allocate a new field op */
507 field = acpi_ps_alloc_op(opcode);
512 field->common.aml_offset = aml_offset;
514 /* Decode the field type */
517 case AML_INT_NAMEDFIELD_OP:
519 /* Get the 4-character name */
521 ACPI_MOVE_32_TO_32(&name, parser_state->aml);
522 acpi_ps_set_name(field, name);
523 parser_state->aml += ACPI_NAME_SIZE;
525 /* Get the length which is encoded as a package length */
527 field->common.value.size =
528 acpi_ps_get_next_package_length(parser_state);
531 case AML_INT_RESERVEDFIELD_OP:
533 /* Get the length which is encoded as a package length */
535 field->common.value.size =
536 acpi_ps_get_next_package_length(parser_state);
539 case AML_INT_ACCESSFIELD_OP:
542 * Get access_type and access_attrib and merge into the field Op
543 * access_type is first operand, access_attribute is second
545 field->common.value.integer =
546 (((u32) ACPI_GET8(parser_state->aml) << 8));
548 field->common.value.integer |= ACPI_GET8(parser_state->aml);
554 /* Opcode was set in previous switch */
561 /*******************************************************************************
563 * FUNCTION: acpi_ps_get_next_arg
565 * PARAMETERS: walk_state - Current state
566 * parser_state - Current parser state object
567 * arg_type - The argument type (AML_*_ARG)
568 * return_arg - Where the next arg is returned
570 * RETURN: Status, and an op object containing the next argument.
572 * DESCRIPTION: Get next argument (including complex list arguments that require
573 * pushing the parser stack)
575 ******************************************************************************/
578 acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
579 struct acpi_parse_state *parser_state,
580 u32 arg_type, union acpi_parse_object **return_arg)
582 union acpi_parse_object *arg = NULL;
583 union acpi_parse_object *prev = NULL;
584 union acpi_parse_object *field;
586 acpi_status status = AE_OK;
588 ACPI_FUNCTION_TRACE_PTR(ps_get_next_arg, parser_state);
596 case ARGP_NAMESTRING:
598 /* Constants, strings, and namestrings are all the same size */
600 arg = acpi_ps_alloc_op(AML_BYTE_OP);
602 return_ACPI_STATUS(AE_NO_MEMORY);
604 acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
609 /* Package length, nothing returned */
611 parser_state->pkg_end =
612 acpi_ps_get_next_package_end(parser_state);
617 if (parser_state->aml < parser_state->pkg_end) {
621 while (parser_state->aml < parser_state->pkg_end) {
622 field = acpi_ps_get_next_field(parser_state);
624 return_ACPI_STATUS(AE_NO_MEMORY);
628 prev->common.next = field;
635 /* Skip to End of byte data */
637 parser_state->aml = parser_state->pkg_end;
643 if (parser_state->aml < parser_state->pkg_end) {
647 arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP);
649 return_ACPI_STATUS(AE_NO_MEMORY);
652 /* Fill in bytelist data */
654 arg->common.value.size = (u32)
655 ACPI_PTR_DIFF(parser_state->pkg_end,
657 arg->named.data = parser_state->aml;
659 /* Skip to End of byte data */
661 parser_state->aml = parser_state->pkg_end;
667 case ARGP_SIMPLENAME:
669 subop = acpi_ps_peek_opcode(parser_state);
671 acpi_ps_is_leading_char(subop) ||
672 acpi_ps_is_prefix_char(subop)) {
674 /* null_name or name_string */
676 arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
678 return_ACPI_STATUS(AE_NO_MEMORY);
682 acpi_ps_get_next_namepath(walk_state, parser_state,
685 /* Single complex argument, nothing returned */
687 walk_state->arg_count = 1;
694 /* Single complex argument, nothing returned */
696 walk_state->arg_count = 1;
699 case ARGP_DATAOBJLIST:
703 if (parser_state->aml < parser_state->pkg_end) {
705 /* Non-empty list of variable arguments, nothing returned */
707 walk_state->arg_count = ACPI_VAR_ARGS;
713 ACPI_ERROR((AE_INFO, "Invalid ArgType: %X", arg_type));
714 status = AE_AML_OPERAND_TYPE;
719 return_ACPI_STATUS(status);