xref: /freebsd/sys/contrib/dev/acpica/components/executer/exconvrt.c (revision d2ba5111c125104b09aa1acd1bfe8af2a24c79cc)
1 /******************************************************************************
2  *
3  * Module Name: exconvrt - Object conversion routines
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
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
19  * property rights.
20  *
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
27  *
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;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
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.
53  *
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
64  * make.
65  *
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
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
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.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
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
88  * PARTICULAR PURPOSE.
89  *
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
97  * LIMITED REMEDY.
98  *
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.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
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.
133  *
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.
145  *
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.
149  *
150  *****************************************************************************/
151 
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>
156 
157 
158 #define _COMPONENT          ACPI_EXECUTER
159         ACPI_MODULE_NAME    ("exconvrt")
160 
161 /* Local prototypes */
162 
163 static UINT32
164 AcpiExConvertToAscii (
165     UINT64                  Integer,
166     UINT16                  Base,
167     UINT8                   *String,
168     UINT8                   MaxLength);
169 
170 
171 /*******************************************************************************
172  *
173  * FUNCTION:    AcpiExConvertToInteger
174  *
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  *              Flags           - Used for string conversion
179  *
180  * RETURN:      Status
181  *
182  * DESCRIPTION: Convert an ACPI Object to an integer.
183  *
184  ******************************************************************************/
185 
186 ACPI_STATUS
187 AcpiExConvertToInteger (
188     ACPI_OPERAND_OBJECT     *ObjDesc,
189     ACPI_OPERAND_OBJECT     **ResultDesc,
190     UINT32                  Flags)
191 {
192     ACPI_OPERAND_OBJECT     *ReturnDesc;
193     UINT8                   *Pointer;
194     UINT64                  Result;
195     UINT32                  i;
196     UINT32                  Count;
197     ACPI_STATUS             Status;
198 
199 
200     ACPI_FUNCTION_TRACE_PTR (ExConvertToInteger, ObjDesc);
201 
202 
203     switch (ObjDesc->Common.Type)
204     {
205     case ACPI_TYPE_INTEGER:
206 
207         /* No conversion necessary */
208 
209         *ResultDesc = ObjDesc;
210         return_ACPI_STATUS (AE_OK);
211 
212     case ACPI_TYPE_BUFFER:
213     case ACPI_TYPE_STRING:
214 
215         /* Note: Takes advantage of common buffer/string fields */
216 
217         Pointer = ObjDesc->Buffer.Pointer;
218         Count   = ObjDesc->Buffer.Length;
219         break;
220 
221     default:
222 
223         return_ACPI_STATUS (AE_TYPE);
224     }
225 
226     /*
227      * Convert the buffer/string to an integer. Note that both buffers and
228      * strings are treated as raw data - we don't convert ascii to hex for
229      * strings.
230      *
231      * There are two terminating conditions for the loop:
232      * 1) The size of an integer has been reached, or
233      * 2) The end of the buffer or string has been reached
234      */
235     Result = 0;
236 
237     /* String conversion is different than Buffer conversion */
238 
239     switch (ObjDesc->Common.Type)
240     {
241     case ACPI_TYPE_STRING:
242         /*
243          * Convert string to an integer - for most cases, the string must be
244          * hexadecimal as per the ACPI specification. The only exception (as
245          * of ACPI 3.0) is that the ToInteger() operator allows both decimal
246          * and hexadecimal strings (hex prefixed with "0x").
247          */
248         Status = AcpiUtStrtoul64 (ACPI_CAST_PTR (char, Pointer),
249             (AcpiGbl_IntegerByteWidth | Flags), &Result);
250         if (ACPI_FAILURE (Status))
251         {
252             return_ACPI_STATUS (Status);
253         }
254         break;
255 
256     case ACPI_TYPE_BUFFER:
257 
258         /* Check for zero-length buffer */
259 
260         if (!Count)
261         {
262             return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
263         }
264 
265         /* Transfer no more than an integer's worth of data */
266 
267         if (Count > AcpiGbl_IntegerByteWidth)
268         {
269             Count = AcpiGbl_IntegerByteWidth;
270         }
271 
272         /*
273          * Convert buffer to an integer - we simply grab enough raw data
274          * from the buffer to fill an integer
275          */
276         for (i = 0; i < Count; i++)
277         {
278             /*
279              * Get next byte and shift it into the Result.
280              * Little endian is used, meaning that the first byte of the buffer
281              * is the LSB of the integer
282              */
283             Result |= (((UINT64) Pointer[i]) << (i * 8));
284         }
285         break;
286 
287     default:
288 
289         /* No other types can get here */
290 
291         break;
292     }
293 
294     /* Create a new integer */
295 
296     ReturnDesc = AcpiUtCreateIntegerObject (Result);
297     if (!ReturnDesc)
298     {
299         return_ACPI_STATUS (AE_NO_MEMORY);
300     }
301 
302     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
303         ACPI_FORMAT_UINT64 (Result)));
304 
305     /* Save the Result */
306 
307     (void) AcpiExTruncateFor32bitTable (ReturnDesc);
308     *ResultDesc = ReturnDesc;
309     return_ACPI_STATUS (AE_OK);
310 }
311 
312 
313 /*******************************************************************************
314  *
315  * FUNCTION:    AcpiExConvertToBuffer
316  *
317  * PARAMETERS:  ObjDesc         - Object to be converted. Must be an
318  *                                Integer, Buffer, or String
319  *              ResultDesc      - Where the new buffer object is returned
320  *
321  * RETURN:      Status
322  *
323  * DESCRIPTION: Convert an ACPI Object to a Buffer
324  *
325  ******************************************************************************/
326 
327 ACPI_STATUS
328 AcpiExConvertToBuffer (
329     ACPI_OPERAND_OBJECT     *ObjDesc,
330     ACPI_OPERAND_OBJECT     **ResultDesc)
331 {
332     ACPI_OPERAND_OBJECT     *ReturnDesc;
333     UINT8                   *NewBuf;
334 
335 
336     ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer, ObjDesc);
337 
338 
339     switch (ObjDesc->Common.Type)
340     {
341     case ACPI_TYPE_BUFFER:
342 
343         /* No conversion necessary */
344 
345         *ResultDesc = ObjDesc;
346         return_ACPI_STATUS (AE_OK);
347 
348 
349     case ACPI_TYPE_INTEGER:
350         /*
351          * Create a new Buffer object.
352          * Need enough space for one integer
353          */
354         ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
355         if (!ReturnDesc)
356         {
357             return_ACPI_STATUS (AE_NO_MEMORY);
358         }
359 
360         /* Copy the integer to the buffer, LSB first */
361 
362         NewBuf = ReturnDesc->Buffer.Pointer;
363         memcpy (NewBuf, &ObjDesc->Integer.Value, AcpiGbl_IntegerByteWidth);
364         break;
365 
366     case ACPI_TYPE_STRING:
367         /*
368          * Create a new Buffer object
369          * Size will be the string length
370          *
371          * NOTE: Add one to the string length to include the null terminator.
372          * The ACPI spec is unclear on this subject, but there is existing
373          * ASL/AML code that depends on the null being transferred to the new
374          * buffer.
375          */
376         ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
377             ObjDesc->String.Length + 1);
378         if (!ReturnDesc)
379         {
380             return_ACPI_STATUS (AE_NO_MEMORY);
381         }
382 
383         /* Copy the string to the buffer */
384 
385         NewBuf = ReturnDesc->Buffer.Pointer;
386         strncpy ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
387             ObjDesc->String.Length);
388         break;
389 
390     default:
391 
392         return_ACPI_STATUS (AE_TYPE);
393     }
394 
395     /* Mark buffer initialized */
396 
397     ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
398     *ResultDesc = ReturnDesc;
399     return_ACPI_STATUS (AE_OK);
400 }
401 
402 
403 /*******************************************************************************
404  *
405  * FUNCTION:    AcpiExConvertToAscii
406  *
407  * PARAMETERS:  Integer         - Value to be converted
408  *              Base            - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
409  *              String          - Where the string is returned
410  *              DataWidth       - Size of data item to be converted, in bytes
411  *
412  * RETURN:      Actual string length
413  *
414  * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
415  *
416  ******************************************************************************/
417 
418 static UINT32
419 AcpiExConvertToAscii (
420     UINT64                  Integer,
421     UINT16                  Base,
422     UINT8                   *String,
423     UINT8                   DataWidth)
424 {
425     UINT64                  Digit;
426     UINT32                  i;
427     UINT32                  j;
428     UINT32                  k = 0;
429     UINT32                  HexLength;
430     UINT32                  DecimalLength;
431     UINT32                  Remainder;
432     BOOLEAN                 SupressZeros;
433 
434 
435     ACPI_FUNCTION_ENTRY ();
436 
437 
438     switch (Base)
439     {
440     case 10:
441 
442         /* Setup max length for the decimal number */
443 
444         switch (DataWidth)
445         {
446         case 1:
447 
448             DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
449             break;
450 
451         case 4:
452 
453             DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
454             break;
455 
456         case 8:
457         default:
458 
459             DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
460             break;
461         }
462 
463         SupressZeros = TRUE;     /* No leading zeros */
464         Remainder = 0;
465 
466         for (i = DecimalLength; i > 0; i--)
467         {
468             /* Divide by nth factor of 10 */
469 
470             Digit = Integer;
471             for (j = 0; j < i; j++)
472             {
473                 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
474             }
475 
476             /* Handle leading zeros */
477 
478             if (Remainder != 0)
479             {
480                 SupressZeros = FALSE;
481             }
482 
483             if (!SupressZeros)
484             {
485                 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
486                 k++;
487             }
488         }
489         break;
490 
491     case 16:
492 
493         /* HexLength: 2 ascii hex chars per data byte */
494 
495         HexLength = ACPI_MUL_2 (DataWidth);
496         for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
497         {
498             /* Get one hex digit, most significant digits first */
499 
500             String[k] = (UINT8)
501                 AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
502             k++;
503         }
504         break;
505 
506     default:
507         return (0);
508     }
509 
510     /*
511      * Since leading zeros are suppressed, we must check for the case where
512      * the integer equals 0
513      *
514      * Finally, null terminate the string and return the length
515      */
516     if (!k)
517     {
518         String [0] = ACPI_ASCII_ZERO;
519         k = 1;
520     }
521 
522     String [k] = 0;
523     return ((UINT32) k);
524 }
525 
526 
527 /*******************************************************************************
528  *
529  * FUNCTION:    AcpiExConvertToString
530  *
531  * PARAMETERS:  ObjDesc         - Object to be converted. Must be an
532  *                                Integer, Buffer, or String
533  *              ResultDesc      - Where the string object is returned
534  *              Type            - String flags (base and conversion type)
535  *
536  * RETURN:      Status
537  *
538  * DESCRIPTION: Convert an ACPI Object to a string
539  *
540  ******************************************************************************/
541 
542 ACPI_STATUS
543 AcpiExConvertToString (
544     ACPI_OPERAND_OBJECT     *ObjDesc,
545     ACPI_OPERAND_OBJECT     **ResultDesc,
546     UINT32                  Type)
547 {
548     ACPI_OPERAND_OBJECT     *ReturnDesc;
549     UINT8                   *NewBuf;
550     UINT32                  i;
551     UINT32                  StringLength = 0;
552     UINT16                  Base = 16;
553     UINT8                   Separator = ',';
554 
555 
556     ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
557 
558 
559     switch (ObjDesc->Common.Type)
560     {
561     case ACPI_TYPE_STRING:
562 
563         /* No conversion necessary */
564 
565         *ResultDesc = ObjDesc;
566         return_ACPI_STATUS (AE_OK);
567 
568     case ACPI_TYPE_INTEGER:
569 
570         switch (Type)
571         {
572         case ACPI_EXPLICIT_CONVERT_DECIMAL:
573 
574             /* Make room for maximum decimal number */
575 
576             StringLength = ACPI_MAX_DECIMAL_DIGITS;
577             Base = 10;
578             break;
579 
580         default:
581 
582             /* Two hex string characters for each integer byte */
583 
584             StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
585             break;
586         }
587 
588         /*
589          * Create a new String
590          * Need enough space for one ASCII integer (plus null terminator)
591          */
592         ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
593         if (!ReturnDesc)
594         {
595             return_ACPI_STATUS (AE_NO_MEMORY);
596         }
597 
598         NewBuf = ReturnDesc->Buffer.Pointer;
599 
600         /* Convert integer to string */
601 
602         StringLength = AcpiExConvertToAscii (
603             ObjDesc->Integer.Value, Base, NewBuf, AcpiGbl_IntegerByteWidth);
604 
605         /* Null terminate at the correct place */
606 
607         ReturnDesc->String.Length = StringLength;
608         NewBuf [StringLength] = 0;
609         break;
610 
611     case ACPI_TYPE_BUFFER:
612 
613         /* Setup string length, base, and separator */
614 
615         switch (Type)
616         {
617         case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
618             /*
619              * From ACPI: "If Data is a buffer, it is converted to a string of
620              * decimal values separated by commas."
621              */
622             Base = 10;
623 
624             /*
625              * Calculate the final string length. Individual string values
626              * are variable length (include separator for each)
627              */
628             for (i = 0; i < ObjDesc->Buffer.Length; i++)
629             {
630                 if (ObjDesc->Buffer.Pointer[i] >= 100)
631                 {
632                     StringLength += 4;
633                 }
634                 else if (ObjDesc->Buffer.Pointer[i] >= 10)
635                 {
636                     StringLength += 3;
637                 }
638                 else
639                 {
640                     StringLength += 2;
641                 }
642             }
643             break;
644 
645         case ACPI_IMPLICIT_CONVERT_HEX:
646             /*
647              * From the ACPI spec:
648              *"The entire contents of the buffer are converted to a string of
649              * two-character hexadecimal numbers, each separated by a space."
650              */
651             Separator = ' ';
652             StringLength = (ObjDesc->Buffer.Length * 3);
653             break;
654 
655         case ACPI_EXPLICIT_CONVERT_HEX:     /* Used by ToHexString */
656             /*
657              * From ACPI: "If Data is a buffer, it is converted to a string of
658              * hexadecimal values separated by commas."
659              */
660             StringLength = (ObjDesc->Buffer.Length * 3);
661             break;
662 
663         default:
664             return_ACPI_STATUS (AE_BAD_PARAMETER);
665         }
666 
667         /*
668          * Create a new string object and string buffer
669          * (-1 because of extra separator included in StringLength from above)
670          * Allow creation of zero-length strings from zero-length buffers.
671          */
672         if (StringLength)
673         {
674             StringLength--;
675         }
676 
677         ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
678         if (!ReturnDesc)
679         {
680             return_ACPI_STATUS (AE_NO_MEMORY);
681         }
682 
683         NewBuf = ReturnDesc->Buffer.Pointer;
684 
685         /*
686          * Convert buffer bytes to hex or decimal values
687          * (separated by commas or spaces)
688          */
689         for (i = 0; i < ObjDesc->Buffer.Length; i++)
690         {
691             NewBuf += AcpiExConvertToAscii (
692                 (UINT64) ObjDesc->Buffer.Pointer[i], Base, NewBuf, 1);
693             *NewBuf++ = Separator; /* each separated by a comma or space */
694         }
695 
696         /*
697          * Null terminate the string
698          * (overwrites final comma/space from above)
699          */
700         if (ObjDesc->Buffer.Length)
701         {
702             NewBuf--;
703         }
704         *NewBuf = 0;
705         break;
706 
707     default:
708 
709         return_ACPI_STATUS (AE_TYPE);
710     }
711 
712     *ResultDesc = ReturnDesc;
713     return_ACPI_STATUS (AE_OK);
714 }
715 
716 
717 /*******************************************************************************
718  *
719  * FUNCTION:    AcpiExConvertToTargetType
720  *
721  * PARAMETERS:  DestinationType     - Current type of the destination
722  *              SourceDesc          - Source object to be converted.
723  *              ResultDesc          - Where the converted object is returned
724  *              WalkState           - Current method state
725  *
726  * RETURN:      Status
727  *
728  * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
729  *
730  ******************************************************************************/
731 
732 ACPI_STATUS
733 AcpiExConvertToTargetType (
734     ACPI_OBJECT_TYPE        DestinationType,
735     ACPI_OPERAND_OBJECT     *SourceDesc,
736     ACPI_OPERAND_OBJECT     **ResultDesc,
737     ACPI_WALK_STATE         *WalkState)
738 {
739     ACPI_STATUS             Status = AE_OK;
740 
741 
742     ACPI_FUNCTION_TRACE (ExConvertToTargetType);
743 
744 
745     /* Default behavior */
746 
747     *ResultDesc = SourceDesc;
748 
749     /*
750      * If required by the target,
751      * perform implicit conversion on the source before we store it.
752      */
753     switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
754     {
755     case ARGI_SIMPLE_TARGET:
756     case ARGI_INTEGER_REF:      /* Handles Increment, Decrement cases */
757 
758         switch (DestinationType)
759         {
760         case ACPI_TYPE_LOCAL_REGION_FIELD:
761             /*
762              * Named field can always handle conversions
763              */
764             break;
765 
766         default:
767 
768             /* No conversion allowed for these types */
769 
770             if (DestinationType != SourceDesc->Common.Type)
771             {
772                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
773                     "Explicit operator, will store (%s) over existing type (%s)\n",
774                     AcpiUtGetObjectTypeName (SourceDesc),
775                     AcpiUtGetTypeName (DestinationType)));
776                 Status = AE_TYPE;
777             }
778         }
779         break;
780 
781     case ARGI_TARGETREF:
782     case ARGI_STORE_TARGET:
783 
784         switch (DestinationType)
785         {
786         case ACPI_TYPE_INTEGER:
787         case ACPI_TYPE_BUFFER_FIELD:
788         case ACPI_TYPE_LOCAL_BANK_FIELD:
789         case ACPI_TYPE_LOCAL_INDEX_FIELD:
790             /*
791              * These types require an Integer operand. We can convert
792              * a Buffer or a String to an Integer if necessary.
793              */
794             Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
795                 ACPI_STRTOUL_BASE16);
796             break;
797 
798         case ACPI_TYPE_STRING:
799             /*
800              * The operand must be a String. We can convert an
801              * Integer or Buffer if necessary
802              */
803             Status = AcpiExConvertToString (SourceDesc, ResultDesc,
804                 ACPI_IMPLICIT_CONVERT_HEX);
805             break;
806 
807         case ACPI_TYPE_BUFFER:
808             /*
809              * The operand must be a Buffer. We can convert an
810              * Integer or String if necessary
811              */
812             Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
813             break;
814 
815         default:
816 
817             ACPI_ERROR ((AE_INFO,
818                 "Bad destination type during conversion: 0x%X",
819                 DestinationType));
820             Status = AE_AML_INTERNAL;
821             break;
822         }
823         break;
824 
825     case ARGI_REFERENCE:
826         /*
827          * CreateXxxxField cases - we are storing the field object into the name
828          */
829         break;
830 
831     default:
832 
833         ACPI_ERROR ((AE_INFO,
834             "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
835             GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
836             WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
837         Status = AE_AML_INTERNAL;
838     }
839 
840     /*
841      * Source-to-Target conversion semantics:
842      *
843      * If conversion to the target type cannot be performed, then simply
844      * overwrite the target with the new object and type.
845      */
846     if (Status == AE_TYPE)
847     {
848         Status = AE_OK;
849     }
850 
851     return_ACPI_STATUS (Status);
852 }
853