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")
53 /* Local prototypes */
56 acpi_ex_convert_to_ascii (
63 /*******************************************************************************
65 * FUNCTION: acpi_ex_convert_to_integer
67 * PARAMETERS: obj_desc - Object to be converted. Must be an
68 * Integer, Buffer, or String
69 * result_desc - Where the new Integer object is returned
70 * Flags - Used for string conversion
74 * DESCRIPTION: Convert an ACPI Object to an integer.
76 ******************************************************************************/
79 acpi_ex_convert_to_integer (
80 union acpi_operand_object
*obj_desc
,
81 union acpi_operand_object
**result_desc
,
84 union acpi_operand_object
*return_desc
;
92 ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_integer", obj_desc
);
95 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
96 case ACPI_TYPE_INTEGER
:
98 /* No conversion necessary */
100 *result_desc
= obj_desc
;
101 return_ACPI_STATUS (AE_OK
);
103 case ACPI_TYPE_BUFFER
:
104 case ACPI_TYPE_STRING
:
106 /* Note: Takes advantage of common buffer/string fields */
108 pointer
= obj_desc
->buffer
.pointer
;
109 count
= obj_desc
->buffer
.length
;
113 return_ACPI_STATUS (AE_TYPE
);
117 * Convert the buffer/string to an integer. Note that both buffers and
118 * strings are treated as raw data - we don't convert ascii to hex for
121 * There are two terminating conditions for the loop:
122 * 1) The size of an integer has been reached, or
123 * 2) The end of the buffer or string has been reached
127 /* String conversion is different than Buffer conversion */
129 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
130 case ACPI_TYPE_STRING
:
133 * Convert string to an integer - for most cases, the string must be
134 * hexadecimal as per the ACPI specification. The only exception (as
135 * of ACPI 3.0) is that the to_integer() operator allows both decimal
136 * and hexadecimal strings (hex prefixed with "0x").
138 status
= acpi_ut_strtoul64 ((char *) pointer
, flags
, &result
);
139 if (ACPI_FAILURE (status
)) {
140 return_ACPI_STATUS (status
);
145 case ACPI_TYPE_BUFFER
:
147 /* Check for zero-length buffer */
150 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
153 /* Transfer no more than an integer's worth of data */
155 if (count
> acpi_gbl_integer_byte_width
) {
156 count
= acpi_gbl_integer_byte_width
;
160 * Convert buffer to an integer - we simply grab enough raw data
161 * from the buffer to fill an integer
163 for (i
= 0; i
< count
; i
++) {
165 * Get next byte and shift it into the Result.
166 * Little endian is used, meaning that the first byte of the buffer
167 * is the LSB of the integer
169 result
|= (((acpi_integer
) pointer
[i
]) << (i
* 8));
175 /* No other types can get here */
179 /* Create a new integer */
181 return_desc
= acpi_ut_create_internal_object (ACPI_TYPE_INTEGER
);
183 return_ACPI_STATUS (AE_NO_MEMORY
);
186 /* Save the Result */
188 return_desc
->integer
.value
= result
;
189 acpi_ex_truncate_for32bit_table (return_desc
);
190 *result_desc
= return_desc
;
191 return_ACPI_STATUS (AE_OK
);
195 /*******************************************************************************
197 * FUNCTION: acpi_ex_convert_to_buffer
199 * PARAMETERS: obj_desc - Object to be converted. Must be an
200 * Integer, Buffer, or String
201 * result_desc - Where the new buffer object is returned
205 * DESCRIPTION: Convert an ACPI Object to a Buffer
207 ******************************************************************************/
210 acpi_ex_convert_to_buffer (
211 union acpi_operand_object
*obj_desc
,
212 union acpi_operand_object
**result_desc
)
214 union acpi_operand_object
*return_desc
;
218 ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_buffer", obj_desc
);
221 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
222 case ACPI_TYPE_BUFFER
:
224 /* No conversion necessary */
226 *result_desc
= obj_desc
;
227 return_ACPI_STATUS (AE_OK
);
230 case ACPI_TYPE_INTEGER
:
233 * Create a new Buffer object.
234 * Need enough space for one integer
236 return_desc
= acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width
);
238 return_ACPI_STATUS (AE_NO_MEMORY
);
241 /* Copy the integer to the buffer, LSB first */
243 new_buf
= return_desc
->buffer
.pointer
;
244 ACPI_MEMCPY (new_buf
,
245 &obj_desc
->integer
.value
,
246 acpi_gbl_integer_byte_width
);
250 case ACPI_TYPE_STRING
:
253 * Create a new Buffer object
254 * Size will be the string length
256 * NOTE: Add one to the string length to include the null terminator.
257 * The ACPI spec is unclear on this subject, but there is existing
258 * ASL/AML code that depends on the null being transferred to the new
261 return_desc
= acpi_ut_create_buffer_object (
262 (acpi_size
) obj_desc
->string
.length
+ 1);
264 return_ACPI_STATUS (AE_NO_MEMORY
);
267 /* Copy the string to the buffer */
269 new_buf
= return_desc
->buffer
.pointer
;
270 ACPI_STRNCPY ((char *) new_buf
, (char *) obj_desc
->string
.pointer
,
271 obj_desc
->string
.length
);
276 return_ACPI_STATUS (AE_TYPE
);
279 /* Mark buffer initialized */
281 return_desc
->common
.flags
|= AOPOBJ_DATA_VALID
;
282 *result_desc
= return_desc
;
283 return_ACPI_STATUS (AE_OK
);
287 /*******************************************************************************
289 * FUNCTION: acpi_ex_convert_to_ascii
291 * PARAMETERS: Integer - Value to be converted
292 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
293 * String - Where the string is returned
294 * data_width - Size of data item to be converted, in bytes
296 * RETURN: Actual string length
298 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
300 ******************************************************************************/
303 acpi_ex_convert_to_ascii (
304 acpi_integer integer
,
312 acpi_native_uint k
= 0;
313 acpi_native_uint hex_length
;
314 acpi_native_uint decimal_length
;
319 ACPI_FUNCTION_ENTRY ();
325 /* Setup max length for the decimal number */
327 switch (data_width
) {
329 decimal_length
= ACPI_MAX8_DECIMAL_DIGITS
;
333 decimal_length
= ACPI_MAX32_DECIMAL_DIGITS
;
338 decimal_length
= ACPI_MAX64_DECIMAL_DIGITS
;
342 supress_zeros
= TRUE
; /* No leading zeros */
345 for (i
= decimal_length
; i
> 0; i
--) {
346 /* Divide by nth factor of 10 */
349 for (j
= 0; j
< i
; j
++) {
350 (void) acpi_ut_short_divide (digit
, 10, &digit
, &remainder
);
353 /* Handle leading zeros */
355 if (remainder
!= 0) {
356 supress_zeros
= FALSE
;
359 if (!supress_zeros
) {
360 string
[k
] = (u8
) (ACPI_ASCII_ZERO
+ remainder
);
368 /* hex_length: 2 ascii hex chars per data byte */
370 hex_length
= ACPI_MUL_2 (data_width
);
371 for (i
= 0, j
= (hex_length
-1); i
< hex_length
; i
++, j
--) {
372 /* Get one hex digit, most significant digits first */
374 string
[k
] = (u8
) acpi_ut_hex_to_ascii_char (integer
, ACPI_MUL_4 (j
));
384 * Since leading zeros are supressed, we must check for the case where
385 * the integer equals 0
387 * Finally, null terminate the string and return the length
390 string
[0] = ACPI_ASCII_ZERO
;
399 /*******************************************************************************
401 * FUNCTION: acpi_ex_convert_to_string
403 * PARAMETERS: obj_desc - Object to be converted. Must be an
404 * Integer, Buffer, or String
405 * result_desc - Where the string object is returned
406 * Type - String flags (base and conversion type)
410 * DESCRIPTION: Convert an ACPI Object to a string
412 ******************************************************************************/
415 acpi_ex_convert_to_string (
416 union acpi_operand_object
*obj_desc
,
417 union acpi_operand_object
**result_desc
,
420 union acpi_operand_object
*return_desc
;
423 u32 string_length
= 0;
428 ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_string", obj_desc
);
431 switch (ACPI_GET_OBJECT_TYPE (obj_desc
)) {
432 case ACPI_TYPE_STRING
:
434 /* No conversion necessary */
436 *result_desc
= obj_desc
;
437 return_ACPI_STATUS (AE_OK
);
440 case ACPI_TYPE_INTEGER
:
443 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
445 /* Make room for maximum decimal number */
447 string_length
= ACPI_MAX_DECIMAL_DIGITS
;
453 /* Two hex string characters for each integer byte */
455 string_length
= ACPI_MUL_2 (acpi_gbl_integer_byte_width
);
460 * Create a new String
461 * Need enough space for one ASCII integer (plus null terminator)
463 return_desc
= acpi_ut_create_string_object ((acpi_size
) string_length
);
465 return_ACPI_STATUS (AE_NO_MEMORY
);
468 new_buf
= return_desc
->buffer
.pointer
;
470 /* Convert integer to string */
472 string_length
= acpi_ex_convert_to_ascii (obj_desc
->integer
.value
, base
,
473 new_buf
, acpi_gbl_integer_byte_width
);
475 /* Null terminate at the correct place */
477 return_desc
->string
.length
= string_length
;
478 new_buf
[string_length
] = 0;
482 case ACPI_TYPE_BUFFER
:
484 /* Setup string length, base, and separator */
487 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by to_decimal_string */
489 * From ACPI: "If Data is a buffer, it is converted to a string of
490 * decimal values separated by commas."
495 * Calculate the final string length. Individual string values
496 * are variable length (include separator for each)
498 for (i
= 0; i
< obj_desc
->buffer
.length
; i
++) {
499 if (obj_desc
->buffer
.pointer
[i
] >= 100) {
502 else if (obj_desc
->buffer
.pointer
[i
] >= 10) {
511 case ACPI_IMPLICIT_CONVERT_HEX
:
513 * From the ACPI spec:
514 *"The entire contents of the buffer are converted to a string of
515 * two-character hexadecimal numbers, each separated by a space."
518 string_length
= (obj_desc
->buffer
.length
* 3);
521 case ACPI_EXPLICIT_CONVERT_HEX
: /* Used by to_hex_string */
523 * From ACPI: "If Data is a buffer, it is converted to a string of
524 * hexadecimal values separated by commas."
526 string_length
= (obj_desc
->buffer
.length
* 3);
530 return_ACPI_STATUS (AE_BAD_PARAMETER
);
534 * Perform the conversion.
535 * (-1 because of extra separator included in string_length from above)
538 if (string_length
> ACPI_MAX_STRING_CONVERSION
) /* ACPI limit */ {
539 return_ACPI_STATUS (AE_AML_STRING_LIMIT
);
542 /* Create a new string object and string buffer */
544 return_desc
= acpi_ut_create_string_object ((acpi_size
) string_length
);
546 return_ACPI_STATUS (AE_NO_MEMORY
);
549 new_buf
= return_desc
->buffer
.pointer
;
552 * Convert buffer bytes to hex or decimal values
553 * (separated by commas or spaces)
555 for (i
= 0; i
< obj_desc
->buffer
.length
; i
++) {
556 new_buf
+= acpi_ex_convert_to_ascii (
557 (acpi_integer
) obj_desc
->buffer
.pointer
[i
], base
,
559 *new_buf
++ = separator
; /* each separated by a comma or space */
563 * Null terminate the string
564 * (overwrites final comma/space from above)
571 return_ACPI_STATUS (AE_TYPE
);
574 *result_desc
= return_desc
;
575 return_ACPI_STATUS (AE_OK
);
579 /*******************************************************************************
581 * FUNCTION: acpi_ex_convert_to_target_type
583 * PARAMETERS: destination_type - Current type of the destination
584 * source_desc - Source object to be converted.
585 * result_desc - Where the converted object is returned
586 * walk_state - Current method state
590 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
592 ******************************************************************************/
595 acpi_ex_convert_to_target_type (
596 acpi_object_type destination_type
,
597 union acpi_operand_object
*source_desc
,
598 union acpi_operand_object
**result_desc
,
599 struct acpi_walk_state
*walk_state
)
601 acpi_status status
= AE_OK
;
604 ACPI_FUNCTION_TRACE ("ex_convert_to_target_type");
607 /* Default behavior */
609 *result_desc
= source_desc
;
612 * If required by the target,
613 * perform implicit conversion on the source before we store it.
615 switch (GET_CURRENT_ARG_TYPE (walk_state
->op_info
->runtime_args
)) {
616 case ARGI_SIMPLE_TARGET
:
617 case ARGI_FIXED_TARGET
:
618 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
620 switch (destination_type
) {
621 case ACPI_TYPE_LOCAL_REGION_FIELD
:
623 * Named field can always handle conversions
628 /* No conversion allowed for these types */
630 if (destination_type
!= ACPI_GET_OBJECT_TYPE (source_desc
)) {
631 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
632 "Explicit operator, will store (%s) over existing type (%s)\n",
633 acpi_ut_get_object_type_name (source_desc
),
634 acpi_ut_get_type_name (destination_type
)));
643 switch (destination_type
) {
644 case ACPI_TYPE_INTEGER
:
645 case ACPI_TYPE_BUFFER_FIELD
:
646 case ACPI_TYPE_LOCAL_BANK_FIELD
:
647 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
649 * These types require an Integer operand. We can convert
650 * a Buffer or a String to an Integer if necessary.
652 status
= acpi_ex_convert_to_integer (source_desc
, result_desc
,
657 case ACPI_TYPE_STRING
:
659 * The operand must be a String. We can convert an
660 * Integer or Buffer if necessary
662 status
= acpi_ex_convert_to_string (source_desc
, result_desc
,
663 ACPI_IMPLICIT_CONVERT_HEX
);
667 case ACPI_TYPE_BUFFER
:
669 * The operand must be a Buffer. We can convert an
670 * Integer or String if necessary
672 status
= acpi_ex_convert_to_buffer (source_desc
, result_desc
);
677 ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
679 status
= AE_AML_INTERNAL
;
687 * create_xxxx_field cases - we are storing the field object into the name
693 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
694 "Unknown Target type ID 0x%X Op %s dest_type %s\n",
695 GET_CURRENT_ARG_TYPE (walk_state
->op_info
->runtime_args
),
696 walk_state
->op_info
->name
, acpi_ut_get_type_name (destination_type
)));
698 ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
699 GET_CURRENT_ARG_TYPE (walk_state
->op_info
->runtime_args
)))
700 status
= AE_AML_INTERNAL
;
704 * Source-to-Target conversion semantics:
706 * If conversion to the target type cannot be performed, then simply
707 * overwrite the target with the new object and type.
709 if (status
== AE_TYPE
) {
713 return_ACPI_STATUS (status
);