1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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;
254 memcpy (NewBuf, &ObjDesc->Integer.Value, AcpiGbl_IntegerByteWidth);
257 case ACPI_TYPE_STRING:
259 * Create a new Buffer object
260 * Size will be the string length
262 * NOTE: Add one to the string length to include the null terminator.
263 * The ACPI spec is unclear on this subject, but there is existing
264 * ASL/AML code that depends on the null being transferred to the new
267 ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
268 ObjDesc->String.Length + 1);
271 return_ACPI_STATUS (AE_NO_MEMORY);
274 /* Copy the string to the buffer */
276 NewBuf = ReturnDesc->Buffer.Pointer;
277 strncpy ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
278 ObjDesc->String.Length);
283 return_ACPI_STATUS (AE_TYPE);
286 /* Mark buffer initialized */
288 ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
289 *ResultDesc = ReturnDesc;
290 return_ACPI_STATUS (AE_OK);
294 /*******************************************************************************
296 * FUNCTION: AcpiExConvertToAscii
298 * PARAMETERS: Integer - Value to be converted
299 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
300 * String - Where the string is returned
301 * DataWidth - Size of data item to be converted, in bytes
303 * RETURN: Actual string length
305 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
307 ******************************************************************************/
310 AcpiExConvertToAscii (
321 UINT32 DecimalLength;
323 BOOLEAN SupressZeros;
326 ACPI_FUNCTION_ENTRY ();
333 /* Setup max length for the decimal number */
339 DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
344 DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
350 DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
354 SupressZeros = TRUE; /* No leading zeros */
357 for (i = DecimalLength; i > 0; i--)
359 /* Divide by nth factor of 10 */
362 for (j = 0; j < i; j++)
364 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
367 /* Handle leading zeros */
371 SupressZeros = FALSE;
376 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
384 /* HexLength: 2 ascii hex chars per data byte */
386 HexLength = ACPI_MUL_2 (DataWidth);
387 for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
389 /* Get one hex digit, most significant digits first */
392 AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
402 * Since leading zeros are suppressed, we must check for the case where
403 * the integer equals 0
405 * Finally, null terminate the string and return the length
409 String [0] = ACPI_ASCII_ZERO;
418 /*******************************************************************************
420 * FUNCTION: AcpiExConvertToString
422 * PARAMETERS: ObjDesc - Object to be converted. Must be an
423 * Integer, Buffer, or String
424 * ResultDesc - Where the string object is returned
425 * Type - String flags (base and conversion type)
429 * DESCRIPTION: Convert an ACPI Object to a string
431 ******************************************************************************/
434 AcpiExConvertToString (
435 ACPI_OPERAND_OBJECT *ObjDesc,
436 ACPI_OPERAND_OBJECT **ResultDesc,
439 ACPI_OPERAND_OBJECT *ReturnDesc;
442 UINT32 StringLength = 0;
444 UINT8 Separator = ',';
447 ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
450 switch (ObjDesc->Common.Type)
452 case ACPI_TYPE_STRING:
454 /* No conversion necessary */
456 *ResultDesc = ObjDesc;
457 return_ACPI_STATUS (AE_OK);
459 case ACPI_TYPE_INTEGER:
463 case ACPI_EXPLICIT_CONVERT_DECIMAL:
465 /* Make room for maximum decimal number */
467 StringLength = ACPI_MAX_DECIMAL_DIGITS;
473 /* Two hex string characters for each integer byte */
475 StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
480 * Create a new String
481 * Need enough space for one ASCII integer (plus null terminator)
483 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
486 return_ACPI_STATUS (AE_NO_MEMORY);
489 NewBuf = ReturnDesc->Buffer.Pointer;
491 /* Convert integer to string */
493 StringLength = AcpiExConvertToAscii (
494 ObjDesc->Integer.Value, Base, NewBuf, AcpiGbl_IntegerByteWidth);
496 /* Null terminate at the correct place */
498 ReturnDesc->String.Length = StringLength;
499 NewBuf [StringLength] = 0;
502 case ACPI_TYPE_BUFFER:
504 /* Setup string length, base, and separator */
508 case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
510 * From ACPI: "If Data is a buffer, it is converted to a string of
511 * decimal values separated by commas."
516 * Calculate the final string length. Individual string values
517 * are variable length (include separator for each)
519 for (i = 0; i < ObjDesc->Buffer.Length; i++)
521 if (ObjDesc->Buffer.Pointer[i] >= 100)
525 else if (ObjDesc->Buffer.Pointer[i] >= 10)
536 case ACPI_IMPLICIT_CONVERT_HEX:
538 * From the ACPI spec:
539 *"The entire contents of the buffer are converted to a string of
540 * two-character hexadecimal numbers, each separated by a space."
543 StringLength = (ObjDesc->Buffer.Length * 3);
546 case ACPI_EXPLICIT_CONVERT_HEX: /* Used by ToHexString */
548 * From ACPI: "If Data is a buffer, it is converted to a string of
549 * hexadecimal values separated by commas."
551 StringLength = (ObjDesc->Buffer.Length * 3);
555 return_ACPI_STATUS (AE_BAD_PARAMETER);
559 * Create a new string object and string buffer
560 * (-1 because of extra separator included in StringLength from above)
561 * Allow creation of zero-length strings from zero-length buffers.
568 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
571 return_ACPI_STATUS (AE_NO_MEMORY);
574 NewBuf = ReturnDesc->Buffer.Pointer;
577 * Convert buffer bytes to hex or decimal values
578 * (separated by commas or spaces)
580 for (i = 0; i < ObjDesc->Buffer.Length; i++)
582 NewBuf += AcpiExConvertToAscii (
583 (UINT64) ObjDesc->Buffer.Pointer[i], Base, NewBuf, 1);
584 *NewBuf++ = Separator; /* each separated by a comma or space */
588 * Null terminate the string
589 * (overwrites final comma/space from above)
591 if (ObjDesc->Buffer.Length)
600 return_ACPI_STATUS (AE_TYPE);
603 *ResultDesc = ReturnDesc;
604 return_ACPI_STATUS (AE_OK);
608 /*******************************************************************************
610 * FUNCTION: AcpiExConvertToTargetType
612 * PARAMETERS: DestinationType - Current type of the destination
613 * SourceDesc - Source object to be converted.
614 * ResultDesc - Where the converted object is returned
615 * WalkState - Current method state
619 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
621 ******************************************************************************/
624 AcpiExConvertToTargetType (
625 ACPI_OBJECT_TYPE DestinationType,
626 ACPI_OPERAND_OBJECT *SourceDesc,
627 ACPI_OPERAND_OBJECT **ResultDesc,
628 ACPI_WALK_STATE *WalkState)
630 ACPI_STATUS Status = AE_OK;
633 ACPI_FUNCTION_TRACE (ExConvertToTargetType);
636 /* Default behavior */
638 *ResultDesc = SourceDesc;
641 * If required by the target,
642 * perform implicit conversion on the source before we store it.
644 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
646 case ARGI_SIMPLE_TARGET:
647 case ARGI_FIXED_TARGET:
648 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
650 switch (DestinationType)
652 case ACPI_TYPE_LOCAL_REGION_FIELD:
654 * Named field can always handle conversions
660 /* No conversion allowed for these types */
662 if (DestinationType != SourceDesc->Common.Type)
664 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
665 "Explicit operator, will store (%s) over existing type (%s)\n",
666 AcpiUtGetObjectTypeName (SourceDesc),
667 AcpiUtGetTypeName (DestinationType)));
674 case ARGI_STORE_TARGET:
676 switch (DestinationType)
678 case ACPI_TYPE_INTEGER:
679 case ACPI_TYPE_BUFFER_FIELD:
680 case ACPI_TYPE_LOCAL_BANK_FIELD:
681 case ACPI_TYPE_LOCAL_INDEX_FIELD:
683 * These types require an Integer operand. We can convert
684 * a Buffer or a String to an Integer if necessary.
686 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc, 16);
689 case ACPI_TYPE_STRING:
691 * The operand must be a String. We can convert an
692 * Integer or Buffer if necessary
694 Status = AcpiExConvertToString (SourceDesc, ResultDesc,
695 ACPI_IMPLICIT_CONVERT_HEX);
698 case ACPI_TYPE_BUFFER:
700 * The operand must be a Buffer. We can convert an
701 * Integer or String if necessary
703 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
708 ACPI_ERROR ((AE_INFO,
709 "Bad destination type during conversion: 0x%X",
711 Status = AE_AML_INTERNAL;
718 * CreateXxxxField cases - we are storing the field object into the name
724 ACPI_ERROR ((AE_INFO,
725 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
726 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
727 WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
728 Status = AE_AML_INTERNAL;
732 * Source-to-Target conversion semantics:
734 * If conversion to the target type cannot be performed, then simply
735 * overwrite the target with the new object and type.
737 if (Status == AE_TYPE)
742 return_ACPI_STATUS (Status);