GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / acpi / acpica / exconvrt.c
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: exconvrt - Object conversion routines
5  *
6  * Copyright (C) 2000 - 2018, Intel Corp.
7  *
8  *****************************************************************************/
9
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acinterp.h"
13 #include "amlcode.h"
14
15 #define _COMPONENT          ACPI_EXECUTER
16 ACPI_MODULE_NAME("exconvrt")
17
18 /* Local prototypes */
19 static u32
20 acpi_ex_convert_to_ascii(u64 integer, u16 base, u8 *string, u8 max_length);
21
22 /*******************************************************************************
23  *
24  * FUNCTION:    acpi_ex_convert_to_integer
25  *
26  * PARAMETERS:  obj_desc            - Object to be converted. Must be an
27  *                                    Integer, Buffer, or String
28  *              result_desc         - Where the new Integer object is returned
29  *              implicit_conversion - Used for string conversion
30  *
31  * RETURN:      Status
32  *
33  * DESCRIPTION: Convert an ACPI Object to an integer.
34  *
35  ******************************************************************************/
36
37 acpi_status
38 acpi_ex_convert_to_integer(union acpi_operand_object *obj_desc,
39                            union acpi_operand_object **result_desc,
40                            u32 implicit_conversion)
41 {
42         union acpi_operand_object *return_desc;
43         u8 *pointer;
44         u64 result;
45         u32 i;
46         u32 count;
47
48         ACPI_FUNCTION_TRACE_PTR(ex_convert_to_integer, obj_desc);
49
50         switch (obj_desc->common.type) {
51         case ACPI_TYPE_INTEGER:
52
53                 /* No conversion necessary */
54
55                 *result_desc = obj_desc;
56                 return_ACPI_STATUS(AE_OK);
57
58         case ACPI_TYPE_BUFFER:
59         case ACPI_TYPE_STRING:
60
61                 /* Note: Takes advantage of common buffer/string fields */
62
63                 pointer = obj_desc->buffer.pointer;
64                 count = obj_desc->buffer.length;
65                 break;
66
67         default:
68
69                 return_ACPI_STATUS(AE_TYPE);
70         }
71
72         /*
73          * Convert the buffer/string to an integer. Note that both buffers and
74          * strings are treated as raw data - we don't convert ascii to hex for
75          * strings.
76          *
77          * There are two terminating conditions for the loop:
78          * 1) The size of an integer has been reached, or
79          * 2) The end of the buffer or string has been reached
80          */
81         result = 0;
82
83         /* String conversion is different than Buffer conversion */
84
85         switch (obj_desc->common.type) {
86         case ACPI_TYPE_STRING:
87                 /*
88                  * Convert string to an integer - for most cases, the string must be
89                  * hexadecimal as per the ACPI specification. The only exception (as
90                  * of ACPI 3.0) is that the to_integer() operator allows both decimal
91                  * and hexadecimal strings (hex prefixed with "0x").
92                  *
93                  * Explicit conversion is used only by to_integer.
94                  * All other string-to-integer conversions are implicit conversions.
95                  */
96                 if (implicit_conversion) {
97                         result =
98                             acpi_ut_implicit_strtoul64(ACPI_CAST_PTR
99                                                        (char, pointer));
100                 } else {
101                         result =
102                             acpi_ut_explicit_strtoul64(ACPI_CAST_PTR
103                                                        (char, pointer));
104                 }
105                 break;
106
107         case ACPI_TYPE_BUFFER:
108
109                 /* Check for zero-length buffer */
110
111                 if (!count) {
112                         return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
113                 }
114
115                 /* Transfer no more than an integer's worth of data */
116
117                 if (count > acpi_gbl_integer_byte_width) {
118                         count = acpi_gbl_integer_byte_width;
119                 }
120
121                 /*
122                  * Convert buffer to an integer - we simply grab enough raw data
123                  * from the buffer to fill an integer
124                  */
125                 for (i = 0; i < count; i++) {
126                         /*
127                          * Get next byte and shift it into the Result.
128                          * Little endian is used, meaning that the first byte of the buffer
129                          * is the LSB of the integer
130                          */
131                         result |= (((u64) pointer[i]) << (i * 8));
132                 }
133                 break;
134
135         default:
136
137                 /* No other types can get here */
138
139                 break;
140         }
141
142         /* Create a new integer */
143
144         return_desc = acpi_ut_create_integer_object(result);
145         if (!return_desc) {
146                 return_ACPI_STATUS(AE_NO_MEMORY);
147         }
148
149         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
150                           ACPI_FORMAT_UINT64(result)));
151
152         /* Save the Result */
153
154         (void)acpi_ex_truncate_for32bit_table(return_desc);
155         *result_desc = return_desc;
156         return_ACPI_STATUS(AE_OK);
157 }
158
159 /*******************************************************************************
160  *
161  * FUNCTION:    acpi_ex_convert_to_buffer
162  *
163  * PARAMETERS:  obj_desc        - Object to be converted. Must be an
164  *                                Integer, Buffer, or String
165  *              result_desc     - Where the new buffer object is returned
166  *
167  * RETURN:      Status
168  *
169  * DESCRIPTION: Convert an ACPI Object to a Buffer
170  *
171  ******************************************************************************/
172
173 acpi_status
174 acpi_ex_convert_to_buffer(union acpi_operand_object *obj_desc,
175                           union acpi_operand_object **result_desc)
176 {
177         union acpi_operand_object *return_desc;
178         u8 *new_buf;
179
180         ACPI_FUNCTION_TRACE_PTR(ex_convert_to_buffer, obj_desc);
181
182         switch (obj_desc->common.type) {
183         case ACPI_TYPE_BUFFER:
184
185                 /* No conversion necessary */
186
187                 *result_desc = obj_desc;
188                 return_ACPI_STATUS(AE_OK);
189
190         case ACPI_TYPE_INTEGER:
191                 /*
192                  * Create a new Buffer object.
193                  * Need enough space for one integer
194                  */
195                 return_desc =
196                     acpi_ut_create_buffer_object(acpi_gbl_integer_byte_width);
197                 if (!return_desc) {
198                         return_ACPI_STATUS(AE_NO_MEMORY);
199                 }
200
201                 /* Copy the integer to the buffer, LSB first */
202
203                 new_buf = return_desc->buffer.pointer;
204                 memcpy(new_buf, &obj_desc->integer.value,
205                        acpi_gbl_integer_byte_width);
206                 break;
207
208         case ACPI_TYPE_STRING:
209                 /*
210                  * Create a new Buffer object
211                  * Size will be the string length
212                  *
213                  * NOTE: Add one to the string length to include the null terminator.
214                  * The ACPI spec is unclear on this subject, but there is existing
215                  * ASL/AML code that depends on the null being transferred to the new
216                  * buffer.
217                  */
218                 return_desc = acpi_ut_create_buffer_object((acpi_size)
219                                                            obj_desc->string.
220                                                            length + 1);
221                 if (!return_desc) {
222                         return_ACPI_STATUS(AE_NO_MEMORY);
223                 }
224
225                 /* Copy the string to the buffer */
226
227                 new_buf = return_desc->buffer.pointer;
228                 strncpy((char *)new_buf, (char *)obj_desc->string.pointer,
229                         obj_desc->string.length);
230                 break;
231
232         default:
233
234                 return_ACPI_STATUS(AE_TYPE);
235         }
236
237         /* Mark buffer initialized */
238
239         return_desc->common.flags |= AOPOBJ_DATA_VALID;
240         *result_desc = return_desc;
241         return_ACPI_STATUS(AE_OK);
242 }
243
244 /*******************************************************************************
245  *
246  * FUNCTION:    acpi_ex_convert_to_ascii
247  *
248  * PARAMETERS:  integer         - Value to be converted
249  *              base            - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
250  *              string          - Where the string is returned
251  *              data_width      - Size of data item to be converted, in bytes
252  *
253  * RETURN:      Actual string length
254  *
255  * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
256  *
257  ******************************************************************************/
258
259 static u32
260 acpi_ex_convert_to_ascii(u64 integer, u16 base, u8 *string, u8 data_width)
261 {
262         u64 digit;
263         u32 i;
264         u32 j;
265         u32 k = 0;
266         u32 hex_length;
267         u32 decimal_length;
268         u32 remainder;
269         u8 supress_zeros;
270
271         ACPI_FUNCTION_ENTRY();
272
273         switch (base) {
274         case 10:
275
276                 /* Setup max length for the decimal number */
277
278                 switch (data_width) {
279                 case 1:
280
281                         decimal_length = ACPI_MAX8_DECIMAL_DIGITS;
282                         break;
283
284                 case 4:
285
286                         decimal_length = ACPI_MAX32_DECIMAL_DIGITS;
287                         break;
288
289                 case 8:
290                 default:
291
292                         decimal_length = ACPI_MAX64_DECIMAL_DIGITS;
293                         break;
294                 }
295
296                 supress_zeros = TRUE;   /* No leading zeros */
297                 remainder = 0;
298
299                 for (i = decimal_length; i > 0; i--) {
300
301                         /* Divide by nth factor of 10 */
302
303                         digit = integer;
304                         for (j = 0; j < i; j++) {
305                                 (void)acpi_ut_short_divide(digit, 10, &digit,
306                                                            &remainder);
307                         }
308
309                         /* Handle leading zeros */
310
311                         if (remainder != 0) {
312                                 supress_zeros = FALSE;
313                         }
314
315                         if (!supress_zeros) {
316                                 string[k] = (u8) (ACPI_ASCII_ZERO + remainder);
317                                 k++;
318                         }
319                 }
320                 break;
321
322         case 16:
323
324                 /* hex_length: 2 ascii hex chars per data byte */
325
326                 hex_length = ACPI_MUL_2(data_width);
327                 for (i = 0, j = (hex_length - 1); i < hex_length; i++, j--) {
328
329                         /* Get one hex digit, most significant digits first */
330
331                         string[k] = (u8)
332                             acpi_ut_hex_to_ascii_char(integer, ACPI_MUL_4(j));
333                         k++;
334                 }
335                 break;
336
337         default:
338                 return (0);
339         }
340
341         /*
342          * Since leading zeros are suppressed, we must check for the case where
343          * the integer equals 0
344          *
345          * Finally, null terminate the string and return the length
346          */
347         if (!k) {
348                 string[0] = ACPI_ASCII_ZERO;
349                 k = 1;
350         }
351
352         string[k] = 0;
353         return ((u32) k);
354 }
355
356 /*******************************************************************************
357  *
358  * FUNCTION:    acpi_ex_convert_to_string
359  *
360  * PARAMETERS:  obj_desc        - Object to be converted. Must be an
361  *                                Integer, Buffer, or String
362  *              result_desc     - Where the string object is returned
363  *              type            - String flags (base and conversion type)
364  *
365  * RETURN:      Status
366  *
367  * DESCRIPTION: Convert an ACPI Object to a string
368  *
369  ******************************************************************************/
370
371 acpi_status
372 acpi_ex_convert_to_string(union acpi_operand_object * obj_desc,
373                           union acpi_operand_object ** result_desc, u32 type)
374 {
375         union acpi_operand_object *return_desc;
376         u8 *new_buf;
377         u32 i;
378         u32 string_length = 0;
379         u16 base = 16;
380         u8 separator = ',';
381
382         ACPI_FUNCTION_TRACE_PTR(ex_convert_to_string, obj_desc);
383
384         switch (obj_desc->common.type) {
385         case ACPI_TYPE_STRING:
386
387                 /* No conversion necessary */
388
389                 *result_desc = obj_desc;
390                 return_ACPI_STATUS(AE_OK);
391
392         case ACPI_TYPE_INTEGER:
393
394                 switch (type) {
395                 case ACPI_EXPLICIT_CONVERT_DECIMAL:
396
397                         /* Make room for maximum decimal number */
398
399                         string_length = ACPI_MAX_DECIMAL_DIGITS;
400                         base = 10;
401                         break;
402
403                 default:
404
405                         /* Two hex string characters for each integer byte */
406
407                         string_length = ACPI_MUL_2(acpi_gbl_integer_byte_width);
408                         break;
409                 }
410
411                 /*
412                  * Create a new String
413                  * Need enough space for one ASCII integer (plus null terminator)
414                  */
415                 return_desc =
416                     acpi_ut_create_string_object((acpi_size)string_length);
417                 if (!return_desc) {
418                         return_ACPI_STATUS(AE_NO_MEMORY);
419                 }
420
421                 new_buf = return_desc->buffer.pointer;
422
423                 /* Convert integer to string */
424
425                 string_length =
426                     acpi_ex_convert_to_ascii(obj_desc->integer.value, base,
427                                              new_buf,
428                                              acpi_gbl_integer_byte_width);
429
430                 /* Null terminate at the correct place */
431
432                 return_desc->string.length = string_length;
433                 new_buf[string_length] = 0;
434                 break;
435
436         case ACPI_TYPE_BUFFER:
437
438                 /* Setup string length, base, and separator */
439
440                 switch (type) {
441                 case ACPI_EXPLICIT_CONVERT_DECIMAL:     /* Used by to_decimal_string */
442                         /*
443                          * From ACPI: "If Data is a buffer, it is converted to a string of
444                          * decimal values separated by commas."
445                          */
446                         base = 10;
447
448                         /*
449                          * Calculate the final string length. Individual string values
450                          * are variable length (include separator for each)
451                          */
452                         for (i = 0; i < obj_desc->buffer.length; i++) {
453                                 if (obj_desc->buffer.pointer[i] >= 100) {
454                                         string_length += 4;
455                                 } else if (obj_desc->buffer.pointer[i] >= 10) {
456                                         string_length += 3;
457                                 } else {
458                                         string_length += 2;
459                                 }
460                         }
461                         break;
462
463                 case ACPI_IMPLICIT_CONVERT_HEX:
464                         /*
465                          * From the ACPI spec:
466                          *"The entire contents of the buffer are converted to a string of
467                          * two-character hexadecimal numbers, each separated by a space."
468                          */
469                         separator = ' ';
470                         string_length = (obj_desc->buffer.length * 3);
471                         break;
472
473                 case ACPI_EXPLICIT_CONVERT_HEX: /* Used by to_hex_string */
474                         /*
475                          * From ACPI: "If Data is a buffer, it is converted to a string of
476                          * hexadecimal values separated by commas."
477                          */
478                         string_length = (obj_desc->buffer.length * 3);
479                         break;
480
481                 default:
482                         return_ACPI_STATUS(AE_BAD_PARAMETER);
483                 }
484
485                 /*
486                  * Create a new string object and string buffer
487                  * (-1 because of extra separator included in string_length from above)
488                  * Allow creation of zero-length strings from zero-length buffers.
489                  */
490                 if (string_length) {
491                         string_length--;
492                 }
493
494                 return_desc =
495                     acpi_ut_create_string_object((acpi_size)string_length);
496                 if (!return_desc) {
497                         return_ACPI_STATUS(AE_NO_MEMORY);
498                 }
499
500                 new_buf = return_desc->buffer.pointer;
501
502                 /*
503                  * Convert buffer bytes to hex or decimal values
504                  * (separated by commas or spaces)
505                  */
506                 for (i = 0; i < obj_desc->buffer.length; i++) {
507                         new_buf += acpi_ex_convert_to_ascii((u64) obj_desc->
508                                                             buffer.pointer[i],
509                                                             base, new_buf, 1);
510                         *new_buf++ = separator; /* each separated by a comma or space */
511                 }
512
513                 /*
514                  * Null terminate the string
515                  * (overwrites final comma/space from above)
516                  */
517                 if (obj_desc->buffer.length) {
518                         new_buf--;
519                 }
520                 *new_buf = 0;
521                 break;
522
523         default:
524
525                 return_ACPI_STATUS(AE_TYPE);
526         }
527
528         *result_desc = return_desc;
529         return_ACPI_STATUS(AE_OK);
530 }
531
532 /*******************************************************************************
533  *
534  * FUNCTION:    acpi_ex_convert_to_target_type
535  *
536  * PARAMETERS:  destination_type    - Current type of the destination
537  *              source_desc         - Source object to be converted.
538  *              result_desc         - Where the converted object is returned
539  *              walk_state          - Current method state
540  *
541  * RETURN:      Status
542  *
543  * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
544  *
545  ******************************************************************************/
546
547 acpi_status
548 acpi_ex_convert_to_target_type(acpi_object_type destination_type,
549                                union acpi_operand_object *source_desc,
550                                union acpi_operand_object **result_desc,
551                                struct acpi_walk_state *walk_state)
552 {
553         acpi_status status = AE_OK;
554
555         ACPI_FUNCTION_TRACE(ex_convert_to_target_type);
556
557         /* Default behavior */
558
559         *result_desc = source_desc;
560
561         /*
562          * If required by the target,
563          * perform implicit conversion on the source before we store it.
564          */
565         switch (GET_CURRENT_ARG_TYPE(walk_state->op_info->runtime_args)) {
566         case ARGI_SIMPLE_TARGET:
567         case ARGI_FIXED_TARGET:
568         case ARGI_INTEGER_REF:  /* Handles Increment, Decrement cases */
569
570                 switch (destination_type) {
571                 case ACPI_TYPE_LOCAL_REGION_FIELD:
572                         /*
573                          * Named field can always handle conversions
574                          */
575                         break;
576
577                 default:
578
579                         /* No conversion allowed for these types */
580
581                         if (destination_type != source_desc->common.type) {
582                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
583                                                   "Explicit operator, will store (%s) over existing type (%s)\n",
584                                                   acpi_ut_get_object_type_name
585                                                   (source_desc),
586                                                   acpi_ut_get_type_name
587                                                   (destination_type)));
588                                 status = AE_TYPE;
589                         }
590                 }
591                 break;
592
593         case ARGI_TARGETREF:
594         case ARGI_STORE_TARGET:
595
596                 switch (destination_type) {
597                 case ACPI_TYPE_INTEGER:
598                 case ACPI_TYPE_BUFFER_FIELD:
599                 case ACPI_TYPE_LOCAL_BANK_FIELD:
600                 case ACPI_TYPE_LOCAL_INDEX_FIELD:
601                         /*
602                          * These types require an Integer operand. We can convert
603                          * a Buffer or a String to an Integer if necessary.
604                          */
605                         status =
606                             acpi_ex_convert_to_integer(source_desc, result_desc,
607                                                        ACPI_IMPLICIT_CONVERSION);
608                         break;
609
610                 case ACPI_TYPE_STRING:
611                         /*
612                          * The operand must be a String. We can convert an
613                          * Integer or Buffer if necessary
614                          */
615                         status =
616                             acpi_ex_convert_to_string(source_desc, result_desc,
617                                                       ACPI_IMPLICIT_CONVERT_HEX);
618                         break;
619
620                 case ACPI_TYPE_BUFFER:
621                         /*
622                          * The operand must be a Buffer. We can convert an
623                          * Integer or String if necessary
624                          */
625                         status =
626                             acpi_ex_convert_to_buffer(source_desc, result_desc);
627                         break;
628
629                 default:
630
631                         ACPI_ERROR((AE_INFO,
632                                     "Bad destination type during conversion: 0x%X",
633                                     destination_type));
634                         status = AE_AML_INTERNAL;
635                         break;
636                 }
637                 break;
638
639         case ARGI_REFERENCE:
640                 /*
641                  * create_xxxx_field cases - we are storing the field object into the name
642                  */
643                 break;
644
645         default:
646
647                 ACPI_ERROR((AE_INFO,
648                             "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
649                             GET_CURRENT_ARG_TYPE(walk_state->op_info->
650                                                  runtime_args),
651                             walk_state->opcode,
652                             acpi_ut_get_type_name(destination_type)));
653                 status = AE_AML_INTERNAL;
654         }
655
656         /*
657          * Source-to-Target conversion semantics:
658          *
659          * If conversion to the target type cannot be performed, then simply
660          * overwrite the target with the new object and type.
661          */
662         if (status == AE_TYPE) {
663                 status = AE_OK;
664         }
665
666         return_ACPI_STATUS(status);
667 }