1246847Sjkim/******************************************************************************
2246847Sjkim *
3246847Sjkim * Module Name: nsconvert - Object conversions for objects returned by
4246847Sjkim *                          predefined methods
5246847Sjkim *
6246847Sjkim *****************************************************************************/
7246847Sjkim
8316303Sjkim/******************************************************************************
9316303Sjkim *
10316303Sjkim * 1. Copyright Notice
11316303Sjkim *
12316303Sjkim * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
13246847Sjkim * All rights reserved.
14246847Sjkim *
15316303Sjkim * 2. License
16316303Sjkim *
17316303Sjkim * 2.1. This is your license from Intel Corp. under its intellectual property
18316303Sjkim * rights. You may have additional license terms from the party that provided
19316303Sjkim * you this software, covering your right to use that party's intellectual
20316303Sjkim * property rights.
21316303Sjkim *
22316303Sjkim * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23316303Sjkim * copy of the source code appearing in this file ("Covered Code") an
24316303Sjkim * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25316303Sjkim * base code distributed originally by Intel ("Original Intel Code") to copy,
26316303Sjkim * make derivatives, distribute, use and display any portion of the Covered
27316303Sjkim * Code in any form, with the right to sublicense such rights; and
28316303Sjkim *
29316303Sjkim * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30316303Sjkim * license (with the right to sublicense), under only those claims of Intel
31316303Sjkim * patents that are infringed by the Original Intel Code, to make, use, sell,
32316303Sjkim * offer to sell, and import the Covered Code and derivative works thereof
33316303Sjkim * solely to the minimum extent necessary to exercise the above copyright
34316303Sjkim * license, and in no event shall the patent license extend to any additions
35316303Sjkim * to or modifications of the Original Intel Code. No other license or right
36316303Sjkim * is granted directly or by implication, estoppel or otherwise;
37316303Sjkim *
38316303Sjkim * The above copyright and patent license is granted only if the following
39316303Sjkim * conditions are met:
40316303Sjkim *
41316303Sjkim * 3. Conditions
42316303Sjkim *
43316303Sjkim * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44316303Sjkim * Redistribution of source code of any substantial portion of the Covered
45316303Sjkim * Code or modification with rights to further distribute source must include
46316303Sjkim * the above Copyright Notice, the above License, this list of Conditions,
47316303Sjkim * and the following Disclaimer and Export Compliance provision. In addition,
48316303Sjkim * Licensee must cause all Covered Code to which Licensee contributes to
49316303Sjkim * contain a file documenting the changes Licensee made to create that Covered
50316303Sjkim * Code and the date of any change. Licensee must include in that file the
51316303Sjkim * documentation of any changes made by any predecessor Licensee. Licensee
52316303Sjkim * must include a prominent statement that the modification is derived,
53316303Sjkim * directly or indirectly, from Original Intel Code.
54316303Sjkim *
55316303Sjkim * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56316303Sjkim * Redistribution of source code of any substantial portion of the Covered
57316303Sjkim * Code or modification without rights to further distribute source must
58316303Sjkim * include the following Disclaimer and Export Compliance provision in the
59316303Sjkim * documentation and/or other materials provided with distribution. In
60316303Sjkim * addition, Licensee may not authorize further sublicense of source of any
61316303Sjkim * portion of the Covered Code, and must include terms to the effect that the
62316303Sjkim * license from Licensee to its licensee is limited to the intellectual
63316303Sjkim * property embodied in the software Licensee provides to its licensee, and
64316303Sjkim * not to intellectual property embodied in modifications its licensee may
65316303Sjkim * make.
66316303Sjkim *
67316303Sjkim * 3.3. Redistribution of Executable. Redistribution in executable form of any
68316303Sjkim * substantial portion of the Covered Code or modification must reproduce the
69316303Sjkim * above Copyright Notice, and the following Disclaimer and Export Compliance
70316303Sjkim * provision in the documentation and/or other materials provided with the
71316303Sjkim * distribution.
72316303Sjkim *
73316303Sjkim * 3.4. Intel retains all right, title, and interest in and to the Original
74316303Sjkim * Intel Code.
75316303Sjkim *
76316303Sjkim * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77316303Sjkim * Intel shall be used in advertising or otherwise to promote the sale, use or
78316303Sjkim * other dealings in products derived from or relating to the Covered Code
79316303Sjkim * without prior written authorization from Intel.
80316303Sjkim *
81316303Sjkim * 4. Disclaimer and Export Compliance
82316303Sjkim *
83316303Sjkim * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84316303Sjkim * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85316303Sjkim * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86316303Sjkim * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87316303Sjkim * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89316303Sjkim * PARTICULAR PURPOSE.
90316303Sjkim *
91316303Sjkim * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92316303Sjkim * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93316303Sjkim * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94316303Sjkim * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95316303Sjkim * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96316303Sjkim * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97316303Sjkim * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98316303Sjkim * LIMITED REMEDY.
99316303Sjkim *
100316303Sjkim * 4.3. Licensee shall not export, either directly or indirectly, any of this
101316303Sjkim * software or system incorporating such software without first obtaining any
102316303Sjkim * required license or other approval from the U. S. Department of Commerce or
103316303Sjkim * any other agency or department of the United States Government. In the
104316303Sjkim * event Licensee exports any such software from the United States or
105316303Sjkim * re-exports any such software from a foreign destination, Licensee shall
106316303Sjkim * ensure that the distribution and export/re-export of the software is in
107316303Sjkim * compliance with all laws, regulations, orders, or other restrictions of the
108316303Sjkim * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109316303Sjkim * any of its subsidiaries will export/re-export any technical data, process,
110316303Sjkim * software, or service, directly or indirectly, to any country for which the
111316303Sjkim * United States government or any agency thereof requires an export license,
112316303Sjkim * other governmental approval, or letter of assurance, without first obtaining
113316303Sjkim * such license, approval or letter.
114316303Sjkim *
115316303Sjkim *****************************************************************************
116316303Sjkim *
117316303Sjkim * Alternatively, you may choose to be licensed under the terms of the
118316303Sjkim * following license:
119316303Sjkim *
120246847Sjkim * Redistribution and use in source and binary forms, with or without
121246847Sjkim * modification, are permitted provided that the following conditions
122246847Sjkim * are met:
123246847Sjkim * 1. Redistributions of source code must retain the above copyright
124246847Sjkim *    notice, this list of conditions, and the following disclaimer,
125246847Sjkim *    without modification.
126246847Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127246847Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
128246847Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
129246847Sjkim *    including a substantially similar Disclaimer requirement for further
130246847Sjkim *    binary redistribution.
131246847Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
132246847Sjkim *    of any contributors may be used to endorse or promote products derived
133246847Sjkim *    from this software without specific prior written permission.
134246847Sjkim *
135316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136316303Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137316303Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138316303Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139316303Sjkim * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140316303Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141316303Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142316303Sjkim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143316303Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144316303Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145316303Sjkim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146316303Sjkim *
147316303Sjkim * Alternatively, you may choose to be licensed under the terms of the
148246847Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
149246847Sjkim * Software Foundation.
150246847Sjkim *
151316303Sjkim *****************************************************************************/
152246847Sjkim
153246849Sjkim#include <contrib/dev/acpica/include/acpi.h>
154246849Sjkim#include <contrib/dev/acpica/include/accommon.h>
155246849Sjkim#include <contrib/dev/acpica/include/acnamesp.h>
156246849Sjkim#include <contrib/dev/acpica/include/acinterp.h>
157246849Sjkim#include <contrib/dev/acpica/include/acpredef.h>
158246849Sjkim#include <contrib/dev/acpica/include/amlresrc.h>
159246847Sjkim
160246847Sjkim#define _COMPONENT          ACPI_NAMESPACE
161246847Sjkim        ACPI_MODULE_NAME    ("nsconvert")
162246847Sjkim
163246847Sjkim
164246847Sjkim/*******************************************************************************
165246847Sjkim *
166246847Sjkim * FUNCTION:    AcpiNsConvertToInteger
167246847Sjkim *
168246847Sjkim * PARAMETERS:  OriginalObject      - Object to be converted
169246847Sjkim *              ReturnObject        - Where the new converted object is returned
170246847Sjkim *
171246847Sjkim * RETURN:      Status. AE_OK if conversion was successful.
172246847Sjkim *
173246847Sjkim * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
174246847Sjkim *
175246847Sjkim ******************************************************************************/
176246847Sjkim
177246847SjkimACPI_STATUS
178246847SjkimAcpiNsConvertToInteger (
179246847Sjkim    ACPI_OPERAND_OBJECT     *OriginalObject,
180246847Sjkim    ACPI_OPERAND_OBJECT     **ReturnObject)
181246847Sjkim{
182246847Sjkim    ACPI_OPERAND_OBJECT     *NewObject;
183246847Sjkim    ACPI_STATUS             Status;
184246847Sjkim    UINT64                  Value = 0;
185246847Sjkim    UINT32                  i;
186246847Sjkim
187246847Sjkim
188246847Sjkim    switch (OriginalObject->Common.Type)
189246847Sjkim    {
190246847Sjkim    case ACPI_TYPE_STRING:
191246847Sjkim
192246847Sjkim        /* String-to-Integer conversion */
193246847Sjkim
194327557Sjkim        Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer, &Value);
195246847Sjkim        if (ACPI_FAILURE (Status))
196246847Sjkim        {
197246847Sjkim            return (Status);
198246847Sjkim        }
199246847Sjkim        break;
200246847Sjkim
201246847Sjkim    case ACPI_TYPE_BUFFER:
202246847Sjkim
203246847Sjkim        /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
204246847Sjkim
205246847Sjkim        if (OriginalObject->Buffer.Length > 8)
206246847Sjkim        {
207246847Sjkim            return (AE_AML_OPERAND_TYPE);
208246847Sjkim        }
209246847Sjkim
210246847Sjkim        /* Extract each buffer byte to create the integer */
211246847Sjkim
212246847Sjkim        for (i = 0; i < OriginalObject->Buffer.Length; i++)
213246847Sjkim        {
214298714Sjkim            Value |= ((UINT64)
215298714Sjkim                OriginalObject->Buffer.Pointer[i] << (i * 8));
216246847Sjkim        }
217246847Sjkim        break;
218246847Sjkim
219246847Sjkim    default:
220250838Sjkim
221246847Sjkim        return (AE_AML_OPERAND_TYPE);
222246847Sjkim    }
223246847Sjkim
224246847Sjkim    NewObject = AcpiUtCreateIntegerObject (Value);
225246847Sjkim    if (!NewObject)
226246847Sjkim    {
227246847Sjkim        return (AE_NO_MEMORY);
228246847Sjkim    }
229246847Sjkim
230246847Sjkim    *ReturnObject = NewObject;
231246847Sjkim    return (AE_OK);
232246847Sjkim}
233246847Sjkim
234246847Sjkim
235246847Sjkim/*******************************************************************************
236246847Sjkim *
237246847Sjkim * FUNCTION:    AcpiNsConvertToString
238246847Sjkim *
239246847Sjkim * PARAMETERS:  OriginalObject      - Object to be converted
240246847Sjkim *              ReturnObject        - Where the new converted object is returned
241246847Sjkim *
242246847Sjkim * RETURN:      Status. AE_OK if conversion was successful.
243246847Sjkim *
244246847Sjkim * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
245246847Sjkim *
246246847Sjkim ******************************************************************************/
247246847Sjkim
248246847SjkimACPI_STATUS
249246847SjkimAcpiNsConvertToString (
250246847Sjkim    ACPI_OPERAND_OBJECT     *OriginalObject,
251246847Sjkim    ACPI_OPERAND_OBJECT     **ReturnObject)
252246847Sjkim{
253246847Sjkim    ACPI_OPERAND_OBJECT     *NewObject;
254246847Sjkim    ACPI_SIZE               Length;
255246847Sjkim    ACPI_STATUS             Status;
256246847Sjkim
257246847Sjkim
258246847Sjkim    switch (OriginalObject->Common.Type)
259246847Sjkim    {
260246847Sjkim    case ACPI_TYPE_INTEGER:
261246847Sjkim        /*
262246847Sjkim         * Integer-to-String conversion. Commonly, convert
263246847Sjkim         * an integer of value 0 to a NULL string. The last element of
264246847Sjkim         * _BIF and _BIX packages occasionally need this fix.
265246847Sjkim         */
266246847Sjkim        if (OriginalObject->Integer.Value == 0)
267246847Sjkim        {
268246847Sjkim            /* Allocate a new NULL string object */
269246847Sjkim
270246847Sjkim            NewObject = AcpiUtCreateStringObject (0);
271246847Sjkim            if (!NewObject)
272246847Sjkim            {
273246847Sjkim                return (AE_NO_MEMORY);
274246847Sjkim            }
275246847Sjkim        }
276246847Sjkim        else
277246847Sjkim        {
278298714Sjkim            Status = AcpiExConvertToString (OriginalObject,
279298714Sjkim                &NewObject, ACPI_IMPLICIT_CONVERT_HEX);
280246847Sjkim            if (ACPI_FAILURE (Status))
281246847Sjkim            {
282246847Sjkim                return (Status);
283246847Sjkim            }
284246847Sjkim        }
285246847Sjkim        break;
286246847Sjkim
287246847Sjkim    case ACPI_TYPE_BUFFER:
288246847Sjkim        /*
289246847Sjkim         * Buffer-to-String conversion. Use a ToString
290246847Sjkim         * conversion, no transform performed on the buffer data. The best
291246847Sjkim         * example of this is the _BIF method, where the string data from
292246847Sjkim         * the battery is often (incorrectly) returned as buffer object(s).
293246847Sjkim         */
294246847Sjkim        Length = 0;
295246847Sjkim        while ((Length < OriginalObject->Buffer.Length) &&
296246847Sjkim                (OriginalObject->Buffer.Pointer[Length]))
297246847Sjkim        {
298246847Sjkim            Length++;
299246847Sjkim        }
300246847Sjkim
301246847Sjkim        /* Allocate a new string object */
302246847Sjkim
303246847Sjkim        NewObject = AcpiUtCreateStringObject (Length);
304246847Sjkim        if (!NewObject)
305246847Sjkim        {
306246847Sjkim            return (AE_NO_MEMORY);
307246847Sjkim        }
308246847Sjkim
309246847Sjkim        /*
310246847Sjkim         * Copy the raw buffer data with no transform. String is already NULL
311246847Sjkim         * terminated at Length+1.
312246847Sjkim         */
313284583Sjkim        memcpy (NewObject->String.Pointer,
314246847Sjkim            OriginalObject->Buffer.Pointer, Length);
315246847Sjkim        break;
316246847Sjkim
317246847Sjkim    default:
318250838Sjkim
319246847Sjkim        return (AE_AML_OPERAND_TYPE);
320246847Sjkim    }
321246847Sjkim
322246847Sjkim    *ReturnObject = NewObject;
323246847Sjkim    return (AE_OK);
324246847Sjkim}
325246847Sjkim
326246847Sjkim
327246847Sjkim/*******************************************************************************
328246847Sjkim *
329246847Sjkim * FUNCTION:    AcpiNsConvertToBuffer
330246847Sjkim *
331246847Sjkim * PARAMETERS:  OriginalObject      - Object to be converted
332246847Sjkim *              ReturnObject        - Where the new converted object is returned
333246847Sjkim *
334246847Sjkim * RETURN:      Status. AE_OK if conversion was successful.
335246847Sjkim *
336246847Sjkim * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
337246847Sjkim *
338246847Sjkim ******************************************************************************/
339246847Sjkim
340246847SjkimACPI_STATUS
341246847SjkimAcpiNsConvertToBuffer (
342246847Sjkim    ACPI_OPERAND_OBJECT     *OriginalObject,
343246847Sjkim    ACPI_OPERAND_OBJECT     **ReturnObject)
344246847Sjkim{
345246847Sjkim    ACPI_OPERAND_OBJECT     *NewObject;
346246847Sjkim    ACPI_STATUS             Status;
347246847Sjkim    ACPI_OPERAND_OBJECT     **Elements;
348246847Sjkim    UINT32                  *DwordBuffer;
349246847Sjkim    UINT32                  Count;
350246847Sjkim    UINT32                  i;
351246847Sjkim
352246847Sjkim
353246847Sjkim    switch (OriginalObject->Common.Type)
354246847Sjkim    {
355246847Sjkim    case ACPI_TYPE_INTEGER:
356246847Sjkim        /*
357246847Sjkim         * Integer-to-Buffer conversion.
358246847Sjkim         * Convert the Integer to a packed-byte buffer. _MAT and other
359246847Sjkim         * objects need this sometimes, if a read has been performed on a
360246847Sjkim         * Field object that is less than or equal to the global integer
361246847Sjkim         * size (32 or 64 bits).
362246847Sjkim         */
363246847Sjkim        Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
364246847Sjkim        if (ACPI_FAILURE (Status))
365246847Sjkim        {
366246847Sjkim            return (Status);
367246847Sjkim        }
368246847Sjkim        break;
369246847Sjkim
370246847Sjkim    case ACPI_TYPE_STRING:
371246847Sjkim
372246847Sjkim        /* String-to-Buffer conversion. Simple data copy */
373246847Sjkim
374298714Sjkim        NewObject = AcpiUtCreateBufferObject
375298714Sjkim            (OriginalObject->String.Length);
376246847Sjkim        if (!NewObject)
377246847Sjkim        {
378246847Sjkim            return (AE_NO_MEMORY);
379246847Sjkim        }
380246847Sjkim
381284583Sjkim        memcpy (NewObject->Buffer.Pointer,
382246847Sjkim            OriginalObject->String.Pointer, OriginalObject->String.Length);
383246847Sjkim        break;
384246847Sjkim
385246847Sjkim    case ACPI_TYPE_PACKAGE:
386246847Sjkim        /*
387246847Sjkim         * This case is often seen for predefined names that must return a
388246847Sjkim         * Buffer object with multiple DWORD integers within. For example,
389246847Sjkim         * _FDE and _GTM. The Package can be converted to a Buffer.
390246847Sjkim         */
391246847Sjkim
392246847Sjkim        /* All elements of the Package must be integers */
393246847Sjkim
394246847Sjkim        Elements = OriginalObject->Package.Elements;
395246847Sjkim        Count = OriginalObject->Package.Count;
396246847Sjkim
397246847Sjkim        for (i = 0; i < Count; i++)
398246847Sjkim        {
399246847Sjkim            if ((!*Elements) ||
400246847Sjkim                ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
401246847Sjkim            {
402246847Sjkim                return (AE_AML_OPERAND_TYPE);
403246847Sjkim            }
404246847Sjkim            Elements++;
405246847Sjkim        }
406246847Sjkim
407246847Sjkim        /* Create the new buffer object to replace the Package */
408246847Sjkim
409246847Sjkim        NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
410246847Sjkim        if (!NewObject)
411246847Sjkim        {
412246847Sjkim            return (AE_NO_MEMORY);
413246847Sjkim        }
414246847Sjkim
415246847Sjkim        /* Copy the package elements (integers) to the buffer as DWORDs */
416246847Sjkim
417246847Sjkim        Elements = OriginalObject->Package.Elements;
418246847Sjkim        DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
419246847Sjkim
420246847Sjkim        for (i = 0; i < Count; i++)
421246847Sjkim        {
422246847Sjkim            *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
423246847Sjkim            DwordBuffer++;
424246847Sjkim            Elements++;
425246847Sjkim        }
426246847Sjkim        break;
427246847Sjkim
428246847Sjkim    default:
429250838Sjkim
430246847Sjkim        return (AE_AML_OPERAND_TYPE);
431246847Sjkim    }
432246847Sjkim
433246847Sjkim    *ReturnObject = NewObject;
434246847Sjkim    return (AE_OK);
435246847Sjkim}
436246847Sjkim
437246847Sjkim
438246847Sjkim/*******************************************************************************
439246847Sjkim *
440246847Sjkim * FUNCTION:    AcpiNsConvertToUnicode
441246847Sjkim *
442298714Sjkim * PARAMETERS:  Scope               - Namespace node for the method/object
443298714Sjkim *              OriginalObject      - ASCII String Object to be converted
444246847Sjkim *              ReturnObject        - Where the new converted object is returned
445246847Sjkim *
446246847Sjkim * RETURN:      Status. AE_OK if conversion was successful.
447246847Sjkim *
448246847Sjkim * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
449246847Sjkim *
450246847Sjkim ******************************************************************************/
451246847Sjkim
452246847SjkimACPI_STATUS
453246847SjkimAcpiNsConvertToUnicode (
454298714Sjkim    ACPI_NAMESPACE_NODE     *Scope,
455246847Sjkim    ACPI_OPERAND_OBJECT     *OriginalObject,
456246847Sjkim    ACPI_OPERAND_OBJECT     **ReturnObject)
457246847Sjkim{
458246847Sjkim    ACPI_OPERAND_OBJECT     *NewObject;
459246847Sjkim    char                    *AsciiString;
460246847Sjkim    UINT16                  *UnicodeBuffer;
461246847Sjkim    UINT32                  UnicodeLength;
462246847Sjkim    UINT32                  i;
463246847Sjkim
464246847Sjkim
465246847Sjkim    if (!OriginalObject)
466246847Sjkim    {
467246847Sjkim        return (AE_OK);
468246847Sjkim    }
469246847Sjkim
470246847Sjkim    /* If a Buffer was returned, it must be at least two bytes long */
471246847Sjkim
472246847Sjkim    if (OriginalObject->Common.Type == ACPI_TYPE_BUFFER)
473246847Sjkim    {
474246847Sjkim        if (OriginalObject->Buffer.Length < 2)
475246847Sjkim        {
476246847Sjkim            return (AE_AML_OPERAND_VALUE);
477246847Sjkim        }
478246847Sjkim
479246847Sjkim        *ReturnObject = NULL;
480246847Sjkim        return (AE_OK);
481246847Sjkim    }
482246847Sjkim
483246847Sjkim    /*
484246847Sjkim     * The original object is an ASCII string. Convert this string to
485246847Sjkim     * a unicode buffer.
486246847Sjkim     */
487246847Sjkim    AsciiString = OriginalObject->String.Pointer;
488246847Sjkim    UnicodeLength = (OriginalObject->String.Length * 2) + 2;
489246847Sjkim
490246847Sjkim    /* Create a new buffer object for the Unicode data */
491246847Sjkim
492246847Sjkim    NewObject = AcpiUtCreateBufferObject (UnicodeLength);
493246847Sjkim    if (!NewObject)
494246847Sjkim    {
495246847Sjkim        return (AE_NO_MEMORY);
496246847Sjkim    }
497246847Sjkim
498246847Sjkim    UnicodeBuffer = ACPI_CAST_PTR (UINT16, NewObject->Buffer.Pointer);
499246847Sjkim
500246847Sjkim    /* Convert ASCII to Unicode */
501246847Sjkim
502246847Sjkim    for (i = 0; i < OriginalObject->String.Length; i++)
503246847Sjkim    {
504246847Sjkim        UnicodeBuffer[i] = (UINT16) AsciiString[i];
505246847Sjkim    }
506246847Sjkim
507246847Sjkim    *ReturnObject = NewObject;
508246847Sjkim    return (AE_OK);
509246847Sjkim}
510246847Sjkim
511246847Sjkim
512246847Sjkim/*******************************************************************************
513246847Sjkim *
514246847Sjkim * FUNCTION:    AcpiNsConvertToResource
515246847Sjkim *
516298714Sjkim * PARAMETERS:  Scope               - Namespace node for the method/object
517298714Sjkim *              OriginalObject      - Object to be converted
518246847Sjkim *              ReturnObject        - Where the new converted object is returned
519246847Sjkim *
520246847Sjkim * RETURN:      Status. AE_OK if conversion was successful
521246847Sjkim *
522246847Sjkim * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate
523246847Sjkim *              Buffer.
524246847Sjkim *
525246847Sjkim ******************************************************************************/
526246847Sjkim
527246847SjkimACPI_STATUS
528246847SjkimAcpiNsConvertToResource (
529298714Sjkim    ACPI_NAMESPACE_NODE     *Scope,
530246847Sjkim    ACPI_OPERAND_OBJECT     *OriginalObject,
531246847Sjkim    ACPI_OPERAND_OBJECT     **ReturnObject)
532246847Sjkim{
533246847Sjkim    ACPI_OPERAND_OBJECT     *NewObject;
534246847Sjkim    UINT8                   *Buffer;
535246847Sjkim
536246847Sjkim
537246847Sjkim    /*
538246847Sjkim     * We can fix the following cases for an expected resource template:
539246847Sjkim     * 1. No return value (interpreter slack mode is disabled)
540246847Sjkim     * 2. A "Return (Zero)" statement
541246847Sjkim     * 3. A "Return empty buffer" statement
542246847Sjkim     *
543246847Sjkim     * We will return a buffer containing a single EndTag
544246847Sjkim     * resource descriptor.
545246847Sjkim     */
546246847Sjkim    if (OriginalObject)
547246847Sjkim    {
548246847Sjkim        switch (OriginalObject->Common.Type)
549246847Sjkim        {
550246847Sjkim        case ACPI_TYPE_INTEGER:
551246847Sjkim
552246847Sjkim            /* We can only repair an Integer==0 */
553246847Sjkim
554246847Sjkim            if (OriginalObject->Integer.Value)
555246847Sjkim            {
556246847Sjkim                return (AE_AML_OPERAND_TYPE);
557246847Sjkim            }
558246847Sjkim            break;
559246847Sjkim
560246847Sjkim        case ACPI_TYPE_BUFFER:
561246847Sjkim
562246847Sjkim            if (OriginalObject->Buffer.Length)
563246847Sjkim            {
564246847Sjkim                /* Additional checks can be added in the future */
565246847Sjkim
566246847Sjkim                *ReturnObject = NULL;
567246847Sjkim                return (AE_OK);
568246847Sjkim            }
569246847Sjkim            break;
570246847Sjkim
571246847Sjkim        case ACPI_TYPE_STRING:
572246847Sjkim        default:
573246847Sjkim
574246847Sjkim            return (AE_AML_OPERAND_TYPE);
575246847Sjkim        }
576246847Sjkim    }
577246847Sjkim
578246847Sjkim    /* Create the new buffer object for the resource descriptor */
579246847Sjkim
580246847Sjkim    NewObject = AcpiUtCreateBufferObject (2);
581246847Sjkim    if (!NewObject)
582246847Sjkim    {
583246847Sjkim        return (AE_NO_MEMORY);
584246847Sjkim    }
585246847Sjkim
586246847Sjkim    Buffer = ACPI_CAST_PTR (UINT8, NewObject->Buffer.Pointer);
587246847Sjkim
588246847Sjkim    /* Initialize the Buffer with a single EndTag descriptor */
589246847Sjkim
590246847Sjkim    Buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
591246847Sjkim    Buffer[1] = 0x00;
592246847Sjkim
593246847Sjkim    *ReturnObject = NewObject;
594246847Sjkim    return (AE_OK);
595246847Sjkim}
596298714Sjkim
597298714Sjkim
598298714Sjkim/*******************************************************************************
599298714Sjkim *
600298714Sjkim * FUNCTION:    AcpiNsConvertToReference
601298714Sjkim *
602298714Sjkim * PARAMETERS:  Scope               - Namespace node for the method/object
603298714Sjkim *              OriginalObject      - Object to be converted
604298714Sjkim *              ReturnObject        - Where the new converted object is returned
605298714Sjkim *
606298714Sjkim * RETURN:      Status. AE_OK if conversion was successful
607298714Sjkim *
608298714Sjkim * DESCRIPTION: Attempt to convert a Integer object to a ObjectReference.
609298714Sjkim *              Buffer.
610298714Sjkim *
611298714Sjkim ******************************************************************************/
612298714Sjkim
613298714SjkimACPI_STATUS
614298714SjkimAcpiNsConvertToReference (
615298714Sjkim    ACPI_NAMESPACE_NODE     *Scope,
616298714Sjkim    ACPI_OPERAND_OBJECT     *OriginalObject,
617298714Sjkim    ACPI_OPERAND_OBJECT     **ReturnObject)
618298714Sjkim{
619298714Sjkim    ACPI_OPERAND_OBJECT     *NewObject = NULL;
620298714Sjkim    ACPI_STATUS             Status;
621298714Sjkim    ACPI_NAMESPACE_NODE     *Node;
622298714Sjkim    ACPI_GENERIC_STATE      ScopeInfo;
623298714Sjkim    char                    *Name;
624298714Sjkim
625298714Sjkim
626298714Sjkim    ACPI_FUNCTION_NAME (NsConvertToReference);
627298714Sjkim
628298714Sjkim
629298714Sjkim    /* Convert path into internal presentation */
630298714Sjkim
631298714Sjkim    Status = AcpiNsInternalizeName (OriginalObject->String.Pointer, &Name);
632298714Sjkim    if (ACPI_FAILURE (Status))
633298714Sjkim    {
634298714Sjkim        return_ACPI_STATUS (Status);
635298714Sjkim    }
636298714Sjkim
637298714Sjkim    /* Find the namespace node */
638298714Sjkim
639298714Sjkim    ScopeInfo.Scope.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Scope);
640298714Sjkim    Status = AcpiNsLookup (&ScopeInfo, Name,
641298714Sjkim        ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
642298714Sjkim        ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE, NULL, &Node);
643298714Sjkim    if (ACPI_FAILURE (Status))
644298714Sjkim    {
645298714Sjkim        /* Check if we are resolving a named reference within a package */
646298714Sjkim
647327557Sjkim        ACPI_ERROR_NAMESPACE (&ScopeInfo,
648327557Sjkim            OriginalObject->String.Pointer, Status);
649298714Sjkim        goto ErrorExit;
650298714Sjkim    }
651298714Sjkim
652298714Sjkim    /* Create and init a new internal ACPI object */
653298714Sjkim
654298714Sjkim    NewObject = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
655298714Sjkim    if (!NewObject)
656298714Sjkim    {
657298714Sjkim        Status = AE_NO_MEMORY;
658298714Sjkim        goto ErrorExit;
659298714Sjkim    }
660298714Sjkim    NewObject->Reference.Node = Node;
661298714Sjkim    NewObject->Reference.Object = Node->Object;
662298714Sjkim    NewObject->Reference.Class = ACPI_REFCLASS_NAME;
663298714Sjkim
664298714Sjkim    /*
665298714Sjkim     * Increase reference of the object if needed (the object is likely a
666298714Sjkim     * null for device nodes).
667298714Sjkim     */
668298714Sjkim    AcpiUtAddReference (Node->Object);
669298714Sjkim
670298714SjkimErrorExit:
671298714Sjkim    ACPI_FREE (Name);
672298714Sjkim    *ReturnObject = NewObject;
673298714Sjkim    return (AE_OK);
674298714Sjkim}
675