xref: /freebsd/sys/contrib/dev/acpica/components/executer/exconvrt.c (revision 92f570c32e9b9b9b4db9b6921c4ab79be686a498)
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 - 2024, 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     BOOLEAN                 LeadingZeros);
170 
171 
172 /*******************************************************************************
173  *
174  * FUNCTION:    AcpiExConvertToInteger
175  *
176  * PARAMETERS:  ObjDesc             - Object to be converted. Must be an
177  *                                    Integer, Buffer, or String
178  *              ResultDesc          - Where the new Integer object is returned
179  *              ImplicitConversion  - Used for string conversion
180  *
181  * RETURN:      Status
182  *
183  * DESCRIPTION: Convert an ACPI Object to an integer.
184  *
185  ******************************************************************************/
186 
187 ACPI_STATUS
AcpiExConvertToInteger(ACPI_OPERAND_OBJECT * ObjDesc,ACPI_OPERAND_OBJECT ** ResultDesc,UINT32 ImplicitConversion)188 AcpiExConvertToInteger (
189     ACPI_OPERAND_OBJECT     *ObjDesc,
190     ACPI_OPERAND_OBJECT     **ResultDesc,
191     UINT32                  ImplicitConversion)
192 {
193     ACPI_OPERAND_OBJECT     *ReturnDesc;
194     UINT8                   *Pointer;
195     UINT64                  Result;
196     UINT32                  i;
197     UINT32                  Count;
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          * Explicit conversion is used only by ToInteger.
249          * All other string-to-integer conversions are implicit conversions.
250          */
251         if (ImplicitConversion)
252         {
253             Result = AcpiUtImplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer));
254         }
255         else
256         {
257             Result = AcpiUtExplicitStrtoul64 (ACPI_CAST_PTR (char, Pointer));
258         }
259         break;
260 
261     case ACPI_TYPE_BUFFER:
262 
263         /* Check for zero-length buffer */
264 
265         if (!Count)
266         {
267             return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
268         }
269 
270         /* Transfer no more than an integer's worth of data */
271 
272         if (Count > AcpiGbl_IntegerByteWidth)
273         {
274             Count = AcpiGbl_IntegerByteWidth;
275         }
276 
277         /*
278          * Convert buffer to an integer - we simply grab enough raw data
279          * from the buffer to fill an integer
280          */
281         for (i = 0; i < Count; i++)
282         {
283             /*
284              * Get next byte and shift it into the Result.
285              * Little endian is used, meaning that the first byte of the buffer
286              * is the LSB of the integer
287              */
288             Result |= (((UINT64) Pointer[i]) << (i * 8));
289         }
290         break;
291 
292     default:
293 
294         /* No other types can get here */
295 
296         break;
297     }
298 
299     /* Create a new integer */
300 
301     ReturnDesc = AcpiUtCreateIntegerObject (Result);
302     if (!ReturnDesc)
303     {
304         return_ACPI_STATUS (AE_NO_MEMORY);
305     }
306 
307     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
308         ACPI_FORMAT_UINT64 (Result)));
309 
310     /* Save the Result */
311 
312     (void) AcpiExTruncateFor32bitTable (ReturnDesc);
313     *ResultDesc = ReturnDesc;
314     return_ACPI_STATUS (AE_OK);
315 }
316 
317 
318 /*******************************************************************************
319  *
320  * FUNCTION:    AcpiExConvertToBuffer
321  *
322  * PARAMETERS:  ObjDesc         - Object to be converted. Must be an
323  *                                Integer, Buffer, or String
324  *              ResultDesc      - Where the new buffer object is returned
325  *
326  * RETURN:      Status
327  *
328  * DESCRIPTION: Convert an ACPI Object to a Buffer
329  *
330  ******************************************************************************/
331 
332 ACPI_STATUS
AcpiExConvertToBuffer(ACPI_OPERAND_OBJECT * ObjDesc,ACPI_OPERAND_OBJECT ** ResultDesc)333 AcpiExConvertToBuffer (
334     ACPI_OPERAND_OBJECT     *ObjDesc,
335     ACPI_OPERAND_OBJECT     **ResultDesc)
336 {
337     ACPI_OPERAND_OBJECT     *ReturnDesc;
338     UINT8                   *NewBuf;
339 
340 
341     ACPI_FUNCTION_TRACE_PTR (ExConvertToBuffer, ObjDesc);
342 
343 
344     switch (ObjDesc->Common.Type)
345     {
346     case ACPI_TYPE_BUFFER:
347 
348         /* No conversion necessary */
349 
350         *ResultDesc = ObjDesc;
351         return_ACPI_STATUS (AE_OK);
352 
353 
354     case ACPI_TYPE_INTEGER:
355         /*
356          * Create a new Buffer object.
357          * Need enough space for one integer
358          */
359         ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth);
360         if (!ReturnDesc)
361         {
362             return_ACPI_STATUS (AE_NO_MEMORY);
363         }
364 
365         /* Copy the integer to the buffer, LSB first */
366 
367         NewBuf = ReturnDesc->Buffer.Pointer;
368         memcpy (NewBuf, &ObjDesc->Integer.Value, AcpiGbl_IntegerByteWidth);
369         break;
370 
371     case ACPI_TYPE_STRING:
372         /*
373          * Create a new Buffer object
374          * Size will be the string length
375          *
376          * NOTE: Add one to the string length to include the null terminator.
377          * The ACPI spec is unclear on this subject, but there is existing
378          * ASL/AML code that depends on the null being transferred to the new
379          * buffer.
380          */
381         ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
382             ObjDesc->String.Length + 1);
383         if (!ReturnDesc)
384         {
385             return_ACPI_STATUS (AE_NO_MEMORY);
386         }
387 
388         /* Copy the string to the buffer */
389 
390         NewBuf = ReturnDesc->Buffer.Pointer;
391         strncpy ((char *) NewBuf, (char *) ObjDesc->String.Pointer,
392             ObjDesc->String.Length);
393         break;
394 
395     default:
396 
397         return_ACPI_STATUS (AE_TYPE);
398     }
399 
400     /* Mark buffer initialized */
401 
402     ReturnDesc->Common.Flags |= AOPOBJ_DATA_VALID;
403     *ResultDesc = ReturnDesc;
404     return_ACPI_STATUS (AE_OK);
405 }
406 
407 
408 /*******************************************************************************
409  *
410  * FUNCTION:    AcpiExConvertToAscii
411  *
412  * PARAMETERS:  Integer         - Value to be converted
413  *              Base            - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
414  *              String          - Where the string is returned
415  *              DataWidth       - Size of data item to be converted, in bytes
416  *              LeadingZeros    - Allow leading zeros
417  *
418  * RETURN:      Actual string length
419  *
420  * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
421  *
422  ******************************************************************************/
423 
424 static UINT32
AcpiExConvertToAscii(UINT64 Integer,UINT16 Base,UINT8 * String,UINT8 DataWidth,BOOLEAN LeadingZeros)425 AcpiExConvertToAscii (
426     UINT64                  Integer,
427     UINT16                  Base,
428     UINT8                   *String,
429     UINT8                   DataWidth,
430     BOOLEAN                 LeadingZeros)
431 {
432     UINT64                  Digit;
433     UINT32                  i;
434     UINT32                  j;
435     UINT32                  k = 0;
436     UINT32                  HexLength;
437     UINT32                  DecimalLength;
438     UINT32                  Remainder;
439     BOOLEAN                 SupressZeros = !LeadingZeros;
440     UINT8                   HexChar;
441 
442 
443     ACPI_FUNCTION_ENTRY ();
444 
445 
446     switch (Base)
447     {
448     case 10:
449 
450         /* Setup max length for the decimal number */
451 
452         switch (DataWidth)
453         {
454         case 1:
455 
456             DecimalLength = ACPI_MAX8_DECIMAL_DIGITS;
457             break;
458 
459         case 4:
460 
461             DecimalLength = ACPI_MAX32_DECIMAL_DIGITS;
462             break;
463 
464         case 8:
465         default:
466 
467             DecimalLength = ACPI_MAX64_DECIMAL_DIGITS;
468             break;
469         }
470 
471         Remainder = 0;
472 
473         for (i = DecimalLength; i > 0; i--)
474         {
475             /* Divide by nth factor of 10 */
476 
477             Digit = Integer;
478             for (j = 0; j < i; j++)
479             {
480                 (void) AcpiUtShortDivide (Digit, 10, &Digit, &Remainder);
481             }
482 
483             /* Handle leading zeros */
484 
485             if (Remainder != 0)
486             {
487                 SupressZeros = FALSE;
488             }
489 
490             if (!SupressZeros)
491             {
492                 String[k] = (UINT8) (ACPI_ASCII_ZERO + Remainder);
493                 k++;
494             }
495         }
496         break;
497 
498     case 16:
499 
500         /* HexLength: 2 ascii hex chars per data byte */
501 
502         HexLength = (DataWidth * 2);
503         for (i = 0, j = (HexLength-1); i < HexLength; i++, j--)
504         {
505             /* Get one hex digit, most significant digits first */
506 
507             HexChar = (UINT8)
508                 AcpiUtHexToAsciiChar (Integer, ACPI_MUL_4 (j));
509 
510             /* Supress leading zeros until the first non-zero character */
511 
512             if (HexChar == ACPI_ASCII_ZERO && SupressZeros)
513             {
514                 continue;
515             }
516 
517             SupressZeros = FALSE;
518             String[k] = HexChar;
519             k++;
520         }
521         break;
522 
523     default:
524         return (0);
525     }
526 
527     /*
528      * Since leading zeros are suppressed, we must check for the case where
529      * the integer equals 0
530      *
531      * Finally, null terminate the string and return the length
532      */
533     if (!k)
534     {
535         String [0] = ACPI_ASCII_ZERO;
536         k = 1;
537     }
538 
539     String [k] = 0;
540     return ((UINT32) k);
541 }
542 
543 
544 /*******************************************************************************
545  *
546  * FUNCTION:    AcpiExConvertToString
547  *
548  * PARAMETERS:  ObjDesc         - Object to be converted. Must be an
549  *                                Integer, Buffer, or String
550  *              ResultDesc      - Where the string object is returned
551  *              Type            - String flags (base and conversion type)
552  *
553  * RETURN:      Status
554  *
555  * DESCRIPTION: Convert an ACPI Object to a string. Supports both implicit
556  *              and explicit conversions and related rules.
557  *
558  ******************************************************************************/
559 
560 ACPI_STATUS
AcpiExConvertToString(ACPI_OPERAND_OBJECT * ObjDesc,ACPI_OPERAND_OBJECT ** ResultDesc,UINT32 Type)561 AcpiExConvertToString (
562     ACPI_OPERAND_OBJECT     *ObjDesc,
563     ACPI_OPERAND_OBJECT     **ResultDesc,
564     UINT32                  Type)
565 {
566     ACPI_OPERAND_OBJECT     *ReturnDesc;
567     UINT8                   *NewBuf;
568     UINT32                  i;
569     UINT32                  StringLength = 0;
570     UINT16                  Base = 16;
571     UINT8                   Separator = ',';
572     BOOLEAN                 LeadingZeros;
573 
574 
575     ACPI_FUNCTION_TRACE_PTR (ExConvertToString, ObjDesc);
576 
577 
578     switch (ObjDesc->Common.Type)
579     {
580     case ACPI_TYPE_STRING:
581 
582         /* No conversion necessary */
583 
584         *ResultDesc = ObjDesc;
585         return_ACPI_STATUS (AE_OK);
586 
587     case ACPI_TYPE_INTEGER:
588 
589         switch (Type)
590         {
591         case ACPI_EXPLICIT_CONVERT_DECIMAL:
592             /*
593              * From ToDecimalString, integer source.
594              *
595              * Make room for the maximum decimal number size
596              */
597             StringLength = ACPI_MAX_DECIMAL_DIGITS;
598             LeadingZeros = FALSE;
599             Base = 10;
600             break;
601 
602         case ACPI_EXPLICIT_CONVERT_HEX:
603             /*
604              * From ToHexString.
605              *
606              * Supress leading zeros and append "0x"
607              */
608             StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth) + 2;
609             LeadingZeros = FALSE;
610             break;
611         default:
612 
613             /* Two hex string characters for each integer byte */
614 
615             StringLength = ACPI_MUL_2 (AcpiGbl_IntegerByteWidth);
616             LeadingZeros = TRUE;
617             break;
618         }
619 
620         /*
621          * Create a new String
622          * Need enough space for one ASCII integer (plus null terminator)
623          */
624         ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
625         if (!ReturnDesc)
626         {
627             return_ACPI_STATUS (AE_NO_MEMORY);
628         }
629 
630         NewBuf = ReturnDesc->Buffer.Pointer;
631         if (Type == ACPI_EXPLICIT_CONVERT_HEX)
632         {
633             /* Append "0x" prefix for explicit hex conversion */
634 
635             *NewBuf++ = '0';
636             *NewBuf++ = 'x';
637         }
638 
639         /* Convert integer to string */
640 
641         StringLength = AcpiExConvertToAscii (
642             ObjDesc->Integer.Value, Base, NewBuf, AcpiGbl_IntegerByteWidth, LeadingZeros);
643 
644         /* Null terminate at the correct place */
645 
646         ReturnDesc->String.Length = StringLength;
647         if (Type == ACPI_EXPLICIT_CONVERT_HEX)
648         {
649             /* Take "0x" prefix into account */
650 
651             ReturnDesc->String.Length += 2;
652         }
653 
654         NewBuf [StringLength] = 0;
655         break;
656 
657     case ACPI_TYPE_BUFFER:
658 
659         /* Setup string length, base, and separator */
660 
661         switch (Type)
662         {
663         case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by ToDecimalString */
664             /*
665              * Explicit conversion from the ToDecimalString ASL operator.
666              *
667              * From ACPI: "If the input is a buffer, it is converted to a
668              * a string of decimal values separated by commas."
669              */
670             LeadingZeros = FALSE;
671             Base = 10;
672 
673             /*
674              * Calculate the final string length. Individual string values
675              * are variable length (include separator for each)
676              */
677             for (i = 0; i < ObjDesc->Buffer.Length; i++)
678             {
679                 if (ObjDesc->Buffer.Pointer[i] >= 100)
680                 {
681                     StringLength += 4;
682                 }
683                 else if (ObjDesc->Buffer.Pointer[i] >= 10)
684                 {
685                     StringLength += 3;
686                 }
687                 else
688                 {
689                     StringLength += 2;
690                 }
691             }
692             break;
693 
694         case ACPI_IMPLICIT_CONVERT_HEX:
695             /*
696              * Implicit buffer-to-string conversion
697              *
698              * From the ACPI spec:
699              * "The entire contents of the buffer are converted to a string of
700              * two-character hexadecimal numbers, each separated by a space."
701              *
702              * Each hex number is prefixed with 0x (11/2018)
703              */
704             LeadingZeros = TRUE;
705             Separator = ' ';
706             StringLength = (ObjDesc->Buffer.Length * 5);
707             break;
708 
709         case ACPI_EXPLICIT_CONVERT_HEX:
710             /*
711              * Explicit conversion from the ToHexString ASL operator.
712              *
713              * From ACPI: "If Data is a buffer, it is converted to a string of
714              * hexadecimal values separated by commas."
715              *
716              * Each hex number is prefixed with 0x (11/2018)
717              */
718             LeadingZeros = TRUE;
719             Separator = ',';
720             StringLength = (ObjDesc->Buffer.Length * 5);
721             break;
722 
723         default:
724             return_ACPI_STATUS (AE_BAD_PARAMETER);
725         }
726 
727         /*
728          * Create a new string object and string buffer
729          * (-1 because of extra separator included in StringLength from above)
730          * Allow creation of zero-length strings from zero-length buffers.
731          */
732         if (StringLength)
733         {
734             StringLength--;
735         }
736 
737         ReturnDesc = AcpiUtCreateStringObject ((ACPI_SIZE) StringLength);
738         if (!ReturnDesc)
739         {
740             return_ACPI_STATUS (AE_NO_MEMORY);
741         }
742 
743         NewBuf = ReturnDesc->Buffer.Pointer;
744 
745         /*
746          * Convert buffer bytes to hex or decimal values
747          * (separated by commas or spaces)
748          */
749         for (i = 0; i < ObjDesc->Buffer.Length; i++)
750         {
751             if (Base == 16)
752             {
753                 /* Emit 0x prefix for explicit/implicit hex conversion */
754 
755                 *NewBuf++ = '0';
756                 *NewBuf++ = 'x';
757             }
758 
759             NewBuf += AcpiExConvertToAscii (
760                 (UINT64) ObjDesc->Buffer.Pointer[i], Base, NewBuf, 1, LeadingZeros);
761 
762             /* Each digit is separated by either a comma or space */
763 
764             *NewBuf++ = Separator;
765         }
766 
767         /*
768          * Null terminate the string
769          * (overwrites final comma/space from above)
770          */
771         if (ObjDesc->Buffer.Length)
772         {
773             NewBuf--;
774         }
775         *NewBuf = 0;
776         break;
777 
778     default:
779 
780         return_ACPI_STATUS (AE_TYPE);
781     }
782 
783     *ResultDesc = ReturnDesc;
784     return_ACPI_STATUS (AE_OK);
785 }
786 
787 
788 /*******************************************************************************
789  *
790  * FUNCTION:    AcpiExConvertToTargetType
791  *
792  * PARAMETERS:  DestinationType     - Current type of the destination
793  *              SourceDesc          - Source object to be converted.
794  *              ResultDesc          - Where the converted object is returned
795  *              WalkState           - Current method state
796  *
797  * RETURN:      Status
798  *
799  * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
800  *
801  ******************************************************************************/
802 
803 ACPI_STATUS
AcpiExConvertToTargetType(ACPI_OBJECT_TYPE DestinationType,ACPI_OPERAND_OBJECT * SourceDesc,ACPI_OPERAND_OBJECT ** ResultDesc,ACPI_WALK_STATE * WalkState)804 AcpiExConvertToTargetType (
805     ACPI_OBJECT_TYPE        DestinationType,
806     ACPI_OPERAND_OBJECT     *SourceDesc,
807     ACPI_OPERAND_OBJECT     **ResultDesc,
808     ACPI_WALK_STATE         *WalkState)
809 {
810     ACPI_STATUS             Status = AE_OK;
811 
812 
813     ACPI_FUNCTION_TRACE (ExConvertToTargetType);
814 
815 
816     /* Default behavior */
817 
818     *ResultDesc = SourceDesc;
819 
820     /*
821      * If required by the target,
822      * perform implicit conversion on the source before we store it.
823      */
824     switch (GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs))
825     {
826     case ARGI_SIMPLE_TARGET:
827     case ARGI_FIXED_TARGET:
828     case ARGI_INTEGER_REF:      /* Handles Increment, Decrement cases */
829 
830         switch (DestinationType)
831         {
832         case ACPI_TYPE_LOCAL_REGION_FIELD:
833             /*
834              * Named field can always handle conversions
835              */
836             break;
837 
838         default:
839 
840             /* No conversion allowed for these types */
841 
842             if (DestinationType != SourceDesc->Common.Type)
843             {
844                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
845                     "Explicit operator, will store (%s) over existing type (%s)\n",
846                     AcpiUtGetObjectTypeName (SourceDesc),
847                     AcpiUtGetTypeName (DestinationType)));
848                 Status = AE_TYPE;
849             }
850         }
851         break;
852 
853     case ARGI_TARGETREF:
854     case ARGI_STORE_TARGET:
855 
856         switch (DestinationType)
857         {
858         case ACPI_TYPE_INTEGER:
859         case ACPI_TYPE_BUFFER_FIELD:
860         case ACPI_TYPE_LOCAL_BANK_FIELD:
861         case ACPI_TYPE_LOCAL_INDEX_FIELD:
862             /*
863              * These types require an Integer operand. We can convert
864              * a Buffer or a String to an Integer if necessary.
865              */
866             Status = AcpiExConvertToInteger (SourceDesc, ResultDesc,
867                 ACPI_IMPLICIT_CONVERSION);
868             break;
869 
870         case ACPI_TYPE_STRING:
871             /*
872              * The operand must be a String. We can convert an
873              * Integer or Buffer if necessary
874              */
875             Status = AcpiExConvertToString (SourceDesc, ResultDesc,
876                 ACPI_IMPLICIT_CONVERT_HEX);
877             break;
878 
879         case ACPI_TYPE_BUFFER:
880             /*
881              * The operand must be a Buffer. We can convert an
882              * Integer or String if necessary
883              */
884             Status = AcpiExConvertToBuffer (SourceDesc, ResultDesc);
885             break;
886 
887         default:
888 
889             ACPI_ERROR ((AE_INFO,
890                 "Bad destination type during conversion: 0x%X",
891                 DestinationType));
892             Status = AE_AML_INTERNAL;
893             break;
894         }
895         break;
896 
897     case ARGI_REFERENCE:
898         /*
899          * CreateXxxxField cases - we are storing the field object into the name
900          */
901         break;
902 
903     default:
904 
905         ACPI_ERROR ((AE_INFO,
906             "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
907             GET_CURRENT_ARG_TYPE (WalkState->OpInfo->RuntimeArgs),
908             WalkState->Opcode, AcpiUtGetTypeName (DestinationType)));
909         Status = AE_AML_INTERNAL;
910     }
911 
912     /*
913      * Source-to-Target conversion semantics:
914      *
915      * If conversion to the target type cannot be performed, then simply
916      * overwrite the target with the new object and type.
917      */
918     if (Status == AE_TYPE)
919     {
920         Status = AE_OK;
921     }
922 
923     return_ACPI_STATUS (Status);
924 }
925