1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
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/acinterp.h>
47 #include <acpi/amlcode.h>
50 #define _COMPONENT ACPI_EXECUTER
51 ACPI_MODULE_NAME ("exconvrt")
54 /*******************************************************************************
56 * FUNCTION: acpi_ex_convert_to_integer
58 * PARAMETERS: obj_desc - Object to be converted. Must be an
59 * Integer, Buffer, or String
60 * result_desc - Where the new Integer object is returned
61 * Flags - Used for string conversion
65 * DESCRIPTION: Convert an ACPI Object to an integer.
67 ******************************************************************************/
70 acpi_ex_convert_to_integer (
71 union acpi_operand_object
*obj_desc
,
72 union acpi_operand_object
**result_desc
,
75 union acpi_operand_object
*return_desc
;
83 ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_integer", obj_desc
);
86 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
87 case ACPI_TYPE_INTEGER
:
89 /* No conversion necessary */
91 *result_desc
= obj_desc
;
92 return_ACPI_STATUS (AE_OK
);
94 case ACPI_TYPE_BUFFER
:
95 case ACPI_TYPE_STRING
:
97 /* Note: Takes advantage of common buffer/string fields */
99 pointer
= obj_desc
->buffer
.pointer
;
100 count
= obj_desc
->buffer
.length
;
104 return_ACPI_STATUS (AE_TYPE
);
108 * Convert the buffer/string to an integer. Note that both buffers and
109 * strings are treated as raw data - we don't convert ascii to hex for
112 * There are two terminating conditions for the loop:
113 * 1) The size of an integer has been reached, or
114 * 2) The end of the buffer or string has been reached
119 * String conversion is different than Buffer conversion
121 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
122 case ACPI_TYPE_STRING
:
125 * Convert string to an integer - for most cases, the string must be
126 * hexadecimal as per the ACPI specification. The only exception (as
127 * of ACPI 3.0) is that the to_integer() operator allows both decimal
128 * and hexadecimal strings (hex prefixed with "0x").
130 status
= acpi_ut_strtoul64 ((char *) pointer
, flags
, &result
);
131 if (ACPI_FAILURE (status
)) {
132 return_ACPI_STATUS (status
);
137 case ACPI_TYPE_BUFFER
:
139 /* Check for zero-length buffer */
142 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
145 /* Transfer no more than an integer's worth of data */
147 if (count
> acpi_gbl_integer_byte_width
) {
148 count
= acpi_gbl_integer_byte_width
;
152 * Convert buffer to an integer - we simply grab enough raw data
153 * from the buffer to fill an integer
155 for (i
= 0; i
< count
; i
++) {
157 * Get next byte and shift it into the Result.
158 * Little endian is used, meaning that the first byte of the buffer
159 * is the LSB of the integer
161 result
|= (((acpi_integer
) pointer
[i
]) << (i
* 8));
167 /* No other types can get here */
172 * Create a new integer
174 return_desc
= acpi_ut_create_internal_object (ACPI_TYPE_INTEGER
);
176 return_ACPI_STATUS (AE_NO_MEMORY
);
179 /* Save the Result */
181 return_desc
->integer
.value
= result
;
182 acpi_ex_truncate_for32bit_table (return_desc
);
183 *result_desc
= return_desc
;
184 return_ACPI_STATUS (AE_OK
);
188 /*******************************************************************************
190 * FUNCTION: acpi_ex_convert_to_buffer
192 * PARAMETERS: obj_desc - Object to be converted. Must be an
193 * Integer, Buffer, or String
194 * result_desc - Where the new buffer object is returned
198 * DESCRIPTION: Convert an ACPI Object to a Buffer
200 ******************************************************************************/
203 acpi_ex_convert_to_buffer (
204 union acpi_operand_object
*obj_desc
,
205 union acpi_operand_object
**result_desc
)
207 union acpi_operand_object
*return_desc
;
211 ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_buffer", obj_desc
);
214 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
215 case ACPI_TYPE_BUFFER
:
217 /* No conversion necessary */
219 *result_desc
= obj_desc
;
220 return_ACPI_STATUS (AE_OK
);
223 case ACPI_TYPE_INTEGER
:
226 * Create a new Buffer object.
227 * Need enough space for one integer
229 return_desc
= acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width
);
231 return_ACPI_STATUS (AE_NO_MEMORY
);
234 /* Copy the integer to the buffer, LSB first */
236 new_buf
= return_desc
->buffer
.pointer
;
237 ACPI_MEMCPY (new_buf
,
238 &obj_desc
->integer
.value
,
239 acpi_gbl_integer_byte_width
);
243 case ACPI_TYPE_STRING
:
246 * Create a new Buffer object
247 * Size will be the string length
249 * NOTE: Add one to the string length to include the null terminator.
250 * The ACPI spec is unclear on this subject, but there is existing
251 * ASL/AML code that depends on the null being transferred to the new
254 return_desc
= acpi_ut_create_buffer_object ((acpi_size
) obj_desc
->string
.length
+ 1);
256 return_ACPI_STATUS (AE_NO_MEMORY
);
259 /* Copy the string to the buffer */
261 new_buf
= return_desc
->buffer
.pointer
;
262 ACPI_STRNCPY ((char *) new_buf
, (char *) obj_desc
->string
.pointer
,
263 obj_desc
->string
.length
);
268 return_ACPI_STATUS (AE_TYPE
);
271 /* Mark buffer initialized */
273 return_desc
->common
.flags
|= AOPOBJ_DATA_VALID
;
274 *result_desc
= return_desc
;
275 return_ACPI_STATUS (AE_OK
);
279 /*******************************************************************************
281 * FUNCTION: acpi_ex_convert_to_ascii
283 * PARAMETERS: Integer - Value to be converted
284 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
285 * String - Where the string is returned
286 * data_width - Size of data item to be converted, in bytes
288 * RETURN: Actual string length
290 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
292 ******************************************************************************/
295 acpi_ex_convert_to_ascii (
296 acpi_integer integer
,
304 acpi_native_uint k
= 0;
305 acpi_native_uint hex_length
;
306 acpi_native_uint decimal_length
;
311 ACPI_FUNCTION_ENTRY ();
317 /* Setup max length for the decimal number */
319 switch (data_width
) {
321 decimal_length
= ACPI_MAX8_DECIMAL_DIGITS
;
325 decimal_length
= ACPI_MAX32_DECIMAL_DIGITS
;
330 decimal_length
= ACPI_MAX64_DECIMAL_DIGITS
;
334 supress_zeros
= TRUE
; /* No leading zeros */
337 for (i
= decimal_length
; i
> 0; i
--) {
338 /* Divide by nth factor of 10 */
341 for (j
= 0; j
< i
; j
++) {
342 (void) acpi_ut_short_divide (digit
, 10, &digit
, &remainder
);
345 /* Handle leading zeros */
347 if (remainder
!= 0) {
348 supress_zeros
= FALSE
;
351 if (!supress_zeros
) {
352 string
[k
] = (u8
) (ACPI_ASCII_ZERO
+ remainder
);
360 hex_length
= ACPI_MUL_2 (data_width
); /* 2 ascii hex chars per data byte */
362 for (i
= 0, j
= (hex_length
-1); i
< hex_length
; i
++, j
--) {
363 /* Get one hex digit, most significant digits first */
365 string
[k
] = (u8
) acpi_ut_hex_to_ascii_char (integer
, ACPI_MUL_4 (j
));
375 * Since leading zeros are supressed, we must check for the case where
376 * the integer equals 0
378 * Finally, null terminate the string and return the length
381 string
[0] = ACPI_ASCII_ZERO
;
390 /*******************************************************************************
392 * FUNCTION: acpi_ex_convert_to_string
394 * PARAMETERS: obj_desc - Object to be converted. Must be an
395 * Integer, Buffer, or String
396 * result_desc - Where the string object is returned
397 * Type - String flags (base and conversion type)
401 * DESCRIPTION: Convert an ACPI Object to a string
403 ******************************************************************************/
406 acpi_ex_convert_to_string (
407 union acpi_operand_object
*obj_desc
,
408 union acpi_operand_object
**result_desc
,
411 union acpi_operand_object
*return_desc
;
414 u32 string_length
= 0;
419 ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_string", obj_desc
);
422 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
423 case ACPI_TYPE_STRING
:
425 /* No conversion necessary */
427 *result_desc
= obj_desc
;
428 return_ACPI_STATUS (AE_OK
);
431 case ACPI_TYPE_INTEGER
:
434 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
436 /* Make room for maximum decimal number */
438 string_length
= ACPI_MAX_DECIMAL_DIGITS
;
444 /* Two hex string characters for each integer byte */
446 string_length
= ACPI_MUL_2 (acpi_gbl_integer_byte_width
);
451 * Create a new String
452 * Need enough space for one ASCII integer (plus null terminator)
454 return_desc
= acpi_ut_create_string_object ((acpi_size
) string_length
);
456 return_ACPI_STATUS (AE_NO_MEMORY
);
459 new_buf
= return_desc
->buffer
.pointer
;
461 /* Convert integer to string */
463 string_length
= acpi_ex_convert_to_ascii (obj_desc
->integer
.value
, base
,
464 new_buf
, acpi_gbl_integer_byte_width
);
466 /* Null terminate at the correct place */
468 return_desc
->string
.length
= string_length
;
469 new_buf
[string_length
] = 0;
473 case ACPI_TYPE_BUFFER
:
475 /* Setup string length, base, and separator */
478 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by to_decimal_string operator */
480 * From ACPI: "If Data is a buffer, it is converted to a string of
481 * decimal values separated by commas."
486 * Calculate the final string length. Individual string values
487 * are variable length (include separator for each)
489 for (i
= 0; i
< obj_desc
->buffer
.length
; i
++) {
490 if (obj_desc
->buffer
.pointer
[i
] >= 100) {
493 else if (obj_desc
->buffer
.pointer
[i
] >= 10) {
502 case ACPI_IMPLICIT_CONVERT_HEX
:
504 * From the ACPI spec:
505 *"The entire contents of the buffer are converted to a string of
506 * two-character hexadecimal numbers, each separated by a space."
509 string_length
= (obj_desc
->buffer
.length
* 3);
512 case ACPI_EXPLICIT_CONVERT_HEX
: /* Used by to_hex_string operator */
514 * From ACPI: "If Data is a buffer, it is converted to a string of
515 * hexadecimal values separated by commas."
517 string_length
= (obj_desc
->buffer
.length
* 3);
521 return_ACPI_STATUS (AE_BAD_PARAMETER
);
525 * Perform the conversion.
526 * (-1 because of extra separator included in string_length from above)
529 if (string_length
> ACPI_MAX_STRING_CONVERSION
) /* ACPI limit */ {
530 return_ACPI_STATUS (AE_AML_STRING_LIMIT
);
534 * Create a new string object and string buffer
536 return_desc
= acpi_ut_create_string_object ((acpi_size
) string_length
);
538 return_ACPI_STATUS (AE_NO_MEMORY
);
541 new_buf
= return_desc
->buffer
.pointer
;
544 * Convert buffer bytes to hex or decimal values
545 * (separated by commas or spaces)
547 for (i
= 0; i
< obj_desc
->buffer
.length
; i
++) {
548 new_buf
+= acpi_ex_convert_to_ascii (
549 (acpi_integer
) obj_desc
->buffer
.pointer
[i
], base
,
551 *new_buf
++ = separator
; /* each separated by a comma or space */
554 /* Null terminate the string (overwrites final comma/space from above) */
561 return_ACPI_STATUS (AE_TYPE
);
564 *result_desc
= return_desc
;
565 return_ACPI_STATUS (AE_OK
);
569 /*******************************************************************************
571 * FUNCTION: acpi_ex_convert_to_target_type
573 * PARAMETERS: destination_type - Current type of the destination
574 * source_desc - Source object to be converted.
575 * result_desc - Where the converted object is returned
576 * walk_state - Current method state
580 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
582 ******************************************************************************/
585 acpi_ex_convert_to_target_type (
586 acpi_object_type destination_type
,
587 union acpi_operand_object
*source_desc
,
588 union acpi_operand_object
**result_desc
,
589 struct acpi_walk_state
*walk_state
)
591 acpi_status status
= AE_OK
;
594 ACPI_FUNCTION_TRACE ("ex_convert_to_target_type");
597 /* Default behavior */
599 *result_desc
= source_desc
;
602 * If required by the target,
603 * perform implicit conversion on the source before we store it.
605 switch (GET_CURRENT_ARG_TYPE (walk_state
->op_info
->runtime_args
)) {
606 case ARGI_SIMPLE_TARGET
:
607 case ARGI_FIXED_TARGET
:
608 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
610 switch (destination_type
) {
611 case ACPI_TYPE_LOCAL_REGION_FIELD
:
613 * Named field can always handle conversions
618 /* No conversion allowed for these types */
620 if (destination_type
!= ACPI_GET_OBJECT_TYPE (source_desc
)) {
621 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
622 "Explicit operator, will store (%s) over existing type (%s)\n",
623 acpi_ut_get_object_type_name (source_desc
),
624 acpi_ut_get_type_name (destination_type
)));
633 switch (destination_type
) {
634 case ACPI_TYPE_INTEGER
:
635 case ACPI_TYPE_BUFFER_FIELD
:
636 case ACPI_TYPE_LOCAL_BANK_FIELD
:
637 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
639 * These types require an Integer operand. We can convert
640 * a Buffer or a String to an Integer if necessary.
642 status
= acpi_ex_convert_to_integer (source_desc
, result_desc
,
647 case ACPI_TYPE_STRING
:
650 * The operand must be a String. We can convert an
651 * Integer or Buffer if necessary
653 status
= acpi_ex_convert_to_string (source_desc
, result_desc
,
654 ACPI_IMPLICIT_CONVERT_HEX
);
658 case ACPI_TYPE_BUFFER
:
661 * The operand must be a Buffer. We can convert an
662 * Integer or String if necessary
664 status
= acpi_ex_convert_to_buffer (source_desc
, result_desc
);
669 ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
671 status
= AE_AML_INTERNAL
;
679 * create_xxxx_field cases - we are storing the field object into the name
685 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
686 "Unknown Target type ID 0x%X Op %s dest_type %s\n",
687 GET_CURRENT_ARG_TYPE (walk_state
->op_info
->runtime_args
),
688 walk_state
->op_info
->name
, acpi_ut_get_type_name (destination_type
)));
690 ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
691 GET_CURRENT_ARG_TYPE (walk_state
->op_info
->runtime_args
)))
692 status
= AE_AML_INTERNAL
;
696 * Source-to-Target conversion semantics:
698 * If conversion to the target type cannot be performed, then simply
699 * overwrite the target with the new object and type.
701 if (status
== AE_TYPE
) {
705 return_ACPI_STATUS (status
);