1ddbd7b91SNathan Whitehorn/******************************************************************************
2ddbd7b91SNathan Whitehorn *
3ddbd7b91SNathan Whitehorn * Module Name: exconvrt - Object conversion routines
4ddbd7b91SNathan Whitehorn *
5ddbd7b91SNathan Whitehorn *****************************************************************************/
6ddbd7b91SNathan Whitehorn
7ddbd7b91SNathan Whitehorn/******************************************************************************
8ddbd7b91SNathan Whitehorn *
9ddbd7b91SNathan Whitehorn * 1. Copyright Notice
10ddbd7b91SNathan Whitehorn *
11ff2e2f81SFredrik Holmqvist * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12ddbd7b91SNathan Whitehorn * All rights reserved.
13ddbd7b91SNathan Whitehorn *
14ddbd7b91SNathan Whitehorn * 2. License
15ddbd7b91SNathan Whitehorn *
16ddbd7b91SNathan Whitehorn * 2.1. This is your license from Intel Corp. under its intellectual property
179b0d045cSFredrik Holmqvist * rights. You may have additional license terms from the party that provided
18ddbd7b91SNathan Whitehorn * you this software, covering your right to use that party's intellectual
19ddbd7b91SNathan Whitehorn * property rights.
20ddbd7b91SNathan Whitehorn *
21ddbd7b91SNathan Whitehorn * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22ddbd7b91SNathan Whitehorn * copy of the source code appearing in this file ("Covered Code") an
23ddbd7b91SNathan Whitehorn * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24ddbd7b91SNathan Whitehorn * base code distributed originally by Intel ("Original Intel Code") to copy,
25ddbd7b91SNathan Whitehorn * make derivatives, distribute, use and display any portion of the Covered
26ddbd7b91SNathan Whitehorn * Code in any form, with the right to sublicense such rights; and
27ddbd7b91SNathan Whitehorn *
28ddbd7b91SNathan Whitehorn * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29ddbd7b91SNathan Whitehorn * license (with the right to sublicense), under only those claims of Intel
30ddbd7b91SNathan Whitehorn * patents that are infringed by the Original Intel Code, to make, use, sell,
31ddbd7b91SNathan Whitehorn * offer to sell, and import the Covered Code and derivative works thereof
32ddbd7b91SNathan Whitehorn * solely to the minimum extent necessary to exercise the above copyright
33ddbd7b91SNathan Whitehorn * license, and in no event shall the patent license extend to any additions
349b0d045cSFredrik Holmqvist * to or modifications of the Original Intel Code. No other license or right
35ddbd7b91SNathan Whitehorn * is granted directly or by implication, estoppel or otherwise;
36ddbd7b91SNathan Whitehorn *
37ddbd7b91SNathan Whitehorn * The above copyright and patent license is granted only if the following
38ddbd7b91SNathan Whitehorn * conditions are met:
39ddbd7b91SNathan Whitehorn *
40ddbd7b91SNathan Whitehorn * 3. Conditions
41ddbd7b91SNathan Whitehorn *
42ddbd7b91SNathan Whitehorn * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43ddbd7b91SNathan Whitehorn * Redistribution of source code of any substantial portion of the Covered
44ddbd7b91SNathan Whitehorn * Code or modification with rights to further distribute source must include
45ddbd7b91SNathan Whitehorn * the above Copyright Notice, the above License, this list of Conditions,
469b0d045cSFredrik Holmqvist * and the following Disclaimer and Export Compliance provision. In addition,
47ddbd7b91SNathan Whitehorn * Licensee must cause all Covered Code to which Licensee contributes to
48ddbd7b91SNathan Whitehorn * contain a file documenting the changes Licensee made to create that Covered
499b0d045cSFredrik Holmqvist * Code and the date of any change. Licensee must include in that file the
509b0d045cSFredrik Holmqvist * documentation of any changes made by any predecessor Licensee. Licensee
51ddbd7b91SNathan Whitehorn * must include a prominent statement that the modification is derived,
52ddbd7b91SNathan Whitehorn * directly or indirectly, from Original Intel Code.
53ddbd7b91SNathan Whitehorn *
54ddbd7b91SNathan Whitehorn * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55ddbd7b91SNathan Whitehorn * Redistribution of source code of any substantial portion of the Covered
56ddbd7b91SNathan Whitehorn * Code or modification without rights to further distribute source must
57ddbd7b91SNathan Whitehorn * include the following Disclaimer and Export Compliance provision in the
589b0d045cSFredrik Holmqvist * documentation and/or other materials provided with distribution. In
59ddbd7b91SNathan Whitehorn * addition, Licensee may not authorize further sublicense of source of any
60ddbd7b91SNathan Whitehorn * portion of the Covered Code, and must include terms to the effect that the
61ddbd7b91SNathan Whitehorn * license from Licensee to its licensee is limited to the intellectual
62ddbd7b91SNathan Whitehorn * property embodied in the software Licensee provides to its licensee, and
63ddbd7b91SNathan Whitehorn * not to intellectual property embodied in modifications its licensee may
64ddbd7b91SNathan Whitehorn * make.
65ddbd7b91SNathan Whitehorn *
66ddbd7b91SNathan Whitehorn * 3.3. Redistribution of Executable. Redistribution in executable form of any
67ddbd7b91SNathan Whitehorn * substantial portion of the Covered Code or modification must reproduce the
68ddbd7b91SNathan Whitehorn * above Copyright Notice, and the following Disclaimer and Export Compliance
69ddbd7b91SNathan Whitehorn * provision in the documentation and/or other materials provided with the
70ddbd7b91SNathan Whitehorn * distribution.
71ddbd7b91SNathan Whitehorn *
72ddbd7b91SNathan Whitehorn * 3.4. Intel retains all right, title, and interest in and to the Original
73ddbd7b91SNathan Whitehorn * Intel Code.
74ddbd7b91SNathan Whitehorn *
75ddbd7b91SNathan Whitehorn * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76ddbd7b91SNathan Whitehorn * Intel shall be used in advertising or otherwise to promote the sale, use or
77ddbd7b91SNathan Whitehorn * other dealings in products derived from or relating to the Covered Code
78ddbd7b91SNathan Whitehorn * without prior written authorization from Intel.
79ddbd7b91SNathan Whitehorn *
80ddbd7b91SNathan Whitehorn * 4. Disclaimer and Export Compliance
81ddbd7b91SNathan Whitehorn *
82ddbd7b91SNathan Whitehorn * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
839b0d045cSFredrik Holmqvist * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
849b0d045cSFredrik Holmqvist * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
859b0d045cSFredrik Holmqvist * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
869b0d045cSFredrik Holmqvist * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87ddbd7b91SNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88ddbd7b91SNathan Whitehorn * PARTICULAR PURPOSE.
89ddbd7b91SNathan Whitehorn *
90ddbd7b91SNathan Whitehorn * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91ddbd7b91SNathan Whitehorn * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92ddbd7b91SNathan Whitehorn * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93ddbd7b91SNathan Whitehorn * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94ddbd7b91SNathan Whitehorn * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
959b0d045cSFredrik Holmqvist * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96ddbd7b91SNathan Whitehorn * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97ddbd7b91SNathan Whitehorn * LIMITED REMEDY.
98ddbd7b91SNathan Whitehorn *
99ddbd7b91SNathan Whitehorn * 4.3. Licensee shall not export, either directly or indirectly, any of this
100ddbd7b91SNathan Whitehorn * software or system incorporating such software without first obtaining any
101ddbd7b91SNathan Whitehorn * required license or other approval from the U. S. Department of Commerce or
1029b0d045cSFredrik Holmqvist * any other agency or department of the United States Government. In the
103ddbd7b91SNathan Whitehorn * event Licensee exports any such software from the United States or
104ddbd7b91SNathan Whitehorn * re-exports any such software from a foreign destination, Licensee shall
105ddbd7b91SNathan Whitehorn * ensure that the distribution and export/re-export of the software is in
106ddbd7b91SNathan Whitehorn * compliance with all laws, regulations, orders, or other restrictions of the
107ddbd7b91SNathan Whitehorn * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108ddbd7b91SNathan Whitehorn * any of its subsidiaries will export/re-export any technical data, process,
109ddbd7b91SNathan Whitehorn * software, or service, directly or indirectly, to any country for which the
110ddbd7b91SNathan Whitehorn * United States government or any agency thereof requires an export license,
111ddbd7b91SNathan Whitehorn * other governmental approval, or letter of assurance, without first obtaining
112ddbd7b91SNathan Whitehorn * such license, approval or letter.
113ddbd7b91SNathan Whitehorn *
114ff2e2f81SFredrik Holmqvist *****************************************************************************
115ff2e2f81SFredrik Holmqvist *
116ff2e2f81SFredrik Holmqvist * Alternatively, you may choose to be licensed under the terms of the
117ff2e2f81SFredrik Holmqvist * following license:
118ff2e2f81SFredrik Holmqvist *
119ff2e2f81SFredrik Holmqvist * Redistribution and use in source and binary forms, with or without
120ff2e2f81SFredrik Holmqvist * modification, are permitted provided that the following conditions
121ff2e2f81SFredrik Holmqvist * are met:
122ff2e2f81SFredrik Holmqvist * 1. Redistributions of source code must retain the above copyright
123ff2e2f81SFredrik Holmqvist *    notice, this list of conditions, and the following disclaimer,
124ff2e2f81SFredrik Holmqvist *    without modification.
125ff2e2f81SFredrik Holmqvist * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126ff2e2f81SFredrik Holmqvist *    substantially similar to the "NO WARRANTY" disclaimer below
127ff2e2f81SFredrik Holmqvist *    ("Disclaimer") and any redistribution must be conditioned upon
128ff2e2f81SFredrik Holmqvist *    including a substantially similar Disclaimer requirement for further
129ff2e2f81SFredrik Holmqvist *    binary redistribution.
130ff2e2f81SFredrik Holmqvist * 3. Neither the names of the above-listed copyright holders nor the names
131ff2e2f81SFredrik Holmqvist *    of any contributors may be used to endorse or promote products derived
132ff2e2f81SFredrik Holmqvist *    from this software without specific prior written permission.
133ff2e2f81SFredrik Holmqvist *
134ff2e2f81SFredrik Holmqvist * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135ff2e2f81SFredrik Holmqvist * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136ff2e2f81SFredrik Holmqvist * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137ff2e2f81SFredrik Holmqvist * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138ff2e2f81SFredrik Holmqvist * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139ff2e2f81SFredrik Holmqvist * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140ff2e2f81SFredrik Holmqvist * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141ff2e2f81SFredrik Holmqvist * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142ff2e2f81SFredrik Holmqvist * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143ff2e2f81SFredrik Holmqvist * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144ff2e2f81SFredrik Holmqvist * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145ff2e2f81SFredrik Holmqvist *
146ff2e2f81SFredrik Holmqvist * Alternatively, you may choose to be licensed under the terms of the
147ff2e2f81SFredrik Holmqvist * GNU General Public License ("GPL") version 2 as published by the Free
148ff2e2f81SFredrik Holmqvist * Software Foundation.
149ff2e2f81SFredrik Holmqvist *
150ddbd7b91SNathan Whitehorn *****************************************************************************/
151ddbd7b91SNathan Whitehorn
152ddbd7b91SNathan Whitehorn#include "acpi.h"
15380377d9aSJérôme Duval#include "accommon.h"
154ddbd7b91SNathan Whitehorn#include "acinterp.h"
155ddbd7b91SNathan Whitehorn#include "amlcode.h"
156ddbd7b91SNathan Whitehorn
157ddbd7b91SNathan Whitehorn
158ddbd7b91SNathan Whitehorn#define _COMPONENT          ACPI_EXECUTER
159ddbd7b91SNathan Whitehorn        ACPI_MODULE_NAME    ("exconvrt")
160ddbd7b91SNathan Whitehorn
16143547dbeSJérôme Duval/* Local prototypes */
16243547dbeSJérôme Duval
16343547dbeSJérôme Duvalstatic UINT32
16443547dbeSJérôme DuvalAcpiExConvertToAscii (
16557c418cdSFredrik Holmqvist    UINT64                  Integer,
16643547dbeSJérôme Duval    UINT16                  Base,
16743547dbeSJérôme Duval    UINT8                   *String,
16843547dbeSJérôme Duval    UINT8                   MaxLength);
16943547dbeSJérôme Duval
170ddbd7b91SNathan Whitehorn
171ddbd7b91SNathan Whitehorn/*******************************************************************************
172ddbd7b91SNathan Whitehorn *
173ddbd7b91SNathan Whitehorn * FUNCTION:    AcpiExConvertToInteger
174ddbd7b91SNathan Whitehorn *
175ff2e2f81SFredrik Holmqvist * PARAMETERS:  ObjDesc             - Object to be converted. Must be an
176ff2e2f81SFredrik Holmqvist *                                    Integer, Buffer, or String
177ff2e2f81SFredrik Holmqvist *              ResultDesc          - Where the new Integer object is returned
178ff2e2f81SFredrik Holmqvist *              ImplicitConversion  - Used for string conversion
179ddbd7b91SNathan Whitehorn *
180ddbd7b91SNathan Whitehorn * RETURN:      Status
181ddbd7b91SNathan Whitehorn *
182ddbd7b91SNathan Whitehorn * DESCRIPTION: Convert an ACPI Object to an integer.
183ddbd7b91SNathan Whitehorn *
184ddbd7b91SNathan Whitehorn ******************************************************************************/
185ddbd7b91SNathan Whitehorn
186ddbd7b91SNathan WhitehornACPI_STATUS
187ddbd7b91SNathan WhitehornAcpiExConvertToInteger (
188ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *ObjDesc,
189ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     **ResultDesc,
190ff2e2f81SFredrik Holmqvist    UINT32                  ImplicitConversion)
191ddbd7b91SNathan Whitehorn{
192ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *ReturnDesc;
193ddbd7b91SNathan Whitehorn    UINT8                   *Pointer;
19457c418cdSFredrik Holmqvist    UINT64                  Result;
195ddbd7b91SNathan Whitehorn    UINT32                  i;
196ddbd7b91SNathan Whitehorn    UINT32                  Count;
197ddbd7b91SNathan Whitehorn
198ddbd7b91SNathan Whitehorn
19943547dbeSJérôme Duval    ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger, ObjDesc);
200ddbd7b91SNathan Whitehorn
201ddbd7b91SNathan Whitehorn
20280377d9aSJérôme Duval    switch (ObjDesc->Common.Type)
203ddbd7b91SNathan Whitehorn    {
204ddbd7b91SNathan Whitehorn    case ACPI_TYPE_INTEGER:
205ddbd7b91SNathan Whitehorn
206ddbd7b91SNathan Whitehorn        /* No conversion necessary */
207ddbd7b91SNathan Whitehorn
208ddbd7b91SNathan Whitehorn        *ResultDesc = ObjDesc;
209ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_OK);
210ddbd7b91SNathan Whitehorn
211ddbd7b91SNathan Whitehorn    case ACPI_TYPE_BUFFER:
212ddbd7b91SNathan Whitehorn    case ACPI_TYPE_STRING:
213ddbd7b91SNathan Whitehorn
214ddbd7b91SNathan Whitehorn        /* Note: Takes advantage of common buffer/string fields */
215ddbd7b91SNathan Whitehorn
216ddbd7b91SNathan Whitehorn        Pointer = ObjDesc->Buffer.Pointer;
217ddbd7b91SNathan Whitehorn        Count   = ObjDesc->Buffer.Length;
218ddbd7b91SNathan Whitehorn        break;
219ddbd7b91SNathan Whitehorn
220ddbd7b91SNathan Whitehorn    default:
221c70258b7SJérôme Duval
222ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_TYPE);
223ddbd7b91SNathan Whitehorn    }
224ddbd7b91SNathan Whitehorn
225ddbd7b91SNathan Whitehorn    /*
22680377d9aSJérôme Duval     * Convert the buffer/string to an integer. Note that both buffers and
227ddbd7b91SNathan Whitehorn     * strings are treated as raw data - we don't convert ascii to hex for
228ddbd7b91SNathan Whitehorn     * strings.
229ddbd7b91SNathan Whitehorn     *
230ddbd7b91SNathan Whitehorn     * There are two terminating conditions for the loop:
231ddbd7b91SNathan Whitehorn     * 1) The size of an integer has been reached, or
232ddbd7b91SNathan Whitehorn     * 2) The end of the buffer or string has been reached
233ddbd7b91SNathan Whitehorn     */
234ddbd7b91SNathan Whitehorn    Result = 0;
235ddbd7b91SNathan Whitehorn
23643547dbeSJérôme Duval    /* String conversion is different than Buffer conversion */
23743547dbeSJérôme Duval
23880377d9aSJérôme Duval    switch (ObjDesc->Common.Type)
239ddbd7b91SNathan Whitehorn    {
240ddbd7b91SNathan Whitehorn    case ACPI_TYPE_STRING:
241ddbd7b91SNathan Whitehorn        /*
242ddbd7b91SNathan Whitehorn         * Convert string to an integer - for most cases, the string must be
24380377d9aSJérôme Duval         * hexadecimal as per the ACPI specification. The only exception (as
244ddbd7b91SNathan Whitehorn         * of ACPI 3.0) is that the ToInteger() operator allows both decimal
245ddbd7b91SNathan Whitehorn         * and hexadecimal strings (hex prefixed with "0x").
246ff2e2f81SFredrik Holmqvist         *
247ff2e2f81SFredrik Holmqvist         * Explicit conversion is used only by ToInteger.
248ff2e2f81SFredrik Holmqvist         * All other string-to-integer conversions are implicit conversions.
249ddbd7b91SNathan Whitehorn         */
250ff2e2f81SFredrik Holmqvist        if (ImplicitConversion)
251ff2e2f81SFredrik Holmqvist        {
252ff2e2f81SFredrik Holmqvist            Result = AcpiUtImplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer));
253ff2e2f81SFredrik Holmqvist        }
254ff2e2f81SFredrik Holmqvist        else
255ddbd7b91SNathan Whitehorn        {
256ff2e2f81SFredrik Holmqvist            Result = AcpiUtExplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer));
257ddbd7b91SNathan Whitehorn        }
258ddbd7b91SNathan Whitehorn        break;
259ddbd7b91SNathan Whitehorn
260ddbd7b91SNathan Whitehorn    case ACPI_TYPE_BUFFER:
261ddbd7b91SNathan Whitehorn
262ddbd7b91SNathan Whitehorn        /* Check for zero-length buffer */
263ddbd7b91SNathan Whitehorn
264ddbd7b91SNathan Whitehorn        if (!Count)
265ddbd7b91SNathan Whitehorn        {
266ddbd7b91SNathan Whitehorn            return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
267ddbd7b91SNathan Whitehorn        }
268ddbd7b91SNathan Whitehorn
269ddbd7b91SNathan Whitehorn        /* Transfer no more than an integer's worth of data */
270ddbd7b91SNathan Whitehorn
271ddbd7b91SNathan Whitehorn        if (Count > AcpiGbl_IntegerByteWidth)
272ddbd7b91SNathan Whitehorn        {
273ddbd7b91SNathan Whitehorn            Count = AcpiGbl_IntegerByteWidth;
274ddbd7b91SNathan Whitehorn        }
275ddbd7b91SNathan Whitehorn
276ddbd7b91SNathan Whitehorn        /*
277ddbd7b91SNathan Whitehorn         * Convert buffer to an integer - we simply grab enough raw data
278ddbd7b91SNathan Whitehorn         * from the buffer to fill an integer
279ddbd7b91SNathan Whitehorn         */
280ddbd7b91SNathan Whitehorn        for (i = 0; i < Count; i++)
281ddbd7b91SNathan Whitehorn        {
282ddbd7b91SNathan Whitehorn            /*
283ddbd7b91SNathan Whitehorn             * Get next byte and shift it into the Result.
284ddbd7b91SNathan Whitehorn             * Little endian is used, meaning that the first byte of the buffer
285ddbd7b91SNathan Whitehorn             * is the LSB of the integer
286ddbd7b91SNathan Whitehorn             */
28757c418cdSFredrik Holmqvist            Result |= (((UINT64) Pointer[i]) << (i * 8));
288ddbd7b91SNathan Whitehorn        }
289ddbd7b91SNathan Whitehorn        break;
290ddbd7b91SNathan Whitehorn
291ddbd7b91SNathan Whitehorn    default:
29280377d9aSJérôme Duval
293ddbd7b91SNathan Whitehorn        /* No other types can get here */
294c70258b7SJérôme Duval
295ddbd7b91SNathan Whitehorn        break;
296ddbd7b91SNathan Whitehorn    }
297ddbd7b91SNathan Whitehorn
29843547dbeSJérôme Duval    /* Create a new integer */
29943547dbeSJérôme Duval
30057c418cdSFredrik Holmqvist    ReturnDesc = AcpiUtCreateIntegerObject (Result);
301ddbd7b91SNathan Whitehorn    if (!ReturnDesc)
302ddbd7b91SNathan Whitehorn    {
303ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_NO_MEMORY);
304ddbd7b91SNathan Whitehorn    }
305ddbd7b91SNathan Whitehorn
30639d02f89SJérôme Duval    ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
30739d02f89SJérôme Duval        ACPI_FORMAT_UINT64 (Result)));
30839d02f89SJérôme Duval
309ddbd7b91SNathan Whitehorn    /* Save the Result */
310ddbd7b91SNathan Whitehorn
311c70258b7SJérôme Duval    (void) AcpiExTruncateFor32bitTable (ReturnDesc);
312ddbd7b91SNathan Whitehorn    *ResultDesc = ReturnDesc;
313ddbd7b91SNathan Whitehorn    return_ACPI_STATUS (AE_OK);
314ddbd7b91SNathan Whitehorn}
315ddbd7b91SNathan Whitehorn
316ddbd7b91SNathan Whitehorn
317ddbd7b91SNathan Whitehorn/*******************************************************************************
318ddbd7b91SNathan Whitehorn *
319ddbd7b91SNathan Whitehorn * FUNCTION:    AcpiExConvertToBuffer
320ddbd7b91SNathan Whitehorn *
32180377d9aSJérôme Duval * PARAMETERS:  ObjDesc         - Object to be converted. Must be an
322ddbd7b91SNathan Whitehorn *                                Integer, Buffer, or String
323ddbd7b91SNathan Whitehorn *              ResultDesc      - Where the new buffer object is returned
324ddbd7b91SNathan Whitehorn *
325ddbd7b91SNathan Whitehorn * RETURN:      Status
326ddbd7b91SNathan Whitehorn *
327ddbd7b91SNathan Whitehorn * DESCRIPTION: Convert an ACPI Object to a Buffer
328ddbd7b91SNathan Whitehorn *
329ddbd7b91SNathan Whitehorn ******************************************************************************/
330ddbd7b91SNathan Whitehorn
331ddbd7b91SNathan WhitehornACPI_STATUS
332ddbd7b91SNathan WhitehornAcpiExConvertToBuffer (
333ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *ObjDesc,
334ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     **ResultDesc)
335ddbd7b91SNathan Whitehorn{
336ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *ReturnDesc;
337ddbd7b91SNathan Whitehorn    UINT8                   *NewBuf;
338ddbd7b91SNathan Whitehorn
339ddbd7b91SNathan Whitehorn
34043547dbeSJérôme Duval    ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer, ObjDesc);
341ddbd7b91SNathan Whitehorn
342ddbd7b91SNathan Whitehorn
34380377d9aSJérôme Duval    switch (ObjDesc->Common.Type)
344ddbd7b91SNathan Whitehorn    {
345ddbd7b91SNathan Whitehorn    case ACPI_TYPE_BUFFER:
346ddbd7b91SNathan Whitehorn
347ddbd7b91SNathan Whitehorn        /* No conversion necessary */
348ddbd7b91SNathan Whitehorn
349ddbd7b91SNathan Whitehorn        *ResultDesc = ObjDesc;
350ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_OK);
351ddbd7b91SNathan Whitehorn
352ddbd7b91SNathan Whitehorn
353ddbd7b91SNathan Whitehorn    case ACPI_TYPE_INTEGER:
354ddbd7b91SNathan Whitehorn        /*
355ddbd7b91SNathan Whitehorn         * Create a new Buffer object.
356ddbd7b91SNathan Whitehorn         * Need enough space for one integer
357ddbd7b91SNathan Whitehorn         */
358ddbd7b91SNathan Whitehorn        ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
359ddbd7b91SNathan Whitehorn        if (!ReturnDesc)
360ddbd7b91SNathan Whitehorn        {
361ddbd7b91SNathan Whitehorn            return_ACPI_STATUS (AE_NO_MEMORY);
362ddbd7b91SNathan Whitehorn        }
363ddbd7b91SNathan Whitehorn
364ddbd7b91SNathan Whitehorn        /* Copy the integer to the buffer, LSB first */
365ddbd7b91SNathan Whitehorn
366ddbd7b91SNathan Whitehorn        NewBuf = ReturnDesc->Buffer.Pointer;
367b64e8511SFredrik Holmqvist        memcpy (NewBuf, &ObjDesc->Integer.Value, AcpiGbl_IntegerByteWidth);
368ddbd7b91SNathan Whitehorn        break;
369ddbd7b91SNathan Whitehorn
370ddbd7b91SNathan Whitehorn    case ACPI_TYPE_STRING:
371ddbd7b91SNathan Whitehorn        /*
372ddbd7b91SNathan Whitehorn         * Create a new Buffer object
373ddbd7b91SNathan Whitehorn         * Size will be the string length
374ddbd7b91SNathan Whitehorn         *
375ddbd7b91SNathan Whitehorn         * NOTE: Add one to the string length to include the null terminator.
376ddbd7b91SNathan Whitehorn         * The ACPI spec is unclear on this subject, but there is existing
377ddbd7b91SNathan Whitehorn         * ASL/AML code that depends on the null being transferred to the new
378ddbd7b91SNathan Whitehorn         * buffer.
379ddbd7b91SNathan Whitehorn         */
380b64e8511SFredrik Holmqvist        ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
381b64e8511SFredrik Holmqvist            ObjDesc->String.Length + 1);
382ddbd7b91SNathan Whitehorn        if (!ReturnDesc)
383ddbd7b91SNathan Whitehorn        {
384ddbd7b91SNathan Whitehorn            return_ACPI_STATUS (AE_NO_MEMORY);
385ddbd7b91SNathan Whitehorn        }
386ddbd7b91SNathan Whitehorn
387ddbd7b91SNathan Whitehorn        /* Copy the string to the buffer */
388ddbd7b91SNathan Whitehorn
389ddbd7b91SNathan Whitehorn        NewBuf = ReturnDesc->Buffer.Pointer;
390e226d1d0SFredrik Holmqvist        strncpy ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
391ddbd7b91SNathan Whitehorn            ObjDesc->String.Length);
392ddbd7b91SNathan Whitehorn        break;
393ddbd7b91SNathan Whitehorn
394ddbd7b91SNathan Whitehorn    default:
395c70258b7SJérôme Duval
396ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_TYPE);
397ddbd7b91SNathan Whitehorn    }
398ddbd7b91SNathan Whitehorn
399ddbd7b91SNathan Whitehorn    /* Mark buffer initialized */
400ddbd7b91SNathan Whitehorn
401ddbd7b91SNathan Whitehorn    ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
402ddbd7b91SNathan Whitehorn    *ResultDesc = ReturnDesc;
403ddbd7b91SNathan Whitehorn    return_ACPI_STATUS (AE_OK);
404ddbd7b91SNathan Whitehorn}
405ddbd7b91SNathan Whitehorn
406ddbd7b91SNathan Whitehorn
407ddbd7b91SNathan Whitehorn/*******************************************************************************
408ddbd7b91SNathan Whitehorn *
409ddbd7b91SNathan Whitehorn * FUNCTION:    AcpiExConvertToAscii
410ddbd7b91SNathan Whitehorn *
411ddbd7b91SNathan Whitehorn * PARAMETERS:  Integer         - Value to be converted
412ddbd7b91SNathan Whitehorn *              Base            - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
413ddbd7b91SNathan Whitehorn *              String          - Where the string is returned
414ddbd7b91SNathan Whitehorn *              DataWidth       - Size of data item to be converted, in bytes
415ddbd7b91SNathan Whitehorn *
416ddbd7b91SNathan Whitehorn * RETURN:      Actual string length
417ddbd7b91SNathan Whitehorn *
418ddbd7b91SNathan Whitehorn * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
419ddbd7b91SNathan Whitehorn *
420ddbd7b91SNathan Whitehorn ******************************************************************************/
421ddbd7b91SNathan Whitehorn
42243547dbeSJérôme Duvalstatic UINT32
423ddbd7b91SNathan WhitehornAcpiExConvertToAscii (
42457c418cdSFredrik Holmqvist    UINT64                  Integer,
425ddbd7b91SNathan Whitehorn    UINT16                  Base,
426ddbd7b91SNathan Whitehorn    UINT8                   *String,
427ddbd7b91SNathan Whitehorn    UINT8                   DataWidth)
428ddbd7b91SNathan Whitehorn{
42957c418cdSFredrik Holmqvist    UINT64                  Digit;
43045e9de08SJérôme Duval    UINT32                  i;
43145e9de08SJérôme Duval    UINT32                  j;
43245e9de08SJérôme Duval    UINT32                  k = 0;
43345e9de08SJérôme Duval    UINT32                  HexLength;
43445e9de08SJérôme Duval    UINT32                  DecimalLength;
435ddbd7b91SNathan Whitehorn    UINT32                  Remainder;
436ddbd7b91SNathan Whitehorn    BOOLEAN                 SupressZeros;
437ddbd7b91SNathan Whitehorn
438ddbd7b91SNathan Whitehorn
439ddbd7b91SNathan Whitehorn    ACPI_FUNCTION_ENTRY ();
440ddbd7b91SNathan Whitehorn
441ddbd7b91SNathan Whitehorn
442ddbd7b91SNathan Whitehorn    switch (Base)
443ddbd7b91SNathan Whitehorn    {
444ddbd7b91SNathan Whitehorn    case 10:
445ddbd7b91SNathan Whitehorn
446ddbd7b91SNathan Whitehorn        /* Setup max length for the decimal number */
447ddbd7b91SNathan Whitehorn
448ddbd7b91SNathan Whitehorn        switch (DataWidth)
449ddbd7b91SNathan Whitehorn        {
450ddbd7b91SNathan Whitehorn        case 1:
451c70258b7SJérôme Duval
452ddbd7b91SNathan Whitehorn            DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
453ddbd7b91SNathan Whitehorn            break;
454ddbd7b91SNathan Whitehorn
455ddbd7b91SNathan Whitehorn        case 4:
456c70258b7SJérôme Duval
457ddbd7b91SNathan Whitehorn            DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
458ddbd7b91SNathan Whitehorn            break;
459ddbd7b91SNathan Whitehorn
460ddbd7b91SNathan Whitehorn        case 8:
461ddbd7b91SNathan Whitehorn        default:
462c70258b7SJérôme Duval
463ddbd7b91SNathan Whitehorn            DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
464ddbd7b91SNathan Whitehorn            break;
465ddbd7b91SNathan Whitehorn        }
466ddbd7b91SNathan Whitehorn
467ddbd7b91SNathan Whitehorn        SupressZeros = TRUE;     /* No leading zeros */
468ddbd7b91SNathan Whitehorn        Remainder = 0;
469ddbd7b91SNathan Whitehorn
470ddbd7b91SNathan Whitehorn        for (i = DecimalLength; i > 0; i--)
471ddbd7b91SNathan Whitehorn        {
472ddbd7b91SNathan Whitehorn            /* Divide by nth factor of 10 */
473ddbd7b91SNathan Whitehorn
474ddbd7b91SNathan Whitehorn            Digit = Integer;
475ddbd7b91SNathan Whitehorn            for (j = 0; j < i; j++)
476ddbd7b91SNathan Whitehorn            {
477ddbd7b91SNathan Whitehorn                (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
478ddbd7b91SNathan Whitehorn            }
479ddbd7b91SNathan Whitehorn
480ddbd7b91SNathan Whitehorn            /* Handle leading zeros */
481ddbd7b91SNathan Whitehorn
482ddbd7b91SNathan Whitehorn            if (Remainder != 0)
483ddbd7b91SNathan Whitehorn            {
484ddbd7b91SNathan Whitehorn                SupressZeros = FALSE;
485ddbd7b91SNathan Whitehorn            }
486ddbd7b91SNathan Whitehorn
487ddbd7b91SNathan Whitehorn            if (!SupressZeros)
488ddbd7b91SNathan Whitehorn            {
489ddbd7b91SNathan Whitehorn                String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
490ddbd7b91SNathan Whitehorn                k++;
491ddbd7b91SNathan Whitehorn            }
492ddbd7b91SNathan Whitehorn        }
493ddbd7b91SNathan Whitehorn        break;
494ddbd7b91SNathan Whitehorn
495ddbd7b91SNathan Whitehorn    case 16:
496ddbd7b91SNathan Whitehorn
49743547dbeSJérôme Duval        /* HexLength: 2 ascii hex chars per data byte */
498ddbd7b91SNathan Whitehorn
49908c9948cSFredrik Holmqvist        HexLength = (DataWidth * 2);
500ddbd7b91SNathan Whitehorn        for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
501ddbd7b91SNathan Whitehorn        {
502ddbd7b91SNathan Whitehorn            /* Get one hex digit, most significant digits first */
503ddbd7b91SNathan Whitehorn
504b64e8511SFredrik Holmqvist            String[k] = (UINT8)
505b64e8511SFredrik Holmqvist                AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
506ddbd7b91SNathan Whitehorn            k++;
507ddbd7b91SNathan Whitehorn        }
508ddbd7b91SNathan Whitehorn        break;
509ddbd7b91SNathan Whitehorn
510ddbd7b91SNathan Whitehorn    default:
511ddbd7b91SNathan Whitehorn        return (0);
512ddbd7b91SNathan Whitehorn    }
513ddbd7b91SNathan Whitehorn
514ddbd7b91SNathan Whitehorn    /*
51580377d9aSJérôme Duval     * Since leading zeros are suppressed, we must check for the case where
516ddbd7b91SNathan Whitehorn     * the integer equals 0
517ddbd7b91SNathan Whitehorn     *
518ddbd7b91SNathan Whitehorn     * Finally, null terminate the string and return the length
519ddbd7b91SNathan Whitehorn     */
520ddbd7b91SNathan Whitehorn    if (!k)
521ddbd7b91SNathan Whitehorn    {
522ddbd7b91SNathan Whitehorn        String [0] = ACPI_ASCII_ZERO;
523ddbd7b91SNathan Whitehorn        k = 1;
524ddbd7b91SNathan Whitehorn    }
525ddbd7b91SNathan Whitehorn
526ddbd7b91SNathan Whitehorn    String [k] = 0;
527ddbd7b91SNathan Whitehorn    return ((UINT32) k);
528ddbd7b91SNathan Whitehorn}
529ddbd7b91SNathan Whitehorn
530ddbd7b91SNathan Whitehorn
531ddbd7b91SNathan Whitehorn/*******************************************************************************
532ddbd7b91SNathan Whitehorn *
533ddbd7b91SNathan Whitehorn * FUNCTION:    AcpiExConvertToString
534ddbd7b91SNathan Whitehorn *
53580377d9aSJérôme Duval * PARAMETERS:  ObjDesc         - Object to be converted. Must be an
536ddbd7b91SNathan Whitehorn *                                Integer, Buffer, or String
537ddbd7b91SNathan Whitehorn *              ResultDesc      - Where the string object is returned
538ddbd7b91SNathan Whitehorn *              Type            - String flags (base and conversion type)
539ddbd7b91SNathan Whitehorn *
540ddbd7b91SNathan Whitehorn * RETURN:      Status
541ddbd7b91SNathan Whitehorn *
54208c9948cSFredrik Holmqvist * DESCRIPTION: Convert an ACPI Object to a string. Supports both implicit
54308c9948cSFredrik Holmqvist *              and explicit conversions and related rules.
544ddbd7b91SNathan Whitehorn *
545ddbd7b91SNathan Whitehorn ******************************************************************************/
546ddbd7b91SNathan Whitehorn
547ddbd7b91SNathan WhitehornACPI_STATUS
548ddbd7b91SNathan WhitehornAcpiExConvertToString (
549ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *ObjDesc,
550ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     **ResultDesc,
551ddbd7b91SNathan Whitehorn    UINT32                  Type)
552ddbd7b91SNathan Whitehorn{
553ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *ReturnDesc;
554ddbd7b91SNathan Whitehorn    UINT8                   *NewBuf;
555ddbd7b91SNathan Whitehorn    UINT32                  i;
556ddbd7b91SNathan Whitehorn    UINT32                  StringLength = 0;
557ddbd7b91SNathan Whitehorn    UINT16                  Base = 16;
558ddbd7b91SNathan Whitehorn    UINT8                   Separator = ',';
559ddbd7b91SNathan Whitehorn
560ddbd7b91SNathan Whitehorn
56143547dbeSJérôme Duval    ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
562ddbd7b91SNathan Whitehorn
563ddbd7b91SNathan Whitehorn
56480377d9aSJérôme Duval    switch (ObjDesc->Common.Type)
565ddbd7b91SNathan Whitehorn    {
566ddbd7b91SNathan Whitehorn    case ACPI_TYPE_STRING:
567ddbd7b91SNathan Whitehorn
568ddbd7b91SNathan Whitehorn        /* No conversion necessary */
569ddbd7b91SNathan Whitehorn
570ddbd7b91SNathan Whitehorn        *ResultDesc = ObjDesc;
571ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_OK);
572ddbd7b91SNathan Whitehorn
573ddbd7b91SNathan Whitehorn    case ACPI_TYPE_INTEGER:
574ddbd7b91SNathan Whitehorn
575ddbd7b91SNathan Whitehorn        switch (Type)
576ddbd7b91SNathan Whitehorn        {
577ddbd7b91SNathan Whitehorn        case ACPI_EXPLICIT_CONVERT_DECIMAL:
57808c9948cSFredrik Holmqvist            /*
57908c9948cSFredrik Holmqvist             * From ToDecimalString, integer source.
58008c9948cSFredrik Holmqvist             *
58108c9948cSFredrik Holmqvist             * Make room for the maximum decimal number size
58208c9948cSFredrik Holmqvist             */
583ddbd7b91SNathan Whitehorn            StringLength = ACPI_MAX_DECIMAL_DIGITS;
584ddbd7b91SNathan Whitehorn            Base = 10;
585ddbd7b91SNathan Whitehorn            break;
586ddbd7b91SNathan Whitehorn
587ddbd7b91SNathan Whitehorn        default:
588ddbd7b91SNathan Whitehorn
589ddbd7b91SNathan Whitehorn            /* Two hex string characters for each integer byte */
590ddbd7b91SNathan Whitehorn
591ddbd7b91SNathan Whitehorn            StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
592ddbd7b91SNathan Whitehorn            break;
593ddbd7b91SNathan Whitehorn        }
594ddbd7b91SNathan Whitehorn
595ddbd7b91SNathan Whitehorn        /*
596ddbd7b91SNathan Whitehorn         * Create a new String
597ddbd7b91SNathan Whitehorn         * Need enough space for one ASCII integer (plus null terminator)
598ddbd7b91SNathan Whitehorn         */
599ddbd7b91SNathan Whitehorn        ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
600ddbd7b91SNathan Whitehorn        if (!ReturnDesc)
601ddbd7b91SNathan Whitehorn        {
602ddbd7b91SNathan Whitehorn            return_ACPI_STATUS (AE_NO_MEMORY);
603ddbd7b91SNathan Whitehorn        }
604ddbd7b91SNathan Whitehorn
605ddbd7b91SNathan Whitehorn        NewBuf = ReturnDesc->Buffer.Pointer;
606ddbd7b91SNathan Whitehorn
607ddbd7b91SNathan Whitehorn        /* Convert integer to string */
608ddbd7b91SNathan Whitehorn
609b64e8511SFredrik Holmqvist        StringLength = AcpiExConvertToAscii (
610b64e8511SFredrik Holmqvist            ObjDesc->Integer.Value, Base, NewBuf, AcpiGbl_IntegerByteWidth);
611ddbd7b91SNathan Whitehorn
612ddbd7b91SNathan Whitehorn        /* Null terminate at the correct place */
613ddbd7b91SNathan Whitehorn
614ddbd7b91SNathan Whitehorn        ReturnDesc->String.Length = StringLength;
615ddbd7b91SNathan Whitehorn        NewBuf [StringLength] = 0;
616ddbd7b91SNathan Whitehorn        break;
617ddbd7b91SNathan Whitehorn
618ddbd7b91SNathan Whitehorn    case ACPI_TYPE_BUFFER:
619ddbd7b91SNathan Whitehorn
620ddbd7b91SNathan Whitehorn        /* Setup string length, base, and separator */
621ddbd7b91SNathan Whitehorn
622ddbd7b91SNathan Whitehorn        switch (Type)
623ddbd7b91SNathan Whitehorn        {
62443547dbeSJérôme Duval        case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
625ddbd7b91SNathan Whitehorn            /*
62608c9948cSFredrik Holmqvist             * Explicit conversion from the ToDecimalString ASL operator.
62708c9948cSFredrik Holmqvist             *
62808c9948cSFredrik Holmqvist             * From ACPI: "If the input is a buffer, it is converted to a
62908c9948cSFredrik Holmqvist             * a string of decimal values separated by commas."
630ddbd7b91SNathan Whitehorn             */
631ddbd7b91SNathan Whitehorn            Base = 10;
632ddbd7b91SNathan Whitehorn
633ddbd7b91SNathan Whitehorn            /*
63480377d9aSJérôme Duval             * Calculate the final string length. Individual string values
635ddbd7b91SNathan Whitehorn             * are variable length (include separator for each)
636ddbd7b91SNathan Whitehorn             */
637ddbd7b91SNathan Whitehorn            for (i = 0; i < ObjDesc->Buffer.Length; i++)
638ddbd7b91SNathan Whitehorn            {
639ddbd7b91SNathan Whitehorn                if (ObjDesc->Buffer.Pointer[i] >= 100)
640ddbd7b91SNathan Whitehorn                {
641ddbd7b91SNathan Whitehorn                    StringLength += 4;
642ddbd7b91SNathan Whitehorn                }
643ddbd7b91SNathan Whitehorn                else if (ObjDesc->Buffer.Pointer[i] >= 10)
644ddbd7b91SNathan Whitehorn                {
645ddbd7b91SNathan Whitehorn                    StringLength += 3;
646ddbd7b91SNathan Whitehorn                }
647ddbd7b91SNathan Whitehorn                else
648ddbd7b91SNathan Whitehorn                {
649ddbd7b91SNathan Whitehorn                    StringLength += 2;
650ddbd7b91SNathan Whitehorn                }
651ddbd7b91SNathan Whitehorn            }
652ddbd7b91SNathan Whitehorn            break;
653ddbd7b91SNathan Whitehorn
654ddbd7b91SNathan Whitehorn        case ACPI_IMPLICIT_CONVERT_HEX:
655ddbd7b91SNathan Whitehorn            /*
65608c9948cSFredrik Holmqvist             * Implicit buffer-to-string conversion
65708c9948cSFredrik Holmqvist             *
658ddbd7b91SNathan Whitehorn             * From the ACPI spec:
65908c9948cSFredrik Holmqvist             * "The entire contents of the buffer are converted to a string of
660ddbd7b91SNathan Whitehorn             * two-character hexadecimal numbers, each separated by a space."
66108c9948cSFredrik Holmqvist             *
66208c9948cSFredrik Holmqvist             * Each hex number is prefixed with 0x (11/2018)
663ddbd7b91SNathan Whitehorn             */
664ddbd7b91SNathan Whitehorn            Separator = ' ';
66508c9948cSFredrik Holmqvist            StringLength = (ObjDesc->Buffer.Length * 5);
666ddbd7b91SNathan Whitehorn            break;
667ddbd7b91SNathan Whitehorn
66808c9948cSFredrik Holmqvist        case ACPI_EXPLICIT_CONVERT_HEX:
669ddbd7b91SNathan Whitehorn            /*
67008c9948cSFredrik Holmqvist             * Explicit conversion from the ToHexString ASL operator.
67108c9948cSFredrik Holmqvist             *
672ddbd7b91SNathan Whitehorn             * From ACPI: "If Data is a buffer, it is converted to a string of
673ddbd7b91SNathan Whitehorn             * hexadecimal values separated by commas."
67408c9948cSFredrik Holmqvist             *
67508c9948cSFredrik Holmqvist             * Each hex number is prefixed with 0x (11/2018)
676ddbd7b91SNathan Whitehorn             */
67708c9948cSFredrik Holmqvist            Separator = ',';
67808c9948cSFredrik Holmqvist            StringLength = (ObjDesc->Buffer.Length * 5);
679ddbd7b91SNathan Whitehorn            break;
680ddbd7b91SNathan Whitehorn
681ddbd7b91SNathan Whitehorn        default:
682ddbd7b91SNathan Whitehorn            return_ACPI_STATUS (AE_BAD_PARAMETER);
683ddbd7b91SNathan Whitehorn        }
684ddbd7b91SNathan Whitehorn
685ddbd7b91SNathan Whitehorn        /*
686ddbd7b91SNathan Whitehorn         * Create a new string object and string buffer
68743547dbeSJérôme Duval         * (-1 because of extra separator included in StringLength from above)
68880377d9aSJérôme Duval         * Allow creation of zero-length strings from zero-length buffers.
689ddbd7b91SNathan Whitehorn         */
69080377d9aSJérôme Duval        if (StringLength)
69180377d9aSJérôme Duval        {
69280377d9aSJérôme Duval            StringLength--;
69380377d9aSJérôme Duval        }
69480377d9aSJérôme Duval
69580377d9aSJérôme Duval        ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
696ddbd7b91SNathan Whitehorn        if (!ReturnDesc)
697ddbd7b91SNathan Whitehorn        {
698ddbd7b91SNathan Whitehorn            return_ACPI_STATUS (AE_NO_MEMORY);
699ddbd7b91SNathan Whitehorn        }
700ddbd7b91SNathan Whitehorn
701ddbd7b91SNathan Whitehorn        NewBuf = ReturnDesc->Buffer.Pointer;
702ddbd7b91SNathan Whitehorn
703ddbd7b91SNathan Whitehorn        /*
704ddbd7b91SNathan Whitehorn         * Convert buffer bytes to hex or decimal values
705ddbd7b91SNathan Whitehorn         * (separated by commas or spaces)
706ddbd7b91SNathan Whitehorn         */
707ddbd7b91SNathan Whitehorn        for (i = 0; i < ObjDesc->Buffer.Length; i++)
708ddbd7b91SNathan Whitehorn        {
70908c9948cSFredrik Holmqvist            if (Base == 16)
71008c9948cSFredrik Holmqvist            {
71108c9948cSFredrik Holmqvist                /* Emit 0x prefix for explict/implicit hex conversion */
71208c9948cSFredrik Holmqvist
71308c9948cSFredrik Holmqvist                *NewBuf++ = '0';
71408c9948cSFredrik Holmqvist                *NewBuf++ = 'x';
71508c9948cSFredrik Holmqvist            }
71608c9948cSFredrik Holmqvist
717ddbd7b91SNathan Whitehorn            NewBuf += AcpiExConvertToAscii (
718b64e8511SFredrik Holmqvist                (UINT64) ObjDesc->Buffer.Pointer[i], Base, NewBuf, 1);
71908c9948cSFredrik Holmqvist
72008c9948cSFredrik Holmqvist            /* Each digit is separated by either a comma or space */
72108c9948cSFredrik Holmqvist
72208c9948cSFredrik Holmqvist            *NewBuf++ = Separator;
723ddbd7b91SNathan Whitehorn        }
724ddbd7b91SNathan Whitehorn
72543547dbeSJérôme Duval        /*
72643547dbeSJérôme Duval         * Null terminate the string
72743547dbeSJérôme Duval         * (overwrites final comma/space from above)
72843547dbeSJérôme Duval         */
72980377d9aSJérôme Duval        if (ObjDesc->Buffer.Length)
73080377d9aSJérôme Duval        {
73180377d9aSJérôme Duval            NewBuf--;
73280377d9aSJérôme Duval        }
733ddbd7b91SNathan Whitehorn        *NewBuf = 0;
734ddbd7b91SNathan Whitehorn        break;
735ddbd7b91SNathan Whitehorn
736ddbd7b91SNathan Whitehorn    default:
737c70258b7SJérôme Duval
738ddbd7b91SNathan Whitehorn        return_ACPI_STATUS (AE_TYPE);
739ddbd7b91SNathan Whitehorn    }
740ddbd7b91SNathan Whitehorn
741ddbd7b91SNathan Whitehorn    *ResultDesc = ReturnDesc;
742ddbd7b91SNathan Whitehorn    return_ACPI_STATUS (AE_OK);
743ddbd7b91SNathan Whitehorn}
744ddbd7b91SNathan Whitehorn
745ddbd7b91SNathan Whitehorn
746ddbd7b91SNathan Whitehorn/*******************************************************************************
747ddbd7b91SNathan Whitehorn *
748ddbd7b91SNathan Whitehorn * FUNCTION:    AcpiExConvertToTargetType
749ddbd7b91SNathan Whitehorn *
750ddbd7b91SNathan Whitehorn * PARAMETERS:  DestinationType     - Current type of the destination
751ddbd7b91SNathan Whitehorn *              SourceDesc          - Source object to be converted.
752ddbd7b91SNathan Whitehorn *              ResultDesc          - Where the converted object is returned
753ddbd7b91SNathan Whitehorn *              WalkState           - Current method state
754ddbd7b91SNathan Whitehorn *
755ddbd7b91SNathan Whitehorn * RETURN:      Status
756ddbd7b91SNathan Whitehorn *
757ddbd7b91SNathan Whitehorn * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
758ddbd7b91SNathan Whitehorn *
759ddbd7b91SNathan Whitehorn ******************************************************************************/
760ddbd7b91SNathan Whitehorn
761ddbd7b91SNathan WhitehornACPI_STATUS
762ddbd7b91SNathan WhitehornAcpiExConvertToTargetType (
763ddbd7b91SNathan Whitehorn    ACPI_OBJECT_TYPE        DestinationType,
764ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     *SourceDesc,
765ddbd7b91SNathan Whitehorn    ACPI_OPERAND_OBJECT     **ResultDesc,
766ddbd7b91SNathan Whitehorn    ACPI_WALK_STATE         *WalkState)
767ddbd7b91SNathan Whitehorn{
768ddbd7b91SNathan Whitehorn    ACPI_STATUS             Status = AE_OK;
769ddbd7b91SNathan Whitehorn
770ddbd7b91SNathan Whitehorn
77143547dbeSJérôme Duval    ACPI_FUNCTION_TRACE (ExConvertToTargetType);
772ddbd7b91SNathan Whitehorn
773ddbd7b91SNathan Whitehorn
774ddbd7b91SNathan Whitehorn    /* Default behavior */
775ddbd7b91SNathan Whitehorn
776ddbd7b91SNathan Whitehorn    *ResultDesc = SourceDesc;
777ddbd7b91SNathan Whitehorn
778ddbd7b91SNathan Whitehorn    /*
779ddbd7b91SNathan Whitehorn     * If required by the target,
780ddbd7b91SNathan Whitehorn     * perform implicit conversion on the source before we store it.
781ddbd7b91SNathan Whitehorn     */
782ddbd7b91SNathan Whitehorn    switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
783ddbd7b91SNathan Whitehorn    {
784ddbd7b91SNathan Whitehorn    case ARGI_SIMPLE_TARGET:
78508c9948cSFredrik Holmqvist    case ARGI_FIXED_TARGET:
786ddbd7b91SNathan Whitehorn    case ARGI_INTEGER_REF:      /* Handles Increment, Decrement cases */
787ddbd7b91SNathan Whitehorn
788ddbd7b91SNathan Whitehorn        switch (DestinationType)
789ddbd7b91SNathan Whitehorn        {
790ddbd7b91SNathan Whitehorn        case ACPI_TYPE_LOCAL_REGION_FIELD:
791ddbd7b91SNathan Whitehorn            /*
792ddbd7b91SNathan Whitehorn             * Named field can always handle conversions
793ddbd7b91SNathan Whitehorn             */
794ddbd7b91SNathan Whitehorn            break;
795ddbd7b91SNathan Whitehorn
796ddbd7b91SNathan Whitehorn        default:
797c70258b7SJérôme Duval
798ddbd7b91SNathan Whitehorn            /* No conversion allowed for these types */
799ddbd7b91SNathan Whitehorn
80080377d9aSJérôme Duval            if (DestinationType != SourceDesc->Common.Type)
801ddbd7b91SNathan Whitehorn            {
802ddbd7b91SNathan Whitehorn                ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
803ddbd7b91SNathan Whitehorn                    "Explicit operator, will store (%s) over existing type (%s)\n",
804ddbd7b91SNathan Whitehorn                    AcpiUtGetObjectTypeName (SourceDesc),
805ddbd7b91SNathan Whitehorn                    AcpiUtGetTypeName (DestinationType)));
806ddbd7b91SNathan Whitehorn                Status = AE_TYPE;
807ddbd7b91SNathan Whitehorn            }
808ddbd7b91SNathan Whitehorn        }
809ddbd7b91SNathan Whitehorn        break;
810ddbd7b91SNathan Whitehorn
811ddbd7b91SNathan Whitehorn    case ARGI_TARGETREF:
812b64e8511SFredrik Holmqvist    case ARGI_STORE_TARGET:
813ddbd7b91SNathan Whitehorn
814ddbd7b91SNathan Whitehorn        switch (DestinationType)
815ddbd7b91SNathan Whitehorn        {
816ddbd7b91SNathan Whitehorn        case ACPI_TYPE_INTEGER:
817ddbd7b91SNathan Whitehorn        case ACPI_TYPE_BUFFER_FIELD:
818ddbd7b91SNathan Whitehorn        case ACPI_TYPE_LOCAL_BANK_FIELD:
819ddbd7b91SNathan Whitehorn        case ACPI_TYPE_LOCAL_INDEX_FIELD:
820ddbd7b91SNathan Whitehorn            /*
82180377d9aSJérôme Duval             * These types require an Integer operand. We can convert
822ddbd7b91SNathan Whitehorn             * a Buffer or a String to an Integer if necessary.
823ddbd7b91SNathan Whitehorn             */
824ff2e2f81SFredrik Holmqvist            Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
825ff2e2f81SFredrik Holmqvist                ACPI_IMPLICIT_CONVERSION);
826ddbd7b91SNathan Whitehorn            break;
827ddbd7b91SNathan Whitehorn
828ddbd7b91SNathan Whitehorn        case ACPI_TYPE_STRING:
829ddbd7b91SNathan Whitehorn            /*
83080377d9aSJérôme Duval             * The operand must be a String. We can convert an
831ddbd7b91SNathan Whitehorn             * Integer or Buffer if necessary
832ddbd7b91SNathan Whitehorn             */
833ddbd7b91SNathan Whitehorn            Status = AcpiExConvertToString (SourceDesc, ResultDesc,
834b64e8511SFredrik Holmqvist                ACPI_IMPLICIT_CONVERT_HEX);
835ddbd7b91SNathan Whitehorn            break;
836ddbd7b91SNathan Whitehorn
837ddbd7b91SNathan Whitehorn        case ACPI_TYPE_BUFFER:
838ddbd7b91SNathan Whitehorn            /*
83980377d9aSJérôme Duval             * The operand must be a Buffer. We can convert an
840ddbd7b91SNathan Whitehorn             * Integer or String if necessary
841ddbd7b91SNathan Whitehorn             */
842ddbd7b91SNathan Whitehorn            Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
843ddbd7b91SNathan Whitehorn            break;
844ddbd7b91SNathan Whitehorn
845ddbd7b91SNathan Whitehorn        default:
846c70258b7SJérôme Duval
847b64e8511SFredrik Holmqvist            ACPI_ERROR ((AE_INFO,
848b64e8511SFredrik Holmqvist                "Bad destination type during conversion: 0x%X",
849ddbd7b91SNathan Whitehorn                DestinationType));
850ddbd7b91SNathan Whitehorn            Status = AE_AML_INTERNAL;
851ddbd7b91SNathan Whitehorn            break;
852ddbd7b91SNathan Whitehorn        }
853ddbd7b91SNathan Whitehorn        break;
854ddbd7b91SNathan Whitehorn
855ddbd7b91SNathan Whitehorn    case ARGI_REFERENCE:
856ddbd7b91SNathan Whitehorn        /*
857ddbd7b91SNathan Whitehorn         * CreateXxxxField cases - we are storing the field object into the name
858ddbd7b91SNathan Whitehorn         */
859ddbd7b91SNathan Whitehorn        break;
860ddbd7b91SNathan Whitehorn
861ddbd7b91SNathan Whitehorn    default:
862c70258b7SJérôme Duval
86343547dbeSJérôme Duval        ACPI_ERROR ((AE_INFO,
864ffb873afSFredrik Holmqvist            "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
865ddbd7b91SNathan Whitehorn            GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
86643547dbeSJérôme Duval            WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
867ddbd7b91SNathan Whitehorn        Status = AE_AML_INTERNAL;
868ddbd7b91SNathan Whitehorn    }
869ddbd7b91SNathan Whitehorn
870ddbd7b91SNathan Whitehorn    /*
871ddbd7b91SNathan Whitehorn     * Source-to-Target conversion semantics:
872ddbd7b91SNathan Whitehorn     *
873ddbd7b91SNathan Whitehorn     * If conversion to the target type cannot be performed, then simply
874ddbd7b91SNathan Whitehorn     * overwrite the target with the new object and type.
875ddbd7b91SNathan Whitehorn     */
876ddbd7b91SNathan Whitehorn    if (Status == AE_TYPE)
877ddbd7b91SNathan Whitehorn    {
878ddbd7b91SNathan Whitehorn        Status = AE_OK;
879ddbd7b91SNathan Whitehorn    }
880ddbd7b91SNathan Whitehorn
881ddbd7b91SNathan Whitehorn    return_ACPI_STATUS (Status);
882ddbd7b91SNathan Whitehorn}
883