Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / contrib / dev / acpica-unix / namespace / nspredef.c
1 /******************************************************************************
2  *
3  * Module Name: nspredef - Validation of ACPI predefined methods and objects
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights.  You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code.  No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision.  In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change.  Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee.  Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution.  In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government.  In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115
116 #define ACPI_CREATE_PREDEFINED_TABLE
117
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "acnamesp.h"
121 #include "acpredef.h"
122
123
124 #define _COMPONENT          ACPI_NAMESPACE
125         ACPI_MODULE_NAME    ("nspredef")
126
127
128 /*******************************************************************************
129  *
130  * This module validates predefined ACPI objects that appear in the namespace,
131  * at the time they are evaluated (via AcpiEvaluateObject). The purpose of this
132  * validation is to detect problems with BIOS-exposed predefined ACPI objects
133  * before the results are returned to the ACPI-related drivers.
134  *
135  * There are several areas that are validated:
136  *
137  *  1) The number of input arguments as defined by the method/object in the
138  *      ASL is validated against the ACPI specification.
139  *  2) The type of the return object (if any) is validated against the ACPI
140  *      specification.
141  *  3) For returned package objects, the count of package elements is
142  *      validated, as well as the type of each package element. Nested
143  *      packages are supported.
144  *
145  * For any problems found, a warning message is issued.
146  *
147  ******************************************************************************/
148
149
150 /* Local prototypes */
151
152 static ACPI_STATUS
153 AcpiNsCheckPackage (
154     ACPI_PREDEFINED_DATA        *Data,
155     ACPI_OPERAND_OBJECT         **ReturnObjectPtr);
156
157 static ACPI_STATUS
158 AcpiNsCheckPackageList (
159     ACPI_PREDEFINED_DATA        *Data,
160     const ACPI_PREDEFINED_INFO  *Package,
161     ACPI_OPERAND_OBJECT         **Elements,
162     UINT32                      Count);
163
164 static ACPI_STATUS
165 AcpiNsCheckPackageElements (
166     ACPI_PREDEFINED_DATA        *Data,
167     ACPI_OPERAND_OBJECT         **Elements,
168     UINT8                       Type1,
169     UINT32                      Count1,
170     UINT8                       Type2,
171     UINT32                      Count2,
172     UINT32                      StartIndex);
173
174 static ACPI_STATUS
175 AcpiNsCheckObjectType (
176     ACPI_PREDEFINED_DATA        *Data,
177     ACPI_OPERAND_OBJECT         **ReturnObjectPtr,
178     UINT32                      ExpectedBtypes,
179     UINT32                      PackageIndex);
180
181 static ACPI_STATUS
182 AcpiNsCheckReference (
183     ACPI_PREDEFINED_DATA        *Data,
184     ACPI_OPERAND_OBJECT         *ReturnObject);
185
186 static void
187 AcpiNsGetExpectedTypes (
188     char                        *Buffer,
189     UINT32                      ExpectedBtypes);
190
191 /*
192  * Names for the types that can be returned by the predefined objects.
193  * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
194  */
195 static const char   *AcpiRtypeNames[] =
196 {
197     "/Integer",
198     "/String",
199     "/Buffer",
200     "/Package",
201     "/Reference",
202 };
203
204
205 /*******************************************************************************
206  *
207  * FUNCTION:    AcpiNsCheckPredefinedNames
208  *
209  * PARAMETERS:  Node            - Namespace node for the method/object
210  *              UserParamCount  - Number of parameters actually passed
211  *              ReturnStatus    - Status from the object evaluation
212  *              ReturnObjectPtr - Pointer to the object returned from the
213  *                                evaluation of a method or object
214  *
215  * RETURN:      Status
216  *
217  * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
218  *
219  ******************************************************************************/
220
221 ACPI_STATUS
222 AcpiNsCheckPredefinedNames (
223     ACPI_NAMESPACE_NODE         *Node,
224     UINT32                      UserParamCount,
225     ACPI_STATUS                 ReturnStatus,
226     ACPI_OPERAND_OBJECT         **ReturnObjectPtr)
227 {
228     ACPI_OPERAND_OBJECT         *ReturnObject = *ReturnObjectPtr;
229     ACPI_STATUS                 Status = AE_OK;
230     const ACPI_PREDEFINED_INFO  *Predefined;
231     char                        *Pathname;
232     ACPI_PREDEFINED_DATA        *Data;
233
234
235     /* Match the name for this method/object against the predefined list */
236
237     Predefined = AcpiNsCheckForPredefinedName (Node);
238
239     /* Get the full pathname to the object, for use in warning messages */
240
241     Pathname = AcpiNsGetExternalPathname (Node);
242     if (!Pathname)
243     {
244         return (AE_OK); /* Could not get pathname, ignore */
245     }
246
247     /*
248      * Check that the parameter count for this method matches the ASL
249      * definition. For predefined names, ensure that both the caller and
250      * the method itself are in accordance with the ACPI specification.
251      */
252     AcpiNsCheckParameterCount (Pathname, Node, UserParamCount, Predefined);
253
254     /* If not a predefined name, we cannot validate the return object */
255
256     if (!Predefined)
257     {
258         goto Cleanup;
259     }
260
261     /*
262      * If the method failed or did not actually return an object, we cannot
263      * validate the return object
264      */
265     if ((ReturnStatus != AE_OK) && (ReturnStatus != AE_CTRL_RETURN_VALUE))
266     {
267         goto Cleanup;
268     }
269
270     /*
271      * If there is no return value, check if we require a return value for
272      * this predefined name. Either one return value is expected, or none,
273      * for both methods and other objects.
274      *
275      * Exit now if there is no return object. Warning if one was expected.
276      */
277     if (!ReturnObject)
278     {
279         if ((Predefined->Info.ExpectedBtypes) &&
280             (!(Predefined->Info.ExpectedBtypes & ACPI_RTYPE_NONE)))
281         {
282             ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, ACPI_WARN_ALWAYS,
283                 "Missing expected return value"));
284
285             Status = AE_AML_NO_RETURN_VALUE;
286         }
287         goto Cleanup;
288     }
289
290     /*
291      * 1) We have a return value, but if one wasn't expected, just exit, this is
292      * not a problem. For example, if the "Implicit Return" feature is
293      * enabled, methods will always return a value.
294      *
295      * 2) If the return value can be of any type, then we cannot perform any
296      * validation, exit.
297      */
298     if ((!Predefined->Info.ExpectedBtypes) ||
299         (Predefined->Info.ExpectedBtypes == ACPI_RTYPE_ALL))
300     {
301         goto Cleanup;
302     }
303
304     /* Create the parameter data block for object validation */
305
306     Data = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PREDEFINED_DATA));
307     if (!Data)
308     {
309         goto Cleanup;
310     }
311     Data->Predefined = Predefined;
312     Data->NodeFlags = Node->Flags;
313     Data->Pathname = Pathname;
314
315     /*
316      * Check that the type of the main return object is what is expected
317      * for this predefined name
318      */
319     Status = AcpiNsCheckObjectType (Data, ReturnObjectPtr,
320                 Predefined->Info.ExpectedBtypes, ACPI_NOT_PACKAGE_ELEMENT);
321     if (ACPI_FAILURE (Status))
322     {
323         goto Exit;
324     }
325
326     /*
327      * For returned Package objects, check the type of all sub-objects.
328      * Note: Package may have been newly created by call above.
329      */
330     if ((*ReturnObjectPtr)->Common.Type == ACPI_TYPE_PACKAGE)
331     {
332         Data->ParentPackage = *ReturnObjectPtr;
333         Status = AcpiNsCheckPackage (Data, ReturnObjectPtr);
334         if (ACPI_FAILURE (Status))
335         {
336             goto Exit;
337         }
338     }
339
340     /*
341      * The return object was OK, or it was successfully repaired above.
342      * Now make some additional checks such as verifying that package
343      * objects are sorted correctly (if required) or buffer objects have
344      * the correct data width (bytes vs. dwords). These repairs are
345      * performed on a per-name basis, i.e., the code is specific to
346      * particular predefined names.
347      */
348     Status = AcpiNsComplexRepairs (Data, Node, Status, ReturnObjectPtr);
349
350 Exit:
351     /*
352      * If the object validation failed or if we successfully repaired one
353      * or more objects, mark the parent node to suppress further warning
354      * messages during the next evaluation of the same method/object.
355      */
356     if (ACPI_FAILURE (Status) || (Data->Flags & ACPI_OBJECT_REPAIRED))
357     {
358         Node->Flags |= ANOBJ_EVALUATED;
359     }
360     ACPI_FREE (Data);
361
362 Cleanup:
363     ACPI_FREE (Pathname);
364     return (Status);
365 }
366
367
368 /*******************************************************************************
369  *
370  * FUNCTION:    AcpiNsCheckParameterCount
371  *
372  * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
373  *              Node            - Namespace node for the method/object
374  *              UserParamCount  - Number of args passed in by the caller
375  *              Predefined      - Pointer to entry in predefined name table
376  *
377  * RETURN:      None
378  *
379  * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
380  *              predefined name is what is expected (i.e., what is defined in
381  *              the ACPI specification for this predefined name.)
382  *
383  ******************************************************************************/
384
385 void
386 AcpiNsCheckParameterCount (
387     char                        *Pathname,
388     ACPI_NAMESPACE_NODE         *Node,
389     UINT32                      UserParamCount,
390     const ACPI_PREDEFINED_INFO  *Predefined)
391 {
392     UINT32                      ParamCount;
393     UINT32                      RequiredParamsCurrent;
394     UINT32                      RequiredParamsOld;
395
396
397     /* Methods have 0-7 parameters. All other types have zero. */
398
399     ParamCount = 0;
400     if (Node->Type == ACPI_TYPE_METHOD)
401     {
402         ParamCount = Node->Object->Method.ParamCount;
403     }
404
405     if (!Predefined)
406     {
407         /*
408          * Check the parameter count for non-predefined methods/objects.
409          *
410          * Warning if too few or too many arguments have been passed by the
411          * caller. An incorrect number of arguments may not cause the method
412          * to fail. However, the method will fail if there are too few
413          * arguments and the method attempts to use one of the missing ones.
414          */
415         if (UserParamCount < ParamCount)
416         {
417             ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, ACPI_WARN_ALWAYS,
418                 "Insufficient arguments - needs %u, found %u",
419                 ParamCount, UserParamCount));
420         }
421         else if (UserParamCount > ParamCount)
422         {
423             ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, ACPI_WARN_ALWAYS,
424                 "Excess arguments - needs %u, found %u",
425                 ParamCount, UserParamCount));
426         }
427         return;
428     }
429
430     /*
431      * Validate the user-supplied parameter count.
432      * Allow two different legal argument counts (_SCP, etc.)
433      */
434     RequiredParamsCurrent = Predefined->Info.ParamCount & 0x0F;
435     RequiredParamsOld = Predefined->Info.ParamCount >> 4;
436
437     if (UserParamCount != ACPI_UINT32_MAX)
438     {
439         if ((UserParamCount != RequiredParamsCurrent) &&
440             (UserParamCount != RequiredParamsOld))
441         {
442             ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, ACPI_WARN_ALWAYS,
443                 "Parameter count mismatch - "
444                 "caller passed %u, ACPI requires %u",
445                 UserParamCount, RequiredParamsCurrent));
446         }
447     }
448
449     /*
450      * Check that the ASL-defined parameter count is what is expected for
451      * this predefined name (parameter count as defined by the ACPI
452      * specification)
453      */
454     if ((ParamCount != RequiredParamsCurrent) &&
455         (ParamCount != RequiredParamsOld))
456     {
457         ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, Node->Flags,
458             "Parameter count mismatch - ASL declared %u, ACPI requires %u",
459             ParamCount, RequiredParamsCurrent));
460     }
461 }
462
463
464 /*******************************************************************************
465  *
466  * FUNCTION:    AcpiNsCheckForPredefinedName
467  *
468  * PARAMETERS:  Node            - Namespace node for the method/object
469  *
470  * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
471  *
472  * DESCRIPTION: Check an object name against the predefined object list.
473  *
474  ******************************************************************************/
475
476 const ACPI_PREDEFINED_INFO *
477 AcpiNsCheckForPredefinedName (
478     ACPI_NAMESPACE_NODE         *Node)
479 {
480     const ACPI_PREDEFINED_INFO  *ThisName;
481
482
483     /* Quick check for a predefined name, first character must be underscore */
484
485     if (Node->Name.Ascii[0] != '_')
486     {
487         return (NULL);
488     }
489
490     /* Search info table for a predefined method/object name */
491
492     ThisName = PredefinedNames;
493     while (ThisName->Info.Name[0])
494     {
495         if (ACPI_COMPARE_NAME (Node->Name.Ascii, ThisName->Info.Name))
496         {
497             return (ThisName);
498         }
499
500         /*
501          * Skip next entry in the table if this name returns a Package
502          * (next entry contains the package info)
503          */
504         if (ThisName->Info.ExpectedBtypes & ACPI_RTYPE_PACKAGE)
505         {
506             ThisName++;
507         }
508
509         ThisName++;
510     }
511
512     return (NULL); /* Not found */
513 }
514
515
516 /*******************************************************************************
517  *
518  * FUNCTION:    AcpiNsCheckPackage
519  *
520  * PARAMETERS:  Data            - Pointer to validation data structure
521  *              ReturnObjectPtr - Pointer to the object returned from the
522  *                                evaluation of a method or object
523  *
524  * RETURN:      Status
525  *
526  * DESCRIPTION: Check a returned package object for the correct count and
527  *              correct type of all sub-objects.
528  *
529  ******************************************************************************/
530
531 static ACPI_STATUS
532 AcpiNsCheckPackage (
533     ACPI_PREDEFINED_DATA        *Data,
534     ACPI_OPERAND_OBJECT         **ReturnObjectPtr)
535 {
536     ACPI_OPERAND_OBJECT         *ReturnObject = *ReturnObjectPtr;
537     const ACPI_PREDEFINED_INFO  *Package;
538     ACPI_OPERAND_OBJECT         **Elements;
539     ACPI_STATUS                 Status = AE_OK;
540     UINT32                      ExpectedCount;
541     UINT32                      Count;
542     UINT32                      i;
543
544
545     ACPI_FUNCTION_NAME (NsCheckPackage);
546
547
548     /* The package info for this name is in the next table entry */
549
550     Package = Data->Predefined + 1;
551
552     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
553         "%s Validating return Package of Type %X, Count %X\n",
554         Data->Pathname, Package->RetInfo.Type, ReturnObject->Package.Count));
555
556     /*
557      * For variable-length Packages, we can safely remove all embedded
558      * and trailing NULL package elements
559      */
560     AcpiNsRemoveNullElements (Data, Package->RetInfo.Type, ReturnObject);
561
562     /* Extract package count and elements array */
563
564     Elements = ReturnObject->Package.Elements;
565     Count = ReturnObject->Package.Count;
566
567     /* The package must have at least one element, else invalid */
568
569     if (!Count)
570     {
571         ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
572             "Return Package has no elements (empty)"));
573
574         return (AE_AML_OPERAND_VALUE);
575     }
576
577     /*
578      * Decode the type of the expected package contents
579      *
580      * PTYPE1 packages contain no subpackages
581      * PTYPE2 packages contain sub-packages
582      */
583     switch (Package->RetInfo.Type)
584     {
585     case ACPI_PTYPE1_FIXED:
586
587         /*
588          * The package count is fixed and there are no sub-packages
589          *
590          * If package is too small, exit.
591          * If package is larger than expected, issue warning but continue
592          */
593         ExpectedCount = Package->RetInfo.Count1 + Package->RetInfo.Count2;
594         if (Count < ExpectedCount)
595         {
596             goto PackageTooSmall;
597         }
598         else if (Count > ExpectedCount)
599         {
600             ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
601                 "%s: Return Package is larger than needed - "
602                 "found %u, expected %u\n",
603                 Data->Pathname, Count, ExpectedCount));
604         }
605
606         /* Validate all elements of the returned package */
607
608         Status = AcpiNsCheckPackageElements (Data, Elements,
609                     Package->RetInfo.ObjectType1, Package->RetInfo.Count1,
610                     Package->RetInfo.ObjectType2, Package->RetInfo.Count2, 0);
611         break;
612
613
614     case ACPI_PTYPE1_VAR:
615
616         /*
617          * The package count is variable, there are no sub-packages, and all
618          * elements must be of the same type
619          */
620         for (i = 0; i < Count; i++)
621         {
622             Status = AcpiNsCheckObjectType (Data, Elements,
623                         Package->RetInfo.ObjectType1, i);
624             if (ACPI_FAILURE (Status))
625             {
626                 return (Status);
627             }
628             Elements++;
629         }
630         break;
631
632
633     case ACPI_PTYPE1_OPTION:
634
635         /*
636          * The package count is variable, there are no sub-packages. There are
637          * a fixed number of required elements, and a variable number of
638          * optional elements.
639          *
640          * Check if package is at least as large as the minimum required
641          */
642         ExpectedCount = Package->RetInfo3.Count;
643         if (Count < ExpectedCount)
644         {
645             goto PackageTooSmall;
646         }
647
648         /* Variable number of sub-objects */
649
650         for (i = 0; i < Count; i++)
651         {
652             if (i < Package->RetInfo3.Count)
653             {
654                 /* These are the required package elements (0, 1, or 2) */
655
656                 Status = AcpiNsCheckObjectType (Data, Elements,
657                             Package->RetInfo3.ObjectType[i], i);
658                 if (ACPI_FAILURE (Status))
659                 {
660                     return (Status);
661                 }
662             }
663             else
664             {
665                 /* These are the optional package elements */
666
667                 Status = AcpiNsCheckObjectType (Data, Elements,
668                             Package->RetInfo3.TailObjectType, i);
669                 if (ACPI_FAILURE (Status))
670                 {
671                     return (Status);
672                 }
673             }
674             Elements++;
675         }
676         break;
677
678
679     case ACPI_PTYPE2_REV_FIXED:
680
681         /* First element is the (Integer) revision */
682
683         Status = AcpiNsCheckObjectType (Data, Elements,
684                     ACPI_RTYPE_INTEGER, 0);
685         if (ACPI_FAILURE (Status))
686         {
687             return (Status);
688         }
689
690         Elements++;
691         Count--;
692
693         /* Examine the sub-packages */
694
695         Status = AcpiNsCheckPackageList (Data, Package, Elements, Count);
696         break;
697
698
699     case ACPI_PTYPE2_PKG_COUNT:
700
701         /* First element is the (Integer) count of sub-packages to follow */
702
703         Status = AcpiNsCheckObjectType (Data, Elements,
704                     ACPI_RTYPE_INTEGER, 0);
705         if (ACPI_FAILURE (Status))
706         {
707             return (Status);
708         }
709
710         /*
711          * Count cannot be larger than the parent package length, but allow it
712          * to be smaller. The >= accounts for the Integer above.
713          */
714         ExpectedCount = (UINT32) (*Elements)->Integer.Value;
715         if (ExpectedCount >= Count)
716         {
717             goto PackageTooSmall;
718         }
719
720         Count = ExpectedCount;
721         Elements++;
722
723         /* Examine the sub-packages */
724
725         Status = AcpiNsCheckPackageList (Data, Package, Elements, Count);
726         break;
727
728
729     case ACPI_PTYPE2:
730     case ACPI_PTYPE2_FIXED:
731     case ACPI_PTYPE2_MIN:
732     case ACPI_PTYPE2_COUNT:
733
734         /*
735          * These types all return a single Package that consists of a
736          * variable number of sub-Packages.
737          *
738          * First, ensure that the first element is a sub-Package. If not,
739          * the BIOS may have incorrectly returned the object as a single
740          * package instead of a Package of Packages (a common error if
741          * there is only one entry). We may be able to repair this by
742          * wrapping the returned Package with a new outer Package.
743          */
744         if (*Elements && ((*Elements)->Common.Type != ACPI_TYPE_PACKAGE))
745         {
746             /* Create the new outer package and populate it */
747
748             Status = AcpiNsRepairPackageList (Data, ReturnObjectPtr);
749             if (ACPI_FAILURE (Status))
750             {
751                 return (Status);
752             }
753
754             /* Update locals to point to the new package (of 1 element) */
755
756             ReturnObject = *ReturnObjectPtr;
757             Elements = ReturnObject->Package.Elements;
758             Count = 1;
759         }
760
761         /* Examine the sub-packages */
762
763         Status = AcpiNsCheckPackageList (Data, Package, Elements, Count);
764         break;
765
766
767     default:
768
769         /* Should not get here if predefined info table is correct */
770
771         ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
772             "Invalid internal return type in table entry: %X",
773             Package->RetInfo.Type));
774
775         return (AE_AML_INTERNAL);
776     }
777
778     return (Status);
779
780
781 PackageTooSmall:
782
783     /* Error exit for the case with an incorrect package count */
784
785     ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
786         "Return Package is too small - found %u elements, expected %u",
787         Count, ExpectedCount));
788
789     return (AE_AML_OPERAND_VALUE);
790 }
791
792
793 /*******************************************************************************
794  *
795  * FUNCTION:    AcpiNsCheckPackageList
796  *
797  * PARAMETERS:  Data            - Pointer to validation data structure
798  *              Package         - Pointer to package-specific info for method
799  *              Elements        - Element list of parent package. All elements
800  *                                of this list should be of type Package.
801  *              Count           - Count of subpackages
802  *
803  * RETURN:      Status
804  *
805  * DESCRIPTION: Examine a list of subpackages
806  *
807  ******************************************************************************/
808
809 static ACPI_STATUS
810 AcpiNsCheckPackageList (
811     ACPI_PREDEFINED_DATA        *Data,
812     const ACPI_PREDEFINED_INFO  *Package,
813     ACPI_OPERAND_OBJECT         **Elements,
814     UINT32                      Count)
815 {
816     ACPI_OPERAND_OBJECT         *SubPackage;
817     ACPI_OPERAND_OBJECT         **SubElements;
818     ACPI_STATUS                 Status;
819     UINT32                      ExpectedCount;
820     UINT32                      i;
821     UINT32                      j;
822
823
824     /*
825      * Validate each sub-Package in the parent Package
826      *
827      * NOTE: assumes list of sub-packages contains no NULL elements.
828      * Any NULL elements should have been removed by earlier call
829      * to AcpiNsRemoveNullElements.
830      */
831     for (i = 0; i < Count; i++)
832     {
833         SubPackage = *Elements;
834         SubElements = SubPackage->Package.Elements;
835         Data->ParentPackage = SubPackage;
836
837         /* Each sub-object must be of type Package */
838
839         Status = AcpiNsCheckObjectType (Data, &SubPackage,
840                     ACPI_RTYPE_PACKAGE, i);
841         if (ACPI_FAILURE (Status))
842         {
843             return (Status);
844         }
845
846         /* Examine the different types of expected sub-packages */
847
848         Data->ParentPackage = SubPackage;
849         switch (Package->RetInfo.Type)
850         {
851         case ACPI_PTYPE2:
852         case ACPI_PTYPE2_PKG_COUNT:
853         case ACPI_PTYPE2_REV_FIXED:
854
855             /* Each subpackage has a fixed number of elements */
856
857             ExpectedCount = Package->RetInfo.Count1 + Package->RetInfo.Count2;
858             if (SubPackage->Package.Count < ExpectedCount)
859             {
860                 goto PackageTooSmall;
861             }
862
863             Status = AcpiNsCheckPackageElements (Data, SubElements,
864                         Package->RetInfo.ObjectType1,
865                         Package->RetInfo.Count1,
866                         Package->RetInfo.ObjectType2,
867                         Package->RetInfo.Count2, 0);
868             if (ACPI_FAILURE (Status))
869             {
870                 return (Status);
871             }
872             break;
873
874
875         case ACPI_PTYPE2_FIXED:
876
877             /* Each sub-package has a fixed length */
878
879             ExpectedCount = Package->RetInfo2.Count;
880             if (SubPackage->Package.Count < ExpectedCount)
881             {
882                 goto PackageTooSmall;
883             }
884
885             /* Check the type of each sub-package element */
886
887             for (j = 0; j < ExpectedCount; j++)
888             {
889                 Status = AcpiNsCheckObjectType (Data, &SubElements[j],
890                             Package->RetInfo2.ObjectType[j], j);
891                 if (ACPI_FAILURE (Status))
892                 {
893                     return (Status);
894                 }
895             }
896             break;
897
898
899         case ACPI_PTYPE2_MIN:
900
901             /* Each sub-package has a variable but minimum length */
902
903             ExpectedCount = Package->RetInfo.Count1;
904             if (SubPackage->Package.Count < ExpectedCount)
905             {
906                 goto PackageTooSmall;
907             }
908
909             /* Check the type of each sub-package element */
910
911             Status = AcpiNsCheckPackageElements (Data, SubElements,
912                         Package->RetInfo.ObjectType1,
913                         SubPackage->Package.Count, 0, 0, 0);
914             if (ACPI_FAILURE (Status))
915             {
916                 return (Status);
917             }
918             break;
919
920
921         case ACPI_PTYPE2_COUNT:
922
923             /*
924              * First element is the (Integer) count of elements, including
925              * the count field (the ACPI name is NumElements)
926              */
927             Status = AcpiNsCheckObjectType (Data, SubElements,
928                         ACPI_RTYPE_INTEGER, 0);
929             if (ACPI_FAILURE (Status))
930             {
931                 return (Status);
932             }
933
934             /*
935              * Make sure package is large enough for the Count and is
936              * is as large as the minimum size
937              */
938             ExpectedCount = (UINT32) (*SubElements)->Integer.Value;
939             if (SubPackage->Package.Count < ExpectedCount)
940             {
941                 goto PackageTooSmall;
942             }
943             if (SubPackage->Package.Count < Package->RetInfo.Count1)
944             {
945                 ExpectedCount = Package->RetInfo.Count1;
946                 goto PackageTooSmall;
947             }
948             if (ExpectedCount == 0)
949             {
950                 /*
951                  * Either the NumEntries element was originally zero or it was
952                  * a NULL element and repaired to an Integer of value zero.
953                  * In either case, repair it by setting NumEntries to be the
954                  * actual size of the subpackage.
955                  */
956                 ExpectedCount = SubPackage->Package.Count;
957                 (*SubElements)->Integer.Value = ExpectedCount;
958             }
959
960             /* Check the type of each sub-package element */
961
962             Status = AcpiNsCheckPackageElements (Data, (SubElements + 1),
963                         Package->RetInfo.ObjectType1,
964                         (ExpectedCount - 1), 0, 0, 1);
965             if (ACPI_FAILURE (Status))
966             {
967                 return (Status);
968             }
969             break;
970
971
972         default: /* Should not get here, type was validated by caller */
973
974             return (AE_AML_INTERNAL);
975         }
976
977         Elements++;
978     }
979
980     return (AE_OK);
981
982
983 PackageTooSmall:
984
985     /* The sub-package count was smaller than required */
986
987     ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
988         "Return Sub-Package[%u] is too small - found %u elements, expected %u",
989         i, SubPackage->Package.Count, ExpectedCount));
990
991     return (AE_AML_OPERAND_VALUE);
992 }
993
994
995 /*******************************************************************************
996  *
997  * FUNCTION:    AcpiNsCheckPackageElements
998  *
999  * PARAMETERS:  Data            - Pointer to validation data structure
1000  *              Elements        - Pointer to the package elements array
1001  *              Type1           - Object type for first group
1002  *              Count1          - Count for first group
1003  *              Type2           - Object type for second group
1004  *              Count2          - Count for second group
1005  *              StartIndex      - Start of the first group of elements
1006  *
1007  * RETURN:      Status
1008  *
1009  * DESCRIPTION: Check that all elements of a package are of the correct object
1010  *              type. Supports up to two groups of different object types.
1011  *
1012  ******************************************************************************/
1013
1014 static ACPI_STATUS
1015 AcpiNsCheckPackageElements (
1016     ACPI_PREDEFINED_DATA        *Data,
1017     ACPI_OPERAND_OBJECT         **Elements,
1018     UINT8                       Type1,
1019     UINT32                      Count1,
1020     UINT8                       Type2,
1021     UINT32                      Count2,
1022     UINT32                      StartIndex)
1023 {
1024     ACPI_OPERAND_OBJECT         **ThisElement = Elements;
1025     ACPI_STATUS                 Status;
1026     UINT32                      i;
1027
1028
1029     /*
1030      * Up to two groups of package elements are supported by the data
1031      * structure. All elements in each group must be of the same type.
1032      * The second group can have a count of zero.
1033      */
1034     for (i = 0; i < Count1; i++)
1035     {
1036         Status = AcpiNsCheckObjectType (Data, ThisElement,
1037                     Type1, i + StartIndex);
1038         if (ACPI_FAILURE (Status))
1039         {
1040             return (Status);
1041         }
1042         ThisElement++;
1043     }
1044
1045     for (i = 0; i < Count2; i++)
1046     {
1047         Status = AcpiNsCheckObjectType (Data, ThisElement,
1048                     Type2, (i + Count1 + StartIndex));
1049         if (ACPI_FAILURE (Status))
1050         {
1051             return (Status);
1052         }
1053         ThisElement++;
1054     }
1055
1056     return (AE_OK);
1057 }
1058
1059
1060 /*******************************************************************************
1061  *
1062  * FUNCTION:    AcpiNsCheckObjectType
1063  *
1064  * PARAMETERS:  Data            - Pointer to validation data structure
1065  *              ReturnObjectPtr - Pointer to the object returned from the
1066  *                                evaluation of a method or object
1067  *              ExpectedBtypes  - Bitmap of expected return type(s)
1068  *              PackageIndex    - Index of object within parent package (if
1069  *                                applicable - ACPI_NOT_PACKAGE_ELEMENT
1070  *                                otherwise)
1071  *
1072  * RETURN:      Status
1073  *
1074  * DESCRIPTION: Check the type of the return object against the expected object
1075  *              type(s). Use of Btype allows multiple expected object types.
1076  *
1077  ******************************************************************************/
1078
1079 static ACPI_STATUS
1080 AcpiNsCheckObjectType (
1081     ACPI_PREDEFINED_DATA        *Data,
1082     ACPI_OPERAND_OBJECT         **ReturnObjectPtr,
1083     UINT32                      ExpectedBtypes,
1084     UINT32                      PackageIndex)
1085 {
1086     ACPI_OPERAND_OBJECT         *ReturnObject = *ReturnObjectPtr;
1087     ACPI_STATUS                 Status = AE_OK;
1088     UINT32                      ReturnBtype;
1089     char                        TypeBuffer[48]; /* Room for 5 types */
1090
1091
1092     /*
1093      * If we get a NULL ReturnObject here, it is a NULL package element.
1094      * Since all extraneous NULL package elements were removed earlier by a
1095      * call to AcpiNsRemoveNullElements, this is an unexpected NULL element.
1096      * We will attempt to repair it.
1097      */
1098     if (!ReturnObject)
1099     {
1100         Status = AcpiNsRepairNullElement (Data, ExpectedBtypes,
1101                     PackageIndex, ReturnObjectPtr);
1102         if (ACPI_SUCCESS (Status))
1103         {
1104             return (AE_OK); /* Repair was successful */
1105         }
1106         goto TypeErrorExit;
1107     }
1108
1109     /* A Namespace node should not get here, but make sure */
1110
1111     if (ACPI_GET_DESCRIPTOR_TYPE (ReturnObject) == ACPI_DESC_TYPE_NAMED)
1112     {
1113         ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
1114             "Invalid return type - Found a Namespace node [%4.4s] type %s",
1115             ReturnObject->Node.Name.Ascii,
1116             AcpiUtGetTypeName (ReturnObject->Node.Type)));
1117         return (AE_AML_OPERAND_TYPE);
1118     }
1119
1120     /*
1121      * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
1122      * The bitmapped type allows multiple possible return types.
1123      *
1124      * Note, the cases below must handle all of the possible types returned
1125      * from all of the predefined names (including elements of returned
1126      * packages)
1127      */
1128     switch (ReturnObject->Common.Type)
1129     {
1130     case ACPI_TYPE_INTEGER:
1131         ReturnBtype = ACPI_RTYPE_INTEGER;
1132         break;
1133
1134     case ACPI_TYPE_BUFFER:
1135         ReturnBtype = ACPI_RTYPE_BUFFER;
1136         break;
1137
1138     case ACPI_TYPE_STRING:
1139         ReturnBtype = ACPI_RTYPE_STRING;
1140         break;
1141
1142     case ACPI_TYPE_PACKAGE:
1143         ReturnBtype = ACPI_RTYPE_PACKAGE;
1144         break;
1145
1146     case ACPI_TYPE_LOCAL_REFERENCE:
1147         ReturnBtype = ACPI_RTYPE_REFERENCE;
1148         break;
1149
1150     default:
1151         /* Not one of the supported objects, must be incorrect */
1152
1153         goto TypeErrorExit;
1154     }
1155
1156     /* Is the object one of the expected types? */
1157
1158     if (ReturnBtype & ExpectedBtypes)
1159     {
1160         /* For reference objects, check that the reference type is correct */
1161
1162         if (ReturnObject->Common.Type == ACPI_TYPE_LOCAL_REFERENCE)
1163         {
1164             Status = AcpiNsCheckReference (Data, ReturnObject);
1165         }
1166
1167         return (Status);
1168     }
1169
1170     /* Type mismatch -- attempt repair of the returned object */
1171
1172     Status = AcpiNsRepairObject (Data, ExpectedBtypes,
1173                 PackageIndex, ReturnObjectPtr);
1174     if (ACPI_SUCCESS (Status))
1175     {
1176         return (AE_OK); /* Repair was successful */
1177     }
1178
1179
1180 TypeErrorExit:
1181
1182     /* Create a string with all expected types for this predefined object */
1183
1184     AcpiNsGetExpectedTypes (TypeBuffer, ExpectedBtypes);
1185
1186     if (PackageIndex == ACPI_NOT_PACKAGE_ELEMENT)
1187     {
1188         ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
1189             "Return type mismatch - found %s, expected %s",
1190             AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));
1191     }
1192     else
1193     {
1194         ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
1195             "Return Package type mismatch at index %u - "
1196             "found %s, expected %s", PackageIndex,
1197             AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));
1198     }
1199
1200     return (AE_AML_OPERAND_TYPE);
1201 }
1202
1203
1204 /*******************************************************************************
1205  *
1206  * FUNCTION:    AcpiNsCheckReference
1207  *
1208  * PARAMETERS:  Data            - Pointer to validation data structure
1209  *              ReturnObject    - Object returned from the evaluation of a
1210  *                                method or object
1211  *
1212  * RETURN:      Status
1213  *
1214  * DESCRIPTION: Check a returned reference object for the correct reference
1215  *              type. The only reference type that can be returned from a
1216  *              predefined method is a named reference. All others are invalid.
1217  *
1218  ******************************************************************************/
1219
1220 static ACPI_STATUS
1221 AcpiNsCheckReference (
1222     ACPI_PREDEFINED_DATA        *Data,
1223     ACPI_OPERAND_OBJECT         *ReturnObject)
1224 {
1225
1226     /*
1227      * Check the reference object for the correct reference type (opcode).
1228      * The only type of reference that can be converted to an ACPI_OBJECT is
1229      * a reference to a named object (reference class: NAME)
1230      */
1231     if (ReturnObject->Reference.Class == ACPI_REFCLASS_NAME)
1232     {
1233         return (AE_OK);
1234     }
1235
1236     ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
1237         "Return type mismatch - unexpected reference object type [%s] %2.2X",
1238         AcpiUtGetReferenceName (ReturnObject),
1239         ReturnObject->Reference.Class));
1240
1241     return (AE_AML_OPERAND_TYPE);
1242 }
1243
1244
1245 /*******************************************************************************
1246  *
1247  * FUNCTION:    AcpiNsGetExpectedTypes
1248  *
1249  * PARAMETERS:  Buffer          - Pointer to where the string is returned
1250  *              ExpectedBtypes  - Bitmap of expected return type(s)
1251  *
1252  * RETURN:      Buffer is populated with type names.
1253  *
1254  * DESCRIPTION: Translate the expected types bitmap into a string of ascii
1255  *              names of expected types, for use in warning messages.
1256  *
1257  ******************************************************************************/
1258
1259 static void
1260 AcpiNsGetExpectedTypes (
1261     char                        *Buffer,
1262     UINT32                      ExpectedBtypes)
1263 {
1264     UINT32                      ThisRtype;
1265     UINT32                      i;
1266     UINT32                      j;
1267
1268
1269     j = 1;
1270     Buffer[0] = 0;
1271     ThisRtype = ACPI_RTYPE_INTEGER;
1272
1273     for (i = 0; i < ACPI_NUM_RTYPES; i++)
1274     {
1275         /* If one of the expected types, concatenate the name of this type */
1276
1277         if (ExpectedBtypes & ThisRtype)
1278         {
1279             ACPI_STRCAT (Buffer, &AcpiRtypeNames[i][j]);
1280             j = 0;              /* Use name separator from now on */
1281         }
1282         ThisRtype <<= 1;    /* Next Rtype */
1283     }
1284 }