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
70 ******************************************************************************/
73 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
78 ACPI_FUNCTION_TRACE("ps_get_next_package_length");
80 encoded_length = (u32) ACPI_GET8(parser_state->aml);
83 switch (encoded_length >> 6) { /* bits 6-7 contain encoding scheme */
84 case 0: /* 1-byte encoding (bits 0-5) */
86 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));
96 case 2: /* 3-byte encoding (next 2 bytes + bits 0-3) */
98 length = ((ACPI_GET8(parser_state->aml + 1) << 12) |
99 (ACPI_GET8(parser_state->aml) << 04) |
100 (encoded_length & 0x0F));
101 parser_state->aml += 2;
104 case 3: /* 4-byte encoding (next 3 bytes + bits 0-3) */
106 length = ((ACPI_GET8(parser_state->aml + 2) << 20) |
107 (ACPI_GET8(parser_state->aml + 1) << 12) |
108 (ACPI_GET8(parser_state->aml) << 04) |
109 (encoded_length & 0x0F));
110 parser_state->aml += 3;
115 /* Can't get here, only 2 bits / 4 cases */
119 return_VALUE(length);
122 /*******************************************************************************
124 * FUNCTION: acpi_ps_get_next_package_end
126 * PARAMETERS: parser_state - Current parser state object
128 * RETURN: Pointer to end-of-package +1
130 * DESCRIPTION: Get next package length and return a pointer past the end of
131 * the package. Consumes the package length field
133 ******************************************************************************/
135 u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
137 u8 *start = parser_state->aml;
138 acpi_native_uint length;
140 ACPI_FUNCTION_TRACE("ps_get_next_package_end");
142 /* Function below changes parser_state->Aml */
145 (acpi_native_uint) acpi_ps_get_next_package_length(parser_state);
147 return_PTR(start + length); /* end of package */
150 /*******************************************************************************
152 * FUNCTION: acpi_ps_get_next_namestring
154 * PARAMETERS: parser_state - Current parser state object
156 * RETURN: Pointer to the start of the name string (pointer points into
159 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
160 * prefix characters. Set parser state to point past the string.
161 * (Name is consumed from the AML.)
163 ******************************************************************************/
165 char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
167 u8 *start = parser_state->aml;
168 u8 *end = parser_state->aml;
170 ACPI_FUNCTION_TRACE("ps_get_next_namestring");
172 /* Handle multiple prefix characters */
174 while (acpi_ps_is_prefix_char(ACPI_GET8(end))) {
175 /* Include prefix '\\' or '^' */
180 /* Decode the path */
182 switch (ACPI_GET8(end)) {
193 case AML_DUAL_NAME_PREFIX:
195 /* Two name segments */
197 end += 1 + (2 * ACPI_NAME_SIZE);
200 case AML_MULTI_NAME_PREFIX_OP:
202 /* Multiple name segments, 4 chars each */
204 end += 2 + ((acpi_size) ACPI_GET8(end + 1) * ACPI_NAME_SIZE);
209 /* Single name segment */
211 end += ACPI_NAME_SIZE;
215 parser_state->aml = (u8 *) end;
216 return_PTR((char *)start);
219 /*******************************************************************************
221 * FUNCTION: acpi_ps_get_next_namepath
223 * PARAMETERS: parser_state - Current parser state object
224 * Arg - Where the namepath will be stored
225 * arg_count - If the namepath points to a control method
226 * the method's argument is returned here.
227 * method_call - Whether the namepath can possibly be the
228 * start of a method call
232 * DESCRIPTION: Get next name (if method call, return # of required args).
233 * Names are looked up in the internal namespace to determine
234 * if the name represents a control method. If a method
235 * is found, the number of arguments to the method is returned.
236 * This information is critical for parsing to continue correctly.
238 ******************************************************************************/
241 acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
242 struct acpi_parse_state *parser_state,
243 union acpi_parse_object *arg, u8 method_call)
246 union acpi_parse_object *name_op;
247 acpi_status status = AE_OK;
248 union acpi_operand_object *method_desc;
249 struct acpi_namespace_node *node;
250 union acpi_generic_state scope_info;
252 ACPI_FUNCTION_TRACE("ps_get_next_namepath");
254 path = acpi_ps_get_next_namestring(parser_state);
256 /* Null path case is allowed */
260 * Lookup the name in the internal namespace
262 scope_info.scope.node = NULL;
263 node = parser_state->start_node;
265 scope_info.scope.node = node;
269 * Lookup object. We don't want to add anything new to the namespace
270 * here, however. So we use MODE_EXECUTE. Allow searching of the
271 * parent tree, but don't open a new scope -- we just want to lookup the
272 * object (MUST BE mode EXECUTE to perform upsearch)
274 status = acpi_ns_lookup(&scope_info, path, ACPI_TYPE_ANY,
276 ACPI_NS_SEARCH_PARENT |
277 ACPI_NS_DONT_OPEN_SCOPE, NULL, &node);
278 if (ACPI_SUCCESS(status) && method_call) {
279 if (node->type == ACPI_TYPE_METHOD) {
280 /* This name is actually a control method invocation */
282 method_desc = acpi_ns_get_attached_object(node);
283 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
284 "Control Method - %p Desc %p Path=%p\n",
285 node, method_desc, path));
287 name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
289 return_ACPI_STATUS(AE_NO_MEMORY);
292 /* Change arg into a METHOD CALL and attach name to it */
294 acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
295 name_op->common.value.name = path;
297 /* Point METHODCALL/NAME to the METHOD Node */
299 name_op->common.node = node;
300 acpi_ps_append_arg(arg, name_op);
303 ACPI_REPORT_ERROR(("ps_get_next_namepath: Control Method %p has no attached object\n", node));
304 return_ACPI_STATUS(AE_AML_INTERNAL);
307 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
308 "Control Method - %p Args %X\n",
313 /* Get the number of arguments to expect */
315 walk_state->arg_count =
316 method_desc->method.param_count;
317 return_ACPI_STATUS(AE_OK);
321 * Else this is normal named object reference.
322 * Just init the NAMEPATH object with the pathname.
327 if (ACPI_FAILURE(status)) {
329 * 1) Any error other than NOT_FOUND is always severe
330 * 2) NOT_FOUND is only important if we are executing a method.
331 * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
334 parse_flags & ACPI_PARSE_MODE_MASK) ==
335 ACPI_PARSE_EXECUTE) && (status == AE_NOT_FOUND)
336 && (walk_state->op->common.aml_opcode !=
338 || (status != AE_NOT_FOUND)) {
339 ACPI_REPORT_NSERROR(path, status);
342 ("search_node %p start_node %p return_node %p\n",
343 scope_info.scope.node,
344 parser_state->start_node, node);
348 * We got a NOT_FOUND during table load or we encountered
349 * a cond_ref_of(x) where the target does not exist.
358 * Regardless of success/failure above,
359 * Just initialize the Op with the pathname.
361 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
362 arg->common.value.name = path;
364 return_ACPI_STATUS(status);
367 /*******************************************************************************
369 * FUNCTION: acpi_ps_get_next_simple_arg
371 * PARAMETERS: parser_state - Current parser state object
372 * arg_type - The argument type (AML_*_ARG)
373 * Arg - Where the argument is returned
377 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
379 ******************************************************************************/
382 acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
383 u32 arg_type, union acpi_parse_object *arg)
386 ACPI_FUNCTION_TRACE_U32("ps_get_next_simple_arg", arg_type);
391 acpi_ps_init_op(arg, AML_BYTE_OP);
392 arg->common.value.integer = (u32) ACPI_GET8(parser_state->aml);
398 acpi_ps_init_op(arg, AML_WORD_OP);
400 /* Get 2 bytes from the AML stream */
402 ACPI_MOVE_16_TO_32(&arg->common.value.integer,
404 parser_state->aml += 2;
409 acpi_ps_init_op(arg, AML_DWORD_OP);
411 /* Get 4 bytes from the AML stream */
413 ACPI_MOVE_32_TO_32(&arg->common.value.integer,
415 parser_state->aml += 4;
420 acpi_ps_init_op(arg, AML_QWORD_OP);
422 /* Get 8 bytes from the AML stream */
424 ACPI_MOVE_64_TO_64(&arg->common.value.integer,
426 parser_state->aml += 8;
431 acpi_ps_init_op(arg, AML_STRING_OP);
432 arg->common.value.string = (char *)parser_state->aml;
434 while (ACPI_GET8(parser_state->aml) != '\0') {
441 case ARGP_NAMESTRING:
443 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
444 arg->common.value.name =
445 acpi_ps_get_next_namestring(parser_state);
450 ACPI_REPORT_ERROR(("Invalid arg_type %X\n", arg_type));
457 /*******************************************************************************
459 * FUNCTION: acpi_ps_get_next_field
461 * PARAMETERS: parser_state - Current parser state object
463 * RETURN: A newly allocated FIELD op
465 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
467 ******************************************************************************/
469 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
472 u32 aml_offset = (u32)
473 ACPI_PTR_DIFF(parser_state->aml,
474 parser_state->aml_start);
475 union acpi_parse_object *field;
479 ACPI_FUNCTION_TRACE("ps_get_next_field");
481 /* Determine field type */
483 switch (ACPI_GET8(parser_state->aml)) {
486 opcode = AML_INT_NAMEDFIELD_OP;
491 opcode = AML_INT_RESERVEDFIELD_OP;
497 opcode = AML_INT_ACCESSFIELD_OP;
502 /* Allocate a new field op */
504 field = acpi_ps_alloc_op(opcode);
509 field->common.aml_offset = aml_offset;
511 /* Decode the field type */
514 case AML_INT_NAMEDFIELD_OP:
516 /* Get the 4-character name */
518 ACPI_MOVE_32_TO_32(&name, parser_state->aml);
519 acpi_ps_set_name(field, name);
520 parser_state->aml += ACPI_NAME_SIZE;
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_RESERVEDFIELD_OP:
530 /* Get the length which is encoded as a package length */
532 field->common.value.size =
533 acpi_ps_get_next_package_length(parser_state);
536 case AML_INT_ACCESSFIELD_OP:
539 * Get access_type and access_attrib and merge into the field Op
540 * access_type is first operand, access_attribute is second
542 field->common.value.integer =
543 (ACPI_GET8(parser_state->aml) << 8);
545 field->common.value.integer |= ACPI_GET8(parser_state->aml);
551 /* Opcode was set in previous switch */
558 /*******************************************************************************
560 * FUNCTION: acpi_ps_get_next_arg
562 * PARAMETERS: walk_state - Current state
563 * parser_state - Current parser state object
564 * arg_type - The argument type (AML_*_ARG)
565 * return_arg - Where the next arg is returned
567 * RETURN: Status, and an op object containing the next argument.
569 * DESCRIPTION: Get next argument (including complex list arguments that require
570 * pushing the parser stack)
572 ******************************************************************************/
575 acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
576 struct acpi_parse_state *parser_state,
577 u32 arg_type, union acpi_parse_object **return_arg)
579 union acpi_parse_object *arg = NULL;
580 union acpi_parse_object *prev = NULL;
581 union acpi_parse_object *field;
583 acpi_status status = AE_OK;
585 ACPI_FUNCTION_TRACE_PTR("ps_get_next_arg", parser_state);
593 case ARGP_NAMESTRING:
595 /* Constants, strings, and namestrings are all the same size */
597 arg = acpi_ps_alloc_op(AML_BYTE_OP);
599 return_ACPI_STATUS(AE_NO_MEMORY);
601 acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
606 /* Package length, nothing returned */
608 parser_state->pkg_end =
609 acpi_ps_get_next_package_end(parser_state);
614 if (parser_state->aml < parser_state->pkg_end) {
617 while (parser_state->aml < parser_state->pkg_end) {
618 field = acpi_ps_get_next_field(parser_state);
620 return_ACPI_STATUS(AE_NO_MEMORY);
624 prev->common.next = field;
631 /* Skip to End of byte data */
633 parser_state->aml = parser_state->pkg_end;
639 if (parser_state->aml < parser_state->pkg_end) {
642 arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP);
644 return_ACPI_STATUS(AE_NO_MEMORY);
647 /* Fill in bytelist data */
649 arg->common.value.size = (u32)
650 ACPI_PTR_DIFF(parser_state->pkg_end,
652 arg->named.data = parser_state->aml;
654 /* Skip to End of byte data */
656 parser_state->aml = parser_state->pkg_end;
662 case ARGP_SIMPLENAME:
664 subop = acpi_ps_peek_opcode(parser_state);
666 acpi_ps_is_leading_char(subop) ||
667 acpi_ps_is_prefix_char(subop)) {
668 /* null_name or name_string */
670 arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
672 return_ACPI_STATUS(AE_NO_MEMORY);
676 acpi_ps_get_next_namepath(walk_state, parser_state,
679 /* Single complex argument, nothing returned */
681 walk_state->arg_count = 1;
688 /* Single complex argument, nothing returned */
690 walk_state->arg_count = 1;
693 case ARGP_DATAOBJLIST:
697 if (parser_state->aml < parser_state->pkg_end) {
698 /* Non-empty list of variable arguments, nothing returned */
700 walk_state->arg_count = ACPI_VAR_ARGS;
706 ACPI_REPORT_ERROR(("Invalid arg_type: %X\n", arg_type));
707 status = AE_AML_OPERAND_TYPE;
712 return_ACPI_STATUS(status);