ACPICA: Update for "orphan" embedded controller _REG method support
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / acpica / nsprepkg.c
1 /******************************************************************************
2  *
3  * Module Name: nsprepkg - Validation of package objects for predefined names
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acpredef.h"
48
49 #define _COMPONENT          ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsprepkg")
51
52 /* Local prototypes */
53 static acpi_status
54 acpi_ns_check_package_list(struct acpi_evaluate_info *info,
55                            const union acpi_predefined_info *package,
56                            union acpi_operand_object **elements, u32 count);
57
58 static acpi_status
59 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
60                                union acpi_operand_object **elements,
61                                u8 type1,
62                                u32 count1,
63                                u8 type2, u32 count2, u32 start_index);
64
65 /*******************************************************************************
66  *
67  * FUNCTION:    acpi_ns_check_package
68  *
69  * PARAMETERS:  info                - Method execution information block
70  *              return_object_ptr   - Pointer to the object returned from the
71  *                                    evaluation of a method or object
72  *
73  * RETURN:      Status
74  *
75  * DESCRIPTION: Check a returned package object for the correct count and
76  *              correct type of all sub-objects.
77  *
78  ******************************************************************************/
79
80 acpi_status
81 acpi_ns_check_package(struct acpi_evaluate_info *info,
82                       union acpi_operand_object **return_object_ptr)
83 {
84         union acpi_operand_object *return_object = *return_object_ptr;
85         const union acpi_predefined_info *package;
86         union acpi_operand_object **elements;
87         acpi_status status = AE_OK;
88         u32 expected_count;
89         u32 count;
90         u32 i;
91
92         ACPI_FUNCTION_NAME(ns_check_package);
93
94         /* The package info for this name is in the next table entry */
95
96         package = info->predefined + 1;
97
98         ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
99                           "%s Validating return Package of Type %X, Count %X\n",
100                           info->full_pathname, package->ret_info.type,
101                           return_object->package.count));
102
103         /*
104          * For variable-length Packages, we can safely remove all embedded
105          * and trailing NULL package elements
106          */
107         acpi_ns_remove_null_elements(info, package->ret_info.type,
108                                      return_object);
109
110         /* Extract package count and elements array */
111
112         elements = return_object->package.elements;
113         count = return_object->package.count;
114
115         /*
116          * Most packages must have at least one element. The only exception
117          * is the variable-length package (ACPI_PTYPE1_VAR).
118          */
119         if (!count) {
120                 if (package->ret_info.type == ACPI_PTYPE1_VAR) {
121                         return (AE_OK);
122                 }
123
124                 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
125                                       info->node_flags,
126                                       "Return Package has no elements (empty)"));
127
128                 return (AE_AML_OPERAND_VALUE);
129         }
130
131         /*
132          * Decode the type of the expected package contents
133          *
134          * PTYPE1 packages contain no subpackages
135          * PTYPE2 packages contain sub-packages
136          */
137         switch (package->ret_info.type) {
138         case ACPI_PTYPE1_FIXED:
139
140                 /*
141                  * The package count is fixed and there are no sub-packages
142                  *
143                  * If package is too small, exit.
144                  * If package is larger than expected, issue warning but continue
145                  */
146                 expected_count =
147                     package->ret_info.count1 + package->ret_info.count2;
148                 if (count < expected_count) {
149                         goto package_too_small;
150                 } else if (count > expected_count) {
151                         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
152                                           "%s: Return Package is larger than needed - "
153                                           "found %u, expected %u\n",
154                                           info->full_pathname, count,
155                                           expected_count));
156                 }
157
158                 /* Validate all elements of the returned package */
159
160                 status = acpi_ns_check_package_elements(info, elements,
161                                                         package->ret_info.
162                                                         object_type1,
163                                                         package->ret_info.
164                                                         count1,
165                                                         package->ret_info.
166                                                         object_type2,
167                                                         package->ret_info.
168                                                         count2, 0);
169                 break;
170
171         case ACPI_PTYPE1_VAR:
172
173                 /*
174                  * The package count is variable, there are no sub-packages, and all
175                  * elements must be of the same type
176                  */
177                 for (i = 0; i < count; i++) {
178                         status = acpi_ns_check_object_type(info, elements,
179                                                            package->ret_info.
180                                                            object_type1, i);
181                         if (ACPI_FAILURE(status)) {
182                                 return (status);
183                         }
184                         elements++;
185                 }
186                 break;
187
188         case ACPI_PTYPE1_OPTION:
189
190                 /*
191                  * The package count is variable, there are no sub-packages. There are
192                  * a fixed number of required elements, and a variable number of
193                  * optional elements.
194                  *
195                  * Check if package is at least as large as the minimum required
196                  */
197                 expected_count = package->ret_info3.count;
198                 if (count < expected_count) {
199                         goto package_too_small;
200                 }
201
202                 /* Variable number of sub-objects */
203
204                 for (i = 0; i < count; i++) {
205                         if (i < package->ret_info3.count) {
206
207                                 /* These are the required package elements (0, 1, or 2) */
208
209                                 status =
210                                     acpi_ns_check_object_type(info, elements,
211                                                               package->
212                                                               ret_info3.
213                                                               object_type[i],
214                                                               i);
215                                 if (ACPI_FAILURE(status)) {
216                                         return (status);
217                                 }
218                         } else {
219                                 /* These are the optional package elements */
220
221                                 status =
222                                     acpi_ns_check_object_type(info, elements,
223                                                               package->
224                                                               ret_info3.
225                                                               tail_object_type,
226                                                               i);
227                                 if (ACPI_FAILURE(status)) {
228                                         return (status);
229                                 }
230                         }
231                         elements++;
232                 }
233                 break;
234
235         case ACPI_PTYPE2_REV_FIXED:
236
237                 /* First element is the (Integer) revision */
238
239                 status = acpi_ns_check_object_type(info, elements,
240                                                    ACPI_RTYPE_INTEGER, 0);
241                 if (ACPI_FAILURE(status)) {
242                         return (status);
243                 }
244
245                 elements++;
246                 count--;
247
248                 /* Examine the sub-packages */
249
250                 status =
251                     acpi_ns_check_package_list(info, package, elements, count);
252                 break;
253
254         case ACPI_PTYPE2_PKG_COUNT:
255
256                 /* First element is the (Integer) count of sub-packages to follow */
257
258                 status = acpi_ns_check_object_type(info, elements,
259                                                    ACPI_RTYPE_INTEGER, 0);
260                 if (ACPI_FAILURE(status)) {
261                         return (status);
262                 }
263
264                 /*
265                  * Count cannot be larger than the parent package length, but allow it
266                  * to be smaller. The >= accounts for the Integer above.
267                  */
268                 expected_count = (u32)(*elements)->integer.value;
269                 if (expected_count >= count) {
270                         goto package_too_small;
271                 }
272
273                 count = expected_count;
274                 elements++;
275
276                 /* Examine the sub-packages */
277
278                 status =
279                     acpi_ns_check_package_list(info, package, elements, count);
280                 break;
281
282         case ACPI_PTYPE2:
283         case ACPI_PTYPE2_FIXED:
284         case ACPI_PTYPE2_MIN:
285         case ACPI_PTYPE2_COUNT:
286         case ACPI_PTYPE2_FIX_VAR:
287
288                 /*
289                  * These types all return a single Package that consists of a
290                  * variable number of sub-Packages.
291                  *
292                  * First, ensure that the first element is a sub-Package. If not,
293                  * the BIOS may have incorrectly returned the object as a single
294                  * package instead of a Package of Packages (a common error if
295                  * there is only one entry). We may be able to repair this by
296                  * wrapping the returned Package with a new outer Package.
297                  */
298                 if (*elements
299                     && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
300
301                         /* Create the new outer package and populate it */
302
303                         status =
304                             acpi_ns_wrap_with_package(info, return_object,
305                                                       return_object_ptr);
306                         if (ACPI_FAILURE(status)) {
307                                 return (status);
308                         }
309
310                         /* Update locals to point to the new package (of 1 element) */
311
312                         return_object = *return_object_ptr;
313                         elements = return_object->package.elements;
314                         count = 1;
315                 }
316
317                 /* Examine the sub-packages */
318
319                 status =
320                     acpi_ns_check_package_list(info, package, elements, count);
321                 break;
322
323         default:
324
325                 /* Should not get here if predefined info table is correct */
326
327                 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
328                                       info->node_flags,
329                                       "Invalid internal return type in table entry: %X",
330                                       package->ret_info.type));
331
332                 return (AE_AML_INTERNAL);
333         }
334
335         return (status);
336
337       package_too_small:
338
339         /* Error exit for the case with an incorrect package count */
340
341         ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
342                               "Return Package is too small - found %u elements, expected %u",
343                               count, expected_count));
344
345         return (AE_AML_OPERAND_VALUE);
346 }
347
348 /*******************************************************************************
349  *
350  * FUNCTION:    acpi_ns_check_package_list
351  *
352  * PARAMETERS:  info            - Method execution information block
353  *              package         - Pointer to package-specific info for method
354  *              elements        - Element list of parent package. All elements
355  *                                of this list should be of type Package.
356  *              count           - Count of subpackages
357  *
358  * RETURN:      Status
359  *
360  * DESCRIPTION: Examine a list of subpackages
361  *
362  ******************************************************************************/
363
364 static acpi_status
365 acpi_ns_check_package_list(struct acpi_evaluate_info *info,
366                            const union acpi_predefined_info *package,
367                            union acpi_operand_object **elements, u32 count)
368 {
369         union acpi_operand_object *sub_package;
370         union acpi_operand_object **sub_elements;
371         acpi_status status;
372         u32 expected_count;
373         u32 i;
374         u32 j;
375
376         /*
377          * Validate each sub-Package in the parent Package
378          *
379          * NOTE: assumes list of sub-packages contains no NULL elements.
380          * Any NULL elements should have been removed by earlier call
381          * to acpi_ns_remove_null_elements.
382          */
383         for (i = 0; i < count; i++) {
384                 sub_package = *elements;
385                 sub_elements = sub_package->package.elements;
386                 info->parent_package = sub_package;
387
388                 /* Each sub-object must be of type Package */
389
390                 status = acpi_ns_check_object_type(info, &sub_package,
391                                                    ACPI_RTYPE_PACKAGE, i);
392                 if (ACPI_FAILURE(status)) {
393                         return (status);
394                 }
395
396                 /* Examine the different types of expected sub-packages */
397
398                 info->parent_package = sub_package;
399                 switch (package->ret_info.type) {
400                 case ACPI_PTYPE2:
401                 case ACPI_PTYPE2_PKG_COUNT:
402                 case ACPI_PTYPE2_REV_FIXED:
403
404                         /* Each subpackage has a fixed number of elements */
405
406                         expected_count =
407                             package->ret_info.count1 + package->ret_info.count2;
408                         if (sub_package->package.count < expected_count) {
409                                 goto package_too_small;
410                         }
411
412                         status =
413                             acpi_ns_check_package_elements(info, sub_elements,
414                                                            package->ret_info.
415                                                            object_type1,
416                                                            package->ret_info.
417                                                            count1,
418                                                            package->ret_info.
419                                                            object_type2,
420                                                            package->ret_info.
421                                                            count2, 0);
422                         if (ACPI_FAILURE(status)) {
423                                 return (status);
424                         }
425                         break;
426
427                 case ACPI_PTYPE2_FIX_VAR:
428                         /*
429                          * Each subpackage has a fixed number of elements and an
430                          * optional element
431                          */
432                         expected_count =
433                             package->ret_info.count1 + package->ret_info.count2;
434                         if (sub_package->package.count < expected_count) {
435                                 goto package_too_small;
436                         }
437
438                         status =
439                             acpi_ns_check_package_elements(info, sub_elements,
440                                                            package->ret_info.
441                                                            object_type1,
442                                                            package->ret_info.
443                                                            count1,
444                                                            package->ret_info.
445                                                            object_type2,
446                                                            sub_package->package.
447                                                            count -
448                                                            package->ret_info.
449                                                            count1, 0);
450                         if (ACPI_FAILURE(status)) {
451                                 return (status);
452                         }
453                         break;
454
455                 case ACPI_PTYPE2_FIXED:
456
457                         /* Each sub-package has a fixed length */
458
459                         expected_count = package->ret_info2.count;
460                         if (sub_package->package.count < expected_count) {
461                                 goto package_too_small;
462                         }
463
464                         /* Check the type of each sub-package element */
465
466                         for (j = 0; j < expected_count; j++) {
467                                 status =
468                                     acpi_ns_check_object_type(info,
469                                                               &sub_elements[j],
470                                                               package->
471                                                               ret_info2.
472                                                               object_type[j],
473                                                               j);
474                                 if (ACPI_FAILURE(status)) {
475                                         return (status);
476                                 }
477                         }
478                         break;
479
480                 case ACPI_PTYPE2_MIN:
481
482                         /* Each sub-package has a variable but minimum length */
483
484                         expected_count = package->ret_info.count1;
485                         if (sub_package->package.count < expected_count) {
486                                 goto package_too_small;
487                         }
488
489                         /* Check the type of each sub-package element */
490
491                         status =
492                             acpi_ns_check_package_elements(info, sub_elements,
493                                                            package->ret_info.
494                                                            object_type1,
495                                                            sub_package->package.
496                                                            count, 0, 0, 0);
497                         if (ACPI_FAILURE(status)) {
498                                 return (status);
499                         }
500                         break;
501
502                 case ACPI_PTYPE2_COUNT:
503
504                         /*
505                          * First element is the (Integer) count of elements, including
506                          * the count field (the ACPI name is num_elements)
507                          */
508                         status = acpi_ns_check_object_type(info, sub_elements,
509                                                            ACPI_RTYPE_INTEGER,
510                                                            0);
511                         if (ACPI_FAILURE(status)) {
512                                 return (status);
513                         }
514
515                         /*
516                          * Make sure package is large enough for the Count and is
517                          * is as large as the minimum size
518                          */
519                         expected_count = (u32)(*sub_elements)->integer.value;
520                         if (sub_package->package.count < expected_count) {
521                                 goto package_too_small;
522                         }
523                         if (sub_package->package.count <
524                             package->ret_info.count1) {
525                                 expected_count = package->ret_info.count1;
526                                 goto package_too_small;
527                         }
528                         if (expected_count == 0) {
529                                 /*
530                                  * Either the num_entries element was originally zero or it was
531                                  * a NULL element and repaired to an Integer of value zero.
532                                  * In either case, repair it by setting num_entries to be the
533                                  * actual size of the subpackage.
534                                  */
535                                 expected_count = sub_package->package.count;
536                                 (*sub_elements)->integer.value = expected_count;
537                         }
538
539                         /* Check the type of each sub-package element */
540
541                         status =
542                             acpi_ns_check_package_elements(info,
543                                                            (sub_elements + 1),
544                                                            package->ret_info.
545                                                            object_type1,
546                                                            (expected_count - 1),
547                                                            0, 0, 1);
548                         if (ACPI_FAILURE(status)) {
549                                 return (status);
550                         }
551                         break;
552
553                 default:        /* Should not get here, type was validated by caller */
554
555                         return (AE_AML_INTERNAL);
556                 }
557
558                 elements++;
559         }
560
561         return (AE_OK);
562
563       package_too_small:
564
565         /* The sub-package count was smaller than required */
566
567         ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
568                               "Return Sub-Package[%u] is too small - found %u elements, expected %u",
569                               i, sub_package->package.count, expected_count));
570
571         return (AE_AML_OPERAND_VALUE);
572 }
573
574 /*******************************************************************************
575  *
576  * FUNCTION:    acpi_ns_check_package_elements
577  *
578  * PARAMETERS:  info            - Method execution information block
579  *              elements        - Pointer to the package elements array
580  *              type1           - Object type for first group
581  *              count1          - Count for first group
582  *              type2           - Object type for second group
583  *              count2          - Count for second group
584  *              start_index     - Start of the first group of elements
585  *
586  * RETURN:      Status
587  *
588  * DESCRIPTION: Check that all elements of a package are of the correct object
589  *              type. Supports up to two groups of different object types.
590  *
591  ******************************************************************************/
592
593 static acpi_status
594 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
595                                union acpi_operand_object **elements,
596                                u8 type1,
597                                u32 count1,
598                                u8 type2, u32 count2, u32 start_index)
599 {
600         union acpi_operand_object **this_element = elements;
601         acpi_status status;
602         u32 i;
603
604         /*
605          * Up to two groups of package elements are supported by the data
606          * structure. All elements in each group must be of the same type.
607          * The second group can have a count of zero.
608          */
609         for (i = 0; i < count1; i++) {
610                 status = acpi_ns_check_object_type(info, this_element,
611                                                    type1, i + start_index);
612                 if (ACPI_FAILURE(status)) {
613                         return (status);
614                 }
615                 this_element++;
616         }
617
618         for (i = 0; i < count2; i++) {
619                 status = acpi_ns_check_object_type(info, this_element,
620                                                    type2,
621                                                    (i + count1 + start_index));
622                 if (ACPI_FAILURE(status)) {
623                         return (status);
624                 }
625                 this_element++;
626         }
627
628         return (AE_OK);
629 }