1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #define __EXCONVRT_C__
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
53 #define _COMPONENT ACPI_EXECUTER
54 ACPI_MODULE_NAME ("exconvrt")
56 /* Local prototypes */
59 AcpiExConvertToAscii (
66 /*******************************************************************************
68 * FUNCTION: AcpiExConvertToInteger
70 * PARAMETERS: ObjDesc - Object to be converted. Must be an
71 * Integer, Buffer, or String
72 * ResultDesc - Where the new Integer object is returned
73 * Flags - Used for string conversion
77 * DESCRIPTION: Convert an ACPI Object to an integer.
79 ******************************************************************************/
82 AcpiExConvertToInteger (
83 ACPI_OPERAND_OBJECT *ObjDesc,
84 ACPI_OPERAND_OBJECT **ResultDesc,
87 ACPI_OPERAND_OBJECT *ReturnDesc;
95 ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger, ObjDesc);
98 switch (ObjDesc->Common.Type)
100 case ACPI_TYPE_INTEGER:
102 /* No conversion necessary */
104 *ResultDesc = ObjDesc;
105 return_ACPI_STATUS (AE_OK);
107 case ACPI_TYPE_BUFFER:
108 case ACPI_TYPE_STRING:
110 /* Note: Takes advantage of common buffer/string fields */
112 Pointer = ObjDesc->Buffer.Pointer;
113 Count = ObjDesc->Buffer.Length;
117 return_ACPI_STATUS (AE_TYPE);
121 * Convert the buffer/string to an integer. Note that both buffers and
122 * strings are treated as raw data - we don't convert ascii to hex for
125 * There are two terminating conditions for the loop:
126 * 1) The size of an integer has been reached, or
127 * 2) The end of the buffer or string has been reached
131 /* String conversion is different than Buffer conversion */
133 switch (ObjDesc->Common.Type)
135 case ACPI_TYPE_STRING:
138 * Convert string to an integer - for most cases, the string must be
139 * hexadecimal as per the ACPI specification. The only exception (as
140 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
141 * and hexadecimal strings (hex prefixed with "0x").
143 Status = AcpiUtStrtoul64 ((char *) Pointer, Flags, &Result);
144 if (ACPI_FAILURE (Status))
146 return_ACPI_STATUS (Status);
151 case ACPI_TYPE_BUFFER:
153 /* Check for zero-length buffer */
157 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
160 /* Transfer no more than an integer's worth of data */
162 if (Count > AcpiGbl_IntegerByteWidth)
164 Count = AcpiGbl_IntegerByteWidth;
168 * Convert buffer to an integer - we simply grab enough raw data
169 * from the buffer to fill an integer
171 for (i = 0; i < Count; i++)
174 * Get next byte and shift it into the Result.
175 * Little endian is used, meaning that the first byte of the buffer
176 * is the LSB of the integer
178 Result |= (((UINT64) Pointer[i]) << (i * 8));
185 /* No other types can get here */
189 /* Create a new integer */
191 ReturnDesc = AcpiUtCreateIntegerObject (Result);
194 return_ACPI_STATUS (AE_NO_MEMORY);
197 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
198 ACPI_FORMAT_UINT64 (Result)));
200 /* Save the Result */
202 AcpiExTruncateFor32bitTable (ReturnDesc);
203 *ResultDesc = ReturnDesc;
204 return_ACPI_STATUS (AE_OK);
208 /*******************************************************************************
210 * FUNCTION: AcpiExConvertToBuffer
212 * PARAMETERS: ObjDesc - Object to be converted. Must be an
213 * Integer, Buffer, or String
214 * ResultDesc - Where the new buffer object is returned
218 * DESCRIPTION: Convert an ACPI Object to a Buffer
220 ******************************************************************************/
223 AcpiExConvertToBuffer (
224 ACPI_OPERAND_OBJECT *ObjDesc,
225 ACPI_OPERAND_OBJECT **ResultDesc)
227 ACPI_OPERAND_OBJECT *ReturnDesc;
231 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer, ObjDesc);
234 switch (ObjDesc->Common.Type)
236 case ACPI_TYPE_BUFFER:
238 /* No conversion necessary */
240 *ResultDesc = ObjDesc;
241 return_ACPI_STATUS (AE_OK);
244 case ACPI_TYPE_INTEGER:
247 * Create a new Buffer object.
248 * Need enough space for one integer
250 ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
253 return_ACPI_STATUS (AE_NO_MEMORY);
256 /* Copy the integer to the buffer, LSB first */
258 NewBuf = ReturnDesc->Buffer.Pointer;
260 &ObjDesc->Integer.Value,
261 AcpiGbl_IntegerByteWidth);
265 case ACPI_TYPE_STRING:
268 * Create a new Buffer object
269 * Size will be the string length
271 * NOTE: Add one to the string length to include the null terminator.
272 * The ACPI spec is unclear on this subject, but there is existing
273 * ASL/AML code that depends on the null being transferred to the new
276 ReturnDesc = AcpiUtCreateBufferObject (
277 (ACPI_SIZE) ObjDesc->String.Length + 1);
280 return_ACPI_STATUS (AE_NO_MEMORY);
283 /* Copy the string to the buffer */
285 NewBuf = ReturnDesc->Buffer.Pointer;
286 ACPI_STRNCPY ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
287 ObjDesc->String.Length);
292 return_ACPI_STATUS (AE_TYPE);
295 /* Mark buffer initialized */
297 ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
298 *ResultDesc = ReturnDesc;
299 return_ACPI_STATUS (AE_OK);
303 /*******************************************************************************
305 * FUNCTION: AcpiExConvertToAscii
307 * PARAMETERS: Integer - Value to be converted
308 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
309 * String - Where the string is returned
310 * DataWidth - Size of data item to be converted, in bytes
312 * RETURN: Actual string length
314 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
316 ******************************************************************************/
319 AcpiExConvertToAscii (
330 UINT32 DecimalLength;
332 BOOLEAN SupressZeros;
335 ACPI_FUNCTION_ENTRY ();
342 /* Setup max length for the decimal number */
347 DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
351 DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
356 DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
360 SupressZeros = TRUE; /* No leading zeros */
363 for (i = DecimalLength; i > 0; i--)
365 /* Divide by nth factor of 10 */
368 for (j = 0; j < i; j++)
370 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
373 /* Handle leading zeros */
377 SupressZeros = FALSE;
382 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
390 /* HexLength: 2 ascii hex chars per data byte */
392 HexLength = ACPI_MUL_2 (DataWidth);
393 for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
395 /* Get one hex digit, most significant digits first */
397 String[k] = (UINT8) AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
407 * Since leading zeros are suppressed, we must check for the case where
408 * the integer equals 0
410 * Finally, null terminate the string and return the length
414 String [0] = ACPI_ASCII_ZERO;
423 /*******************************************************************************
425 * FUNCTION: AcpiExConvertToString
427 * PARAMETERS: ObjDesc - Object to be converted. Must be an
428 * Integer, Buffer, or String
429 * ResultDesc - Where the string object is returned
430 * Type - String flags (base and conversion type)
434 * DESCRIPTION: Convert an ACPI Object to a string
436 ******************************************************************************/
439 AcpiExConvertToString (
440 ACPI_OPERAND_OBJECT *ObjDesc,
441 ACPI_OPERAND_OBJECT **ResultDesc,
444 ACPI_OPERAND_OBJECT *ReturnDesc;
447 UINT32 StringLength = 0;
449 UINT8 Separator = ',';
452 ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
455 switch (ObjDesc->Common.Type)
457 case ACPI_TYPE_STRING:
459 /* No conversion necessary */
461 *ResultDesc = ObjDesc;
462 return_ACPI_STATUS (AE_OK);
465 case ACPI_TYPE_INTEGER:
469 case ACPI_EXPLICIT_CONVERT_DECIMAL:
471 /* Make room for maximum decimal number */
473 StringLength = ACPI_MAX_DECIMAL_DIGITS;
479 /* Two hex string characters for each integer byte */
481 StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
486 * Create a new String
487 * Need enough space for one ASCII integer (plus null terminator)
489 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
492 return_ACPI_STATUS (AE_NO_MEMORY);
495 NewBuf = ReturnDesc->Buffer.Pointer;
497 /* Convert integer to string */
499 StringLength = AcpiExConvertToAscii (ObjDesc->Integer.Value, Base,
500 NewBuf, AcpiGbl_IntegerByteWidth);
502 /* Null terminate at the correct place */
504 ReturnDesc->String.Length = StringLength;
505 NewBuf [StringLength] = 0;
509 case ACPI_TYPE_BUFFER:
511 /* Setup string length, base, and separator */
515 case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
517 * From ACPI: "If Data is a buffer, it is converted to a string of
518 * decimal values separated by commas."
523 * Calculate the final string length. Individual string values
524 * are variable length (include separator for each)
526 for (i = 0; i < ObjDesc->Buffer.Length; i++)
528 if (ObjDesc->Buffer.Pointer[i] >= 100)
532 else if (ObjDesc->Buffer.Pointer[i] >= 10)
543 case ACPI_IMPLICIT_CONVERT_HEX:
545 * From the ACPI spec:
546 *"The entire contents of the buffer are converted to a string of
547 * two-character hexadecimal numbers, each separated by a space."
550 StringLength = (ObjDesc->Buffer.Length * 3);
553 case ACPI_EXPLICIT_CONVERT_HEX: /* Used by ToHexString */
555 * From ACPI: "If Data is a buffer, it is converted to a string of
556 * hexadecimal values separated by commas."
558 StringLength = (ObjDesc->Buffer.Length * 3);
562 return_ACPI_STATUS (AE_BAD_PARAMETER);
566 * Create a new string object and string buffer
567 * (-1 because of extra separator included in StringLength from above)
568 * Allow creation of zero-length strings from zero-length buffers.
575 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
578 return_ACPI_STATUS (AE_NO_MEMORY);
581 NewBuf = ReturnDesc->Buffer.Pointer;
584 * Convert buffer bytes to hex or decimal values
585 * (separated by commas or spaces)
587 for (i = 0; i < ObjDesc->Buffer.Length; i++)
589 NewBuf += AcpiExConvertToAscii (
590 (UINT64) ObjDesc->Buffer.Pointer[i], Base,
592 *NewBuf++ = Separator; /* each separated by a comma or space */
596 * Null terminate the string
597 * (overwrites final comma/space from above)
599 if (ObjDesc->Buffer.Length)
607 return_ACPI_STATUS (AE_TYPE);
610 *ResultDesc = ReturnDesc;
611 return_ACPI_STATUS (AE_OK);
615 /*******************************************************************************
617 * FUNCTION: AcpiExConvertToTargetType
619 * PARAMETERS: DestinationType - Current type of the destination
620 * SourceDesc - Source object to be converted.
621 * ResultDesc - Where the converted object is returned
622 * WalkState - Current method state
626 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
628 ******************************************************************************/
631 AcpiExConvertToTargetType (
632 ACPI_OBJECT_TYPE DestinationType,
633 ACPI_OPERAND_OBJECT *SourceDesc,
634 ACPI_OPERAND_OBJECT **ResultDesc,
635 ACPI_WALK_STATE *WalkState)
637 ACPI_STATUS Status = AE_OK;
640 ACPI_FUNCTION_TRACE (ExConvertToTargetType);
643 /* Default behavior */
645 *ResultDesc = SourceDesc;
648 * If required by the target,
649 * perform implicit conversion on the source before we store it.
651 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
653 case ARGI_SIMPLE_TARGET:
654 case ARGI_FIXED_TARGET:
655 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
657 switch (DestinationType)
659 case ACPI_TYPE_LOCAL_REGION_FIELD:
661 * Named field can always handle conversions
666 /* No conversion allowed for these types */
668 if (DestinationType != SourceDesc->Common.Type)
670 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
671 "Explicit operator, will store (%s) over existing type (%s)\n",
672 AcpiUtGetObjectTypeName (SourceDesc),
673 AcpiUtGetTypeName (DestinationType)));
682 switch (DestinationType)
684 case ACPI_TYPE_INTEGER:
685 case ACPI_TYPE_BUFFER_FIELD:
686 case ACPI_TYPE_LOCAL_BANK_FIELD:
687 case ACPI_TYPE_LOCAL_INDEX_FIELD:
689 * These types require an Integer operand. We can convert
690 * a Buffer or a String to an Integer if necessary.
692 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
697 case ACPI_TYPE_STRING:
699 * The operand must be a String. We can convert an
700 * Integer or Buffer if necessary
702 Status = AcpiExConvertToString (SourceDesc, ResultDesc,
703 ACPI_IMPLICIT_CONVERT_HEX);
707 case ACPI_TYPE_BUFFER:
709 * The operand must be a Buffer. We can convert an
710 * Integer or String if necessary
712 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
717 ACPI_ERROR ((AE_INFO, "Bad destination type during conversion: 0x%X",
719 Status = AE_AML_INTERNAL;
727 * CreateXxxxField cases - we are storing the field object into the name
733 ACPI_ERROR ((AE_INFO,
734 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
735 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
736 WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
737 Status = AE_AML_INTERNAL;
741 * Source-to-Target conversion semantics:
743 * If conversion to the target type cannot be performed, then simply
744 * overwrite the target with the new object and type.
746 if (Status == AE_TYPE)
751 return_ACPI_STATUS (Status);