1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2003, 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 *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/exconvrt.c,v 1.1 2003/09/24 03:32:15 drhodus Exp $ */
119 #define __EXCONVRT_C__
122 #include "acinterp.h"
126 #define _COMPONENT ACPI_EXECUTER
127 ACPI_MODULE_NAME ("exconvrt")
130 /*******************************************************************************
132 * FUNCTION: AcpiExConvertToInteger
134 * PARAMETERS: *ObjDesc - Object to be converted. Must be an
135 * Integer, Buffer, or String
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 * WalkState - Current method state
282 * DESCRIPTION: Convert an ACPI Object to a Buffer
284 ******************************************************************************/
287 AcpiExConvertToBuffer (
288 ACPI_OPERAND_OBJECT *ObjDesc,
289 ACPI_OPERAND_OBJECT **ResultDesc,
290 ACPI_WALK_STATE *WalkState)
292 ACPI_OPERAND_OBJECT *RetDesc;
297 ACPI_FUNCTION_TRACE_PTR ("ExConvertToBuffer", ObjDesc);
300 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
302 case ACPI_TYPE_BUFFER:
304 /* No conversion necessary */
306 *ResultDesc = ObjDesc;
307 return_ACPI_STATUS (AE_OK);
310 case ACPI_TYPE_INTEGER:
313 * Create a new Buffer object.
314 * Need enough space for one integer
316 RetDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
319 return_ACPI_STATUS (AE_NO_MEMORY);
322 /* Copy the integer to the buffer */
324 NewBuf = RetDesc->Buffer.Pointer;
325 for (i = 0; i < AcpiGbl_IntegerByteWidth; i++)
327 NewBuf[i] = (UINT8) (ObjDesc->Integer.Value >> (i * 8));
332 case ACPI_TYPE_STRING:
335 * Create a new Buffer object
336 * Size will be the string length
338 RetDesc = AcpiUtCreateBufferObject ((ACPI_SIZE) ObjDesc->String.Length);
341 return_ACPI_STATUS (AE_NO_MEMORY);
344 /* Copy the string to the buffer */
346 NewBuf = RetDesc->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 RetDesc->Common.Flags |= AOPOBJ_DATA_VALID;
361 * If we are about to overwrite the original object on the operand stack,
362 * we must remove a reference on the original object because we are
363 * essentially removing it from the stack.
365 if (*ResultDesc == ObjDesc)
367 if (WalkState->Opcode != AML_STORE_OP)
369 AcpiUtRemoveReference (ObjDesc);
373 *ResultDesc = RetDesc;
374 return_ACPI_STATUS (AE_OK);
378 /*******************************************************************************
380 * FUNCTION: AcpiExConvertAscii
382 * PARAMETERS: Integer - Value to be converted
384 * String - Where the string is returned
385 * DataWidth - Size of data item to be converted
387 * RETURN: Actual string length
389 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
391 ******************************************************************************/
394 AcpiExConvertToAscii (
395 ACPI_INTEGER Integer,
410 ACPI_FUNCTION_ENTRY ();
412 if (DataWidth < sizeof (ACPI_INTEGER))
420 Length = sizeof (ACPI_INTEGER);
429 for (i = ACPI_MAX_DECIMAL_DIGITS; i > 0 ; i--)
431 /* Divide by nth factor of 10 */
434 for (j = 1; j < i; j++)
436 (void) AcpiUtShortDivide (&Digit, 10, &Digit, &Remainder);
439 /* Create the decimal digit */
448 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
456 /* Copy the integer to the buffer */
458 for (i = 0, j = ((Length * 2) -1); i < (Length * 2); i++, j--)
461 HexDigit = AcpiUtHexToAsciiChar (Integer, (j * 4));
462 if (HexDigit != ACPI_ASCII_ZERO)
469 String[k] = (UINT8) HexDigit;
480 * Since leading zeros are supressed, we must check for the case where
481 * the integer equals 0.
483 * Finally, null terminate the string and return the length
487 String [0] = ACPI_ASCII_ZERO;
496 /*******************************************************************************
498 * FUNCTION: AcpiExConvertToString
500 * PARAMETERS: *ObjDesc - Object to be converted. Must be an
501 * Integer, Buffer, or String
502 * WalkState - Current method state
506 * DESCRIPTION: Convert an ACPI Object to a string
508 ******************************************************************************/
511 AcpiExConvertToString (
512 ACPI_OPERAND_OBJECT *ObjDesc,
513 ACPI_OPERAND_OBJECT **ResultDesc,
516 ACPI_WALK_STATE *WalkState)
518 ACPI_OPERAND_OBJECT *RetDesc;
525 ACPI_FUNCTION_TRACE_PTR ("ExConvertToString", ObjDesc);
528 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
530 case ACPI_TYPE_STRING:
532 if (MaxLength >= ObjDesc->String.Length)
534 *ResultDesc = ObjDesc;
535 return_ACPI_STATUS (AE_OK);
539 /* Must copy the string first and then truncate it */
541 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
545 case ACPI_TYPE_INTEGER:
547 StringLength = AcpiGbl_IntegerByteWidth * 2;
550 StringLength = ACPI_MAX_DECIMAL_DIGITS;
554 * Create a new String
556 RetDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
559 return_ACPI_STATUS (AE_NO_MEMORY);
562 /* Need enough space for one ASCII integer plus null terminator */
564 NewBuf = ACPI_MEM_CALLOCATE ((ACPI_SIZE) StringLength + 1);
568 (("ExConvertToString: Buffer allocation failure\n"));
569 AcpiUtRemoveReference (RetDesc);
570 return_ACPI_STATUS (AE_NO_MEMORY);
575 i = AcpiExConvertToAscii (ObjDesc->Integer.Value, Base, NewBuf, sizeof (ACPI_INTEGER));
577 /* Null terminate at the correct place */
581 NewBuf[MaxLength] = 0;
582 RetDesc->String.Length = MaxLength;
587 RetDesc->String.Length = i;
590 RetDesc->Buffer.Pointer = NewBuf;
594 case ACPI_TYPE_BUFFER:
596 /* Find the string length */
598 Pointer = ObjDesc->Buffer.Pointer;
599 for (StringLength = 0; StringLength < ObjDesc->Buffer.Length; StringLength++)
601 /* Exit on null terminator */
603 if (!Pointer[StringLength])
609 if (MaxLength > ACPI_MAX_STRING_CONVERSION)
611 if (StringLength > ACPI_MAX_STRING_CONVERSION)
613 return_ACPI_STATUS (AE_AML_STRING_LIMIT);
618 * Create a new string object
620 RetDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
623 return_ACPI_STATUS (AE_NO_MEMORY);
626 /* String length is the lesser of the Max or the actual length */
628 if (MaxLength < StringLength)
630 StringLength = MaxLength;
633 NewBuf = ACPI_MEM_CALLOCATE ((ACPI_SIZE) StringLength + 1);
637 (("ExConvertToString: Buffer allocation failure\n"));
638 AcpiUtRemoveReference (RetDesc);
639 return_ACPI_STATUS (AE_NO_MEMORY);
642 /* Copy the appropriate number of buffer characters */
644 ACPI_MEMCPY (NewBuf, Pointer, StringLength);
648 NewBuf [StringLength] = 0;
649 RetDesc->Buffer.Pointer = NewBuf;
650 RetDesc->String.Length = StringLength;
655 return_ACPI_STATUS (AE_TYPE);
660 * If we are about to overwrite the original object on the operand stack,
661 * we must remove a reference on the original object because we are
662 * essentially removing it from the stack.
664 if (*ResultDesc == ObjDesc)
666 if (WalkState->Opcode != AML_STORE_OP)
668 AcpiUtRemoveReference (ObjDesc);
672 *ResultDesc = RetDesc;
673 return_ACPI_STATUS (AE_OK);
677 /*******************************************************************************
679 * FUNCTION: AcpiExConvertToTargetType
681 * PARAMETERS: DestinationType - Current type of the destination
682 * SourceDesc - Source object to be converted.
683 * WalkState - Current method state
687 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
689 ******************************************************************************/
692 AcpiExConvertToTargetType (
693 ACPI_OBJECT_TYPE DestinationType,
694 ACPI_OPERAND_OBJECT *SourceDesc,
695 ACPI_OPERAND_OBJECT **ResultDesc,
696 ACPI_WALK_STATE *WalkState)
698 ACPI_STATUS Status = AE_OK;
701 ACPI_FUNCTION_TRACE ("ExConvertToTargetType");
704 /* Default behavior */
706 *ResultDesc = SourceDesc;
709 * If required by the target,
710 * perform implicit conversion on the source before we store it.
712 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
714 case ARGI_SIMPLE_TARGET:
715 case ARGI_FIXED_TARGET:
716 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
718 switch (DestinationType)
720 case ACPI_TYPE_LOCAL_REGION_FIELD:
722 * Named field can always handle conversions
727 /* No conversion allowed for these types */
729 if (DestinationType != ACPI_GET_OBJECT_TYPE (SourceDesc))
731 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
732 "Explicit operator, will store (%s) over existing type (%s)\n",
733 AcpiUtGetObjectTypeName (SourceDesc),
734 AcpiUtGetTypeName (DestinationType)));
743 switch (DestinationType)
745 case ACPI_TYPE_INTEGER:
746 case ACPI_TYPE_BUFFER_FIELD:
747 case ACPI_TYPE_LOCAL_BANK_FIELD:
748 case ACPI_TYPE_LOCAL_INDEX_FIELD:
750 * These types require an Integer operand. We can convert
751 * a Buffer or a String to an Integer if necessary.
753 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc, WalkState);
757 case ACPI_TYPE_STRING:
760 * The operand must be a String. We can convert an
761 * Integer or Buffer if necessary
763 Status = AcpiExConvertToString (SourceDesc, ResultDesc, 16, ACPI_UINT32_MAX, WalkState);
767 case ACPI_TYPE_BUFFER:
770 * The operand must be a Buffer. We can convert an
771 * Integer or String if necessary
773 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc, WalkState);
778 Status = AE_AML_INTERNAL;
786 * CreateXxxxField cases - we are storing the field object into the name
792 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
793 "Unknown Target type ID 0x%X Op %s DestType %s\n",
794 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
795 WalkState->OpInfo->Name, AcpiUtGetTypeName (DestinationType)));
797 Status = AE_AML_INTERNAL;
801 * Source-to-Target conversion semantics:
803 * If conversion to the target type cannot be performed, then simply
804 * overwrite the target with the new object and type.
806 if (Status == AE_TYPE)
811 return_ACPI_STATUS (Status);