ACPICA: Fix PCI configuration space port address range
[linux-2.6] / drivers / acpi / acpica / nspredef.c
1 /******************************************************************************
2  *
3  * Module Name: nspredef - Validation of ACPI predefined methods and objects
4  *              $Revision: 1.1 $
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2008, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
48 #include "acpredef.h"
49
50 #define _COMPONENT          ACPI_NAMESPACE
51 ACPI_MODULE_NAME("nspredef")
52
53 /*******************************************************************************
54  *
55  * This module validates predefined ACPI objects that appear in the namespace,
56  * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
57  * validation is to detect problems with BIOS-exposed predefined ACPI objects
58  * before the results are returned to the ACPI-related drivers.
59  *
60  * There are several areas that are validated:
61  *
62  *  1) The number of input arguments as defined by the method/object in the
63  *      ASL is validated against the ACPI specification.
64  *  2) The type of the return object (if any) is validated against the ACPI
65  *      specification.
66  *  3) For returned package objects, the count of package elements is
67  *      validated, as well as the type of each package element. Nested
68  *      packages are supported.
69  *
70  * For any problems found, a warning message is issued.
71  *
72  ******************************************************************************/
73 /* Local prototypes */
74 static acpi_status
75 acpi_ns_check_package(char *pathname,
76                       union acpi_operand_object **return_object_ptr,
77                       const union acpi_predefined_info *predefined);
78
79 static acpi_status
80 acpi_ns_check_package_elements(char *pathname,
81                                union acpi_operand_object **elements,
82                                u8 type1, u32 count1, u8 type2, u32 count2);
83
84 static acpi_status
85 acpi_ns_check_object_type(char *pathname,
86                           union acpi_operand_object **return_object_ptr,
87                           u32 expected_btypes, u32 package_index);
88
89 static acpi_status
90 acpi_ns_check_reference(char *pathname,
91                         union acpi_operand_object *return_object);
92
93 static acpi_status
94 acpi_ns_repair_object(u32 expected_btypes,
95                       u32 package_index,
96                       union acpi_operand_object **return_object_ptr);
97
98 /*
99  * Names for the types that can be returned by the predefined objects.
100  * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
101  */
102 static const char *acpi_rtype_names[] = {
103         "/Integer",
104         "/String",
105         "/Buffer",
106         "/Package",
107         "/Reference",
108 };
109
110 #define ACPI_NOT_PACKAGE    ACPI_UINT32_MAX
111
112 /*******************************************************************************
113  *
114  * FUNCTION:    acpi_ns_check_predefined_names
115  *
116  * PARAMETERS:  Node            - Namespace node for the method/object
117  *              return_object_ptr - Pointer to the object returned from the
118  *                                evaluation of a method or object
119  *
120  * RETURN:      Status
121  *
122  * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
123  *
124  ******************************************************************************/
125
126 acpi_status
127 acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
128                                u32 user_param_count,
129                                acpi_status return_status,
130                                union acpi_operand_object **return_object_ptr)
131 {
132         union acpi_operand_object *return_object = *return_object_ptr;
133         acpi_status status = AE_OK;
134         const union acpi_predefined_info *predefined;
135         char *pathname;
136
137         /* Match the name for this method/object against the predefined list */
138
139         predefined = acpi_ns_check_for_predefined_name(node);
140
141         /* Get the full pathname to the object, for use in error messages */
142
143         pathname = acpi_ns_get_external_pathname(node);
144         if (!pathname) {
145                 pathname = ACPI_CAST_PTR(char, predefined->info.name);
146         }
147
148         /*
149          * Check that the parameter count for this method matches the ASL
150          * definition. For predefined names, ensure that both the caller and
151          * the method itself are in accordance with the ACPI specification.
152          */
153         acpi_ns_check_parameter_count(pathname, node, user_param_count,
154                                       predefined);
155
156         /* If not a predefined name, we cannot validate the return object */
157
158         if (!predefined) {
159                 goto exit;
160         }
161
162         /* If the method failed, we cannot validate the return object */
163
164         if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
165                 goto exit;
166         }
167
168         /*
169          * Only validate the return value on the first successful evaluation of
170          * the method. This ensures that any warnings will only be emitted during
171          * the very first evaluation of the method/object.
172          */
173         if (node->flags & ANOBJ_EVALUATED) {
174                 goto exit;
175         }
176
177         /* Mark the node as having been successfully evaluated */
178
179         node->flags |= ANOBJ_EVALUATED;
180
181         /*
182          * If there is no return value, check if we require a return value for
183          * this predefined name. Either one return value is expected, or none,
184          * for both methods and other objects.
185          *
186          * Exit now if there is no return object. Warning if one was expected.
187          */
188         if (!return_object) {
189                 if ((predefined->info.expected_btypes) &&
190                     (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
191                         ACPI_ERROR((AE_INFO,
192                                     "%s: Missing expected return value",
193                                     pathname));
194
195                         status = AE_AML_NO_RETURN_VALUE;
196                 }
197                 goto exit;
198         }
199
200         /*
201          * We have a return value, but if one wasn't expected, just exit, this is
202          * not a problem
203          *
204          * For example, if the "Implicit Return" feature is enabled, methods will
205          * always return a value
206          */
207         if (!predefined->info.expected_btypes) {
208                 goto exit;
209         }
210
211         /*
212          * Check that the type of the return object is what is expected for
213          * this predefined name
214          */
215         status = acpi_ns_check_object_type(pathname, return_object_ptr,
216                                            predefined->info.expected_btypes,
217                                            ACPI_NOT_PACKAGE);
218         if (ACPI_FAILURE(status)) {
219                 goto exit;
220         }
221
222         /* For returned Package objects, check the type of all sub-objects */
223
224         if (return_object->common.type == ACPI_TYPE_PACKAGE) {
225                 status =
226                     acpi_ns_check_package(pathname, return_object_ptr,
227                                           predefined);
228         }
229
230       exit:
231         if (pathname != predefined->info.name) {
232                 ACPI_FREE(pathname);
233         }
234
235         return (status);
236 }
237
238 /*******************************************************************************
239  *
240  * FUNCTION:    acpi_ns_check_parameter_count
241  *
242  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
243  *              Node            - Namespace node for the method/object
244  *              user_param_count - Number of args passed in by the caller
245  *              Predefined      - Pointer to entry in predefined name table
246  *
247  * RETURN:      None
248  *
249  * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
250  *              predefined name is what is expected (i.e., what is defined in
251  *              the ACPI specification for this predefined name.)
252  *
253  ******************************************************************************/
254
255 void
256 acpi_ns_check_parameter_count(char *pathname,
257                               struct acpi_namespace_node *node,
258                               u32 user_param_count,
259                               const union acpi_predefined_info *predefined)
260 {
261         u32 param_count;
262         u32 required_params_current;
263         u32 required_params_old;
264
265         /* Methods have 0-7 parameters. All other types have zero. */
266
267         param_count = 0;
268         if (node->type == ACPI_TYPE_METHOD) {
269                 param_count = node->object->method.param_count;
270         }
271
272         /* Argument count check for non-predefined methods/objects */
273
274         if (!predefined) {
275                 /*
276                  * Warning if too few or too many arguments have been passed by the
277                  * caller. An incorrect number of arguments may not cause the method
278                  * to fail. However, the method will fail if there are too few
279                  * arguments and the method attempts to use one of the missing ones.
280                  */
281                 if (user_param_count < param_count) {
282                         ACPI_WARNING((AE_INFO,
283                                       "%s: Insufficient arguments - needs %d, found %d",
284                                       pathname, param_count, user_param_count));
285                 } else if (user_param_count > param_count) {
286                         ACPI_WARNING((AE_INFO,
287                                       "%s: Excess arguments - needs %d, found %d",
288                                       pathname, param_count, user_param_count));
289                 }
290                 return;
291         }
292
293         /* Allow two different legal argument counts (_SCP, etc.) */
294
295         required_params_current = predefined->info.param_count & 0x0F;
296         required_params_old = predefined->info.param_count >> 4;
297
298         if (user_param_count != ACPI_UINT32_MAX) {
299
300                 /* Validate the user-supplied parameter count */
301
302                 if ((user_param_count != required_params_current) &&
303                     (user_param_count != required_params_old)) {
304                         ACPI_WARNING((AE_INFO,
305                                       "%s: Parameter count mismatch - "
306                                       "caller passed %d, ACPI requires %d",
307                                       pathname, user_param_count,
308                                       required_params_current));
309                 }
310         }
311
312         /*
313          * Only validate the argument count on the first successful evaluation of
314          * the method. This ensures that any warnings will only be emitted during
315          * the very first evaluation of the method/object.
316          */
317         if (node->flags & ANOBJ_EVALUATED) {
318                 return;
319         }
320
321         /*
322          * Check that the ASL-defined parameter count is what is expected for
323          * this predefined name.
324          */
325         if ((param_count != required_params_current) &&
326             (param_count != required_params_old)) {
327                 ACPI_WARNING((AE_INFO,
328                               "%s: Parameter count mismatch - ASL declared %d, ACPI requires %d",
329                               pathname, param_count, required_params_current));
330         }
331 }
332
333 /*******************************************************************************
334  *
335  * FUNCTION:    acpi_ns_check_for_predefined_name
336  *
337  * PARAMETERS:  Node            - Namespace node for the method/object
338  *
339  * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
340  *
341  * DESCRIPTION: Check an object name against the predefined object list.
342  *
343  ******************************************************************************/
344
345 const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
346                                                                     acpi_namespace_node
347                                                                     *node)
348 {
349         const union acpi_predefined_info *this_name;
350
351         /* Quick check for a predefined name, first character must be underscore */
352
353         if (node->name.ascii[0] != '_') {
354                 return (NULL);
355         }
356
357         /* Search info table for a predefined method/object name */
358
359         this_name = predefined_names;
360         while (this_name->info.name[0]) {
361                 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
362
363                         /* Return pointer to this table entry */
364
365                         return (this_name);
366                 }
367
368                 /*
369                  * Skip next entry in the table if this name returns a Package
370                  * (next entry contains the package info)
371                  */
372                 if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
373                         this_name++;
374                 }
375
376                 this_name++;
377         }
378
379         return (NULL);
380 }
381
382 /*******************************************************************************
383  *
384  * FUNCTION:    acpi_ns_check_package
385  *
386  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
387  *              return_object_ptr - Pointer to the object returned from the
388  *                                evaluation of a method or object
389  *              Predefined      - Pointer to entry in predefined name table
390  *
391  * RETURN:      Status
392  *
393  * DESCRIPTION: Check a returned package object for the correct count and
394  *              correct type of all sub-objects.
395  *
396  ******************************************************************************/
397
398 static acpi_status
399 acpi_ns_check_package(char *pathname,
400                       union acpi_operand_object **return_object_ptr,
401                       const union acpi_predefined_info *predefined)
402 {
403         union acpi_operand_object *return_object = *return_object_ptr;
404         const union acpi_predefined_info *package;
405         union acpi_operand_object *sub_package;
406         union acpi_operand_object **elements;
407         union acpi_operand_object **sub_elements;
408         acpi_status status;
409         u32 expected_count;
410         u32 count;
411         u32 i;
412         u32 j;
413
414         ACPI_FUNCTION_NAME(ns_check_package);
415
416         /* The package info for this name is in the next table entry */
417
418         package = predefined + 1;
419
420         ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
421                           "%s Validating return Package of Type %X, Count %X\n",
422                           pathname, package->ret_info.type,
423                           return_object->package.count));
424
425         /* Extract package count and elements array */
426
427         elements = return_object->package.elements;
428         count = return_object->package.count;
429
430         /* The package must have at least one element, else invalid */
431
432         if (!count) {
433                 ACPI_WARNING((AE_INFO,
434                               "%s: Return Package has no elements (empty)",
435                               pathname));
436
437                 return (AE_AML_OPERAND_VALUE);
438         }
439
440         /*
441          * Decode the type of the expected package contents
442          *
443          * PTYPE1 packages contain no subpackages
444          * PTYPE2 packages contain sub-packages
445          */
446         switch (package->ret_info.type) {
447         case ACPI_PTYPE1_FIXED:
448
449                 /*
450                  * The package count is fixed and there are no sub-packages
451                  *
452                  * If package is too small, exit.
453                  * If package is larger than expected, issue warning but continue
454                  */
455                 expected_count =
456                     package->ret_info.count1 + package->ret_info.count2;
457                 if (count < expected_count) {
458                         goto package_too_small;
459                 } else if (count > expected_count) {
460                         ACPI_WARNING((AE_INFO,
461                                       "%s: Return Package is larger than needed - "
462                                       "found %u, expected %u", pathname, count,
463                                       expected_count));
464                 }
465
466                 /* Validate all elements of the returned package */
467
468                 status = acpi_ns_check_package_elements(pathname, elements,
469                                                         package->ret_info.
470                                                         object_type1,
471                                                         package->ret_info.
472                                                         count1,
473                                                         package->ret_info.
474                                                         object_type2,
475                                                         package->ret_info.
476                                                         count2);
477                 if (ACPI_FAILURE(status)) {
478                         return (status);
479                 }
480                 break;
481
482         case ACPI_PTYPE1_VAR:
483
484                 /*
485                  * The package count is variable, there are no sub-packages, and all
486                  * elements must be of the same type
487                  */
488                 for (i = 0; i < count; i++) {
489                         status = acpi_ns_check_object_type(pathname, elements,
490                                                            package->ret_info.
491                                                            object_type1, i);
492                         if (ACPI_FAILURE(status)) {
493                                 return (status);
494                         }
495                         elements++;
496                 }
497                 break;
498
499         case ACPI_PTYPE1_OPTION:
500
501                 /*
502                  * The package count is variable, there are no sub-packages. There are
503                  * a fixed number of required elements, and a variable number of
504                  * optional elements.
505                  *
506                  * Check if package is at least as large as the minimum required
507                  */
508                 expected_count = package->ret_info3.count;
509                 if (count < expected_count) {
510                         goto package_too_small;
511                 }
512
513                 /* Variable number of sub-objects */
514
515                 for (i = 0; i < count; i++) {
516                         if (i < package->ret_info3.count) {
517
518                                 /* These are the required package elements (0, 1, or 2) */
519
520                                 status =
521                                     acpi_ns_check_object_type(pathname,
522                                                               elements,
523                                                               package->
524                                                               ret_info3.
525                                                               object_type[i],
526                                                               i);
527                                 if (ACPI_FAILURE(status)) {
528                                         return (status);
529                                 }
530                         } else {
531                                 /* These are the optional package elements */
532
533                                 status =
534                                     acpi_ns_check_object_type(pathname,
535                                                               elements,
536                                                               package->
537                                                               ret_info3.
538                                                               tail_object_type,
539                                                               i);
540                                 if (ACPI_FAILURE(status)) {
541                                         return (status);
542                                 }
543                         }
544                         elements++;
545                 }
546                 break;
547
548         case ACPI_PTYPE2_PKG_COUNT:
549
550                 /* First element is the (Integer) count of sub-packages to follow */
551
552                 status = acpi_ns_check_object_type(pathname, elements,
553                                                    ACPI_RTYPE_INTEGER, 0);
554                 if (ACPI_FAILURE(status)) {
555                         return (status);
556                 }
557
558                 /*
559                  * Count cannot be larger than the parent package length, but allow it
560                  * to be smaller. The >= accounts for the Integer above.
561                  */
562                 expected_count = (u32) (*elements)->integer.value;
563                 if (expected_count >= count) {
564                         goto package_too_small;
565                 }
566
567                 count = expected_count;
568                 elements++;
569
570                 /* Now we can walk the sub-packages */
571
572                 /*lint -fallthrough */
573
574         case ACPI_PTYPE2:
575         case ACPI_PTYPE2_FIXED:
576         case ACPI_PTYPE2_MIN:
577         case ACPI_PTYPE2_COUNT:
578
579                 /*
580                  * These types all return a single package that consists of a variable
581                  * number of sub-packages
582                  */
583                 for (i = 0; i < count; i++) {
584                         sub_package = *elements;
585                         sub_elements = sub_package->package.elements;
586
587                         /* Each sub-object must be of type Package */
588
589                         status =
590                             acpi_ns_check_object_type(pathname, &sub_package,
591                                                       ACPI_RTYPE_PACKAGE, i);
592                         if (ACPI_FAILURE(status)) {
593                                 return (status);
594                         }
595
596                         /* Examine the different types of sub-packages */
597
598                         switch (package->ret_info.type) {
599                         case ACPI_PTYPE2:
600                         case ACPI_PTYPE2_PKG_COUNT:
601
602                                 /* Each subpackage has a fixed number of elements */
603
604                                 expected_count =
605                                     package->ret_info.count1 +
606                                     package->ret_info.count2;
607                                 if (sub_package->package.count !=
608                                     expected_count) {
609                                         count = sub_package->package.count;
610                                         goto package_too_small;
611                                 }
612
613                                 status =
614                                     acpi_ns_check_package_elements(pathname,
615                                                                    sub_elements,
616                                                                    package->
617                                                                    ret_info.
618                                                                    object_type1,
619                                                                    package->
620                                                                    ret_info.
621                                                                    count1,
622                                                                    package->
623                                                                    ret_info.
624                                                                    object_type2,
625                                                                    package->
626                                                                    ret_info.
627                                                                    count2);
628                                 if (ACPI_FAILURE(status)) {
629                                         return (status);
630                                 }
631                                 break;
632
633                         case ACPI_PTYPE2_FIXED:
634
635                                 /* Each sub-package has a fixed length */
636
637                                 expected_count = package->ret_info2.count;
638                                 if (sub_package->package.count < expected_count) {
639                                         count = sub_package->package.count;
640                                         goto package_too_small;
641                                 }
642
643                                 /* Check the type of each sub-package element */
644
645                                 for (j = 0; j < expected_count; j++) {
646                                         status =
647                                             acpi_ns_check_object_type(pathname,
648                                                 &sub_elements[j],
649                                                 package->ret_info2.object_type[j], j);
650                                         if (ACPI_FAILURE(status)) {
651                                                 return (status);
652                                         }
653                                 }
654                                 break;
655
656                         case ACPI_PTYPE2_MIN:
657
658                                 /* Each sub-package has a variable but minimum length */
659
660                                 expected_count = package->ret_info.count1;
661                                 if (sub_package->package.count < expected_count) {
662                                         count = sub_package->package.count;
663                                         goto package_too_small;
664                                 }
665
666                                 /* Check the type of each sub-package element */
667
668                                 status =
669                                     acpi_ns_check_package_elements(pathname,
670                                                                    sub_elements,
671                                                                    package->
672                                                                    ret_info.
673                                                                    object_type1,
674                                                                    sub_package->
675                                                                    package.
676                                                                    count, 0, 0);
677                                 if (ACPI_FAILURE(status)) {
678                                         return (status);
679                                 }
680                                 break;
681
682                         case ACPI_PTYPE2_COUNT:
683
684                                 /* First element is the (Integer) count of elements to follow */
685
686                                 status =
687                                     acpi_ns_check_object_type(pathname,
688                                                               sub_elements,
689                                                               ACPI_RTYPE_INTEGER,
690                                                               0);
691                                 if (ACPI_FAILURE(status)) {
692                                         return (status);
693                                 }
694
695                                 /* Make sure package is large enough for the Count */
696
697                                 expected_count =
698                                     (u32) (*sub_elements)->integer.value;
699                                 if (sub_package->package.count < expected_count) {
700                                         count = sub_package->package.count;
701                                         goto package_too_small;
702                                 }
703
704                                 /* Check the type of each sub-package element */
705
706                                 status =
707                                     acpi_ns_check_package_elements(pathname,
708                                                                    (sub_elements
709                                                                     + 1),
710                                                                    package->
711                                                                    ret_info.
712                                                                    object_type1,
713                                                                    (expected_count
714                                                                     - 1), 0, 0);
715                                 if (ACPI_FAILURE(status)) {
716                                         return (status);
717                                 }
718                                 break;
719
720                         default:
721                                 break;
722                         }
723
724                         elements++;
725                 }
726                 break;
727
728         default:
729
730                 /* Should not get here if predefined info table is correct */
731
732                 ACPI_WARNING((AE_INFO,
733                               "%s: Invalid internal return type in table entry: %X",
734                               pathname, package->ret_info.type));
735
736                 return (AE_AML_INTERNAL);
737         }
738
739         return (AE_OK);
740
741       package_too_small:
742
743         /* Error exit for the case with an incorrect package count */
744
745         ACPI_WARNING((AE_INFO, "%s: Return Package is too small - "
746                       "found %u, expected %u", pathname, count,
747                       expected_count));
748
749         return (AE_AML_OPERAND_VALUE);
750 }
751
752 /*******************************************************************************
753  *
754  * FUNCTION:    acpi_ns_check_package_elements
755  *
756  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
757  *              Elements        - Pointer to the package elements array
758  *              Type1           - Object type for first group
759  *              Count1          - Count for first group
760  *              Type2           - Object type for second group
761  *              Count2          - Count for second group
762  *
763  * RETURN:      Status
764  *
765  * DESCRIPTION: Check that all elements of a package are of the correct object
766  *              type. Supports up to two groups of different object types.
767  *
768  ******************************************************************************/
769
770 static acpi_status
771 acpi_ns_check_package_elements(char *pathname,
772                                union acpi_operand_object **elements,
773                                u8 type1, u32 count1, u8 type2, u32 count2)
774 {
775         union acpi_operand_object **this_element = elements;
776         acpi_status status;
777         u32 i;
778
779         /*
780          * Up to two groups of package elements are supported by the data
781          * structure. All elements in each group must be of the same type.
782          * The second group can have a count of zero.
783          */
784         for (i = 0; i < count1; i++) {
785                 status = acpi_ns_check_object_type(pathname, this_element,
786                                                    type1, i);
787                 if (ACPI_FAILURE(status)) {
788                         return (status);
789                 }
790                 this_element++;
791         }
792
793         for (i = 0; i < count2; i++) {
794                 status = acpi_ns_check_object_type(pathname, this_element,
795                                                    type2, (i + count1));
796                 if (ACPI_FAILURE(status)) {
797                         return (status);
798                 }
799                 this_element++;
800         }
801
802         return (AE_OK);
803 }
804
805 /*******************************************************************************
806  *
807  * FUNCTION:    acpi_ns_check_object_type
808  *
809  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
810  *              return_object_ptr - Pointer to the object returned from the
811  *                                evaluation of a method or object
812  *              expected_btypes - Bitmap of expected return type(s)
813  *              package_index   - Index of object within parent package (if
814  *                                applicable - ACPI_NOT_PACKAGE otherwise)
815  *
816  * RETURN:      Status
817  *
818  * DESCRIPTION: Check the type of the return object against the expected object
819  *              type(s). Use of Btype allows multiple expected object types.
820  *
821  ******************************************************************************/
822
823 static acpi_status
824 acpi_ns_check_object_type(char *pathname,
825                           union acpi_operand_object **return_object_ptr,
826                           u32 expected_btypes, u32 package_index)
827 {
828         union acpi_operand_object *return_object = *return_object_ptr;
829         acpi_status status = AE_OK;
830         u32 return_btype;
831         char type_buffer[48];   /* Room for 5 types */
832         u32 this_rtype;
833         u32 i;
834         u32 j;
835
836         /*
837          * If we get a NULL return_object here, it is a NULL package element,
838          * and this is always an error.
839          */
840         if (!return_object) {
841                 goto type_error_exit;
842         }
843
844         /* A Namespace node should not get here, but make sure */
845
846         if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
847                 ACPI_WARNING((AE_INFO,
848                               "%s: Invalid return type - Found a Namespace node [%4.4s] type %s",
849                               pathname, return_object->node.name.ascii,
850                               acpi_ut_get_type_name(return_object->node.type)));
851                 return (AE_AML_OPERAND_TYPE);
852         }
853
854         /*
855          * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
856          * The bitmapped type allows multiple possible return types.
857          *
858          * Note, the cases below must handle all of the possible types returned
859          * from all of the predefined names (including elements of returned
860          * packages)
861          */
862         switch (return_object->common.type) {
863         case ACPI_TYPE_INTEGER:
864                 return_btype = ACPI_RTYPE_INTEGER;
865                 break;
866
867         case ACPI_TYPE_BUFFER:
868                 return_btype = ACPI_RTYPE_BUFFER;
869                 break;
870
871         case ACPI_TYPE_STRING:
872                 return_btype = ACPI_RTYPE_STRING;
873                 break;
874
875         case ACPI_TYPE_PACKAGE:
876                 return_btype = ACPI_RTYPE_PACKAGE;
877                 break;
878
879         case ACPI_TYPE_LOCAL_REFERENCE:
880                 return_btype = ACPI_RTYPE_REFERENCE;
881                 break;
882
883         default:
884                 /* Not one of the supported objects, must be incorrect */
885
886                 goto type_error_exit;
887         }
888
889         /* Is the object one of the expected types? */
890
891         if (!(return_btype & expected_btypes)) {
892
893                 /* Type mismatch -- attempt repair of the returned object */
894
895                 status = acpi_ns_repair_object(expected_btypes, package_index,
896                                                return_object_ptr);
897                 if (ACPI_SUCCESS(status)) {
898                         return (status);
899                 }
900                 goto type_error_exit;
901         }
902
903         /* For reference objects, check that the reference type is correct */
904
905         if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
906                 status = acpi_ns_check_reference(pathname, return_object);
907         }
908
909         return (status);
910
911       type_error_exit:
912
913         /* Create a string with all expected types for this predefined object */
914
915         j = 1;
916         type_buffer[0] = 0;
917         this_rtype = ACPI_RTYPE_INTEGER;
918
919         for (i = 0; i < ACPI_NUM_RTYPES; i++) {
920
921                 /* If one of the expected types, concatenate the name of this type */
922
923                 if (expected_btypes & this_rtype) {
924                         ACPI_STRCAT(type_buffer, &acpi_rtype_names[i][j]);
925                         j = 0;  /* Use name separator from now on */
926                 }
927                 this_rtype <<= 1;       /* Next Rtype */
928         }
929
930         if (package_index == ACPI_NOT_PACKAGE) {
931                 ACPI_WARNING((AE_INFO,
932                               "%s: Return type mismatch - found %s, expected %s",
933                               pathname,
934                               acpi_ut_get_object_type_name(return_object),
935                               type_buffer));
936         } else {
937                 ACPI_WARNING((AE_INFO,
938                               "%s: Return Package type mismatch at index %u - "
939                               "found %s, expected %s", pathname, package_index,
940                               acpi_ut_get_object_type_name(return_object),
941                               type_buffer));
942         }
943
944         return (AE_AML_OPERAND_TYPE);
945 }
946
947 /*******************************************************************************
948  *
949  * FUNCTION:    acpi_ns_check_reference
950  *
951  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
952  *              return_object   - Object returned from the evaluation of a
953  *                                method or object
954  *
955  * RETURN:      Status
956  *
957  * DESCRIPTION: Check a returned reference object for the correct reference
958  *              type. The only reference type that can be returned from a
959  *              predefined method is a named reference. All others are invalid.
960  *
961  ******************************************************************************/
962
963 static acpi_status
964 acpi_ns_check_reference(char *pathname,
965                         union acpi_operand_object *return_object)
966 {
967
968         /*
969          * Check the reference object for the correct reference type (opcode).
970          * The only type of reference that can be converted to an union acpi_object is
971          * a reference to a named object (reference class: NAME)
972          */
973         if (return_object->reference.class == ACPI_REFCLASS_NAME) {
974                 return (AE_OK);
975         }
976
977         ACPI_WARNING((AE_INFO,
978                       "%s: Return type mismatch - "
979                       "unexpected reference object type [%s] %2.2X",
980                       pathname, acpi_ut_get_reference_name(return_object),
981                       return_object->reference.class));
982
983         return (AE_AML_OPERAND_TYPE);
984 }
985
986 /*******************************************************************************
987  *
988  * FUNCTION:    acpi_ns_repair_object
989  *
990  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
991  *              package_index   - Used to determine if target is in a package
992  *              return_object_ptr - Pointer to the object returned from the
993  *                                evaluation of a method or object
994  *
995  * RETURN:      Status. AE_OK if repair was successful.
996  *
997  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
998  *              not expected.
999  *
1000  ******************************************************************************/
1001
1002 static acpi_status
1003 acpi_ns_repair_object(u32 expected_btypes,
1004                       u32 package_index,
1005                       union acpi_operand_object **return_object_ptr)
1006 {
1007         union acpi_operand_object *return_object = *return_object_ptr;
1008         union acpi_operand_object *new_object;
1009         acpi_size length;
1010
1011         switch (return_object->common.type) {
1012         case ACPI_TYPE_BUFFER:
1013
1014                 if (!(expected_btypes & ACPI_RTYPE_STRING)) {
1015                         return (AE_AML_OPERAND_TYPE);
1016                 }
1017
1018                 /*
1019                  * Have a Buffer, expected a String, convert. Use a to_string
1020                  * conversion, no transform performed on the buffer data. The best
1021                  * example of this is the _BIF method, where the string data from
1022                  * the battery is often (incorrectly) returned as buffer object(s).
1023                  */
1024                 length = 0;
1025                 while ((length < return_object->buffer.length) &&
1026                        (return_object->buffer.pointer[length])) {
1027                         length++;
1028                 }
1029
1030                 /* Allocate a new string object */
1031
1032                 new_object = acpi_ut_create_string_object(length);
1033                 if (!new_object) {
1034                         return (AE_NO_MEMORY);
1035                 }
1036
1037                 /*
1038                  * Copy the raw buffer data with no transform. String is already NULL
1039                  * terminated at Length+1.
1040                  */
1041                 ACPI_MEMCPY(new_object->string.pointer,
1042                             return_object->buffer.pointer, length);
1043
1044                 /* Install the new return object */
1045
1046                 acpi_ut_remove_reference(return_object);
1047                 *return_object_ptr = new_object;
1048
1049                 /*
1050                  * If the object is a package element, we need to:
1051                  * 1. Decrement the reference count of the orignal object, it was
1052                  *    incremented when building the package
1053                  * 2. Increment the reference count of the new object, it will be
1054                  *    decremented when releasing the package
1055                  */
1056                 if (package_index != ACPI_NOT_PACKAGE) {
1057                         acpi_ut_remove_reference(return_object);
1058                         acpi_ut_add_reference(new_object);
1059                 }
1060                 return (AE_OK);
1061
1062         default:
1063                 break;
1064         }
1065
1066         return (AE_AML_OPERAND_TYPE);
1067 }