1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
117 #define __EXCONVRT_C__
120 #include "accommon.h"
121 #include "acinterp.h"
125 #define _COMPONENT ACPI_EXECUTER
126 ACPI_MODULE_NAME ("exconvrt")
128 /* Local prototypes */
131 AcpiExConvertToAscii (
138 /*******************************************************************************
140 * FUNCTION: AcpiExConvertToInteger
142 * PARAMETERS: ObjDesc - Object to be converted. Must be an
143 * Integer, Buffer, or String
144 * ResultDesc - Where the new Integer object is returned
145 * Flags - Used for string conversion
149 * DESCRIPTION: Convert an ACPI Object to an integer.
151 ******************************************************************************/
154 AcpiExConvertToInteger (
155 ACPI_OPERAND_OBJECT
*ObjDesc
,
156 ACPI_OPERAND_OBJECT
**ResultDesc
,
159 ACPI_OPERAND_OBJECT
*ReturnDesc
;
167 ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger
, ObjDesc
);
170 switch (ObjDesc
->Common
.Type
)
172 case ACPI_TYPE_INTEGER
:
174 /* No conversion necessary */
176 *ResultDesc
= ObjDesc
;
177 return_ACPI_STATUS (AE_OK
);
179 case ACPI_TYPE_BUFFER
:
180 case ACPI_TYPE_STRING
:
182 /* Note: Takes advantage of common buffer/string fields */
184 Pointer
= ObjDesc
->Buffer
.Pointer
;
185 Count
= ObjDesc
->Buffer
.Length
;
189 return_ACPI_STATUS (AE_TYPE
);
193 * Convert the buffer/string to an integer. Note that both buffers and
194 * strings are treated as raw data - we don't convert ascii to hex for
197 * There are two terminating conditions for the loop:
198 * 1) The size of an integer has been reached, or
199 * 2) The end of the buffer or string has been reached
203 /* String conversion is different than Buffer conversion */
205 switch (ObjDesc
->Common
.Type
)
207 case ACPI_TYPE_STRING
:
210 * Convert string to an integer - for most cases, the string must be
211 * hexadecimal as per the ACPI specification. The only exception (as
212 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
213 * and hexadecimal strings (hex prefixed with "0x").
215 Status
= AcpiUtStrtoul64 ((char *) Pointer
, Flags
, &Result
);
216 if (ACPI_FAILURE (Status
))
218 return_ACPI_STATUS (Status
);
223 case ACPI_TYPE_BUFFER
:
225 /* Check for zero-length buffer */
229 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
232 /* Transfer no more than an integer's worth of data */
234 if (Count
> AcpiGbl_IntegerByteWidth
)
236 Count
= AcpiGbl_IntegerByteWidth
;
240 * Convert buffer to an integer - we simply grab enough raw data
241 * from the buffer to fill an integer
243 for (i
= 0; i
< Count
; i
++)
246 * Get next byte and shift it into the Result.
247 * Little endian is used, meaning that the first byte of the buffer
248 * is the LSB of the integer
250 Result
|= (((UINT64
) Pointer
[i
]) << (i
* 8));
257 /* No other types can get here */
261 /* Create a new integer */
263 ReturnDesc
= AcpiUtCreateIntegerObject (Result
);
266 return_ACPI_STATUS (AE_NO_MEMORY
);
269 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
270 ACPI_FORMAT_UINT64 (Result
)));
272 /* Save the Result */
274 AcpiExTruncateFor32bitTable (ReturnDesc
);
275 *ResultDesc
= ReturnDesc
;
276 return_ACPI_STATUS (AE_OK
);
280 /*******************************************************************************
282 * FUNCTION: AcpiExConvertToBuffer
284 * PARAMETERS: ObjDesc - Object to be converted. Must be an
285 * Integer, Buffer, or String
286 * ResultDesc - Where the new buffer object is returned
290 * DESCRIPTION: Convert an ACPI Object to a Buffer
292 ******************************************************************************/
295 AcpiExConvertToBuffer (
296 ACPI_OPERAND_OBJECT
*ObjDesc
,
297 ACPI_OPERAND_OBJECT
**ResultDesc
)
299 ACPI_OPERAND_OBJECT
*ReturnDesc
;
303 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer
, ObjDesc
);
306 switch (ObjDesc
->Common
.Type
)
308 case ACPI_TYPE_BUFFER
:
310 /* No conversion necessary */
312 *ResultDesc
= ObjDesc
;
313 return_ACPI_STATUS (AE_OK
);
316 case ACPI_TYPE_INTEGER
:
319 * Create a new Buffer object.
320 * Need enough space for one integer
322 ReturnDesc
= AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth
);
325 return_ACPI_STATUS (AE_NO_MEMORY
);
328 /* Copy the integer to the buffer, LSB first */
330 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
332 &ObjDesc
->Integer
.Value
,
333 AcpiGbl_IntegerByteWidth
);
337 case ACPI_TYPE_STRING
:
340 * Create a new Buffer object
341 * Size will be the string length
343 * NOTE: Add one to the string length to include the null terminator.
344 * The ACPI spec is unclear on this subject, but there is existing
345 * ASL/AML code that depends on the null being transferred to the new
348 ReturnDesc
= AcpiUtCreateBufferObject (
349 (ACPI_SIZE
) ObjDesc
->String
.Length
+ 1);
352 return_ACPI_STATUS (AE_NO_MEMORY
);
355 /* Copy the string to the buffer */
357 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
358 ACPI_STRNCPY ((char *) NewBuf
, (char *) ObjDesc
->String
.Pointer
,
359 ObjDesc
->String
.Length
);
364 return_ACPI_STATUS (AE_TYPE
);
367 /* Mark buffer initialized */
369 ReturnDesc
->Common
.Flags
|= AOPOBJ_DATA_VALID
;
370 *ResultDesc
= ReturnDesc
;
371 return_ACPI_STATUS (AE_OK
);
375 /*******************************************************************************
377 * FUNCTION: AcpiExConvertToAscii
379 * PARAMETERS: Integer - Value to be converted
380 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
381 * String - Where the string is returned
382 * DataWidth - Size of data item to be converted, in bytes
384 * RETURN: Actual string length
386 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
388 ******************************************************************************/
391 AcpiExConvertToAscii (
402 UINT32 DecimalLength
;
404 BOOLEAN SupressZeros
;
407 ACPI_FUNCTION_ENTRY ();
414 /* Setup max length for the decimal number */
419 DecimalLength
= ACPI_MAX8_DECIMAL_DIGITS
;
423 DecimalLength
= ACPI_MAX32_DECIMAL_DIGITS
;
428 DecimalLength
= ACPI_MAX64_DECIMAL_DIGITS
;
432 SupressZeros
= TRUE
; /* No leading zeros */
435 for (i
= DecimalLength
; i
> 0; i
--)
437 /* Divide by nth factor of 10 */
440 for (j
= 0; j
< i
; j
++)
442 (void) AcpiUtShortDivide (Digit
, 10, &Digit
, &Remainder
);
445 /* Handle leading zeros */
449 SupressZeros
= FALSE
;
454 String
[k
] = (UINT8
) (ACPI_ASCII_ZERO
+ Remainder
);
462 /* HexLength: 2 ascii hex chars per data byte */
464 HexLength
= ACPI_MUL_2 (DataWidth
);
465 for (i
= 0, j
= (HexLength
-1); i
< HexLength
; i
++, j
--)
467 /* Get one hex digit, most significant digits first */
469 String
[k
] = (UINT8
) AcpiUtHexToAsciiChar (Integer
, ACPI_MUL_4 (j
));
479 * Since leading zeros are suppressed, we must check for the case where
480 * the integer equals 0
482 * Finally, null terminate the string and return the length
486 String
[0] = ACPI_ASCII_ZERO
;
495 /*******************************************************************************
497 * FUNCTION: AcpiExConvertToString
499 * PARAMETERS: ObjDesc - Object to be converted. Must be an
500 * Integer, Buffer, or String
501 * ResultDesc - Where the string object is returned
502 * Type - String flags (base and conversion type)
506 * DESCRIPTION: Convert an ACPI Object to a string
508 ******************************************************************************/
511 AcpiExConvertToString (
512 ACPI_OPERAND_OBJECT
*ObjDesc
,
513 ACPI_OPERAND_OBJECT
**ResultDesc
,
516 ACPI_OPERAND_OBJECT
*ReturnDesc
;
519 UINT32 StringLength
= 0;
521 UINT8 Separator
= ',';
524 ACPI_FUNCTION_TRACE_PTR (ExConvertToString
, ObjDesc
);
527 switch (ObjDesc
->Common
.Type
)
529 case ACPI_TYPE_STRING
:
531 /* No conversion necessary */
533 *ResultDesc
= ObjDesc
;
534 return_ACPI_STATUS (AE_OK
);
537 case ACPI_TYPE_INTEGER
:
541 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
543 /* Make room for maximum decimal number */
545 StringLength
= ACPI_MAX_DECIMAL_DIGITS
;
551 /* Two hex string characters for each integer byte */
553 StringLength
= ACPI_MUL_2 (AcpiGbl_IntegerByteWidth
);
558 * Create a new String
559 * Need enough space for one ASCII integer (plus null terminator)
561 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
564 return_ACPI_STATUS (AE_NO_MEMORY
);
567 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
569 /* Convert integer to string */
571 StringLength
= AcpiExConvertToAscii (ObjDesc
->Integer
.Value
, Base
,
572 NewBuf
, AcpiGbl_IntegerByteWidth
);
574 /* Null terminate at the correct place */
576 ReturnDesc
->String
.Length
= StringLength
;
577 NewBuf
[StringLength
] = 0;
581 case ACPI_TYPE_BUFFER
:
583 /* Setup string length, base, and separator */
587 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by ToDecimalString */
589 * From ACPI: "If Data is a buffer, it is converted to a string of
590 * decimal values separated by commas."
595 * Calculate the final string length. Individual string values
596 * are variable length (include separator for each)
598 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
600 if (ObjDesc
->Buffer
.Pointer
[i
] >= 100)
604 else if (ObjDesc
->Buffer
.Pointer
[i
] >= 10)
615 case ACPI_IMPLICIT_CONVERT_HEX
:
617 * From the ACPI spec:
618 *"The entire contents of the buffer are converted to a string of
619 * two-character hexadecimal numbers, each separated by a space."
622 StringLength
= (ObjDesc
->Buffer
.Length
* 3);
625 case ACPI_EXPLICIT_CONVERT_HEX
: /* Used by ToHexString */
627 * From ACPI: "If Data is a buffer, it is converted to a string of
628 * hexadecimal values separated by commas."
630 StringLength
= (ObjDesc
->Buffer
.Length
* 3);
634 return_ACPI_STATUS (AE_BAD_PARAMETER
);
638 * Create a new string object and string buffer
639 * (-1 because of extra separator included in StringLength from above)
640 * Allow creation of zero-length strings from zero-length buffers.
647 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
650 return_ACPI_STATUS (AE_NO_MEMORY
);
653 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
656 * Convert buffer bytes to hex or decimal values
657 * (separated by commas or spaces)
659 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
661 NewBuf
+= AcpiExConvertToAscii (
662 (UINT64
) ObjDesc
->Buffer
.Pointer
[i
], Base
,
664 *NewBuf
++ = Separator
; /* each separated by a comma or space */
668 * Null terminate the string
669 * (overwrites final comma/space from above)
671 if (ObjDesc
->Buffer
.Length
)
679 return_ACPI_STATUS (AE_TYPE
);
682 *ResultDesc
= ReturnDesc
;
683 return_ACPI_STATUS (AE_OK
);
687 /*******************************************************************************
689 * FUNCTION: AcpiExConvertToTargetType
691 * PARAMETERS: DestinationType - Current type of the destination
692 * SourceDesc - Source object to be converted.
693 * ResultDesc - Where the converted object is returned
694 * WalkState - Current method state
698 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
700 ******************************************************************************/
703 AcpiExConvertToTargetType (
704 ACPI_OBJECT_TYPE DestinationType
,
705 ACPI_OPERAND_OBJECT
*SourceDesc
,
706 ACPI_OPERAND_OBJECT
**ResultDesc
,
707 ACPI_WALK_STATE
*WalkState
)
709 ACPI_STATUS Status
= AE_OK
;
712 ACPI_FUNCTION_TRACE (ExConvertToTargetType
);
715 /* Default behavior */
717 *ResultDesc
= SourceDesc
;
720 * If required by the target,
721 * perform implicit conversion on the source before we store it.
723 switch (GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
))
725 case ARGI_SIMPLE_TARGET
:
726 case ARGI_FIXED_TARGET
:
727 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
729 switch (DestinationType
)
731 case ACPI_TYPE_LOCAL_REGION_FIELD
:
733 * Named field can always handle conversions
738 /* No conversion allowed for these types */
740 if (DestinationType
!= SourceDesc
->Common
.Type
)
742 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
743 "Explicit operator, will store (%s) over existing type (%s)\n",
744 AcpiUtGetObjectTypeName (SourceDesc
),
745 AcpiUtGetTypeName (DestinationType
)));
754 switch (DestinationType
)
756 case ACPI_TYPE_INTEGER
:
757 case ACPI_TYPE_BUFFER_FIELD
:
758 case ACPI_TYPE_LOCAL_BANK_FIELD
:
759 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
761 * These types require an Integer operand. We can convert
762 * a Buffer or a String to an Integer if necessary.
764 Status
= AcpiExConvertToInteger (SourceDesc
, ResultDesc
,
769 case ACPI_TYPE_STRING
:
771 * The operand must be a String. We can convert an
772 * Integer or Buffer if necessary
774 Status
= AcpiExConvertToString (SourceDesc
, ResultDesc
,
775 ACPI_IMPLICIT_CONVERT_HEX
);
779 case ACPI_TYPE_BUFFER
:
781 * The operand must be a Buffer. We can convert an
782 * Integer or String if necessary
784 Status
= AcpiExConvertToBuffer (SourceDesc
, ResultDesc
);
789 ACPI_ERROR ((AE_INFO
, "Bad destination type during conversion: 0x%X",
791 Status
= AE_AML_INTERNAL
;
799 * CreateXxxxField cases - we are storing the field object into the name
805 ACPI_ERROR ((AE_INFO
,
806 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
807 GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
),
808 WalkState
->Opcode
, AcpiUtGetTypeName (DestinationType
)));
809 Status
= AE_AML_INTERNAL
;
813 * Source-to-Target conversion semantics:
815 * If conversion to the target type cannot be performed, then simply
816 * overwrite the target with the new object and type.
818 if (Status
== AE_TYPE
)
823 return_ACPI_STATUS (Status
);