1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: exconvrt - Object conversion routines
6 * Copyright (C) 2000 - 2023, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
15 #define _COMPONENT ACPI_EXECUTER
16 ACPI_MODULE_NAME("exconvrt")
18 /* Local prototypes */
20 acpi_ex_convert_to_ascii(u64 integer
,
21 u16 base
, u8
*string
, u8 max_length
, u8 leading_zeros
);
23 /*******************************************************************************
25 * FUNCTION: acpi_ex_convert_to_integer
27 * PARAMETERS: obj_desc - Object to be converted. Must be an
28 * Integer, Buffer, or String
29 * result_desc - Where the new Integer object is returned
30 * implicit_conversion - Used for string conversion
34 * DESCRIPTION: Convert an ACPI Object to an integer.
36 ******************************************************************************/
39 acpi_ex_convert_to_integer(union acpi_operand_object
*obj_desc
,
40 union acpi_operand_object
**result_desc
,
41 u32 implicit_conversion
)
43 union acpi_operand_object
*return_desc
;
49 ACPI_FUNCTION_TRACE_PTR(ex_convert_to_integer
, obj_desc
);
51 switch (obj_desc
->common
.type
) {
52 case ACPI_TYPE_INTEGER
:
54 /* No conversion necessary */
56 *result_desc
= obj_desc
;
57 return_ACPI_STATUS(AE_OK
);
59 case ACPI_TYPE_BUFFER
:
60 case ACPI_TYPE_STRING
:
62 /* Note: Takes advantage of common buffer/string fields */
64 pointer
= obj_desc
->buffer
.pointer
;
65 count
= obj_desc
->buffer
.length
;
70 return_ACPI_STATUS(AE_TYPE
);
74 * Convert the buffer/string to an integer. Note that both buffers and
75 * strings are treated as raw data - we don't convert ascii to hex for
78 * There are two terminating conditions for the loop:
79 * 1) The size of an integer has been reached, or
80 * 2) The end of the buffer or string has been reached
84 /* String conversion is different than Buffer conversion */
86 switch (obj_desc
->common
.type
) {
87 case ACPI_TYPE_STRING
:
89 * Convert string to an integer - for most cases, the string must be
90 * hexadecimal as per the ACPI specification. The only exception (as
91 * of ACPI 3.0) is that the to_integer() operator allows both decimal
92 * and hexadecimal strings (hex prefixed with "0x").
94 * Explicit conversion is used only by to_integer.
95 * All other string-to-integer conversions are implicit conversions.
97 if (implicit_conversion
) {
99 acpi_ut_implicit_strtoul64(ACPI_CAST_PTR
103 acpi_ut_explicit_strtoul64(ACPI_CAST_PTR
108 case ACPI_TYPE_BUFFER
:
110 /* Check for zero-length buffer */
113 return_ACPI_STATUS(AE_AML_BUFFER_LIMIT
);
116 /* Transfer no more than an integer's worth of data */
118 if (count
> acpi_gbl_integer_byte_width
) {
119 count
= acpi_gbl_integer_byte_width
;
123 * Convert buffer to an integer - we simply grab enough raw data
124 * from the buffer to fill an integer
126 for (i
= 0; i
< count
; i
++) {
128 * Get next byte and shift it into the Result.
129 * Little endian is used, meaning that the first byte of the buffer
130 * is the LSB of the integer
132 result
|= (((u64
) pointer
[i
]) << (i
* 8));
138 /* No other types can get here */
143 /* Create a new integer */
145 return_desc
= acpi_ut_create_integer_object(result
);
147 return_ACPI_STATUS(AE_NO_MEMORY
);
150 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
151 ACPI_FORMAT_UINT64(result
)));
153 /* Save the Result */
155 (void)acpi_ex_truncate_for32bit_table(return_desc
);
156 *result_desc
= return_desc
;
157 return_ACPI_STATUS(AE_OK
);
160 /*******************************************************************************
162 * FUNCTION: acpi_ex_convert_to_buffer
164 * PARAMETERS: obj_desc - Object to be converted. Must be an
165 * Integer, Buffer, or String
166 * result_desc - Where the new buffer object is returned
170 * DESCRIPTION: Convert an ACPI Object to a Buffer
172 ******************************************************************************/
175 acpi_ex_convert_to_buffer(union acpi_operand_object
*obj_desc
,
176 union acpi_operand_object
**result_desc
)
178 union acpi_operand_object
*return_desc
;
181 ACPI_FUNCTION_TRACE_PTR(ex_convert_to_buffer
, obj_desc
);
183 switch (obj_desc
->common
.type
) {
184 case ACPI_TYPE_BUFFER
:
186 /* No conversion necessary */
188 *result_desc
= obj_desc
;
189 return_ACPI_STATUS(AE_OK
);
191 case ACPI_TYPE_INTEGER
:
193 * Create a new Buffer object.
194 * Need enough space for one integer
197 acpi_ut_create_buffer_object(acpi_gbl_integer_byte_width
);
199 return_ACPI_STATUS(AE_NO_MEMORY
);
202 /* Copy the integer to the buffer, LSB first */
204 new_buf
= return_desc
->buffer
.pointer
;
205 memcpy(new_buf
, &obj_desc
->integer
.value
,
206 acpi_gbl_integer_byte_width
);
209 case ACPI_TYPE_STRING
:
211 * Create a new Buffer object
212 * Size will be the string length
214 * NOTE: Add one to the string length to include the null terminator.
215 * The ACPI spec is unclear on this subject, but there is existing
216 * ASL/AML code that depends on the null being transferred to the new
219 return_desc
= acpi_ut_create_buffer_object((acpi_size
)
223 return_ACPI_STATUS(AE_NO_MEMORY
);
226 /* Copy the string to the buffer */
228 new_buf
= return_desc
->buffer
.pointer
;
229 strncpy((char *)new_buf
, (char *)obj_desc
->string
.pointer
,
230 obj_desc
->string
.length
);
235 return_ACPI_STATUS(AE_TYPE
);
238 /* Mark buffer initialized */
240 return_desc
->common
.flags
|= AOPOBJ_DATA_VALID
;
241 *result_desc
= return_desc
;
242 return_ACPI_STATUS(AE_OK
);
245 /*******************************************************************************
247 * FUNCTION: acpi_ex_convert_to_ascii
249 * PARAMETERS: integer - Value to be converted
250 * base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
251 * string - Where the string is returned
252 * data_width - Size of data item to be converted, in bytes
253 * leading_zeros - Allow leading zeros
255 * RETURN: Actual string length
257 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
259 ******************************************************************************/
262 acpi_ex_convert_to_ascii(u64 integer
,
263 u16 base
, u8
*string
, u8 data_width
, u8 leading_zeros
)
272 u8 supress_zeros
= !leading_zeros
;
275 ACPI_FUNCTION_ENTRY();
280 /* Setup max length for the decimal number */
282 switch (data_width
) {
285 decimal_length
= ACPI_MAX8_DECIMAL_DIGITS
;
290 decimal_length
= ACPI_MAX32_DECIMAL_DIGITS
;
296 decimal_length
= ACPI_MAX64_DECIMAL_DIGITS
;
302 for (i
= decimal_length
; i
> 0; i
--) {
304 /* Divide by nth factor of 10 */
307 for (j
= 0; j
< i
; j
++) {
308 (void)acpi_ut_short_divide(digit
, 10, &digit
,
312 /* Handle leading zeros */
314 if (remainder
!= 0) {
315 supress_zeros
= FALSE
;
318 if (!supress_zeros
) {
319 string
[k
] = (u8
) (ACPI_ASCII_ZERO
+ remainder
);
327 /* hex_length: 2 ascii hex chars per data byte */
329 hex_length
= (data_width
* 2);
330 for (i
= 0, j
= (hex_length
- 1); i
< hex_length
; i
++, j
--) {
332 /* Get one hex digit, most significant digits first */
335 acpi_ut_hex_to_ascii_char(integer
, ACPI_MUL_4(j
));
337 /* Supress leading zeros until the first non-zero character */
339 if (hex_char
== ACPI_ASCII_ZERO
&& supress_zeros
) {
343 supress_zeros
= FALSE
;
344 string
[k
] = hex_char
;
354 * Since leading zeros are suppressed, we must check for the case where
355 * the integer equals 0
357 * Finally, null terminate the string and return the length
360 string
[0] = ACPI_ASCII_ZERO
;
368 /*******************************************************************************
370 * FUNCTION: acpi_ex_convert_to_string
372 * PARAMETERS: obj_desc - Object to be converted. Must be an
373 * Integer, Buffer, or String
374 * result_desc - Where the string object is returned
375 * type - String flags (base and conversion type)
379 * DESCRIPTION: Convert an ACPI Object to a string. Supports both implicit
380 * and explicit conversions and related rules.
382 ******************************************************************************/
385 acpi_ex_convert_to_string(union acpi_operand_object
* obj_desc
,
386 union acpi_operand_object
** result_desc
, u32 type
)
388 union acpi_operand_object
*return_desc
;
391 u32 string_length
= 0;
396 ACPI_FUNCTION_TRACE_PTR(ex_convert_to_string
, obj_desc
);
398 switch (obj_desc
->common
.type
) {
399 case ACPI_TYPE_STRING
:
401 /* No conversion necessary */
403 *result_desc
= obj_desc
;
404 return_ACPI_STATUS(AE_OK
);
406 case ACPI_TYPE_INTEGER
:
409 case ACPI_EXPLICIT_CONVERT_DECIMAL
:
411 * From to_decimal_string, integer source.
413 * Make room for the maximum decimal number size
415 string_length
= ACPI_MAX_DECIMAL_DIGITS
;
416 leading_zeros
= FALSE
;
420 case ACPI_EXPLICIT_CONVERT_HEX
:
422 * From to_hex_string.
424 * Supress leading zeros and append "0x"
427 ACPI_MUL_2(acpi_gbl_integer_byte_width
) + 2;
428 leading_zeros
= FALSE
;
432 /* Two hex string characters for each integer byte */
434 string_length
= ACPI_MUL_2(acpi_gbl_integer_byte_width
);
435 leading_zeros
= TRUE
;
440 * Create a new String
441 * Need enough space for one ASCII integer (plus null terminator)
444 acpi_ut_create_string_object((acpi_size
)string_length
);
446 return_ACPI_STATUS(AE_NO_MEMORY
);
449 new_buf
= return_desc
->buffer
.pointer
;
450 if (type
== ACPI_EXPLICIT_CONVERT_HEX
) {
452 /* Append "0x" prefix for explicit hex conversion */
458 /* Convert integer to string */
461 acpi_ex_convert_to_ascii(obj_desc
->integer
.value
, base
,
463 acpi_gbl_integer_byte_width
,
466 /* Null terminate at the correct place */
468 return_desc
->string
.length
= string_length
;
469 if (type
== ACPI_EXPLICIT_CONVERT_HEX
) {
471 /* Take "0x" prefix into account */
473 return_desc
->string
.length
+= 2;
476 new_buf
[string_length
] = 0;
479 case ACPI_TYPE_BUFFER
:
481 /* Setup string length, base, and separator */
484 case ACPI_EXPLICIT_CONVERT_DECIMAL
: /* Used by to_decimal_string */
486 * Explicit conversion from the to_decimal_string ASL operator.
488 * From ACPI: "If the input is a buffer, it is converted to a
489 * a string of decimal values separated by commas."
491 leading_zeros
= FALSE
;
495 * Calculate the final string length. Individual string values
496 * are variable length (include separator for each)
498 for (i
= 0; i
< obj_desc
->buffer
.length
; i
++) {
499 if (obj_desc
->buffer
.pointer
[i
] >= 100) {
501 } else if (obj_desc
->buffer
.pointer
[i
] >= 10) {
509 case ACPI_IMPLICIT_CONVERT_HEX
:
511 * Implicit buffer-to-string conversion
513 * From the ACPI spec:
514 * "The entire contents of the buffer are converted to a string of
515 * two-character hexadecimal numbers, each separated by a space."
517 * Each hex number is prefixed with 0x (11/2018)
519 leading_zeros
= TRUE
;
521 string_length
= (obj_desc
->buffer
.length
* 5);
524 case ACPI_EXPLICIT_CONVERT_HEX
:
526 * Explicit conversion from the to_hex_string ASL operator.
528 * From ACPI: "If Data is a buffer, it is converted to a string of
529 * hexadecimal values separated by commas."
531 * Each hex number is prefixed with 0x (11/2018)
533 leading_zeros
= TRUE
;
535 string_length
= (obj_desc
->buffer
.length
* 5);
539 return_ACPI_STATUS(AE_BAD_PARAMETER
);
543 * Create a new string object and string buffer
544 * (-1 because of extra separator included in string_length from above)
545 * Allow creation of zero-length strings from zero-length buffers.
552 acpi_ut_create_string_object((acpi_size
)string_length
);
554 return_ACPI_STATUS(AE_NO_MEMORY
);
557 new_buf
= return_desc
->buffer
.pointer
;
560 * Convert buffer bytes to hex or decimal values
561 * (separated by commas or spaces)
563 for (i
= 0; i
< obj_desc
->buffer
.length
; i
++) {
566 /* Emit 0x prefix for explicit/implicit hex conversion */
572 new_buf
+= acpi_ex_convert_to_ascii((u64
) obj_desc
->
577 /* Each digit is separated by either a comma or space */
579 *new_buf
++ = separator
;
583 * Null terminate the string
584 * (overwrites final comma/space from above)
586 if (obj_desc
->buffer
.length
) {
594 return_ACPI_STATUS(AE_TYPE
);
597 *result_desc
= return_desc
;
598 return_ACPI_STATUS(AE_OK
);
601 /*******************************************************************************
603 * FUNCTION: acpi_ex_convert_to_target_type
605 * PARAMETERS: destination_type - Current type of the destination
606 * source_desc - Source object to be converted.
607 * result_desc - Where the converted object is returned
608 * walk_state - Current method state
612 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
614 ******************************************************************************/
617 acpi_ex_convert_to_target_type(acpi_object_type destination_type
,
618 union acpi_operand_object
*source_desc
,
619 union acpi_operand_object
**result_desc
,
620 struct acpi_walk_state
*walk_state
)
622 acpi_status status
= AE_OK
;
624 ACPI_FUNCTION_TRACE(ex_convert_to_target_type
);
626 /* Default behavior */
628 *result_desc
= source_desc
;
631 * If required by the target,
632 * perform implicit conversion on the source before we store it.
634 switch (GET_CURRENT_ARG_TYPE(walk_state
->op_info
->runtime_args
)) {
635 case ARGI_SIMPLE_TARGET
:
636 case ARGI_FIXED_TARGET
:
637 case ARGI_INTEGER_REF
: /* Handles Increment, Decrement cases */
639 switch (destination_type
) {
640 case ACPI_TYPE_LOCAL_REGION_FIELD
:
642 * Named field can always handle conversions
648 /* No conversion allowed for these types */
650 if (destination_type
!= source_desc
->common
.type
) {
651 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
652 "Explicit operator, will store (%s) over existing type (%s)\n",
653 acpi_ut_get_object_type_name
655 acpi_ut_get_type_name
656 (destination_type
)));
663 case ARGI_STORE_TARGET
:
665 switch (destination_type
) {
666 case ACPI_TYPE_INTEGER
:
667 case ACPI_TYPE_BUFFER_FIELD
:
668 case ACPI_TYPE_LOCAL_BANK_FIELD
:
669 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
671 * These types require an Integer operand. We can convert
672 * a Buffer or a String to an Integer if necessary.
675 acpi_ex_convert_to_integer(source_desc
, result_desc
,
676 ACPI_IMPLICIT_CONVERSION
);
679 case ACPI_TYPE_STRING
:
681 * The operand must be a String. We can convert an
682 * Integer or Buffer if necessary
685 acpi_ex_convert_to_string(source_desc
, result_desc
,
686 ACPI_IMPLICIT_CONVERT_HEX
);
689 case ACPI_TYPE_BUFFER
:
691 * The operand must be a Buffer. We can convert an
692 * Integer or String if necessary
695 acpi_ex_convert_to_buffer(source_desc
, result_desc
);
701 "Bad destination type during conversion: 0x%X",
703 status
= AE_AML_INTERNAL
;
710 * create_xxxx_field cases - we are storing the field object into the name
717 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
718 GET_CURRENT_ARG_TYPE(walk_state
->op_info
->
721 acpi_ut_get_type_name(destination_type
)));
722 status
= AE_AML_INTERNAL
;
726 * Source-to-Target conversion semantics:
728 * If conversion to the target type cannot be performed, then simply
729 * overwrite the target with the new object and type.
731 if (status
== AE_TYPE
) {
735 return_ACPI_STATUS(status
);