1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2019, 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 * ImplicitConversion - 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
,
82 UINT32 ImplicitConversion
)
84 ACPI_OPERAND_OBJECT
*ReturnDesc
;
91 ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger
, ObjDesc
);
94 switch (ObjDesc
->Common
.Type
)
96 case ACPI_TYPE_INTEGER
:
98 /* No conversion necessary */
100 *ResultDesc
= ObjDesc
;
101 return_ACPI_STATUS (AE_OK
);
103 case ACPI_TYPE_BUFFER
:
104 case ACPI_TYPE_STRING
:
106 /* Note: Takes advantage of common buffer/string fields */
108 Pointer
= ObjDesc
->Buffer
.Pointer
;
109 Count
= ObjDesc
->Buffer
.Length
;
114 return_ACPI_STATUS (AE_TYPE
);
118 * Convert the buffer/string to an integer. Note that both buffers and
119 * strings are treated as raw data - we don't convert ascii to hex for
122 * There are two terminating conditions for the loop:
123 * 1) The size of an integer has been reached, or
124 * 2) The end of the buffer or string has been reached
128 /* String conversion is different than Buffer conversion */
130 switch (ObjDesc
->Common
.Type
)
132 case ACPI_TYPE_STRING
:
134 * Convert string to an integer - for most cases, the string must be
135 * hexadecimal as per the ACPI specification. The only exception (as
136 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
137 * and hexadecimal strings (hex prefixed with "0x").
139 * Explicit conversion is used only by ToInteger.
140 * All other string-to-integer conversions are implicit conversions.
142 if (ImplicitConversion
)
144 Result
= AcpiUtImplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer
));
148 Result
= AcpiUtExplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer
));
152 case ACPI_TYPE_BUFFER
:
154 /* Check for zero-length buffer */
158 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT
);
161 /* Transfer no more than an integer's worth of data */
163 if (Count
> AcpiGbl_IntegerByteWidth
)
165 Count
= AcpiGbl_IntegerByteWidth
;
169 * Convert buffer to an integer - we simply grab enough raw data
170 * from the buffer to fill an integer
172 for (i
= 0; i
< Count
; i
++)
175 * Get next byte and shift it into the Result.
176 * Little endian is used, meaning that the first byte of the buffer
177 * is the LSB of the integer
179 Result
|= (((UINT64
) Pointer
[i
]) << (i
* 8));
185 /* No other types can get here */
190 /* Create a new integer */
192 ReturnDesc
= AcpiUtCreateIntegerObject (Result
);
195 return_ACPI_STATUS (AE_NO_MEMORY
);
198 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
199 ACPI_FORMAT_UINT64 (Result
)));
201 /* Save the Result */
203 (void) AcpiExTruncateFor32bitTable (ReturnDesc
);
204 *ResultDesc
= ReturnDesc
;
205 return_ACPI_STATUS (AE_OK
);
209 /*******************************************************************************
211 * FUNCTION: AcpiExConvertToBuffer
213 * PARAMETERS: ObjDesc - Object to be converted. Must be an
214 * Integer, Buffer, or String
215 * ResultDesc - Where the new buffer object is returned
219 * DESCRIPTION: Convert an ACPI Object to a Buffer
221 ******************************************************************************/
224 AcpiExConvertToBuffer (
225 ACPI_OPERAND_OBJECT
*ObjDesc
,
226 ACPI_OPERAND_OBJECT
**ResultDesc
)
228 ACPI_OPERAND_OBJECT
*ReturnDesc
;
232 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer
, ObjDesc
);
235 switch (ObjDesc
->Common
.Type
)
237 case ACPI_TYPE_BUFFER
:
239 /* No conversion necessary */
241 *ResultDesc
= ObjDesc
;
242 return_ACPI_STATUS (AE_OK
);
245 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
;
259 memcpy (NewBuf
, &ObjDesc
->Integer
.Value
, AcpiGbl_IntegerByteWidth
);
262 case ACPI_TYPE_STRING
:
264 * Create a new Buffer object
265 * Size will be the string length
267 * NOTE: Add one to the string length to include the null terminator.
268 * The ACPI spec is unclear on this subject, but there is existing
269 * ASL/AML code that depends on the null being transferred to the new
272 ReturnDesc
= AcpiUtCreateBufferObject ((ACPI_SIZE
)
273 ObjDesc
->String
.Length
+ 1);
276 return_ACPI_STATUS (AE_NO_MEMORY
);
279 /* Copy the string to the buffer */
281 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
282 strncpy ((char *) NewBuf
, (char *) ObjDesc
->String
.Pointer
,
283 ObjDesc
->String
.Length
);
288 return_ACPI_STATUS (AE_TYPE
);
291 /* Mark buffer initialized */
293 ReturnDesc
->Common
.Flags
|= AOPOBJ_DATA_VALID
;
294 *ResultDesc
= ReturnDesc
;
295 return_ACPI_STATUS (AE_OK
);
299 /*******************************************************************************
301 * FUNCTION: AcpiExConvertToAscii
303 * PARAMETERS: Integer - Value to be converted
304 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
305 * String - Where the string is returned
306 * DataWidth - Size of data item to be converted, in bytes
308 * RETURN: Actual string length
310 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
312 ******************************************************************************/
315 AcpiExConvertToAscii (
326 UINT32 DecimalLength
;
328 BOOLEAN SupressZeros
;
331 ACPI_FUNCTION_ENTRY ();
338 /* Setup max length for the decimal number */
344 DecimalLength
= ACPI_MAX8_DECIMAL_DIGITS
;
349 DecimalLength
= ACPI_MAX32_DECIMAL_DIGITS
;
355 DecimalLength
= ACPI_MAX64_DECIMAL_DIGITS
;
359 SupressZeros
= TRUE
; /* No leading zeros */
362 for (i
= DecimalLength
; i
> 0; i
--)
364 /* Divide by nth factor of 10 */
367 for (j
= 0; j
< i
; j
++)
369 (void) AcpiUtShortDivide (Digit
, 10, &Digit
, &Remainder
);
372 /* Handle leading zeros */
376 SupressZeros
= FALSE
;
381 String
[k
] = (UINT8
) (ACPI_ASCII_ZERO
+ Remainder
);
389 /* HexLength: 2 ascii hex chars per data byte */
391 HexLength
= (DataWidth
* 2);
392 for (i
= 0, j
= (HexLength
-1); i
< HexLength
; i
++, j
--)
394 /* Get one hex digit, most significant digits first */
397 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. Supports both implicit
435 * and explicit conversions and related rules.
437 ******************************************************************************/
440 AcpiExConvertToString (
441 ACPI_OPERAND_OBJECT
*ObjDesc
,
442 ACPI_OPERAND_OBJECT
**ResultDesc
,
445 ACPI_OPERAND_OBJECT
*ReturnDesc
;
448 UINT32 StringLength
= 0;
450 UINT8 Separator
= ',';
453 ACPI_FUNCTION_TRACE_PTR (ExConvertToString
, ObjDesc
);
456 switch (ObjDesc
->Common
.Type
)
458 case ACPI_TYPE_STRING
:
460 /* No conversion necessary */
462 *ResultDesc
= ObjDesc
;
463 return_ACPI_STATUS (AE_OK
);
465 case ACPI_TYPE_INTEGER
:
469 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
471 * From ToDecimalString, integer source.
473 * Make room for the maximum decimal number size
475 StringLength
= ACPI_MAX_DECIMAL_DIGITS
;
481 /* Two hex string characters for each integer byte */
483 StringLength
= ACPI_MUL_2 (AcpiGbl_IntegerByteWidth
);
488 * Create a new String
489 * Need enough space for one ASCII integer (plus null terminator)
491 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
494 return_ACPI_STATUS (AE_NO_MEMORY
);
497 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
499 /* Convert integer to string */
501 StringLength
= AcpiExConvertToAscii (
502 ObjDesc
->Integer
.Value
, Base
, NewBuf
, AcpiGbl_IntegerByteWidth
);
504 /* Null terminate at the correct place */
506 ReturnDesc
->String
.Length
= StringLength
;
507 NewBuf
[StringLength
] = 0;
510 case ACPI_TYPE_BUFFER
:
512 /* Setup string length, base, and separator */
516 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by ToDecimalString */
518 * Explicit conversion from the ToDecimalString ASL operator.
520 * From ACPI: "If the input is a buffer, it is converted to a
521 * a string of decimal values separated by commas."
526 * Calculate the final string length. Individual string values
527 * are variable length (include separator for each)
529 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
531 if (ObjDesc
->Buffer
.Pointer
[i
] >= 100)
535 else if (ObjDesc
->Buffer
.Pointer
[i
] >= 10)
546 case ACPI_IMPLICIT_CONVERT_HEX
:
548 * Implicit buffer-to-string conversion
550 * From the ACPI spec:
551 * "The entire contents of the buffer are converted to a string of
552 * two-character hexadecimal numbers, each separated by a space."
554 * Each hex number is prefixed with 0x (11/2018)
557 StringLength
= (ObjDesc
->Buffer
.Length
* 5);
560 case ACPI_EXPLICIT_CONVERT_HEX
:
562 * Explicit conversion from the ToHexString ASL operator.
564 * From ACPI: "If Data is a buffer, it is converted to a string of
565 * hexadecimal values separated by commas."
567 * Each hex number is prefixed with 0x (11/2018)
570 StringLength
= (ObjDesc
->Buffer
.Length
* 5);
574 return_ACPI_STATUS (AE_BAD_PARAMETER
);
578 * Create a new string object and string buffer
579 * (-1 because of extra separator included in StringLength from above)
580 * Allow creation of zero-length strings from zero-length buffers.
587 ReturnDesc
= AcpiUtCreateStringObject ((ACPI_SIZE
) StringLength
);
590 return_ACPI_STATUS (AE_NO_MEMORY
);
593 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
596 * Convert buffer bytes to hex or decimal values
597 * (separated by commas or spaces)
599 for (i
= 0; i
< ObjDesc
->Buffer
.Length
; i
++)
603 /* Emit 0x prefix for explict/implicit hex conversion */
609 NewBuf
+= AcpiExConvertToAscii (
610 (UINT64
) ObjDesc
->Buffer
.Pointer
[i
], Base
, NewBuf
, 1);
612 /* Each digit is separated by either a comma or space */
614 *NewBuf
++ = Separator
;
618 * Null terminate the string
619 * (overwrites final comma/space from above)
621 if (ObjDesc
->Buffer
.Length
)
630 return_ACPI_STATUS (AE_TYPE
);
633 *ResultDesc
= ReturnDesc
;
634 return_ACPI_STATUS (AE_OK
);
638 /*******************************************************************************
640 * FUNCTION: AcpiExConvertToTargetType
642 * PARAMETERS: DestinationType - Current type of the destination
643 * SourceDesc - Source object to be converted.
644 * ResultDesc - Where the converted object is returned
645 * WalkState - Current method state
649 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
651 ******************************************************************************/
654 AcpiExConvertToTargetType (
655 ACPI_OBJECT_TYPE DestinationType
,
656 ACPI_OPERAND_OBJECT
*SourceDesc
,
657 ACPI_OPERAND_OBJECT
**ResultDesc
,
658 ACPI_WALK_STATE
*WalkState
)
660 ACPI_STATUS Status
= AE_OK
;
663 ACPI_FUNCTION_TRACE (ExConvertToTargetType
);
666 /* Default behavior */
668 *ResultDesc
= SourceDesc
;
671 * If required by the target,
672 * perform implicit conversion on the source before we store it.
674 switch (GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
))
676 case ARGI_SIMPLE_TARGET
:
677 case ARGI_FIXED_TARGET
:
678 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
680 switch (DestinationType
)
682 case ACPI_TYPE_LOCAL_REGION_FIELD
:
684 * Named field can always handle conversions
690 /* No conversion allowed for these types */
692 if (DestinationType
!= SourceDesc
->Common
.Type
)
694 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
695 "Explicit operator, will store (%s) over existing type (%s)\n",
696 AcpiUtGetObjectTypeName (SourceDesc
),
697 AcpiUtGetTypeName (DestinationType
)));
704 case ARGI_STORE_TARGET
:
706 switch (DestinationType
)
708 case ACPI_TYPE_INTEGER
:
709 case ACPI_TYPE_BUFFER_FIELD
:
710 case ACPI_TYPE_LOCAL_BANK_FIELD
:
711 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
713 * These types require an Integer operand. We can convert
714 * a Buffer or a String to an Integer if necessary.
716 Status
= AcpiExConvertToInteger (SourceDesc
, ResultDesc
,
717 ACPI_IMPLICIT_CONVERSION
);
720 case ACPI_TYPE_STRING
:
722 * The operand must be a String. We can convert an
723 * Integer or Buffer if necessary
725 Status
= AcpiExConvertToString (SourceDesc
, ResultDesc
,
726 ACPI_IMPLICIT_CONVERT_HEX
);
729 case ACPI_TYPE_BUFFER
:
731 * The operand must be a Buffer. We can convert an
732 * Integer or String if necessary
734 Status
= AcpiExConvertToBuffer (SourceDesc
, ResultDesc
);
739 ACPI_ERROR ((AE_INFO
,
740 "Bad destination type during conversion: 0x%X",
742 Status
= AE_AML_INTERNAL
;
749 * CreateXxxxField cases - we are storing the field object into the name
755 ACPI_ERROR ((AE_INFO
,
756 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
757 GET_CURRENT_ARG_TYPE (WalkState
->OpInfo
->RuntimeArgs
),
758 WalkState
->Opcode
, AcpiUtGetTypeName (DestinationType
)));
759 Status
= AE_AML_INTERNAL
;
763 * Source-to-Target conversion semantics:
765 * If conversion to the target type cannot be performed, then simply
766 * overwrite the target with the new object and type.
768 if (Status
== AE_TYPE
)
773 return_ACPI_STATUS (Status
);