1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
50 #define _COMPONENT ACPI_EXECUTER
51 ACPI_MODULE_NAME ("exconvrt")
53 /* Local prototypes */
56 AcpiExConvertToAscii (
63 /*******************************************************************************
65 * FUNCTION: AcpiExConvertToInteger
67 * PARAMETERS: ObjDesc - Object to be converted. Must be an
68 * Integer, Buffer, or String
69 * ResultDesc - Where the new Integer object is returned
70 * Flags - Used for string conversion
74 * DESCRIPTION: Convert an ACPI Object to an integer.
76 ******************************************************************************/
79 AcpiExConvertToInteger (
80 ACPI_OPERAND_OBJECT *ObjDesc,
81 ACPI_OPERAND_OBJECT **ResultDesc,
84 ACPI_OPERAND_OBJECT *ReturnDesc;
92 ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger, ObjDesc);
95 switch (ObjDesc->Common.Type)
97 case ACPI_TYPE_INTEGER:
99 /* No conversion necessary */
101 *ResultDesc = ObjDesc;
102 return_ACPI_STATUS (AE_OK);
104 case ACPI_TYPE_BUFFER:
105 case ACPI_TYPE_STRING:
107 /* Note: Takes advantage of common buffer/string fields */
109 Pointer = ObjDesc->Buffer.Pointer;
110 Count = ObjDesc->Buffer.Length;
115 return_ACPI_STATUS (AE_TYPE);
119 * Convert the buffer/string to an integer. Note that both buffers and
120 * strings are treated as raw data - we don't convert ascii to hex for
123 * There are two terminating conditions for the loop:
124 * 1) The size of an integer has been reached, or
125 * 2) The end of the buffer or string has been reached
129 /* String conversion is different than Buffer conversion */
131 switch (ObjDesc->Common.Type)
133 case ACPI_TYPE_STRING:
135 * Convert string to an integer - for most cases, the string must be
136 * hexadecimal as per the ACPI specification. The only exception (as
137 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
138 * and hexadecimal strings (hex prefixed with "0x").
140 Status = AcpiUtStrtoul64 ((char *) Pointer, Flags, &Result);
141 if (ACPI_FAILURE (Status))
143 return_ACPI_STATUS (Status);
147 case ACPI_TYPE_BUFFER:
149 /* Check for zero-length buffer */
153 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
156 /* Transfer no more than an integer's worth of data */
158 if (Count > AcpiGbl_IntegerByteWidth)
160 Count = AcpiGbl_IntegerByteWidth;
164 * Convert buffer to an integer - we simply grab enough raw data
165 * from the buffer to fill an integer
167 for (i = 0; i < Count; i++)
170 * Get next byte and shift it into the Result.
171 * Little endian is used, meaning that the first byte of the buffer
172 * is the LSB of the integer
174 Result |= (((UINT64) Pointer[i]) << (i * 8));
180 /* No other types can get here */
185 /* Create a new integer */
187 ReturnDesc = AcpiUtCreateIntegerObject (Result);
190 return_ACPI_STATUS (AE_NO_MEMORY);
193 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
194 ACPI_FORMAT_UINT64 (Result)));
196 /* Save the Result */
198 (void) AcpiExTruncateFor32bitTable (ReturnDesc);
199 *ResultDesc = ReturnDesc;
200 return_ACPI_STATUS (AE_OK);
204 /*******************************************************************************
206 * FUNCTION: AcpiExConvertToBuffer
208 * PARAMETERS: ObjDesc - Object to be converted. Must be an
209 * Integer, Buffer, or String
210 * ResultDesc - Where the new buffer object is returned
214 * DESCRIPTION: Convert an ACPI Object to a Buffer
216 ******************************************************************************/
219 AcpiExConvertToBuffer (
220 ACPI_OPERAND_OBJECT *ObjDesc,
221 ACPI_OPERAND_OBJECT **ResultDesc)
223 ACPI_OPERAND_OBJECT *ReturnDesc;
227 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer, ObjDesc);
230 switch (ObjDesc->Common.Type)
232 case ACPI_TYPE_BUFFER:
234 /* No conversion necessary */
236 *ResultDesc = ObjDesc;
237 return_ACPI_STATUS (AE_OK);
240 case ACPI_TYPE_INTEGER:
242 * Create a new Buffer object.
243 * Need enough space for one integer
245 ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
248 return_ACPI_STATUS (AE_NO_MEMORY);
251 /* Copy the integer to the buffer, LSB first */
253 NewBuf = ReturnDesc->Buffer.Pointer;
255 &ObjDesc->Integer.Value,
256 AcpiGbl_IntegerByteWidth);
259 case ACPI_TYPE_STRING:
261 * Create a new Buffer object
262 * Size will be the string length
264 * NOTE: Add one to the string length to include the null terminator.
265 * The ACPI spec is unclear on this subject, but there is existing
266 * ASL/AML code that depends on the null being transferred to the new
269 ReturnDesc = AcpiUtCreateBufferObject (
270 (ACPI_SIZE) ObjDesc->String.Length + 1);
273 return_ACPI_STATUS (AE_NO_MEMORY);
276 /* Copy the string to the buffer */
278 NewBuf = ReturnDesc->Buffer.Pointer;
279 ACPI_STRNCPY ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
280 ObjDesc->String.Length);
285 return_ACPI_STATUS (AE_TYPE);
288 /* Mark buffer initialized */
290 ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
291 *ResultDesc = ReturnDesc;
292 return_ACPI_STATUS (AE_OK);
296 /*******************************************************************************
298 * FUNCTION: AcpiExConvertToAscii
300 * PARAMETERS: Integer - Value to be converted
301 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
302 * String - Where the string is returned
303 * DataWidth - Size of data item to be converted, in bytes
305 * RETURN: Actual string length
307 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
309 ******************************************************************************/
312 AcpiExConvertToAscii (
323 UINT32 DecimalLength;
325 BOOLEAN SupressZeros;
328 ACPI_FUNCTION_ENTRY ();
335 /* Setup max length for the decimal number */
341 DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
346 DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
352 DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
356 SupressZeros = TRUE; /* No leading zeros */
359 for (i = DecimalLength; i > 0; i--)
361 /* Divide by nth factor of 10 */
364 for (j = 0; j < i; j++)
366 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
369 /* Handle leading zeros */
373 SupressZeros = FALSE;
378 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
386 /* HexLength: 2 ascii hex chars per data byte */
388 HexLength = ACPI_MUL_2 (DataWidth);
389 for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
391 /* Get one hex digit, most significant digits first */
393 String[k] = (UINT8) AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
403 * Since leading zeros are suppressed, we must check for the case where
404 * the integer equals 0
406 * Finally, null terminate the string and return the length
410 String [0] = ACPI_ASCII_ZERO;
419 /*******************************************************************************
421 * FUNCTION: AcpiExConvertToString
423 * PARAMETERS: ObjDesc - Object to be converted. Must be an
424 * Integer, Buffer, or String
425 * ResultDesc - Where the string object is returned
426 * Type - String flags (base and conversion type)
430 * DESCRIPTION: Convert an ACPI Object to a string
432 ******************************************************************************/
435 AcpiExConvertToString (
436 ACPI_OPERAND_OBJECT *ObjDesc,
437 ACPI_OPERAND_OBJECT **ResultDesc,
440 ACPI_OPERAND_OBJECT *ReturnDesc;
443 UINT32 StringLength = 0;
445 UINT8 Separator = ',';
448 ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
451 switch (ObjDesc->Common.Type)
453 case ACPI_TYPE_STRING:
455 /* No conversion necessary */
457 *ResultDesc = ObjDesc;
458 return_ACPI_STATUS (AE_OK);
460 case ACPI_TYPE_INTEGER:
464 case ACPI_EXPLICIT_CONVERT_DECIMAL:
466 /* Make room for maximum decimal number */
468 StringLength = ACPI_MAX_DECIMAL_DIGITS;
474 /* Two hex string characters for each integer byte */
476 StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
481 * Create a new String
482 * Need enough space for one ASCII integer (plus null terminator)
484 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
487 return_ACPI_STATUS (AE_NO_MEMORY);
490 NewBuf = ReturnDesc->Buffer.Pointer;
492 /* Convert integer to string */
494 StringLength = AcpiExConvertToAscii (ObjDesc->Integer.Value, Base,
495 NewBuf, AcpiGbl_IntegerByteWidth);
497 /* Null terminate at the correct place */
499 ReturnDesc->String.Length = StringLength;
500 NewBuf [StringLength] = 0;
503 case ACPI_TYPE_BUFFER:
505 /* Setup string length, base, and separator */
509 case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
511 * From ACPI: "If Data is a buffer, it is converted to a string of
512 * decimal values separated by commas."
517 * Calculate the final string length. Individual string values
518 * are variable length (include separator for each)
520 for (i = 0; i < ObjDesc->Buffer.Length; i++)
522 if (ObjDesc->Buffer.Pointer[i] >= 100)
526 else if (ObjDesc->Buffer.Pointer[i] >= 10)
537 case ACPI_IMPLICIT_CONVERT_HEX:
539 * From the ACPI spec:
540 *"The entire contents of the buffer are converted to a string of
541 * two-character hexadecimal numbers, each separated by a space."
544 StringLength = (ObjDesc->Buffer.Length * 3);
547 case ACPI_EXPLICIT_CONVERT_HEX: /* Used by ToHexString */
549 * From ACPI: "If Data is a buffer, it is converted to a string of
550 * hexadecimal values separated by commas."
552 StringLength = (ObjDesc->Buffer.Length * 3);
556 return_ACPI_STATUS (AE_BAD_PARAMETER);
560 * Create a new string object and string buffer
561 * (-1 because of extra separator included in StringLength from above)
562 * Allow creation of zero-length strings from zero-length buffers.
569 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
572 return_ACPI_STATUS (AE_NO_MEMORY);
575 NewBuf = ReturnDesc->Buffer.Pointer;
578 * Convert buffer bytes to hex or decimal values
579 * (separated by commas or spaces)
581 for (i = 0; i < ObjDesc->Buffer.Length; i++)
583 NewBuf += AcpiExConvertToAscii (
584 (UINT64) ObjDesc->Buffer.Pointer[i], Base,
586 *NewBuf++ = Separator; /* each separated by a comma or space */
590 * Null terminate the string
591 * (overwrites final comma/space from above)
593 if (ObjDesc->Buffer.Length)
602 return_ACPI_STATUS (AE_TYPE);
605 *ResultDesc = ReturnDesc;
606 return_ACPI_STATUS (AE_OK);
610 /*******************************************************************************
612 * FUNCTION: AcpiExConvertToTargetType
614 * PARAMETERS: DestinationType - Current type of the destination
615 * SourceDesc - Source object to be converted.
616 * ResultDesc - Where the converted object is returned
617 * WalkState - Current method state
621 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
623 ******************************************************************************/
626 AcpiExConvertToTargetType (
627 ACPI_OBJECT_TYPE DestinationType,
628 ACPI_OPERAND_OBJECT *SourceDesc,
629 ACPI_OPERAND_OBJECT **ResultDesc,
630 ACPI_WALK_STATE *WalkState)
632 ACPI_STATUS Status = AE_OK;
635 ACPI_FUNCTION_TRACE (ExConvertToTargetType);
638 /* Default behavior */
640 *ResultDesc = SourceDesc;
643 * If required by the target,
644 * perform implicit conversion on the source before we store it.
646 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
648 case ARGI_SIMPLE_TARGET:
649 case ARGI_FIXED_TARGET:
650 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
652 switch (DestinationType)
654 case ACPI_TYPE_LOCAL_REGION_FIELD:
656 * Named field can always handle conversions
662 /* No conversion allowed for these types */
664 if (DestinationType != SourceDesc->Common.Type)
666 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
667 "Explicit operator, will store (%s) over existing type (%s)\n",
668 AcpiUtGetObjectTypeName (SourceDesc),
669 AcpiUtGetTypeName (DestinationType)));
677 switch (DestinationType)
679 case ACPI_TYPE_INTEGER:
680 case ACPI_TYPE_BUFFER_FIELD:
681 case ACPI_TYPE_LOCAL_BANK_FIELD:
682 case ACPI_TYPE_LOCAL_INDEX_FIELD:
684 * These types require an Integer operand. We can convert
685 * a Buffer or a String to an Integer if necessary.
687 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
691 case ACPI_TYPE_STRING:
693 * The operand must be a String. We can convert an
694 * Integer or Buffer if necessary
696 Status = AcpiExConvertToString (SourceDesc, ResultDesc,
697 ACPI_IMPLICIT_CONVERT_HEX);
700 case ACPI_TYPE_BUFFER:
702 * The operand must be a Buffer. We can convert an
703 * Integer or String if necessary
705 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
710 ACPI_ERROR ((AE_INFO, "Bad destination type during conversion: 0x%X",
712 Status = AE_AML_INTERNAL;
719 * CreateXxxxField cases - we are storing the field object into the name
725 ACPI_ERROR ((AE_INFO,
726 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
727 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
728 WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
729 Status = AE_AML_INTERNAL;
733 * Source-to-Target conversion semantics:
735 * If conversion to the target type cannot be performed, then simply
736 * overwrite the target with the new object and type.
738 if (Status == AE_TYPE)
743 return_ACPI_STATUS (Status);