1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2015, 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 #include "accommon.h"
118 #include "acinterp.h"
122 #define _COMPONENT ACPI_EXECUTER
123 ACPI_MODULE_NAME ("exconvrt")
125 /* Local prototypes */
128 AcpiExConvertToAscii (
135 /*******************************************************************************
137 * FUNCTION: AcpiExConvertToInteger
139 * PARAMETERS: ObjDesc - Object to be converted. Must be an
140 * Integer, Buffer, or String
141 * ResultDesc - Where the new Integer object is returned
142 * Flags - Used for string conversion
146 * DESCRIPTION: Convert an ACPI Object to an integer.
148 ******************************************************************************/
151 AcpiExConvertToInteger (
152 ACPI_OPERAND_OBJECT
*ObjDesc
,
153 ACPI_OPERAND_OBJECT
**ResultDesc
,
156 ACPI_OPERAND_OBJECT
*ReturnDesc
;
164 ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger
, ObjDesc
);
167 switch (ObjDesc
->Common
.Type
)
169 case ACPI_TYPE_INTEGER
:
171 /* No conversion necessary */
173 *ResultDesc
= ObjDesc
;
174 return_ACPI_STATUS (AE_OK
);
176 case ACPI_TYPE_BUFFER
:
177 case ACPI_TYPE_STRING
:
179 /* Note: Takes advantage of common buffer/string fields */
181 Pointer
= ObjDesc
->Buffer
.Pointer
;
182 Count
= ObjDesc
->Buffer
.Length
;
187 return_ACPI_STATUS (AE_TYPE
);
191 * Convert the buffer/string to an integer. Note that both buffers and
192 * strings are treated as raw data - we don't convert ascii to hex for
195 * There are two terminating conditions for the loop:
196 * 1) The size of an integer has been reached, or
197 * 2) The end of the buffer or string has been reached
201 /* String conversion is different than Buffer conversion */
203 switch (ObjDesc
->Common
.Type
)
205 case ACPI_TYPE_STRING
:
207 * Convert string to an integer - for most cases, the string must be
208 * hexadecimal as per the ACPI specification. The only exception (as
209 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
210 * and hexadecimal strings (hex prefixed with "0x").
212 Status
= AcpiUtStrtoul64 ((char *) Pointer
, Flags
, &Result
);
213 if (ACPI_FAILURE (Status
))
215 return_ACPI_STATUS (Status
);
219 case ACPI_TYPE_BUFFER
:
221 /* Check for zero-length buffer */
225 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
228 /* Transfer no more than an integer's worth of data */
230 if (Count
> AcpiGbl_IntegerByteWidth
)
232 Count
= AcpiGbl_IntegerByteWidth
;
236 * Convert buffer to an integer - we simply grab enough raw data
237 * from the buffer to fill an integer
239 for (i
= 0; i
< Count
; i
++)
242 * Get next byte and shift it into the Result.
243 * Little endian is used, meaning that the first byte of the buffer
244 * is the LSB of the integer
246 Result
|= (((UINT64
) Pointer
[i
]) << (i
* 8));
252 /* No other types can get here */
257 /* Create a new integer */
259 ReturnDesc
= AcpiUtCreateIntegerObject (Result
);
262 return_ACPI_STATUS (AE_NO_MEMORY
);
265 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
266 ACPI_FORMAT_UINT64 (Result
)));
268 /* Save the Result */
270 (void) AcpiExTruncateFor32bitTable (ReturnDesc
);
271 *ResultDesc
= ReturnDesc
;
272 return_ACPI_STATUS (AE_OK
);
276 /*******************************************************************************
278 * FUNCTION: AcpiExConvertToBuffer
280 * PARAMETERS: ObjDesc - Object to be converted. Must be an
281 * Integer, Buffer, or String
282 * ResultDesc - Where the new buffer object is returned
286 * DESCRIPTION: Convert an ACPI Object to a Buffer
288 ******************************************************************************/
291 AcpiExConvertToBuffer (
292 ACPI_OPERAND_OBJECT
*ObjDesc
,
293 ACPI_OPERAND_OBJECT
**ResultDesc
)
295 ACPI_OPERAND_OBJECT
*ReturnDesc
;
299 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer
, ObjDesc
);
302 switch (ObjDesc
->Common
.Type
)
304 case ACPI_TYPE_BUFFER
:
306 /* No conversion necessary */
308 *ResultDesc
= ObjDesc
;
309 return_ACPI_STATUS (AE_OK
);
312 case ACPI_TYPE_INTEGER
:
314 * Create a new Buffer object.
315 * Need enough space for one integer
317 ReturnDesc
= AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth
);
320 return_ACPI_STATUS (AE_NO_MEMORY
);
323 /* Copy the integer to the buffer, LSB first */
325 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
326 memcpy (NewBuf
, &ObjDesc
->Integer
.Value
, AcpiGbl_IntegerByteWidth
);
329 case ACPI_TYPE_STRING
:
331 * Create a new Buffer object
332 * Size will be the string length
334 * NOTE: Add one to the string length to include the null terminator.
335 * The ACPI spec is unclear on this subject, but there is existing
336 * ASL/AML code that depends on the null being transferred to the new
339 ReturnDesc
= AcpiUtCreateBufferObject ((ACPI_SIZE
)
340 ObjDesc
->String
.Length
+ 1);
343 return_ACPI_STATUS (AE_NO_MEMORY
);
346 /* Copy the string to the buffer */
348 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
349 strncpy ((char *) NewBuf
, (char *) ObjDesc
->String
.Pointer
,
350 ObjDesc
->String
.Length
);
355 return_ACPI_STATUS (AE_TYPE
);
358 /* Mark buffer initialized */
360 ReturnDesc
->Common
.Flags
|= AOPOBJ_DATA_VALID
;
361 *ResultDesc
= ReturnDesc
;
362 return_ACPI_STATUS (AE_OK
);
366 /*******************************************************************************
368 * FUNCTION: AcpiExConvertToAscii
370 * PARAMETERS: Integer - Value to be converted
371 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
372 * String - Where the string is returned
373 * DataWidth - Size of data item to be converted, in bytes
375 * RETURN: Actual string length
377 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
379 ******************************************************************************/
382 AcpiExConvertToAscii (
393 UINT32 DecimalLength
;
395 BOOLEAN SupressZeros
;
398 ACPI_FUNCTION_ENTRY ();
405 /* Setup max length for the decimal number */
411 DecimalLength
= ACPI_MAX8_DECIMAL_DIGITS
;
416 DecimalLength
= ACPI_MAX32_DECIMAL_DIGITS
;
422 DecimalLength
= ACPI_MAX64_DECIMAL_DIGITS
;
426 SupressZeros
= TRUE
; /* No leading zeros */
429 for (i
= DecimalLength
; i
> 0; i
--)
431 /* Divide by nth factor of 10 */
434 for (j
= 0; j
< i
; j
++)
436 (void) AcpiUtShortDivide (Digit
, 10, &Digit
, &Remainder
);
439 /* Handle leading zeros */
443 SupressZeros
= FALSE
;
448 String
[k
] = (UINT8
) (ACPI_ASCII_ZERO
+ Remainder
);
456 /* HexLength: 2 ascii hex chars per data byte */
458 HexLength
= ACPI_MUL_2 (DataWidth
);
459 for (i
= 0, j
= (HexLength
-1); i
< HexLength
; i
++, j
--)
461 /* Get one hex digit, most significant digits first */
464 AcpiUtHexToAsciiChar (Integer
, ACPI_MUL_4 (j
));
474 * Since leading zeros are suppressed, we must check for the case where
475 * the integer equals 0
477 * Finally, null terminate the string and return the length
481 String
[0] = ACPI_ASCII_ZERO
;
490 /*******************************************************************************
492 * FUNCTION: AcpiExConvertToString
494 * PARAMETERS: ObjDesc - Object to be converted. Must be an
495 * Integer, Buffer, or String
496 * ResultDesc - Where the string object is returned
497 * Type - String flags (base and conversion type)
501 * DESCRIPTION: Convert an ACPI Object to a string
503 ******************************************************************************/
506 AcpiExConvertToString (
507 ACPI_OPERAND_OBJECT
*ObjDesc
,
508 ACPI_OPERAND_OBJECT
**ResultDesc
,
511 ACPI_OPERAND_OBJECT
*ReturnDesc
;
514 UINT32 StringLength
= 0;
516 UINT8 Separator
= ',';
519 ACPI_FUNCTION_TRACE_PTR (ExConvertToString
, ObjDesc
);
522 switch (ObjDesc
->Common
.Type
)
524 case ACPI_TYPE_STRING
:
526 /* No conversion necessary */
528 *ResultDesc
= ObjDesc
;
529 return_ACPI_STATUS (AE_OK
);
531 case ACPI_TYPE_INTEGER
:
535 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
537 /* Make room for maximum decimal number */
539 StringLength
= ACPI_MAX_DECIMAL_DIGITS
;
545 /* Two hex string characters for each integer byte */
547 StringLength
= ACPI_MUL_2 (AcpiGbl_IntegerByteWidth
);
552 * Create a new String
553 * Need enough space for one ASCII integer (plus null terminator)
555 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
558 return_ACPI_STATUS (AE_NO_MEMORY
);
561 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
563 /* Convert integer to string */
565 StringLength
= AcpiExConvertToAscii (
566 ObjDesc
->Integer
.Value
, Base
, NewBuf
, AcpiGbl_IntegerByteWidth
);
568 /* Null terminate at the correct place */
570 ReturnDesc
->String
.Length
= StringLength
;
571 NewBuf
[StringLength
] = 0;
574 case ACPI_TYPE_BUFFER
:
576 /* Setup string length, base, and separator */
580 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by ToDecimalString */
582 * From ACPI: "If Data is a buffer, it is converted to a string of
583 * decimal values separated by commas."
588 * Calculate the final string length. Individual string values
589 * are variable length (include separator for each)
591 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
593 if (ObjDesc
->Buffer
.Pointer
[i
] >= 100)
597 else if (ObjDesc
->Buffer
.Pointer
[i
] >= 10)
608 case ACPI_IMPLICIT_CONVERT_HEX
:
610 * From the ACPI spec:
611 *"The entire contents of the buffer are converted to a string of
612 * two-character hexadecimal numbers, each separated by a space."
615 StringLength
= (ObjDesc
->Buffer
.Length
* 3);
618 case ACPI_EXPLICIT_CONVERT_HEX
: /* Used by ToHexString */
620 * From ACPI: "If Data is a buffer, it is converted to a string of
621 * hexadecimal values separated by commas."
623 StringLength
= (ObjDesc
->Buffer
.Length
* 3);
627 return_ACPI_STATUS (AE_BAD_PARAMETER
);
631 * Create a new string object and string buffer
632 * (-1 because of extra separator included in StringLength from above)
633 * Allow creation of zero-length strings from zero-length buffers.
640 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
643 return_ACPI_STATUS (AE_NO_MEMORY
);
646 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
649 * Convert buffer bytes to hex or decimal values
650 * (separated by commas or spaces)
652 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
654 NewBuf
+= AcpiExConvertToAscii (
655 (UINT64
) ObjDesc
->Buffer
.Pointer
[i
], Base
, NewBuf
, 1);
656 *NewBuf
++ = Separator
; /* each separated by a comma or space */
660 * Null terminate the string
661 * (overwrites final comma/space from above)
663 if (ObjDesc
->Buffer
.Length
)
672 return_ACPI_STATUS (AE_TYPE
);
675 *ResultDesc
= ReturnDesc
;
676 return_ACPI_STATUS (AE_OK
);
680 /*******************************************************************************
682 * FUNCTION: AcpiExConvertToTargetType
684 * PARAMETERS: DestinationType - Current type of the destination
685 * SourceDesc - Source object to be converted.
686 * ResultDesc - Where the converted object is returned
687 * WalkState - Current method state
691 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
693 ******************************************************************************/
696 AcpiExConvertToTargetType (
697 ACPI_OBJECT_TYPE DestinationType
,
698 ACPI_OPERAND_OBJECT
*SourceDesc
,
699 ACPI_OPERAND_OBJECT
**ResultDesc
,
700 ACPI_WALK_STATE
*WalkState
)
702 ACPI_STATUS Status
= AE_OK
;
705 ACPI_FUNCTION_TRACE (ExConvertToTargetType
);
708 /* Default behavior */
710 *ResultDesc
= SourceDesc
;
713 * If required by the target,
714 * perform implicit conversion on the source before we store it.
716 switch (GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
))
718 case ARGI_SIMPLE_TARGET
:
719 case ARGI_FIXED_TARGET
:
720 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
722 switch (DestinationType
)
724 case ACPI_TYPE_LOCAL_REGION_FIELD
:
726 * Named field can always handle conversions
732 /* No conversion allowed for these types */
734 if (DestinationType
!= SourceDesc
->Common
.Type
)
736 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
737 "Explicit operator, will store (%s) over existing type (%s)\n",
738 AcpiUtGetObjectTypeName (SourceDesc
),
739 AcpiUtGetTypeName (DestinationType
)));
746 case ARGI_STORE_TARGET
:
748 switch (DestinationType
)
750 case ACPI_TYPE_INTEGER
:
751 case ACPI_TYPE_BUFFER_FIELD
:
752 case ACPI_TYPE_LOCAL_BANK_FIELD
:
753 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
755 * These types require an Integer operand. We can convert
756 * a Buffer or a String to an Integer if necessary.
758 Status
= AcpiExConvertToInteger (SourceDesc
, ResultDesc
, 16);
761 case ACPI_TYPE_STRING
:
763 * The operand must be a String. We can convert an
764 * Integer or Buffer if necessary
766 Status
= AcpiExConvertToString (SourceDesc
, ResultDesc
,
767 ACPI_IMPLICIT_CONVERT_HEX
);
770 case ACPI_TYPE_BUFFER
:
772 * The operand must be a Buffer. We can convert an
773 * Integer or String if necessary
775 Status
= AcpiExConvertToBuffer (SourceDesc
, ResultDesc
);
780 ACPI_ERROR ((AE_INFO
,
781 "Bad destination type during conversion: 0x%X",
783 Status
= AE_AML_INTERNAL
;
790 * CreateXxxxField cases - we are storing the field object into the name
796 ACPI_ERROR ((AE_INFO
,
797 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
798 GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
),
799 WalkState
->Opcode
, AcpiUtGetTypeName (DestinationType
)));
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
);