1 /******************************************************************************
3 * Module Name: exconvrt - Object conversion routines
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acinterp.h>
155 #include <contrib/dev/acpica/include/amlcode.h>
158 #define _COMPONENT ACPI_EXECUTER
159 ACPI_MODULE_NAME ("exconvrt")
161 /* Local prototypes */
164 AcpiExConvertToAscii (
171 /*******************************************************************************
173 * FUNCTION: AcpiExConvertToInteger
175 * PARAMETERS: ObjDesc - Object to be converted. Must be an
176 * Integer, Buffer, or String
177 * ResultDesc - Where the new Integer object is returned
178 * ImplicitConversion - Used for string conversion
182 * DESCRIPTION: Convert an ACPI Object to an integer.
184 ******************************************************************************/
187 AcpiExConvertToInteger (
188 ACPI_OPERAND_OBJECT *ObjDesc,
189 ACPI_OPERAND_OBJECT **ResultDesc,
190 UINT32 ImplicitConversion)
192 ACPI_OPERAND_OBJECT *ReturnDesc;
199 ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger, ObjDesc);
202 switch (ObjDesc->Common.Type)
204 case ACPI_TYPE_INTEGER:
206 /* No conversion necessary */
208 *ResultDesc = ObjDesc;
209 return_ACPI_STATUS (AE_OK);
211 case ACPI_TYPE_BUFFER:
212 case ACPI_TYPE_STRING:
214 /* Note: Takes advantage of common buffer/string fields */
216 Pointer = ObjDesc->Buffer.Pointer;
217 Count = ObjDesc->Buffer.Length;
222 return_ACPI_STATUS (AE_TYPE);
226 * Convert the buffer/string to an integer. Note that both buffers and
227 * strings are treated as raw data - we don't convert ascii to hex for
230 * There are two terminating conditions for the loop:
231 * 1) The size of an integer has been reached, or
232 * 2) The end of the buffer or string has been reached
236 /* String conversion is different than Buffer conversion */
238 switch (ObjDesc->Common.Type)
240 case ACPI_TYPE_STRING:
242 * Convert string to an integer - for most cases, the string must be
243 * hexadecimal as per the ACPI specification. The only exception (as
244 * of ACPI 3.0) is that the ToInteger() operator allows both decimal
245 * and hexadecimal strings (hex prefixed with "0x").
247 * Explicit conversion is used only by ToInteger.
248 * All other string-to-integer conversions are implicit conversions.
250 if (ImplicitConversion)
252 Result = AcpiUtImplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer));
256 Result = AcpiUtExplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer));
260 case ACPI_TYPE_BUFFER:
262 /* Check for zero-length buffer */
266 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
269 /* Transfer no more than an integer's worth of data */
271 if (Count > AcpiGbl_IntegerByteWidth)
273 Count = AcpiGbl_IntegerByteWidth;
277 * Convert buffer to an integer - we simply grab enough raw data
278 * from the buffer to fill an integer
280 for (i = 0; i < Count; i++)
283 * Get next byte and shift it into the Result.
284 * Little endian is used, meaning that the first byte of the buffer
285 * is the LSB of the integer
287 Result |= (((UINT64) Pointer[i]) << (i * 8));
293 /* No other types can get here */
298 /* Create a new integer */
300 ReturnDesc = AcpiUtCreateIntegerObject (Result);
303 return_ACPI_STATUS (AE_NO_MEMORY);
306 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
307 ACPI_FORMAT_UINT64 (Result)));
309 /* Save the Result */
311 (void) AcpiExTruncateFor32bitTable (ReturnDesc);
312 *ResultDesc = ReturnDesc;
313 return_ACPI_STATUS (AE_OK);
317 /*******************************************************************************
319 * FUNCTION: AcpiExConvertToBuffer
321 * PARAMETERS: ObjDesc - Object to be converted. Must be an
322 * Integer, Buffer, or String
323 * ResultDesc - Where the new buffer object is returned
327 * DESCRIPTION: Convert an ACPI Object to a Buffer
329 ******************************************************************************/
332 AcpiExConvertToBuffer (
333 ACPI_OPERAND_OBJECT *ObjDesc,
334 ACPI_OPERAND_OBJECT **ResultDesc)
336 ACPI_OPERAND_OBJECT *ReturnDesc;
340 ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer, ObjDesc);
343 switch (ObjDesc->Common.Type)
345 case ACPI_TYPE_BUFFER:
347 /* No conversion necessary */
349 *ResultDesc = ObjDesc;
350 return_ACPI_STATUS (AE_OK);
353 case ACPI_TYPE_INTEGER:
355 * Create a new Buffer object.
356 * Need enough space for one integer
358 ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
361 return_ACPI_STATUS (AE_NO_MEMORY);
364 /* Copy the integer to the buffer, LSB first */
366 NewBuf = ReturnDesc->Buffer.Pointer;
367 memcpy (NewBuf, &ObjDesc->Integer.Value, AcpiGbl_IntegerByteWidth);
370 case ACPI_TYPE_STRING:
372 * Create a new Buffer object
373 * Size will be the string length
375 * NOTE: Add one to the string length to include the null terminator.
376 * The ACPI spec is unclear on this subject, but there is existing
377 * ASL/AML code that depends on the null being transferred to the new
380 ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
381 ObjDesc->String.Length + 1);
384 return_ACPI_STATUS (AE_NO_MEMORY);
387 /* Copy the string to the buffer */
389 NewBuf = ReturnDesc->Buffer.Pointer;
390 strncpy ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
391 ObjDesc->String.Length);
396 return_ACPI_STATUS (AE_TYPE);
399 /* Mark buffer initialized */
401 ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
402 *ResultDesc = ReturnDesc;
403 return_ACPI_STATUS (AE_OK);
407 /*******************************************************************************
409 * FUNCTION: AcpiExConvertToAscii
411 * PARAMETERS: Integer - Value to be converted
412 * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
413 * String - Where the string is returned
414 * DataWidth - Size of data item to be converted, in bytes
416 * RETURN: Actual string length
418 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
420 ******************************************************************************/
423 AcpiExConvertToAscii (
434 UINT32 DecimalLength;
436 BOOLEAN SupressZeros;
439 ACPI_FUNCTION_ENTRY ();
446 /* Setup max length for the decimal number */
452 DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
457 DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
463 DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
467 SupressZeros = TRUE; /* No leading zeros */
470 for (i = DecimalLength; i > 0; i--)
472 /* Divide by nth factor of 10 */
475 for (j = 0; j < i; j++)
477 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
480 /* Handle leading zeros */
484 SupressZeros = FALSE;
489 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
497 /* HexLength: 2 ascii hex chars per data byte */
499 HexLength = (DataWidth * 2);
500 for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
502 /* Get one hex digit, most significant digits first */
505 AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
515 * Since leading zeros are suppressed, we must check for the case where
516 * the integer equals 0
518 * Finally, null terminate the string and return the length
522 String [0] = ACPI_ASCII_ZERO;
531 /*******************************************************************************
533 * FUNCTION: AcpiExConvertToString
535 * PARAMETERS: ObjDesc - Object to be converted. Must be an
536 * Integer, Buffer, or String
537 * ResultDesc - Where the string object is returned
538 * Type - String flags (base and conversion type)
542 * DESCRIPTION: Convert an ACPI Object to a string. Supports both implicit
543 * and explicit conversions and related rules.
545 ******************************************************************************/
548 AcpiExConvertToString (
549 ACPI_OPERAND_OBJECT *ObjDesc,
550 ACPI_OPERAND_OBJECT **ResultDesc,
553 ACPI_OPERAND_OBJECT *ReturnDesc;
556 UINT32 StringLength = 0;
558 UINT8 Separator = ',';
561 ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
564 switch (ObjDesc->Common.Type)
566 case ACPI_TYPE_STRING:
568 /* No conversion necessary */
570 *ResultDesc = ObjDesc;
571 return_ACPI_STATUS (AE_OK);
573 case ACPI_TYPE_INTEGER:
577 case ACPI_EXPLICIT_CONVERT_DECIMAL:
579 * From ToDecimalString, integer source.
581 * Make room for the maximum decimal number size
583 StringLength = ACPI_MAX_DECIMAL_DIGITS;
589 /* Two hex string characters for each integer byte */
591 StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
596 * Create a new String
597 * Need enough space for one ASCII integer (plus null terminator)
599 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
602 return_ACPI_STATUS (AE_NO_MEMORY);
605 NewBuf = ReturnDesc->Buffer.Pointer;
607 /* Convert integer to string */
609 StringLength = AcpiExConvertToAscii (
610 ObjDesc->Integer.Value, Base, NewBuf, AcpiGbl_IntegerByteWidth);
612 /* Null terminate at the correct place */
614 ReturnDesc->String.Length = StringLength;
615 NewBuf [StringLength] = 0;
618 case ACPI_TYPE_BUFFER:
620 /* Setup string length, base, and separator */
624 case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
626 * Explicit conversion from the ToDecimalString ASL operator.
628 * From ACPI: "If the input is a buffer, it is converted to a
629 * a string of decimal values separated by commas."
634 * Calculate the final string length. Individual string values
635 * are variable length (include separator for each)
637 for (i = 0; i < ObjDesc->Buffer.Length; i++)
639 if (ObjDesc->Buffer.Pointer[i] >= 100)
643 else if (ObjDesc->Buffer.Pointer[i] >= 10)
654 case ACPI_IMPLICIT_CONVERT_HEX:
656 * Implicit buffer-to-string conversion
658 * From the ACPI spec:
659 * "The entire contents of the buffer are converted to a string of
660 * two-character hexadecimal numbers, each separated by a space."
662 * Each hex number is prefixed with 0x (11/2018)
665 StringLength = (ObjDesc->Buffer.Length * 5);
668 case ACPI_EXPLICIT_CONVERT_HEX:
670 * Explicit conversion from the ToHexString ASL operator.
672 * From ACPI: "If Data is a buffer, it is converted to a string of
673 * hexadecimal values separated by commas."
675 * Each hex number is prefixed with 0x (11/2018)
678 StringLength = (ObjDesc->Buffer.Length * 5);
682 return_ACPI_STATUS (AE_BAD_PARAMETER);
686 * Create a new string object and string buffer
687 * (-1 because of extra separator included in StringLength from above)
688 * Allow creation of zero-length strings from zero-length buffers.
695 ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
698 return_ACPI_STATUS (AE_NO_MEMORY);
701 NewBuf = ReturnDesc->Buffer.Pointer;
704 * Convert buffer bytes to hex or decimal values
705 * (separated by commas or spaces)
707 for (i = 0; i < ObjDesc->Buffer.Length; i++)
711 /* Emit 0x prefix for explict/implicit hex conversion */
717 NewBuf += AcpiExConvertToAscii (
718 (UINT64) ObjDesc->Buffer.Pointer[i], Base, NewBuf, 1);
720 /* Each digit is separated by either a comma or space */
722 *NewBuf++ = Separator;
726 * Null terminate the string
727 * (overwrites final comma/space from above)
729 if (ObjDesc->Buffer.Length)
738 return_ACPI_STATUS (AE_TYPE);
741 *ResultDesc = ReturnDesc;
742 return_ACPI_STATUS (AE_OK);
746 /*******************************************************************************
748 * FUNCTION: AcpiExConvertToTargetType
750 * PARAMETERS: DestinationType - Current type of the destination
751 * SourceDesc - Source object to be converted.
752 * ResultDesc - Where the converted object is returned
753 * WalkState - Current method state
757 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
759 ******************************************************************************/
762 AcpiExConvertToTargetType (
763 ACPI_OBJECT_TYPE DestinationType,
764 ACPI_OPERAND_OBJECT *SourceDesc,
765 ACPI_OPERAND_OBJECT **ResultDesc,
766 ACPI_WALK_STATE *WalkState)
768 ACPI_STATUS Status = AE_OK;
771 ACPI_FUNCTION_TRACE (ExConvertToTargetType);
774 /* Default behavior */
776 *ResultDesc = SourceDesc;
779 * If required by the target,
780 * perform implicit conversion on the source before we store it.
782 switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
784 case ARGI_SIMPLE_TARGET:
785 case ARGI_FIXED_TARGET:
786 case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
788 switch (DestinationType)
790 case ACPI_TYPE_LOCAL_REGION_FIELD:
792 * Named field can always handle conversions
798 /* No conversion allowed for these types */
800 if (DestinationType != SourceDesc->Common.Type)
802 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
803 "Explicit operator, will store (%s) over existing type (%s)\n",
804 AcpiUtGetObjectTypeName (SourceDesc),
805 AcpiUtGetTypeName (DestinationType)));
812 case ARGI_STORE_TARGET:
814 switch (DestinationType)
816 case ACPI_TYPE_INTEGER:
817 case ACPI_TYPE_BUFFER_FIELD:
818 case ACPI_TYPE_LOCAL_BANK_FIELD:
819 case ACPI_TYPE_LOCAL_INDEX_FIELD:
821 * These types require an Integer operand. We can convert
822 * a Buffer or a String to an Integer if necessary.
824 Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
825 ACPI_IMPLICIT_CONVERSION);
828 case ACPI_TYPE_STRING:
830 * The operand must be a String. We can convert an
831 * Integer or Buffer if necessary
833 Status = AcpiExConvertToString (SourceDesc, ResultDesc,
834 ACPI_IMPLICIT_CONVERT_HEX);
837 case ACPI_TYPE_BUFFER:
839 * The operand must be a Buffer. We can convert an
840 * Integer or String if necessary
842 Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
847 ACPI_ERROR ((AE_INFO,
848 "Bad destination type during conversion: 0x%X",
850 Status = AE_AML_INTERNAL;
857 * CreateXxxxField cases - we are storing the field object into the name
863 ACPI_ERROR ((AE_INFO,
864 "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
865 GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
866 WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
867 Status = AE_AML_INTERNAL;
871 * Source-to-Target conversion semantics:
873 * If conversion to the target type cannot be performed, then simply
874 * overwrite the target with the new object and type.
876 if (Status == AE_TYPE)
881 return_ACPI_STATUS (Status);