1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2004, 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 /* Transfer no more than an integer's worth of data */
219 if (Count > AcpiGbl_IntegerByteWidth)
221 Count = AcpiGbl_IntegerByteWidth;
225 * Convert buffer to an integer - we simply grab enough raw data
226 * from the buffer to fill an integer
228 for (i = 0; i < Count; i++)
231 * Get next byte and shift it into the Result.
232 * Little endian is used, meaning that the first byte of the buffer
233 * is the LSB of the integer
235 Result |= (((ACPI_INTEGER) Pointer[i]) << (i * 8));
241 /* No other types can get here */
246 * Create a new integer
248 ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
251 return_ACPI_STATUS (AE_NO_MEMORY);
254 /* Save the Result */
256 ReturnDesc->Integer.Value = Result;
257 AcpiExTruncateFor32bitTable (ReturnDesc);
258 *ResultDesc = ReturnDesc;
259 return_ACPI_STATUS (AE_OK);
263 /*******************************************************************************
265 * FUNCTION: AcpiExConvertToBuffer
267 * PARAMETERS: ObjDesc - Object to be converted. Must be an
268 * Integer, Buffer, or String
269 * ResultDesc - Where the new buffer object is returned
273 * DESCRIPTION: Convert an ACPI Object to a Buffer
275 ******************************************************************************/
278 AcpiExConvertToBuffer (
279 ACPI_OPERAND_OBJECT *ObjDesc,
280 ACPI_OPERAND_OBJECT **ResultDesc)
282 ACPI_OPERAND_OBJECT *ReturnDesc;
286 ACPI_FUNCTION_TRACE_PTR ("ExConvertToBuffer", ObjDesc);
289 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
291 case ACPI_TYPE_BUFFER:
293 /* No conversion necessary */
295 *ResultDesc = ObjDesc;
296 return_ACPI_STATUS (AE_OK);
299 case ACPI_TYPE_INTEGER:
302 * Create a new Buffer object.
303 * Need enough space for one integer
305 ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
308 return_ACPI_STATUS (AE_NO_MEMORY);
311 /* Copy the integer to the buffer, LSB first */
313 NewBuf = ReturnDesc->Buffer.Pointer;
315 &ObjDesc->Integer.Value,
316 AcpiGbl_IntegerByteWidth);
320 case ACPI_TYPE_STRING:
323 * Create a new Buffer object
324 * Size will be the string length
326 ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE) ObjDesc->String.Length);
329 return_ACPI_STATUS (AE_NO_MEMORY);
332 /* Copy the string to the buffer */
334 NewBuf = ReturnDesc->Buffer.Pointer;
335 ACPI_STRNCPY ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
336 ObjDesc->String.Length);
341 return_ACPI_STATUS (AE_TYPE);
344 /* Mark buffer initialized */
346 ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
347 *ResultDesc = ReturnDesc;
348 return_ACPI_STATUS (AE_OK);
352 /*******************************************************************************
354 * FUNCTION: AcpiExConvertToAscii
356 * PARAMETERS: Integer - Value to be converted
357 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
358 * String - Where the string is returned
359 * DataWidth - Size of data item to be converted, in bytes
361 * RETURN: Actual string length
363 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
365 ******************************************************************************/
368 AcpiExConvertToAscii (
369 ACPI_INTEGER Integer,
377 ACPI_NATIVE_UINT k = 0;
378 ACPI_NATIVE_UINT HexLength;
379 ACPI_NATIVE_UINT DecimalLength;
381 BOOLEAN SupressZeros;
384 ACPI_FUNCTION_ENTRY ();
391 /* Setup max length for the decimal number */
396 DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
400 DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
405 DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
409 SupressZeros = TRUE; /* No leading zeros */
412 for (i = DecimalLength; i > 0; i--)
414 /* Divide by nth factor of 10 */
417 for (j = 0; j < i; j++)
419 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
422 /* Handle leading zeros */
426 SupressZeros = FALSE;
431 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
439 HexLength = ACPI_MUL_2 (DataWidth); /* 2 ascii hex chars per data byte */
441 for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
443 /* Get one hex digit, most significant digits first */
445 String[k] = (UINT8) AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
455 * Since leading zeros are supressed, we must check for the case where
456 * the integer equals 0
458 * Finally, null terminate the string and return the length
462 String [0] = ACPI_ASCII_ZERO;
471 /*******************************************************************************
473 * FUNCTION: AcpiExConvertToString
475 * PARAMETERS: ObjDesc - Object to be converted. Must be an
476 * Integer, Buffer, or String
477 * ResultDesc - Where the string object is returned
478 * Type - String flags (base and conversion type)
482 * DESCRIPTION: Convert an ACPI Object to a string
484 ******************************************************************************/
487 AcpiExConvertToString (
488 ACPI_OPERAND_OBJECT *ObjDesc,
489 ACPI_OPERAND_OBJECT **ResultDesc,
492 ACPI_OPERAND_OBJECT *ReturnDesc;
494 UINT32 StringLength = 0;
497 UINT8 Separator = ',';
500 ACPI_FUNCTION_TRACE_PTR ("ExConvertToString", ObjDesc);
503 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
505 case ACPI_TYPE_STRING:
507 /* No conversion necessary */
509 *ResultDesc = ObjDesc;
510 return_ACPI_STATUS (AE_OK);
513 case ACPI_TYPE_INTEGER:
517 case ACPI_EXPLICIT_CONVERT_DECIMAL:
519 /* Make room for maximum decimal number */
521 StringLength = ACPI_MAX_DECIMAL_DIGITS;
527 /* Two hex string characters for each integer byte */
529 StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
534 * Create a new String
535 * Need enough space for one ASCII integer (plus null terminator)
537 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
540 return_ACPI_STATUS (AE_NO_MEMORY);
543 NewBuf = ReturnDesc->Buffer.Pointer;
545 /* Convert integer to string */
547 StringLength = AcpiExConvertToAscii (ObjDesc->Integer.Value, Base,
548 NewBuf, AcpiGbl_IntegerByteWidth);
550 /* Null terminate at the correct place */
552 ReturnDesc->String.Length = StringLength;
553 NewBuf [StringLength] = 0;
557 case ACPI_TYPE_BUFFER:
561 case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString operator */
563 * From ACPI: "If Data is a buffer, it is converted to a string of
564 * decimal values separated by commas."
567 StringLength = ObjDesc->Buffer.Length; /* 4 chars for each decimal */
569 /*lint -fallthrough */
571 case ACPI_IMPLICIT_CONVERT_HEX:
573 * From the ACPI spec:
574 *"The entire contents of the buffer are converted to a string of
575 * two-character hexadecimal numbers, each separated by a space."
577 if (Type == ACPI_IMPLICIT_CONVERT_HEX)
582 /*lint -fallthrough */
584 case ACPI_EXPLICIT_CONVERT_HEX: /* Used by ToHexString operator */
586 * From ACPI: "If Data is a buffer, it is converted to a string of
587 * hexadecimal values separated by commas."
589 StringLength += (ObjDesc->Buffer.Length * 3);
590 if (StringLength > ACPI_MAX_STRING_CONVERSION) /* ACPI limit */
592 return_ACPI_STATUS (AE_AML_STRING_LIMIT);
595 /* Create a new string object and string buffer */
597 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength -1);
600 return_ACPI_STATUS (AE_NO_MEMORY);
603 NewBuf = ReturnDesc->Buffer.Pointer;
606 * Convert buffer bytes to hex or decimal values
607 * (separated by commas)
609 for (i = 0; i < ObjDesc->Buffer.Length; i++)
611 NewBuf += AcpiExConvertToAscii (
612 (ACPI_INTEGER) ObjDesc->Buffer.Pointer[i], Base,
614 *NewBuf++ = Separator; /* each separated by a comma or space */
617 /* Null terminate the string (overwrites final comma from above) */
622 /* Recalculate length */
624 ReturnDesc->String.Length = (UINT32)
625 ACPI_STRLEN (ReturnDesc->String.Pointer);
629 return_ACPI_STATUS (AE_BAD_PARAMETER);
634 return_ACPI_STATUS (AE_TYPE);
637 *ResultDesc = ReturnDesc;
638 return_ACPI_STATUS (AE_OK);
642 /*******************************************************************************
644 * FUNCTION: AcpiExConvertToTargetType
646 * PARAMETERS: DestinationType - Current type of the destination
647 * SourceDesc - Source object to be converted.
648 * ResultDesc - Where the converted object is returned
649 * WalkState - Current method state
653 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
655 ******************************************************************************/
658 AcpiExConvertToTargetType (
659 ACPI_OBJECT_TYPE DestinationType,
660 ACPI_OPERAND_OBJECT *SourceDesc,
661 ACPI_OPERAND_OBJECT **ResultDesc,
662 ACPI_WALK_STATE *WalkState)
664 ACPI_STATUS Status = AE_OK;
667 ACPI_FUNCTION_TRACE ("ExConvertToTargetType");
670 /* Default behavior */
672 *ResultDesc = SourceDesc;
675 * If required by the target,
676 * perform implicit conversion on the source before we store it.
678 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
680 case ARGI_SIMPLE_TARGET:
681 case ARGI_FIXED_TARGET:
682 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
684 switch (DestinationType)
686 case ACPI_TYPE_LOCAL_REGION_FIELD:
688 * Named field can always handle conversions
693 /* No conversion allowed for these types */
695 if (DestinationType != ACPI_GET_OBJECT_TYPE (SourceDesc))
697 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
698 "Explicit operator, will store (%s) over existing type (%s)\n",
699 AcpiUtGetObjectTypeName (SourceDesc),
700 AcpiUtGetTypeName (DestinationType)));
709 switch (DestinationType)
711 case ACPI_TYPE_INTEGER:
712 case ACPI_TYPE_BUFFER_FIELD:
713 case ACPI_TYPE_LOCAL_BANK_FIELD:
714 case ACPI_TYPE_LOCAL_INDEX_FIELD:
716 * These types require an Integer operand. We can convert
717 * a Buffer or a String to an Integer if necessary.
719 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
724 case ACPI_TYPE_STRING:
727 * The operand must be a String. We can convert an
728 * Integer or Buffer if necessary
730 Status = AcpiExConvertToString (SourceDesc, ResultDesc,
731 ACPI_IMPLICIT_CONVERT_HEX);
735 case ACPI_TYPE_BUFFER:
738 * The operand must be a Buffer. We can convert an
739 * Integer or String if necessary
741 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
746 ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
748 Status = AE_AML_INTERNAL;
756 * CreateXxxxField cases - we are storing the field object into the name
762 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
763 "Unknown Target type ID 0x%X Op %s DestType %s\n",
764 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
765 WalkState->OpInfo->Name, AcpiUtGetTypeName (DestinationType)));
767 ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
768 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs)))
769 Status = AE_AML_INTERNAL;
773 * Source-to-Target conversion semantics:
775 * If conversion to the target type cannot be performed, then simply
776 * overwrite the target with the new object and type.
778 if (Status == AE_TYPE)
783 return_ACPI_STATUS (Status);