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.
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acnamesp.h>
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME ("psargs")
53 /* Local prototypes */
56 acpi_ps_get_next_package_length (
57 struct acpi_parse_state *parser_state);
59 static union acpi_parse_object *
60 acpi_ps_get_next_field (
61 struct acpi_parse_state *parser_state);
64 /*******************************************************************************
66 * FUNCTION: acpi_ps_get_next_package_length
68 * PARAMETERS: parser_state - Current parser state object
70 * RETURN: Decoded package length. On completion, the AML pointer points
71 * past the length byte or bytes.
73 * DESCRIPTION: Decode and return a package length field
75 ******************************************************************************/
78 acpi_ps_get_next_package_length (
79 struct acpi_parse_state *parser_state)
85 ACPI_FUNCTION_TRACE ("ps_get_next_package_length");
88 encoded_length = (u32) ACPI_GET8 (parser_state->aml);
91 switch (encoded_length >> 6) /* bits 6-7 contain encoding scheme */ {
92 case 0: /* 1-byte encoding (bits 0-5) */
94 length = (encoded_length & 0x3F);
98 case 1: /* 2-byte encoding (next byte + bits 0-3) */
100 length = ((ACPI_GET8 (parser_state->aml) << 04) |
101 (encoded_length & 0x0F));
106 case 2: /* 3-byte encoding (next 2 bytes + bits 0-3) */
108 length = ((ACPI_GET8 (parser_state->aml + 1) << 12) |
109 (ACPI_GET8 (parser_state->aml) << 04) |
110 (encoded_length & 0x0F));
111 parser_state->aml += 2;
115 case 3: /* 4-byte encoding (next 3 bytes + bits 0-3) */
117 length = ((ACPI_GET8 (parser_state->aml + 2) << 20) |
118 (ACPI_GET8 (parser_state->aml + 1) << 12) |
119 (ACPI_GET8 (parser_state->aml) << 04) |
120 (encoded_length & 0x0F));
121 parser_state->aml += 3;
126 /* Can't get here, only 2 bits / 4 cases */
130 return_VALUE (length);
134 /*******************************************************************************
136 * FUNCTION: acpi_ps_get_next_package_end
138 * PARAMETERS: parser_state - Current parser state object
140 * RETURN: Pointer to end-of-package +1
142 * DESCRIPTION: Get next package length and return a pointer past the end of
143 * the package. Consumes the package length field
145 ******************************************************************************/
148 acpi_ps_get_next_package_end (
149 struct acpi_parse_state *parser_state)
151 u8 *start = parser_state->aml;
152 acpi_native_uint length;
155 ACPI_FUNCTION_TRACE ("ps_get_next_package_end");
158 /* Function below changes parser_state->Aml */
160 length = (acpi_native_uint) acpi_ps_get_next_package_length (parser_state);
162 return_PTR (start + length); /* end of package */
166 /*******************************************************************************
168 * FUNCTION: acpi_ps_get_next_namestring
170 * PARAMETERS: parser_state - Current parser state object
172 * RETURN: Pointer to the start of the name string (pointer points into
175 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
176 * prefix characters. Set parser state to point past the string.
177 * (Name is consumed from the AML.)
179 ******************************************************************************/
182 acpi_ps_get_next_namestring (
183 struct acpi_parse_state *parser_state)
185 u8 *start = parser_state->aml;
186 u8 *end = parser_state->aml;
189 ACPI_FUNCTION_TRACE ("ps_get_next_namestring");
192 /* Handle multiple prefix characters */
194 while (acpi_ps_is_prefix_char (ACPI_GET8 (end))) {
195 /* Include prefix '\\' or '^' */
200 /* Decode the path */
202 switch (ACPI_GET8 (end)) {
213 case AML_DUAL_NAME_PREFIX:
215 /* Two name segments */
217 end += 1 + (2 * ACPI_NAME_SIZE);
220 case AML_MULTI_NAME_PREFIX_OP:
222 /* Multiple name segments, 4 chars each */
224 end += 2 + ((acpi_size) ACPI_GET8 (end + 1) * ACPI_NAME_SIZE);
229 /* Single name segment */
231 end += ACPI_NAME_SIZE;
235 parser_state->aml = (u8*) end;
236 return_PTR ((char *) start);
240 /*******************************************************************************
242 * FUNCTION: acpi_ps_get_next_namepath
244 * PARAMETERS: parser_state - Current parser state object
245 * Arg - Where the namepath will be stored
246 * arg_count - If the namepath points to a control method
247 * the method's argument is returned here.
248 * method_call - Whether the namepath can possibly be the
249 * start of a method call
253 * DESCRIPTION: Get next name (if method call, return # of required args).
254 * Names are looked up in the internal namespace to determine
255 * if the name represents a control method. If a method
256 * is found, the number of arguments to the method is returned.
257 * This information is critical for parsing to continue correctly.
259 ******************************************************************************/
262 acpi_ps_get_next_namepath (
263 struct acpi_walk_state *walk_state,
264 struct acpi_parse_state *parser_state,
265 union acpi_parse_object *arg,
269 union acpi_parse_object *name_op;
270 acpi_status status = AE_OK;
271 union acpi_operand_object *method_desc;
272 struct acpi_namespace_node *node;
273 union acpi_generic_state scope_info;
276 ACPI_FUNCTION_TRACE ("ps_get_next_namepath");
279 path = acpi_ps_get_next_namestring (parser_state);
281 /* Null path case is allowed */
285 * Lookup the name in the internal namespace
287 scope_info.scope.node = NULL;
288 node = parser_state->start_node;
290 scope_info.scope.node = node;
294 * Lookup object. We don't want to add anything new to the namespace
295 * here, however. So we use MODE_EXECUTE. Allow searching of the
296 * parent tree, but don't open a new scope -- we just want to lookup the
297 * object (MUST BE mode EXECUTE to perform upsearch)
299 status = acpi_ns_lookup (&scope_info, path, ACPI_TYPE_ANY,
301 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
303 if (ACPI_SUCCESS (status) && method_call) {
304 if (node->type == ACPI_TYPE_METHOD) {
305 /* This name is actually a control method invocation */
307 method_desc = acpi_ns_get_attached_object (node);
308 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
309 "Control Method - %p Desc %p Path=%p\n",
310 node, method_desc, path));
312 name_op = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
314 return_ACPI_STATUS (AE_NO_MEMORY);
317 /* Change arg into a METHOD CALL and attach name to it */
319 acpi_ps_init_op (arg, AML_INT_METHODCALL_OP);
320 name_op->common.value.name = path;
322 /* Point METHODCALL/NAME to the METHOD Node */
324 name_op->common.node = node;
325 acpi_ps_append_arg (arg, name_op);
329 "ps_get_next_namepath: Control Method %p has no attached object\n",
331 return_ACPI_STATUS (AE_AML_INTERNAL);
334 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
335 "Control Method - %p Args %X\n",
336 node, method_desc->method.param_count));
338 /* Get the number of arguments to expect */
340 walk_state->arg_count = method_desc->method.param_count;
341 return_ACPI_STATUS (AE_OK);
345 * Else this is normal named object reference.
346 * Just init the NAMEPATH object with the pathname.
351 if (ACPI_FAILURE (status)) {
353 * 1) Any error other than NOT_FOUND is always severe
354 * 2) NOT_FOUND is only important if we are executing a method.
355 * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
357 if ((((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) &&
358 (status == AE_NOT_FOUND) &&
359 (walk_state->op->common.aml_opcode != AML_COND_REF_OF_OP)) ||
361 (status != AE_NOT_FOUND)) {
362 ACPI_REPORT_NSERROR (path, status);
364 acpi_os_printf ("search_node %p start_node %p return_node %p\n",
365 scope_info.scope.node, parser_state->start_node, node);
371 * We got a NOT_FOUND during table load or we encountered
372 * a cond_ref_of(x) where the target does not exist.
381 * Regardless of success/failure above,
382 * Just initialize the Op with the pathname.
384 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
385 arg->common.value.name = path;
387 return_ACPI_STATUS (status);
391 /*******************************************************************************
393 * FUNCTION: acpi_ps_get_next_simple_arg
395 * PARAMETERS: parser_state - Current parser state object
396 * arg_type - The argument type (AML_*_ARG)
397 * Arg - Where the argument is returned
401 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
403 ******************************************************************************/
406 acpi_ps_get_next_simple_arg (
407 struct acpi_parse_state *parser_state,
409 union acpi_parse_object *arg)
412 ACPI_FUNCTION_TRACE_U32 ("ps_get_next_simple_arg", arg_type);
418 acpi_ps_init_op (arg, AML_BYTE_OP);
419 arg->common.value.integer = (u32) ACPI_GET8 (parser_state->aml);
426 acpi_ps_init_op (arg, AML_WORD_OP);
428 /* Get 2 bytes from the AML stream */
430 ACPI_MOVE_16_TO_32 (&arg->common.value.integer, parser_state->aml);
431 parser_state->aml += 2;
437 acpi_ps_init_op (arg, AML_DWORD_OP);
439 /* Get 4 bytes from the AML stream */
441 ACPI_MOVE_32_TO_32 (&arg->common.value.integer, parser_state->aml);
442 parser_state->aml += 4;
448 acpi_ps_init_op (arg, AML_QWORD_OP);
450 /* Get 8 bytes from the AML stream */
452 ACPI_MOVE_64_TO_64 (&arg->common.value.integer, parser_state->aml);
453 parser_state->aml += 8;
459 acpi_ps_init_op (arg, AML_STRING_OP);
460 arg->common.value.string = (char *) parser_state->aml;
462 while (ACPI_GET8 (parser_state->aml) != '\0') {
470 case ARGP_NAMESTRING:
472 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
473 arg->common.value.name = acpi_ps_get_next_namestring (parser_state);
479 ACPI_REPORT_ERROR (("Invalid arg_type %X\n", arg_type));
487 /*******************************************************************************
489 * FUNCTION: acpi_ps_get_next_field
491 * PARAMETERS: parser_state - Current parser state object
493 * RETURN: A newly allocated FIELD op
495 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
497 ******************************************************************************/
499 static union acpi_parse_object *
500 acpi_ps_get_next_field (
501 struct acpi_parse_state *parser_state)
503 u32 aml_offset = (u32)
504 ACPI_PTR_DIFF (parser_state->aml,
505 parser_state->aml_start);
506 union acpi_parse_object *field;
511 ACPI_FUNCTION_TRACE ("ps_get_next_field");
514 /* Determine field type */
516 switch (ACPI_GET8 (parser_state->aml)) {
519 opcode = AML_INT_NAMEDFIELD_OP;
524 opcode = AML_INT_RESERVEDFIELD_OP;
530 opcode = AML_INT_ACCESSFIELD_OP;
535 /* Allocate a new field op */
537 field = acpi_ps_alloc_op (opcode);
542 field->common.aml_offset = aml_offset;
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 = 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 = acpi_ps_get_next_package_length (parser_state);
569 case AML_INT_ACCESSFIELD_OP:
572 * Get access_type and access_attrib and merge into the field Op
573 * access_type is first operand, access_attribute is second
575 field->common.value.integer = (ACPI_GET8 (parser_state->aml) << 8);
577 field->common.value.integer |= ACPI_GET8 (parser_state->aml);
583 /* Opcode was set in previous switch */
591 /*******************************************************************************
593 * FUNCTION: acpi_ps_get_next_arg
595 * PARAMETERS: walk_state - Current state
596 * parser_state - Current parser state object
597 * arg_type - The argument type (AML_*_ARG)
598 * return_arg - Where the next arg is returned
600 * RETURN: Status, and an op object containing the next argument.
602 * DESCRIPTION: Get next argument (including complex list arguments that require
603 * pushing the parser stack)
605 ******************************************************************************/
608 acpi_ps_get_next_arg (
609 struct acpi_walk_state *walk_state,
610 struct acpi_parse_state *parser_state,
612 union acpi_parse_object **return_arg)
614 union acpi_parse_object *arg = NULL;
615 union acpi_parse_object *prev = NULL;
616 union acpi_parse_object *field;
618 acpi_status status = AE_OK;
621 ACPI_FUNCTION_TRACE_PTR ("ps_get_next_arg", parser_state);
630 case ARGP_NAMESTRING:
632 /* Constants, strings, and namestrings are all the same size */
634 arg = acpi_ps_alloc_op (AML_BYTE_OP);
636 return_ACPI_STATUS (AE_NO_MEMORY);
638 acpi_ps_get_next_simple_arg (parser_state, arg_type, arg);
644 /* Package length, nothing returned */
646 parser_state->pkg_end = acpi_ps_get_next_package_end (parser_state);
652 if (parser_state->aml < parser_state->pkg_end) {
655 while (parser_state->aml < parser_state->pkg_end) {
656 field = acpi_ps_get_next_field (parser_state);
658 return_ACPI_STATUS (AE_NO_MEMORY);
662 prev->common.next = field;
670 /* Skip to End of byte data */
672 parser_state->aml = parser_state->pkg_end;
679 if (parser_state->aml < parser_state->pkg_end) {
682 arg = acpi_ps_alloc_op (AML_INT_BYTELIST_OP);
684 return_ACPI_STATUS (AE_NO_MEMORY);
687 /* Fill in bytelist data */
689 arg->common.value.size = (u32)
690 ACPI_PTR_DIFF (parser_state->pkg_end, parser_state->aml);
691 arg->named.data = parser_state->aml;
693 /* Skip to End of byte data */
695 parser_state->aml = parser_state->pkg_end;
702 case ARGP_SIMPLENAME:
704 subop = acpi_ps_peek_opcode (parser_state);
706 acpi_ps_is_leading_char (subop) ||
707 acpi_ps_is_prefix_char (subop)) {
708 /* null_name or name_string */
710 arg = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
712 return_ACPI_STATUS (AE_NO_MEMORY);
715 status = acpi_ps_get_next_namepath (walk_state, parser_state, arg, 0);
718 /* Single complex argument, nothing returned */
720 walk_state->arg_count = 1;
728 /* Single complex argument, nothing returned */
730 walk_state->arg_count = 1;
734 case ARGP_DATAOBJLIST:
738 if (parser_state->aml < parser_state->pkg_end) {
739 /* Non-empty list of variable arguments, nothing returned */
741 walk_state->arg_count = ACPI_VAR_ARGS;
748 ACPI_REPORT_ERROR (("Invalid arg_type: %X\n", arg_type));
749 status = AE_AML_OPERAND_TYPE;
754 return_ACPI_STATUS (status);