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