ACPICA: Cleanup some invocation indentations, no functional change
[linux-2.6-block.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 - 2016, 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 subpackages
136          */
137         switch (package->ret_info.type) {
138         case ACPI_PTYPE1_FIXED:
139                 /*
140                  * The package count is fixed and there are no subpackages
141                  *
142                  * If package is too small, exit.
143                  * If package is larger than expected, issue warning but continue
144                  */
145                 expected_count =
146                     package->ret_info.count1 + package->ret_info.count2;
147                 if (count < expected_count) {
148                         goto package_too_small;
149                 } else if (count > expected_count) {
150                         ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
151                                           "%s: Return Package is larger than needed - "
152                                           "found %u, expected %u\n",
153                                           info->full_pathname, count,
154                                           expected_count));
155                 }
156
157                 /* Validate all elements of the returned package */
158
159                 status = acpi_ns_check_package_elements(info, elements,
160                                                         package->ret_info.
161                                                         object_type1,
162                                                         package->ret_info.
163                                                         count1,
164                                                         package->ret_info.
165                                                         object_type2,
166                                                         package->ret_info.
167                                                         count2, 0);
168                 break;
169
170         case ACPI_PTYPE1_VAR:
171                 /*
172                  * The package count is variable, there are no subpackages, and all
173                  * elements must be of the same type
174                  */
175                 for (i = 0; i < count; i++) {
176                         status = acpi_ns_check_object_type(info, elements,
177                                                            package->ret_info.
178                                                            object_type1, i);
179                         if (ACPI_FAILURE(status)) {
180                                 return (status);
181                         }
182
183                         elements++;
184                 }
185                 break;
186
187         case ACPI_PTYPE1_OPTION:
188                 /*
189                  * The package count is variable, there are no subpackages. There are
190                  * a fixed number of required elements, and a variable number of
191                  * optional elements.
192                  *
193                  * Check if package is at least as large as the minimum required
194                  */
195                 expected_count = package->ret_info3.count;
196                 if (count < expected_count) {
197                         goto package_too_small;
198                 }
199
200                 /* Variable number of sub-objects */
201
202                 for (i = 0; i < count; i++) {
203                         if (i < package->ret_info3.count) {
204
205                                 /* These are the required package elements (0, 1, or 2) */
206
207                                 status =
208                                     acpi_ns_check_object_type(info, elements,
209                                                               package->
210                                                               ret_info3.
211                                                               object_type[i],
212                                                               i);
213                                 if (ACPI_FAILURE(status)) {
214                                         return (status);
215                                 }
216                         } else {
217                                 /* These are the optional package elements */
218
219                                 status =
220                                     acpi_ns_check_object_type(info, elements,
221                                                               package->
222                                                               ret_info3.
223                                                               tail_object_type,
224                                                               i);
225                                 if (ACPI_FAILURE(status)) {
226                                         return (status);
227                                 }
228                         }
229
230                         elements++;
231                 }
232                 break;
233
234         case ACPI_PTYPE2_REV_FIXED:
235
236                 /* First element is the (Integer) revision */
237
238                 status =
239                     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 subpackages */
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 subpackages to follow */
257
258                 status =
259                     acpi_ns_check_object_type(info, elements,
260                                               ACPI_RTYPE_INTEGER, 0);
261                 if (ACPI_FAILURE(status)) {
262                         return (status);
263                 }
264
265                 /*
266                  * Count cannot be larger than the parent package length, but allow it
267                  * to be smaller. The >= accounts for the Integer above.
268                  */
269                 expected_count = (u32)(*elements)->integer.value;
270                 if (expected_count >= count) {
271                         goto package_too_small;
272                 }
273
274                 count = expected_count;
275                 elements++;
276
277                 /* Examine the subpackages */
278
279                 status =
280                     acpi_ns_check_package_list(info, package, elements, count);
281                 break;
282
283         case ACPI_PTYPE2:
284         case ACPI_PTYPE2_FIXED:
285         case ACPI_PTYPE2_MIN:
286         case ACPI_PTYPE2_COUNT:
287         case ACPI_PTYPE2_FIX_VAR:
288                 /*
289                  * These types all return a single Package that consists of a
290                  * variable number of subpackages.
291                  *
292                  * First, ensure that the first element is a subpackage. 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 subpackages */
318
319                 status =
320                     acpi_ns_check_package_list(info, package, elements, count);
321                 break;
322
323         case ACPI_PTYPE2_VAR_VAR:
324                 /*
325                  * Returns a variable list of packages, each with a variable list
326                  * of objects.
327                  */
328                 break;
329
330         case ACPI_PTYPE2_UUID_PAIR:
331
332                 /* The package must contain pairs of (UUID + type) */
333
334                 if (count & 1) {
335                         expected_count = count + 1;
336                         goto package_too_small;
337                 }
338
339                 while (count > 0) {
340                         status = acpi_ns_check_object_type(info, elements,
341                                                            package->ret_info.
342                                                            object_type1, 0);
343                         if (ACPI_FAILURE(status)) {
344                                 return (status);
345                         }
346
347                         /* Validate length of the UUID buffer */
348
349                         if ((*elements)->buffer.length != 16) {
350                                 ACPI_WARN_PREDEFINED((AE_INFO,
351                                                       info->full_pathname,
352                                                       info->node_flags,
353                                                       "Invalid length for UUID Buffer"));
354                                 return (AE_AML_OPERAND_VALUE);
355                         }
356
357                         status = acpi_ns_check_object_type(info, elements + 1,
358                                                            package->ret_info.
359                                                            object_type2, 0);
360                         if (ACPI_FAILURE(status)) {
361                                 return (status);
362                         }
363
364                         elements += 2;
365                         count -= 2;
366                 }
367                 break;
368
369         default:
370
371                 /* Should not get here if predefined info table is correct */
372
373                 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
374                                       info->node_flags,
375                                       "Invalid internal return type in table entry: %X",
376                                       package->ret_info.type));
377
378                 return (AE_AML_INTERNAL);
379         }
380
381         return (status);
382
383 package_too_small:
384
385         /* Error exit for the case with an incorrect package count */
386
387         ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
388                               "Return Package is too small - found %u elements, expected %u",
389                               count, expected_count));
390
391         return (AE_AML_OPERAND_VALUE);
392 }
393
394 /*******************************************************************************
395  *
396  * FUNCTION:    acpi_ns_check_package_list
397  *
398  * PARAMETERS:  info            - Method execution information block
399  *              package         - Pointer to package-specific info for method
400  *              elements        - Element list of parent package. All elements
401  *                                of this list should be of type Package.
402  *              count           - Count of subpackages
403  *
404  * RETURN:      Status
405  *
406  * DESCRIPTION: Examine a list of subpackages
407  *
408  ******************************************************************************/
409
410 static acpi_status
411 acpi_ns_check_package_list(struct acpi_evaluate_info *info,
412                            const union acpi_predefined_info *package,
413                            union acpi_operand_object **elements, u32 count)
414 {
415         union acpi_operand_object *sub_package;
416         union acpi_operand_object **sub_elements;
417         acpi_status status;
418         u32 expected_count;
419         u32 i;
420         u32 j;
421
422         /*
423          * Validate each subpackage in the parent Package
424          *
425          * NOTE: assumes list of subpackages contains no NULL elements.
426          * Any NULL elements should have been removed by earlier call
427          * to acpi_ns_remove_null_elements.
428          */
429         for (i = 0; i < count; i++) {
430                 sub_package = *elements;
431                 sub_elements = sub_package->package.elements;
432                 info->parent_package = sub_package;
433
434                 /* Each sub-object must be of type Package */
435
436                 status = acpi_ns_check_object_type(info, &sub_package,
437                                                    ACPI_RTYPE_PACKAGE, i);
438                 if (ACPI_FAILURE(status)) {
439                         return (status);
440                 }
441
442                 /* Examine the different types of expected subpackages */
443
444                 info->parent_package = sub_package;
445                 switch (package->ret_info.type) {
446                 case ACPI_PTYPE2:
447                 case ACPI_PTYPE2_PKG_COUNT:
448                 case ACPI_PTYPE2_REV_FIXED:
449
450                         /* Each subpackage has a fixed number of elements */
451
452                         expected_count =
453                             package->ret_info.count1 + package->ret_info.count2;
454                         if (sub_package->package.count < expected_count) {
455                                 goto package_too_small;
456                         }
457
458                         status =
459                             acpi_ns_check_package_elements(info, sub_elements,
460                                                            package->ret_info.
461                                                            object_type1,
462                                                            package->ret_info.
463                                                            count1,
464                                                            package->ret_info.
465                                                            object_type2,
466                                                            package->ret_info.
467                                                            count2, 0);
468                         if (ACPI_FAILURE(status)) {
469                                 return (status);
470                         }
471                         break;
472
473                 case ACPI_PTYPE2_FIX_VAR:
474                         /*
475                          * Each subpackage has a fixed number of elements and an
476                          * optional element
477                          */
478                         expected_count =
479                             package->ret_info.count1 + package->ret_info.count2;
480                         if (sub_package->package.count < expected_count) {
481                                 goto package_too_small;
482                         }
483
484                         status =
485                             acpi_ns_check_package_elements(info, sub_elements,
486                                                            package->ret_info.
487                                                            object_type1,
488                                                            package->ret_info.
489                                                            count1,
490                                                            package->ret_info.
491                                                            object_type2,
492                                                            sub_package->package.
493                                                            count -
494                                                            package->ret_info.
495                                                            count1, 0);
496                         if (ACPI_FAILURE(status)) {
497                                 return (status);
498                         }
499                         break;
500
501                 case ACPI_PTYPE2_VAR_VAR:
502                         /*
503                          * Each subpackage has a fixed or variable number of elements
504                          */
505                         break;
506
507                 case ACPI_PTYPE2_FIXED:
508
509                         /* Each subpackage has a fixed length */
510
511                         expected_count = package->ret_info2.count;
512                         if (sub_package->package.count < expected_count) {
513                                 goto package_too_small;
514                         }
515
516                         /* Check the type of each subpackage element */
517
518                         for (j = 0; j < expected_count; j++) {
519                                 status =
520                                     acpi_ns_check_object_type(info,
521                                                               &sub_elements[j],
522                                                               package->
523                                                               ret_info2.
524                                                               object_type[j],
525                                                               j);
526                                 if (ACPI_FAILURE(status)) {
527                                         return (status);
528                                 }
529                         }
530                         break;
531
532                 case ACPI_PTYPE2_MIN:
533
534                         /* Each subpackage has a variable but minimum length */
535
536                         expected_count = package->ret_info.count1;
537                         if (sub_package->package.count < expected_count) {
538                                 goto package_too_small;
539                         }
540
541                         /* Check the type of each subpackage element */
542
543                         status =
544                             acpi_ns_check_package_elements(info, sub_elements,
545                                                            package->ret_info.
546                                                            object_type1,
547                                                            sub_package->package.
548                                                            count, 0, 0, 0);
549                         if (ACPI_FAILURE(status)) {
550                                 return (status);
551                         }
552                         break;
553
554                 case ACPI_PTYPE2_COUNT:
555                         /*
556                          * First element is the (Integer) count of elements, including
557                          * the count field (the ACPI name is num_elements)
558                          */
559                         status = acpi_ns_check_object_type(info, sub_elements,
560                                                            ACPI_RTYPE_INTEGER,
561                                                            0);
562                         if (ACPI_FAILURE(status)) {
563                                 return (status);
564                         }
565
566                         /*
567                          * Make sure package is large enough for the Count and is
568                          * is as large as the minimum size
569                          */
570                         expected_count = (u32)(*sub_elements)->integer.value;
571                         if (sub_package->package.count < expected_count) {
572                                 goto package_too_small;
573                         }
574
575                         if (sub_package->package.count <
576                             package->ret_info.count1) {
577                                 expected_count = package->ret_info.count1;
578                                 goto package_too_small;
579                         }
580
581                         if (expected_count == 0) {
582                                 /*
583                                  * Either the num_entries element was originally zero or it was
584                                  * a NULL element and repaired to an Integer of value zero.
585                                  * In either case, repair it by setting num_entries to be the
586                                  * actual size of the subpackage.
587                                  */
588                                 expected_count = sub_package->package.count;
589                                 (*sub_elements)->integer.value = expected_count;
590                         }
591
592                         /* Check the type of each subpackage element */
593
594                         status =
595                             acpi_ns_check_package_elements(info,
596                                                            (sub_elements + 1),
597                                                            package->ret_info.
598                                                            object_type1,
599                                                            (expected_count - 1),
600                                                            0, 0, 1);
601                         if (ACPI_FAILURE(status)) {
602                                 return (status);
603                         }
604                         break;
605
606                 default:        /* Should not get here, type was validated by caller */
607
608                         return (AE_AML_INTERNAL);
609                 }
610
611                 elements++;
612         }
613
614         return (AE_OK);
615
616 package_too_small:
617
618         /* The subpackage count was smaller than required */
619
620         ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
621                               "Return SubPackage[%u] is too small - found %u elements, expected %u",
622                               i, sub_package->package.count, expected_count));
623
624         return (AE_AML_OPERAND_VALUE);
625 }
626
627 /*******************************************************************************
628  *
629  * FUNCTION:    acpi_ns_check_package_elements
630  *
631  * PARAMETERS:  info            - Method execution information block
632  *              elements        - Pointer to the package elements array
633  *              type1           - Object type for first group
634  *              count1          - Count for first group
635  *              type2           - Object type for second group
636  *              count2          - Count for second group
637  *              start_index     - Start of the first group of elements
638  *
639  * RETURN:      Status
640  *
641  * DESCRIPTION: Check that all elements of a package are of the correct object
642  *              type. Supports up to two groups of different object types.
643  *
644  ******************************************************************************/
645
646 static acpi_status
647 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
648                                union acpi_operand_object **elements,
649                                u8 type1,
650                                u32 count1,
651                                u8 type2, u32 count2, u32 start_index)
652 {
653         union acpi_operand_object **this_element = elements;
654         acpi_status status;
655         u32 i;
656
657         /*
658          * Up to two groups of package elements are supported by the data
659          * structure. All elements in each group must be of the same type.
660          * The second group can have a count of zero.
661          */
662         for (i = 0; i < count1; i++) {
663                 status = acpi_ns_check_object_type(info, this_element,
664                                                    type1, i + start_index);
665                 if (ACPI_FAILURE(status)) {
666                         return (status);
667                 }
668
669                 this_element++;
670         }
671
672         for (i = 0; i < count2; i++) {
673                 status = acpi_ns_check_object_type(info, this_element,
674                                                    type2,
675                                                    (i + count1 + start_index));
676                 if (ACPI_FAILURE(status)) {
677                         return (status);
678                 }
679
680                 this_element++;
681         }
682
683         return (AE_OK);
684 }