1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #define __EXCONVRT_C__
121 #include "acinterp.h"
125 #define _COMPONENT ACPI_EXECUTER
126 ACPI_MODULE_NAME ("exconvrt")
129 /*******************************************************************************
131 * FUNCTION: AcpiExConvertToInteger
133 * PARAMETERS: ObjDesc - Object to be converted. Must be an
134 * Integer, Buffer, or String
135 * ResultDesc - Where the new Integer object is returned
136 * Flags - Used for string conversion
140 * DESCRIPTION: Convert an ACPI Object to an integer.
142 ******************************************************************************/
145 AcpiExConvertToInteger (
146 ACPI_OPERAND_OBJECT
*ObjDesc
,
147 ACPI_OPERAND_OBJECT
**ResultDesc
,
150 ACPI_OPERAND_OBJECT
*ReturnDesc
;
158 ACPI_FUNCTION_TRACE_PTR ("ExConvertToInteger", ObjDesc
);
161 switch (ACPI_GET_OBJECT_TYPE (ObjDesc
))
163 case ACPI_TYPE_INTEGER
:
165 /* No conversion necessary */
167 *ResultDesc
= ObjDesc
;
168 return_ACPI_STATUS (AE_OK
);
170 case ACPI_TYPE_BUFFER
:
171 case ACPI_TYPE_STRING
:
173 /* Note: Takes advantage of common buffer/string fields */
175 Pointer
= ObjDesc
->Buffer
.Pointer
;
176 Count
= ObjDesc
->Buffer
.Length
;
180 return_ACPI_STATUS (AE_TYPE
);
184 * Convert the buffer/string to an integer. Note that both buffers and
185 * strings are treated as raw data - we don't convert ascii to hex for
188 * There are two terminating conditions for the loop:
189 * 1) The size of an integer has been reached, or
190 * 2) The end of the buffer or string has been reached
195 * String conversion is different than Buffer conversion
197 switch (ACPI_GET_OBJECT_TYPE (ObjDesc
))
199 case ACPI_TYPE_STRING
:
202 * Convert string to an integer - for most cases, the string must be
203 * hexadecimal as per the ACPI specification. The only exception (as
204 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
205 * and hexadecimal strings (hex prefixed with "0x").
207 Status
= AcpiUtStrtoul64 ((char *) Pointer
, Flags
, &Result
);
208 if (ACPI_FAILURE (Status
))
210 return_ACPI_STATUS (Status
);
215 case ACPI_TYPE_BUFFER
:
217 /* Check for zero-length buffer */
221 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
224 /* Transfer no more than an integer's worth of data */
226 if (Count
> AcpiGbl_IntegerByteWidth
)
228 Count
= AcpiGbl_IntegerByteWidth
;
232 * Convert buffer to an integer - we simply grab enough raw data
233 * from the buffer to fill an integer
235 for (i
= 0; i
< Count
; i
++)
238 * Get next byte and shift it into the Result.
239 * Little endian is used, meaning that the first byte of the buffer
240 * is the LSB of the integer
242 Result
|= (((ACPI_INTEGER
) Pointer
[i
]) << (i
* 8));
248 /* No other types can get here */
253 * Create a new integer
255 ReturnDesc
= AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER
);
258 return_ACPI_STATUS (AE_NO_MEMORY
);
261 /* Save the Result */
263 ReturnDesc
->Integer
.Value
= Result
;
264 AcpiExTruncateFor32bitTable (ReturnDesc
);
265 *ResultDesc
= ReturnDesc
;
266 return_ACPI_STATUS (AE_OK
);
270 /*******************************************************************************
272 * FUNCTION: AcpiExConvertToBuffer
274 * PARAMETERS: ObjDesc - Object to be converted. Must be an
275 * Integer, Buffer, or String
276 * ResultDesc - Where the new buffer object is returned
280 * DESCRIPTION: Convert an ACPI Object to a Buffer
282 ******************************************************************************/
285 AcpiExConvertToBuffer (
286 ACPI_OPERAND_OBJECT
*ObjDesc
,
287 ACPI_OPERAND_OBJECT
**ResultDesc
)
289 ACPI_OPERAND_OBJECT
*ReturnDesc
;
293 ACPI_FUNCTION_TRACE_PTR ("ExConvertToBuffer", ObjDesc
);
296 switch (ACPI_GET_OBJECT_TYPE (ObjDesc
))
298 case ACPI_TYPE_BUFFER
:
300 /* No conversion necessary */
302 *ResultDesc
= ObjDesc
;
303 return_ACPI_STATUS (AE_OK
);
306 case ACPI_TYPE_INTEGER
:
309 * Create a new Buffer object.
310 * Need enough space for one integer
312 ReturnDesc
= AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth
);
315 return_ACPI_STATUS (AE_NO_MEMORY
);
318 /* Copy the integer to the buffer, LSB first */
320 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
322 &ObjDesc
->Integer
.Value
,
323 AcpiGbl_IntegerByteWidth
);
327 case ACPI_TYPE_STRING
:
330 * Create a new Buffer object
331 * Size will be the string length
333 * NOTE: Add one to the string length to include the null terminator.
334 * The ACPI spec is unclear on this subject, but there is existing
335 * ASL/AML code that depends on the null being transferred to the new
338 ReturnDesc
= AcpiUtCreateBufferObject ((ACPI_SIZE
) ObjDesc
->String
.Length
+ 1);
341 return_ACPI_STATUS (AE_NO_MEMORY
);
344 /* Copy the string to the buffer */
346 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
347 ACPI_STRNCPY ((char *) NewBuf
, (char *) ObjDesc
->String
.Pointer
,
348 ObjDesc
->String
.Length
);
353 return_ACPI_STATUS (AE_TYPE
);
356 /* Mark buffer initialized */
358 ReturnDesc
->Common
.Flags
|= AOPOBJ_DATA_VALID
;
359 *ResultDesc
= ReturnDesc
;
360 return_ACPI_STATUS (AE_OK
);
364 /*******************************************************************************
366 * FUNCTION: AcpiExConvertToAscii
368 * PARAMETERS: Integer - Value to be converted
369 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
370 * String - Where the string is returned
371 * DataWidth - Size of data item to be converted, in bytes
373 * RETURN: Actual string length
375 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
377 ******************************************************************************/
380 AcpiExConvertToAscii (
381 ACPI_INTEGER Integer
,
389 ACPI_NATIVE_UINT k
= 0;
390 ACPI_NATIVE_UINT HexLength
;
391 ACPI_NATIVE_UINT DecimalLength
;
393 BOOLEAN SupressZeros
;
396 ACPI_FUNCTION_ENTRY ();
403 /* Setup max length for the decimal number */
408 DecimalLength
= ACPI_MAX8_DECIMAL_DIGITS
;
412 DecimalLength
= ACPI_MAX32_DECIMAL_DIGITS
;
417 DecimalLength
= ACPI_MAX64_DECIMAL_DIGITS
;
421 SupressZeros
= TRUE
; /* No leading zeros */
424 for (i
= DecimalLength
; i
> 0; i
--)
426 /* Divide by nth factor of 10 */
429 for (j
= 0; j
< i
; j
++)
431 (void) AcpiUtShortDivide (Digit
, 10, &Digit
, &Remainder
);
434 /* Handle leading zeros */
438 SupressZeros
= FALSE
;
443 String
[k
] = (UINT8
) (ACPI_ASCII_ZERO
+ Remainder
);
451 HexLength
= ACPI_MUL_2 (DataWidth
); /* 2 ascii hex chars per data byte */
453 for (i
= 0, j
= (HexLength
-1); i
< HexLength
; i
++, j
--)
455 /* Get one hex digit, most significant digits first */
457 String
[k
] = (UINT8
) AcpiUtHexToAsciiChar (Integer
, ACPI_MUL_4 (j
));
467 * Since leading zeros are supressed, we must check for the case where
468 * the integer equals 0
470 * Finally, null terminate the string and return the length
474 String
[0] = ACPI_ASCII_ZERO
;
483 /*******************************************************************************
485 * FUNCTION: AcpiExConvertToString
487 * PARAMETERS: ObjDesc - Object to be converted. Must be an
488 * Integer, Buffer, or String
489 * ResultDesc - Where the string object is returned
490 * Type - String flags (base and conversion type)
494 * DESCRIPTION: Convert an ACPI Object to a string
496 ******************************************************************************/
499 AcpiExConvertToString (
500 ACPI_OPERAND_OBJECT
*ObjDesc
,
501 ACPI_OPERAND_OBJECT
**ResultDesc
,
504 ACPI_OPERAND_OBJECT
*ReturnDesc
;
507 UINT32 StringLength
= 0;
509 UINT8 Separator
= ',';
512 ACPI_FUNCTION_TRACE_PTR ("ExConvertToString", ObjDesc
);
515 switch (ACPI_GET_OBJECT_TYPE (ObjDesc
))
517 case ACPI_TYPE_STRING
:
519 /* No conversion necessary */
521 *ResultDesc
= ObjDesc
;
522 return_ACPI_STATUS (AE_OK
);
525 case ACPI_TYPE_INTEGER
:
529 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
531 /* Make room for maximum decimal number */
533 StringLength
= ACPI_MAX_DECIMAL_DIGITS
;
539 /* Two hex string characters for each integer byte */
541 StringLength
= ACPI_MUL_2 (AcpiGbl_IntegerByteWidth
);
546 * Create a new String
547 * Need enough space for one ASCII integer (plus null terminator)
549 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
552 return_ACPI_STATUS (AE_NO_MEMORY
);
555 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
557 /* Convert integer to string */
559 StringLength
= AcpiExConvertToAscii (ObjDesc
->Integer
.Value
, Base
,
560 NewBuf
, AcpiGbl_IntegerByteWidth
);
562 /* Null terminate at the correct place */
564 ReturnDesc
->String
.Length
= StringLength
;
565 NewBuf
[StringLength
] = 0;
569 case ACPI_TYPE_BUFFER
:
571 /* Setup string length, base, and separator */
575 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by ToDecimalString operator */
577 * From ACPI: "If Data is a buffer, it is converted to a string of
578 * decimal values separated by commas."
583 * Calculate the final string length. Individual string values
584 * are variable length (include separator for each)
586 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
588 if (ObjDesc
->Buffer
.Pointer
[i
] >= 100)
592 else if (ObjDesc
->Buffer
.Pointer
[i
] >= 10)
603 case ACPI_IMPLICIT_CONVERT_HEX
:
605 * From the ACPI spec:
606 *"The entire contents of the buffer are converted to a string of
607 * two-character hexadecimal numbers, each separated by a space."
610 StringLength
= (ObjDesc
->Buffer
.Length
* 3);
613 case ACPI_EXPLICIT_CONVERT_HEX
: /* Used by ToHexString operator */
615 * From ACPI: "If Data is a buffer, it is converted to a string of
616 * hexadecimal values separated by commas."
618 StringLength
= (ObjDesc
->Buffer
.Length
* 3);
622 return_ACPI_STATUS (AE_BAD_PARAMETER
);
626 * Perform the conversion.
627 * (-1 because of extra separator included in StringLength from above)
630 if (StringLength
> ACPI_MAX_STRING_CONVERSION
) /* ACPI limit */
632 return_ACPI_STATUS (AE_AML_STRING_LIMIT
);
636 * Create a new string object and string buffer
638 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
641 return_ACPI_STATUS (AE_NO_MEMORY
);
644 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
647 * Convert buffer bytes to hex or decimal values
648 * (separated by commas or spaces)
650 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
652 NewBuf
+= AcpiExConvertToAscii (
653 (ACPI_INTEGER
) ObjDesc
->Buffer
.Pointer
[i
], Base
,
655 *NewBuf
++ = Separator
; /* each separated by a comma or space */
658 /* Null terminate the string (overwrites final comma/space from above) */
665 return_ACPI_STATUS (AE_TYPE
);
668 *ResultDesc
= ReturnDesc
;
669 return_ACPI_STATUS (AE_OK
);
673 /*******************************************************************************
675 * FUNCTION: AcpiExConvertToTargetType
677 * PARAMETERS: DestinationType - Current type of the destination
678 * SourceDesc - Source object to be converted.
679 * ResultDesc - Where the converted object is returned
680 * WalkState - Current method state
684 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
686 ******************************************************************************/
689 AcpiExConvertToTargetType (
690 ACPI_OBJECT_TYPE DestinationType
,
691 ACPI_OPERAND_OBJECT
*SourceDesc
,
692 ACPI_OPERAND_OBJECT
**ResultDesc
,
693 ACPI_WALK_STATE
*WalkState
)
695 ACPI_STATUS Status
= AE_OK
;
698 ACPI_FUNCTION_TRACE ("ExConvertToTargetType");
701 /* Default behavior */
703 *ResultDesc
= SourceDesc
;
706 * If required by the target,
707 * perform implicit conversion on the source before we store it.
709 switch (GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
))
711 case ARGI_SIMPLE_TARGET
:
712 case ARGI_FIXED_TARGET
:
713 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
715 switch (DestinationType
)
717 case ACPI_TYPE_LOCAL_REGION_FIELD
:
719 * Named field can always handle conversions
724 /* No conversion allowed for these types */
726 if (DestinationType
!= ACPI_GET_OBJECT_TYPE (SourceDesc
))
728 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
729 "Explicit operator, will store (%s) over existing type (%s)\n",
730 AcpiUtGetObjectTypeName (SourceDesc
),
731 AcpiUtGetTypeName (DestinationType
)));
740 switch (DestinationType
)
742 case ACPI_TYPE_INTEGER
:
743 case ACPI_TYPE_BUFFER_FIELD
:
744 case ACPI_TYPE_LOCAL_BANK_FIELD
:
745 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
747 * These types require an Integer operand. We can convert
748 * a Buffer or a String to an Integer if necessary.
750 Status
= AcpiExConvertToInteger (SourceDesc
, ResultDesc
,
755 case ACPI_TYPE_STRING
:
758 * The operand must be a String. We can convert an
759 * Integer or Buffer if necessary
761 Status
= AcpiExConvertToString (SourceDesc
, ResultDesc
,
762 ACPI_IMPLICIT_CONVERT_HEX
);
766 case ACPI_TYPE_BUFFER
:
769 * The operand must be a Buffer. We can convert an
770 * Integer or String if necessary
772 Status
= AcpiExConvertToBuffer (SourceDesc
, ResultDesc
);
777 ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
779 Status
= AE_AML_INTERNAL
;
787 * CreateXxxxField cases - we are storing the field object into the name
793 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
794 "Unknown Target type ID 0x%X Op %s DestType %s\n",
795 GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
),
796 WalkState
->OpInfo
->Name
, AcpiUtGetTypeName (DestinationType
)));
798 ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
799 GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
)))
800 Status
= AE_AML_INTERNAL
;
804 * Source-to-Target conversion semantics:
806 * If conversion to the target type cannot be performed, then simply
807 * overwrite the target with the new object and type.
809 if (Status
== AE_TYPE
)
814 return_ACPI_STATUS (Status
);