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 (ACPI_CAST_PTR (char, Pointer
),
141 (AcpiGbl_IntegerByteWidth
| Flags
), &Result
);
142 if (ACPI_FAILURE (Status
))
144 return_ACPI_STATUS (Status
);
148 case ACPI_TYPE_BUFFER
:
150 /* Check for zero-length buffer */
154 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
157 /* Transfer no more than an integer's worth of data */
159 if (Count
> AcpiGbl_IntegerByteWidth
)
161 Count
= AcpiGbl_IntegerByteWidth
;
165 * Convert buffer to an integer - we simply grab enough raw data
166 * from the buffer to fill an integer
168 for (i
= 0; i
< Count
; i
++)
171 * Get next byte and shift it into the Result.
172 * Little endian is used, meaning that the first byte of the buffer
173 * is the LSB of the integer
175 Result
|= (((UINT64
) Pointer
[i
]) << (i
* 8));
181 /* No other types can get here */
186 /* Create a new integer */
188 ReturnDesc
= AcpiUtCreateIntegerObject (Result
);
191 return_ACPI_STATUS (AE_NO_MEMORY
);
194 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
195 ACPI_FORMAT_UINT64 (Result
)));
197 /* Save the Result */
199 (void) AcpiExTruncateFor32bitTable (ReturnDesc
);
200 *ResultDesc
= ReturnDesc
;
201 return_ACPI_STATUS (AE_OK
);
205 /*******************************************************************************
207 * FUNCTION: AcpiExConvertToBuffer
209 * PARAMETERS: ObjDesc - Object to be converted. Must be an
210 * Integer, Buffer, or String
211 * ResultDesc - Where the new buffer object is returned
215 * DESCRIPTION: Convert an ACPI Object to a Buffer
217 ******************************************************************************/
220 AcpiExConvertToBuffer (
221 ACPI_OPERAND_OBJECT
*ObjDesc
,
222 ACPI_OPERAND_OBJECT
**ResultDesc
)
224 ACPI_OPERAND_OBJECT
*ReturnDesc
;
228 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer
, ObjDesc
);
231 switch (ObjDesc
->Common
.Type
)
233 case ACPI_TYPE_BUFFER
:
235 /* No conversion necessary */
237 *ResultDesc
= ObjDesc
;
238 return_ACPI_STATUS (AE_OK
);
241 case ACPI_TYPE_INTEGER
:
243 * Create a new Buffer object.
244 * Need enough space for one integer
246 ReturnDesc
= AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth
);
249 return_ACPI_STATUS (AE_NO_MEMORY
);
252 /* Copy the integer to the buffer, LSB first */
254 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
255 memcpy (NewBuf
, &ObjDesc
->Integer
.Value
, AcpiGbl_IntegerByteWidth
);
258 case ACPI_TYPE_STRING
:
260 * Create a new Buffer object
261 * Size will be the string length
263 * NOTE: Add one to the string length to include the null terminator.
264 * The ACPI spec is unclear on this subject, but there is existing
265 * ASL/AML code that depends on the null being transferred to the new
268 ReturnDesc
= AcpiUtCreateBufferObject ((ACPI_SIZE
)
269 ObjDesc
->String
.Length
+ 1);
272 return_ACPI_STATUS (AE_NO_MEMORY
);
275 /* Copy the string to the buffer */
277 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
278 strncpy ((char *) NewBuf
, (char *) ObjDesc
->String
.Pointer
,
279 ObjDesc
->String
.Length
);
284 return_ACPI_STATUS (AE_TYPE
);
287 /* Mark buffer initialized */
289 ReturnDesc
->Common
.Flags
|= AOPOBJ_DATA_VALID
;
290 *ResultDesc
= ReturnDesc
;
291 return_ACPI_STATUS (AE_OK
);
295 /*******************************************************************************
297 * FUNCTION: AcpiExConvertToAscii
299 * PARAMETERS: Integer - Value to be converted
300 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
301 * String - Where the string is returned
302 * DataWidth - Size of data item to be converted, in bytes
304 * RETURN: Actual string length
306 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
308 ******************************************************************************/
311 AcpiExConvertToAscii (
322 UINT32 DecimalLength
;
324 BOOLEAN SupressZeros
;
327 ACPI_FUNCTION_ENTRY ();
334 /* Setup max length for the decimal number */
340 DecimalLength
= ACPI_MAX8_DECIMAL_DIGITS
;
345 DecimalLength
= ACPI_MAX32_DECIMAL_DIGITS
;
351 DecimalLength
= ACPI_MAX64_DECIMAL_DIGITS
;
355 SupressZeros
= TRUE
; /* No leading zeros */
358 for (i
= DecimalLength
; i
> 0; i
--)
360 /* Divide by nth factor of 10 */
363 for (j
= 0; j
< i
; j
++)
365 (void) AcpiUtShortDivide (Digit
, 10, &Digit
, &Remainder
);
368 /* Handle leading zeros */
372 SupressZeros
= FALSE
;
377 String
[k
] = (UINT8
) (ACPI_ASCII_ZERO
+ Remainder
);
385 /* HexLength: 2 ascii hex chars per data byte */
387 HexLength
= ACPI_MUL_2 (DataWidth
);
388 for (i
= 0, j
= (HexLength
-1); i
< HexLength
; i
++, j
--)
390 /* Get one hex digit, most significant digits first */
393 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 (
495 ObjDesc
->Integer
.Value
, Base
, 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
, NewBuf
, 1);
585 *NewBuf
++ = Separator
; /* each separated by a comma or space */
589 * Null terminate the string
590 * (overwrites final comma/space from above)
592 if (ObjDesc
->Buffer
.Length
)
601 return_ACPI_STATUS (AE_TYPE
);
604 *ResultDesc
= ReturnDesc
;
605 return_ACPI_STATUS (AE_OK
);
609 /*******************************************************************************
611 * FUNCTION: AcpiExConvertToTargetType
613 * PARAMETERS: DestinationType - Current type of the destination
614 * SourceDesc - Source object to be converted.
615 * ResultDesc - Where the converted object is returned
616 * WalkState - Current method state
620 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
622 ******************************************************************************/
625 AcpiExConvertToTargetType (
626 ACPI_OBJECT_TYPE DestinationType
,
627 ACPI_OPERAND_OBJECT
*SourceDesc
,
628 ACPI_OPERAND_OBJECT
**ResultDesc
,
629 ACPI_WALK_STATE
*WalkState
)
631 ACPI_STATUS Status
= AE_OK
;
634 ACPI_FUNCTION_TRACE (ExConvertToTargetType
);
637 /* Default behavior */
639 *ResultDesc
= SourceDesc
;
642 * If required by the target,
643 * perform implicit conversion on the source before we store it.
645 switch (GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
))
647 case ARGI_SIMPLE_TARGET
:
648 case ARGI_FIXED_TARGET
:
649 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
651 switch (DestinationType
)
653 case ACPI_TYPE_LOCAL_REGION_FIELD
:
655 * Named field can always handle conversions
661 /* No conversion allowed for these types */
663 if (DestinationType
!= SourceDesc
->Common
.Type
)
665 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
666 "Explicit operator, will store (%s) over existing type (%s)\n",
667 AcpiUtGetObjectTypeName (SourceDesc
),
668 AcpiUtGetTypeName (DestinationType
)));
675 case ARGI_STORE_TARGET
:
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
,
688 ACPI_STRTOUL_BASE16
);
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
,
711 "Bad destination type during conversion: 0x%X",
713 Status
= AE_AML_INTERNAL
;
720 * CreateXxxxField cases - we are storing the field object into the name
726 ACPI_ERROR ((AE_INFO
,
727 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
728 GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
),
729 WalkState
->Opcode
, AcpiUtGetTypeName (DestinationType
)));
730 Status
= AE_AML_INTERNAL
;
734 * Source-to-Target conversion semantics:
736 * If conversion to the target type cannot be performed, then simply
737 * overwrite the target with the new object and type.
739 if (Status
== AE_TYPE
)
744 return_ACPI_STATUS (Status
);