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