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")
54 /*******************************************************************************
56 * FUNCTION: acpi_ps_get_next_package_length
58 * PARAMETERS: parser_state - Current parser state object
60 * RETURN: Decoded package length. On completion, the AML pointer points
61 * past the length byte or bytes.
63 * DESCRIPTION: Decode and return a package length field
65 ******************************************************************************/
68 acpi_ps_get_next_package_length (
69 struct acpi_parse_state *parser_state)
75 ACPI_FUNCTION_TRACE ("ps_get_next_package_length");
78 encoded_length = (u32) ACPI_GET8 (parser_state->aml);
82 switch (encoded_length >> 6) /* bits 6-7 contain encoding scheme */ {
83 case 0: /* 1-byte encoding (bits 0-5) */
85 length = (encoded_length & 0x3F);
89 case 1: /* 2-byte encoding (next byte + bits 0-3) */
91 length = ((ACPI_GET8 (parser_state->aml) << 04) |
92 (encoded_length & 0x0F));
97 case 2: /* 3-byte encoding (next 2 bytes + bits 0-3) */
99 length = ((ACPI_GET8 (parser_state->aml + 1) << 12) |
100 (ACPI_GET8 (parser_state->aml) << 04) |
101 (encoded_length & 0x0F));
102 parser_state->aml += 2;
106 case 3: /* 4-byte encoding (next 3 bytes + bits 0-3) */
108 length = ((ACPI_GET8 (parser_state->aml + 2) << 20) |
109 (ACPI_GET8 (parser_state->aml + 1) << 12) |
110 (ACPI_GET8 (parser_state->aml) << 04) |
111 (encoded_length & 0x0F));
112 parser_state->aml += 3;
117 /* Can't get here, only 2 bits / 4 cases */
121 return_VALUE (length);
125 /*******************************************************************************
127 * FUNCTION: acpi_ps_get_next_package_end
129 * PARAMETERS: parser_state - 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 acpi_ps_get_next_package_end (
140 struct acpi_parse_state *parser_state)
142 u8 *start = parser_state->aml;
143 acpi_native_uint length;
146 ACPI_FUNCTION_TRACE ("ps_get_next_package_end");
149 /* Function below changes parser_state->Aml */
151 length = (acpi_native_uint) acpi_ps_get_next_package_length (parser_state);
153 return_PTR (start + length); /* end of package */
157 /*******************************************************************************
159 * FUNCTION: acpi_ps_get_next_namestring
161 * PARAMETERS: parser_state - 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 acpi_ps_get_next_namestring (
174 struct acpi_parse_state *parser_state)
176 u8 *start = parser_state->aml;
177 u8 *end = parser_state->aml;
180 ACPI_FUNCTION_TRACE ("ps_get_next_namestring");
183 /* Handle multiple prefix characters */
185 while (acpi_ps_is_prefix_char (ACPI_GET8 (end))) {
186 /* Include prefix '\\' or '^' */
191 /* Decode the path */
193 switch (ACPI_GET8 (end)) {
204 case AML_DUAL_NAME_PREFIX:
206 /* Two name segments */
208 end += 1 + (2 * ACPI_NAME_SIZE);
211 case AML_MULTI_NAME_PREFIX_OP:
213 /* Multiple name segments, 4 chars each */
215 end += 2 + ((acpi_size) ACPI_GET8 (end + 1) * ACPI_NAME_SIZE);
220 /* Single name segment */
222 end += ACPI_NAME_SIZE;
226 parser_state->aml = (u8*) end;
227 return_PTR ((char *) start);
231 /*******************************************************************************
233 * FUNCTION: acpi_ps_get_next_namepath
235 * PARAMETERS: parser_state - Current parser state object
236 * Arg - Where the namepath will be stored
237 * arg_count - If the namepath points to a control method
238 * the method's argument is returned here.
239 * method_call - Whether the namepath can possibly be the
240 * start of a method call
244 * DESCRIPTION: Get next name (if method call, return # of required args).
245 * Names are looked up in the internal namespace to determine
246 * if the name represents a control method. If a method
247 * is found, the number of arguments to the method is returned.
248 * This information is critical for parsing to continue correctly.
250 ******************************************************************************/
253 acpi_ps_get_next_namepath (
254 struct acpi_walk_state *walk_state,
255 struct acpi_parse_state *parser_state,
256 union acpi_parse_object *arg,
260 union acpi_parse_object *name_op;
261 acpi_status status = AE_OK;
262 union acpi_operand_object *method_desc;
263 struct acpi_namespace_node *node;
264 union acpi_generic_state scope_info;
267 ACPI_FUNCTION_TRACE ("ps_get_next_namepath");
270 path = acpi_ps_get_next_namestring (parser_state);
272 /* Null path case is allowed */
276 * Lookup the name in the internal namespace
278 scope_info.scope.node = NULL;
279 node = parser_state->start_node;
281 scope_info.scope.node = node;
285 * Lookup object. We don't want to add anything new to the namespace
286 * here, however. So we use MODE_EXECUTE. Allow searching of the
287 * parent tree, but don't open a new scope -- we just want to lookup the
288 * object (MUST BE mode EXECUTE to perform upsearch)
290 status = acpi_ns_lookup (&scope_info, path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
291 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE, NULL, &node);
292 if (ACPI_SUCCESS (status) && method_call) {
293 if (node->type == ACPI_TYPE_METHOD) {
295 * This name is actually a control method invocation
297 method_desc = acpi_ns_get_attached_object (node);
298 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
299 "Control Method - %p Desc %p Path=%p\n",
300 node, method_desc, path));
302 name_op = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
304 return_ACPI_STATUS (AE_NO_MEMORY);
307 /* Change arg into a METHOD CALL and attach name to it */
309 acpi_ps_init_op (arg, AML_INT_METHODCALL_OP);
310 name_op->common.value.name = path;
312 /* Point METHODCALL/NAME to the METHOD Node */
314 name_op->common.node = node;
315 acpi_ps_append_arg (arg, name_op);
319 "ps_get_next_namepath: Control Method %p has no attached object\n",
321 return_ACPI_STATUS (AE_AML_INTERNAL);
324 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
325 "Control Method - %p Args %X\n",
326 node, method_desc->method.param_count));
328 /* Get the number of arguments to expect */
330 walk_state->arg_count = method_desc->method.param_count;
331 return_ACPI_STATUS (AE_OK);
335 * Else this is normal named object reference.
336 * Just init the NAMEPATH object with the pathname.
341 if (ACPI_FAILURE (status)) {
343 * 1) Any error other than NOT_FOUND is always severe
344 * 2) NOT_FOUND is only important if we are executing a method.
345 * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
347 if ((((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) &&
348 (status == AE_NOT_FOUND) &&
349 (walk_state->op->common.aml_opcode != AML_COND_REF_OF_OP)) ||
351 (status != AE_NOT_FOUND)) {
352 ACPI_REPORT_NSERROR (path, status);
354 acpi_os_printf ("search_node %p start_node %p return_node %p\n",
355 scope_info.scope.node, parser_state->start_node, node);
361 * We got a NOT_FOUND during table load or we encountered
362 * a cond_ref_of(x) where the target does not exist.
363 * -- either case is ok
371 * Regardless of success/failure above,
372 * Just initialize the Op with the pathname.
374 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
375 arg->common.value.name = path;
377 return_ACPI_STATUS (status);
381 /*******************************************************************************
383 * FUNCTION: acpi_ps_get_next_simple_arg
385 * PARAMETERS: parser_state - Current parser state object
386 * arg_type - The argument type (AML_*_ARG)
387 * Arg - Where the argument is returned
391 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
393 ******************************************************************************/
396 acpi_ps_get_next_simple_arg (
397 struct acpi_parse_state *parser_state,
399 union acpi_parse_object *arg)
402 ACPI_FUNCTION_TRACE_U32 ("ps_get_next_simple_arg", arg_type);
408 acpi_ps_init_op (arg, AML_BYTE_OP);
409 arg->common.value.integer = (u32) ACPI_GET8 (parser_state->aml);
416 acpi_ps_init_op (arg, AML_WORD_OP);
418 /* Get 2 bytes from the AML stream */
420 ACPI_MOVE_16_TO_32 (&arg->common.value.integer, parser_state->aml);
421 parser_state->aml += 2;
427 acpi_ps_init_op (arg, AML_DWORD_OP);
429 /* Get 4 bytes from the AML stream */
431 ACPI_MOVE_32_TO_32 (&arg->common.value.integer, parser_state->aml);
432 parser_state->aml += 4;
438 acpi_ps_init_op (arg, AML_QWORD_OP);
440 /* Get 8 bytes from the AML stream */
442 ACPI_MOVE_64_TO_64 (&arg->common.value.integer, parser_state->aml);
443 parser_state->aml += 8;
449 acpi_ps_init_op (arg, AML_STRING_OP);
450 arg->common.value.string = (char *) parser_state->aml;
452 while (ACPI_GET8 (parser_state->aml) != '\0') {
460 case ARGP_NAMESTRING:
462 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
463 arg->common.value.name = acpi_ps_get_next_namestring (parser_state);
469 ACPI_REPORT_ERROR (("Invalid arg_type %X\n", arg_type));
477 /*******************************************************************************
479 * FUNCTION: acpi_ps_get_next_field
481 * PARAMETERS: parser_state - Current parser state object
483 * RETURN: A newly allocated FIELD op
485 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
487 ******************************************************************************/
489 union acpi_parse_object *
490 acpi_ps_get_next_field (
491 struct acpi_parse_state *parser_state)
493 u32 aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
494 parser_state->aml_start);
495 union acpi_parse_object *field;
500 ACPI_FUNCTION_TRACE ("ps_get_next_field");
503 /* determine field type */
505 switch (ACPI_GET8 (parser_state->aml)) {
508 opcode = AML_INT_NAMEDFIELD_OP;
513 opcode = AML_INT_RESERVEDFIELD_OP;
519 opcode = AML_INT_ACCESSFIELD_OP;
525 /* Allocate a new field op */
527 field = acpi_ps_alloc_op (opcode);
532 field->common.aml_offset = aml_offset;
534 /* Decode the field type */
537 case AML_INT_NAMEDFIELD_OP:
539 /* Get the 4-character name */
541 ACPI_MOVE_32_TO_32 (&name, parser_state->aml);
542 acpi_ps_set_name (field, name);
543 parser_state->aml += ACPI_NAME_SIZE;
545 /* Get the length which is encoded as a package length */
547 field->common.value.size = acpi_ps_get_next_package_length (parser_state);
551 case AML_INT_RESERVEDFIELD_OP:
553 /* Get the length which is encoded as a package length */
555 field->common.value.size = acpi_ps_get_next_package_length (parser_state);
559 case AML_INT_ACCESSFIELD_OP:
562 * Get access_type and access_attrib and merge into the field Op
563 * access_type is first operand, access_attribute is second
565 field->common.value.integer = (ACPI_GET8 (parser_state->aml) << 8);
567 field->common.value.integer |= ACPI_GET8 (parser_state->aml);
573 /* Opcode was set in previous switch */
581 /*******************************************************************************
583 * FUNCTION: acpi_ps_get_next_arg
585 * PARAMETERS: parser_state - Current parser state object
586 * arg_type - The argument type (AML_*_ARG)
587 * arg_count - If the argument points to a control method
588 * the method's argument is returned here.
590 * RETURN: Status, and an op object containing the next argument.
592 * DESCRIPTION: Get next argument (including complex list arguments that require
593 * pushing the parser stack)
595 ******************************************************************************/
598 acpi_ps_get_next_arg (
599 struct acpi_walk_state *walk_state,
600 struct acpi_parse_state *parser_state,
602 union acpi_parse_object **return_arg)
604 union acpi_parse_object *arg = NULL;
605 union acpi_parse_object *prev = NULL;
606 union acpi_parse_object *field;
608 acpi_status status = AE_OK;
611 ACPI_FUNCTION_TRACE_PTR ("ps_get_next_arg", parser_state);
620 case ARGP_NAMESTRING:
622 /* constants, strings, and namestrings are all the same size */
624 arg = acpi_ps_alloc_op (AML_BYTE_OP);
626 return_ACPI_STATUS (AE_NO_MEMORY);
628 acpi_ps_get_next_simple_arg (parser_state, arg_type, arg);
634 /* Package length, nothing returned */
636 parser_state->pkg_end = acpi_ps_get_next_package_end (parser_state);
642 if (parser_state->aml < parser_state->pkg_end) {
645 while (parser_state->aml < parser_state->pkg_end) {
646 field = acpi_ps_get_next_field (parser_state);
648 return_ACPI_STATUS (AE_NO_MEMORY);
652 prev->common.next = field;
661 /* Skip to End of byte data */
663 parser_state->aml = parser_state->pkg_end;
670 if (parser_state->aml < parser_state->pkg_end) {
673 arg = acpi_ps_alloc_op (AML_INT_BYTELIST_OP);
675 return_ACPI_STATUS (AE_NO_MEMORY);
678 /* Fill in bytelist data */
680 arg->common.value.size = (u32) ACPI_PTR_DIFF (parser_state->pkg_end,
682 arg->named.data = parser_state->aml;
684 /* Skip to End of byte data */
686 parser_state->aml = parser_state->pkg_end;
693 case ARGP_SIMPLENAME:
695 subop = acpi_ps_peek_opcode (parser_state);
697 acpi_ps_is_leading_char (subop) ||
698 acpi_ps_is_prefix_char (subop)) {
699 /* null_name or name_string */
701 arg = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
703 return_ACPI_STATUS (AE_NO_MEMORY);
706 status = acpi_ps_get_next_namepath (walk_state, parser_state, arg, 0);
709 /* single complex argument, nothing returned */
711 walk_state->arg_count = 1;
719 /* single complex argument, nothing returned */
721 walk_state->arg_count = 1;
725 case ARGP_DATAOBJLIST:
729 if (parser_state->aml < parser_state->pkg_end) {
730 /* non-empty list of variable arguments, nothing returned */
732 walk_state->arg_count = ACPI_VAR_ARGS;
739 ACPI_REPORT_ERROR (("Invalid arg_type: %X\n", arg_type));
740 status = AE_AML_OPERAND_TYPE;
745 return_ACPI_STATUS (status);