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 * WalkState - Current method state
140 * DESCRIPTION: Convert an ACPI Object to an integer.
142 ******************************************************************************/
145 AcpiExConvertToInteger (
146 ACPI_OPERAND_OBJECT *ObjDesc,
147 ACPI_OPERAND_OBJECT **ResultDesc,
148 ACPI_WALK_STATE *WalkState)
151 ACPI_OPERAND_OBJECT *RetDesc;
158 ACPI_FUNCTION_TRACE_PTR ("ExConvertToInteger", ObjDesc);
161 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
163 case ACPI_TYPE_INTEGER:
164 *ResultDesc = ObjDesc;
165 return_ACPI_STATUS (AE_OK);
167 case ACPI_TYPE_STRING:
168 Pointer = (UINT8 *) ObjDesc->String.Pointer;
169 Count = ObjDesc->String.Length;
172 case ACPI_TYPE_BUFFER:
173 Pointer = ObjDesc->Buffer.Pointer;
174 Count = ObjDesc->Buffer.Length;
178 return_ACPI_STATUS (AE_TYPE);
182 * Convert the buffer/string to an integer. Note that both buffers and
183 * strings are treated as raw data - we don't convert ascii to hex for
186 * There are two terminating conditions for the loop:
187 * 1) The size of an integer has been reached, or
188 * 2) The end of the buffer or string has been reached
192 /* Transfer no more than an integer's worth of data */
194 if (Count > AcpiGbl_IntegerByteWidth)
196 Count = AcpiGbl_IntegerByteWidth;
200 * String conversion is different than Buffer conversion
202 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
204 case ACPI_TYPE_STRING:
207 * Convert string to an integer
208 * String must be hexadecimal as per the ACPI specification
210 Status = AcpiUtStrtoul64 ((char *) Pointer, 16, &Result);
211 if (ACPI_FAILURE (Status))
213 return_ACPI_STATUS (Status);
218 case ACPI_TYPE_BUFFER:
221 * Buffer conversion - we simply grab enough raw data from the
222 * buffer to fill an integer
224 for (i = 0; i < Count; i++)
227 * Get next byte and shift it into the Result.
228 * Little endian is used, meaning that the first byte of the buffer
229 * is the LSB of the integer
231 Result |= (((ACPI_INTEGER) Pointer[i]) << (i * 8));
237 /* No other types can get here */
242 * Create a new integer
244 RetDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
247 return_ACPI_STATUS (AE_NO_MEMORY);
250 /* Save the Result */
252 RetDesc->Integer.Value = Result;
255 * If we are about to overwrite the original object on the operand stack,
256 * we must remove a reference on the original object because we are
257 * essentially removing it from the stack.
259 if (*ResultDesc == ObjDesc)
261 if (WalkState->Opcode != AML_STORE_OP)
263 AcpiUtRemoveReference (ObjDesc);
267 *ResultDesc = RetDesc;
268 return_ACPI_STATUS (AE_OK);
272 /*******************************************************************************
274 * FUNCTION: AcpiExConvertToBuffer
276 * PARAMETERS: ObjDesc - Object to be converted. Must be an
277 * Integer, Buffer, or String
278 * ResultDesc - Where the new buffer object is returned
279 * WalkState - Current method state
283 * DESCRIPTION: Convert an ACPI Object to a Buffer
285 ******************************************************************************/
288 AcpiExConvertToBuffer (
289 ACPI_OPERAND_OBJECT *ObjDesc,
290 ACPI_OPERAND_OBJECT **ResultDesc,
291 ACPI_WALK_STATE *WalkState)
293 ACPI_OPERAND_OBJECT *RetDesc;
298 ACPI_FUNCTION_TRACE_PTR ("ExConvertToBuffer", ObjDesc);
301 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
303 case ACPI_TYPE_BUFFER:
305 /* No conversion necessary */
307 *ResultDesc = ObjDesc;
308 return_ACPI_STATUS (AE_OK);
311 case ACPI_TYPE_INTEGER:
314 * Create a new Buffer object.
315 * Need enough space for one integer
317 RetDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
320 return_ACPI_STATUS (AE_NO_MEMORY);
323 /* Copy the integer to the buffer */
325 NewBuf = RetDesc->Buffer.Pointer;
326 for (i = 0; i < AcpiGbl_IntegerByteWidth; i++)
328 NewBuf[i] = (UINT8) (ObjDesc->Integer.Value >> (i * 8));
333 case ACPI_TYPE_STRING:
336 * Create a new Buffer object
337 * Size will be the string length
339 RetDesc = AcpiUtCreateBufferObject ((ACPI_SIZE) ObjDesc->String.Length);
342 return_ACPI_STATUS (AE_NO_MEMORY);
345 /* Copy the string to the buffer */
347 NewBuf = RetDesc->Buffer.Pointer;
348 ACPI_STRNCPY ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
349 ObjDesc->String.Length);
354 return_ACPI_STATUS (AE_TYPE);
357 /* Mark buffer initialized */
359 RetDesc->Common.Flags |= AOPOBJ_DATA_VALID;
362 * If we are about to overwrite the original object on the operand stack,
363 * we must remove a reference on the original object because we are
364 * essentially removing it from the stack.
366 if (*ResultDesc == ObjDesc)
368 if (WalkState->Opcode != AML_STORE_OP)
370 AcpiUtRemoveReference (ObjDesc);
374 *ResultDesc = RetDesc;
375 return_ACPI_STATUS (AE_OK);
379 /*******************************************************************************
381 * FUNCTION: AcpiExConvertAscii
383 * PARAMETERS: Integer - Value to be converted
385 * String - Where the string is returned
386 * DataWidth - Size of data item to be converted
388 * RETURN: Actual string length
390 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
392 ******************************************************************************/
395 AcpiExConvertToAscii (
396 ACPI_INTEGER Integer,
411 ACPI_FUNCTION_ENTRY ();
414 if (DataWidth < sizeof (ACPI_INTEGER))
422 Length = sizeof (ACPI_INTEGER);
430 for (i = ACPI_MAX_DECIMAL_DIGITS; i > 0; i--)
432 /* Divide by nth factor of 10 */
435 for (j = 0; j < i; j++)
437 (void) AcpiUtShortDivide (&Digit, 10, &Digit, &Remainder);
440 /* Create the decimal digit */
449 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
458 /* Copy the integer to the buffer */
460 for (i = 0, j = ((Length * 2) -1); i < (Length * 2); i++, j--)
463 HexDigit = AcpiUtHexToAsciiChar (Integer, (j * 4));
464 if (HexDigit != ACPI_ASCII_ZERO)
471 String[k] = (UINT8) HexDigit;
483 * Since leading zeros are supressed, we must check for the case where
484 * the integer equals 0
486 * Finally, null terminate the string and return the length
490 String [0] = ACPI_ASCII_ZERO;
499 /*******************************************************************************
501 * FUNCTION: AcpiExConvertToString
503 * PARAMETERS: ObjDesc - Object to be converted. Must be an
504 * Integer, Buffer, or String
505 * ResultDesc - Where the string object is returned
507 * MaxLength - Max length of the returned string
508 * WalkState - Current method state
512 * DESCRIPTION: Convert an ACPI Object to a string
514 ******************************************************************************/
517 AcpiExConvertToString (
518 ACPI_OPERAND_OBJECT *ObjDesc,
519 ACPI_OPERAND_OBJECT **ResultDesc,
522 ACPI_WALK_STATE *WalkState)
524 ACPI_OPERAND_OBJECT *RetDesc;
531 ACPI_FUNCTION_TRACE_PTR ("ExConvertToString", ObjDesc);
534 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
536 case ACPI_TYPE_STRING:
538 if (MaxLength >= ObjDesc->String.Length)
540 *ResultDesc = ObjDesc;
541 return_ACPI_STATUS (AE_OK);
545 /* Must copy the string first and then truncate it */
547 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
551 case ACPI_TYPE_INTEGER:
553 StringLength = AcpiGbl_IntegerByteWidth * 2;
556 StringLength = ACPI_MAX_DECIMAL_DIGITS;
560 * Create a new String
562 RetDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
565 return_ACPI_STATUS (AE_NO_MEMORY);
568 /* Need enough space for one ASCII integer plus null terminator */
570 NewBuf = ACPI_MEM_CALLOCATE ((ACPI_SIZE) StringLength + 1);
574 (("ExConvertToString: Buffer allocation failure\n"));
575 AcpiUtRemoveReference (RetDesc);
576 return_ACPI_STATUS (AE_NO_MEMORY);
581 i = AcpiExConvertToAscii (ObjDesc->Integer.Value, Base, NewBuf, sizeof (ACPI_INTEGER));
583 /* Null terminate at the correct place */
587 NewBuf[MaxLength] = 0;
588 RetDesc->String.Length = MaxLength;
593 RetDesc->String.Length = i;
596 RetDesc->Buffer.Pointer = NewBuf;
600 case ACPI_TYPE_BUFFER:
602 /* Find the string length */
604 Pointer = ObjDesc->Buffer.Pointer;
605 for (StringLength = 0; StringLength < ObjDesc->Buffer.Length; StringLength++)
607 /* Exit on null terminator */
609 if (!Pointer[StringLength])
615 if (MaxLength > ACPI_MAX_STRING_CONVERSION)
617 if (StringLength > ACPI_MAX_STRING_CONVERSION)
619 return_ACPI_STATUS (AE_AML_STRING_LIMIT);
624 * Create a new string object
626 RetDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
629 return_ACPI_STATUS (AE_NO_MEMORY);
632 /* String length is the lesser of the Max or the actual length */
634 if (MaxLength < StringLength)
636 StringLength = MaxLength;
639 NewBuf = ACPI_MEM_CALLOCATE ((ACPI_SIZE) StringLength + 1);
643 (("ExConvertToString: Buffer allocation failure\n"));
644 AcpiUtRemoveReference (RetDesc);
645 return_ACPI_STATUS (AE_NO_MEMORY);
648 /* Copy the appropriate number of buffer characters */
650 ACPI_MEMCPY (NewBuf, Pointer, StringLength);
654 NewBuf [StringLength] = 0;
655 RetDesc->Buffer.Pointer = NewBuf;
656 RetDesc->String.Length = StringLength;
661 return_ACPI_STATUS (AE_TYPE);
665 * If we are about to overwrite the original object on the operand stack,
666 * we must remove a reference on the original object because we are
667 * essentially removing it from the stack.
669 if (*ResultDesc == ObjDesc)
671 if (WalkState->Opcode != AML_STORE_OP)
673 AcpiUtRemoveReference (ObjDesc);
677 *ResultDesc = RetDesc;
678 return_ACPI_STATUS (AE_OK);
682 /*******************************************************************************
684 * FUNCTION: AcpiExConvertToTargetType
686 * PARAMETERS: DestinationType - Current type of the destination
687 * SourceDesc - Source object to be converted.
688 * ResultDesc - Where the converted object is returned
689 * WalkState - Current method state
693 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
695 ******************************************************************************/
698 AcpiExConvertToTargetType (
699 ACPI_OBJECT_TYPE DestinationType,
700 ACPI_OPERAND_OBJECT *SourceDesc,
701 ACPI_OPERAND_OBJECT **ResultDesc,
702 ACPI_WALK_STATE *WalkState)
704 ACPI_STATUS Status = AE_OK;
707 ACPI_FUNCTION_TRACE ("ExConvertToTargetType");
710 /* Default behavior */
712 *ResultDesc = SourceDesc;
715 * If required by the target,
716 * perform implicit conversion on the source before we store it.
718 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
720 case ARGI_SIMPLE_TARGET:
721 case ARGI_FIXED_TARGET:
722 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
724 switch (DestinationType)
726 case ACPI_TYPE_LOCAL_REGION_FIELD:
728 * Named field can always handle conversions
733 /* No conversion allowed for these types */
735 if (DestinationType != ACPI_GET_OBJECT_TYPE (SourceDesc))
737 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
738 "Explicit operator, will store (%s) over existing type (%s)\n",
739 AcpiUtGetObjectTypeName (SourceDesc),
740 AcpiUtGetTypeName (DestinationType)));
749 switch (DestinationType)
751 case ACPI_TYPE_INTEGER:
752 case ACPI_TYPE_BUFFER_FIELD:
753 case ACPI_TYPE_LOCAL_BANK_FIELD:
754 case ACPI_TYPE_LOCAL_INDEX_FIELD:
756 * These types require an Integer operand. We can convert
757 * a Buffer or a String to an Integer if necessary.
759 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc, WalkState);
763 case ACPI_TYPE_STRING:
766 * The operand must be a String. We can convert an
767 * Integer or Buffer if necessary
769 Status = AcpiExConvertToString (SourceDesc, ResultDesc, 16, ACPI_UINT32_MAX, WalkState);
773 case ACPI_TYPE_BUFFER:
776 * The operand must be a Buffer. We can convert an
777 * Integer or String if necessary
779 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc, WalkState);
784 ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
786 Status = AE_AML_INTERNAL;
794 * CreateXxxxField cases - we are storing the field object into the name
800 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
801 "Unknown Target type ID 0x%X Op %s DestType %s\n",
802 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
803 WalkState->OpInfo->Name, AcpiUtGetTypeName (DestinationType)));
805 ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
806 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs)))
807 Status = AE_AML_INTERNAL;
811 * Source-to-Target conversion semantics:
813 * If conversion to the target type cannot be performed, then simply
814 * overwrite the target with the new object and type.
816 if (Status == AE_TYPE)
821 return_ACPI_STATUS (Status);