xref: /freebsd/sys/contrib/dev/acpica/components/namespace/nspredef.c (revision e14ddd1f16e7e5788392c50de21ea7c927e0690c)
1 /******************************************************************************
2  *
3  * Module Name: nspredef - Validation of ACPI predefined methods and objects
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #define ACPI_CREATE_PREDEFINED_TABLE
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acnamesp.h>
49 #include <contrib/dev/acpica/include/acpredef.h>
50 
51 
52 #define _COMPONENT          ACPI_NAMESPACE
53         ACPI_MODULE_NAME    ("nspredef")
54 
55 
56 /*******************************************************************************
57  *
58  * This module validates predefined ACPI objects that appear in the namespace,
59  * at the time they are evaluated (via AcpiEvaluateObject). The purpose of this
60  * validation is to detect problems with BIOS-exposed predefined ACPI objects
61  * before the results are returned to the ACPI-related drivers.
62  *
63  * There are several areas that are validated:
64  *
65  *  1) The number of input arguments as defined by the method/object in the
66  *     ASL is validated against the ACPI specification.
67  *  2) The type of the return object (if any) is validated against the ACPI
68  *     specification.
69  *  3) For returned package objects, the count of package elements is
70  *     validated, as well as the type of each package element. Nested
71  *     packages are supported.
72  *
73  * For any problems found, a warning message is issued.
74  *
75  ******************************************************************************/
76 
77 
78 /* Local prototypes */
79 
80 static ACPI_STATUS
81 AcpiNsCheckReference (
82     ACPI_EVALUATE_INFO          *Info,
83     ACPI_OPERAND_OBJECT         *ReturnObject);
84 
85 static UINT32
86 AcpiNsGetBitmappedType (
87     ACPI_OPERAND_OBJECT         *ReturnObject);
88 
89 
90 /*******************************************************************************
91  *
92  * FUNCTION:    AcpiNsCheckReturnValue
93  *
94  * PARAMETERS:  Node            - Namespace node for the method/object
95  *              Info            - Method execution information block
96  *              UserParamCount  - Number of parameters actually passed
97  *              ReturnStatus    - Status from the object evaluation
98  *              ReturnObjectPtr - Pointer to the object returned from the
99  *                                evaluation of a method or object
100  *
101  * RETURN:      Status
102  *
103  * DESCRIPTION: Check the value returned from a predefined name.
104  *
105  ******************************************************************************/
106 
107 ACPI_STATUS
108 AcpiNsCheckReturnValue (
109     ACPI_NAMESPACE_NODE         *Node,
110     ACPI_EVALUATE_INFO          *Info,
111     UINT32                      UserParamCount,
112     ACPI_STATUS                 ReturnStatus,
113     ACPI_OPERAND_OBJECT         **ReturnObjectPtr)
114 {
115     ACPI_STATUS                 Status;
116     const ACPI_PREDEFINED_INFO  *Predefined;
117 
118 
119     /* If not a predefined name, we cannot validate the return object */
120 
121     Predefined = Info->Predefined;
122     if (!Predefined)
123     {
124         return (AE_OK);
125     }
126 
127     /*
128      * If the method failed or did not actually return an object, we cannot
129      * validate the return object
130      */
131     if ((ReturnStatus != AE_OK) &&
132         (ReturnStatus != AE_CTRL_RETURN_VALUE))
133     {
134         return (AE_OK);
135     }
136 
137     /*
138      * Return value validation and possible repair.
139      *
140      * 1) Don't perform return value validation/repair if this feature
141      * has been disabled via a global option.
142      *
143      * 2) We have a return value, but if one wasn't expected, just exit,
144      * this is not a problem. For example, if the "Implicit Return"
145      * feature is enabled, methods will always return a value.
146      *
147      * 3) If the return value can be of any type, then we cannot perform
148      * any validation, just exit.
149      */
150     if (AcpiGbl_DisableAutoRepair ||
151         (!Predefined->Info.ExpectedBtypes) ||
152         (Predefined->Info.ExpectedBtypes == ACPI_RTYPE_ALL))
153     {
154         return (AE_OK);
155     }
156 
157     /*
158      * Check that the type of the main return object is what is expected
159      * for this predefined name
160      */
161     Status = AcpiNsCheckObjectType (Info, ReturnObjectPtr,
162         Predefined->Info.ExpectedBtypes, ACPI_NOT_PACKAGE_ELEMENT);
163     if (ACPI_FAILURE (Status))
164     {
165         goto Exit;
166     }
167 
168     /*
169      * For returned Package objects, check the type of all sub-objects.
170      * Note: Package may have been newly created by call above.
171      */
172     if ((*ReturnObjectPtr)->Common.Type == ACPI_TYPE_PACKAGE)
173     {
174         Info->ParentPackage = *ReturnObjectPtr;
175         Status = AcpiNsCheckPackage (Info, ReturnObjectPtr);
176         if (ACPI_FAILURE (Status))
177         {
178             goto Exit;
179         }
180     }
181 
182     /*
183      * The return object was OK, or it was successfully repaired above.
184      * Now make some additional checks such as verifying that package
185      * objects are sorted correctly (if required) or buffer objects have
186      * the correct data width (bytes vs. dwords). These repairs are
187      * performed on a per-name basis, i.e., the code is specific to
188      * particular predefined names.
189      */
190     Status = AcpiNsComplexRepairs (Info, Node, Status, ReturnObjectPtr);
191 
192 Exit:
193     /*
194      * If the object validation failed or if we successfully repaired one
195      * or more objects, mark the parent node to suppress further warning
196      * messages during the next evaluation of the same method/object.
197      */
198     if (ACPI_FAILURE (Status) ||
199        (Info->ReturnFlags & ACPI_OBJECT_REPAIRED))
200     {
201         Node->Flags |= ANOBJ_EVALUATED;
202     }
203 
204     return (Status);
205 }
206 
207 
208 /*******************************************************************************
209  *
210  * FUNCTION:    AcpiNsCheckObjectType
211  *
212  * PARAMETERS:  Info            - Method execution information block
213  *              ReturnObjectPtr - Pointer to the object returned from the
214  *                                evaluation of a method or object
215  *              ExpectedBtypes  - Bitmap of expected return type(s)
216  *              PackageIndex    - Index of object within parent package (if
217  *                                applicable - ACPI_NOT_PACKAGE_ELEMENT
218  *                                otherwise)
219  *
220  * RETURN:      Status
221  *
222  * DESCRIPTION: Check the type of the return object against the expected object
223  *              type(s). Use of Btype allows multiple expected object types.
224  *
225  ******************************************************************************/
226 
227 ACPI_STATUS
228 AcpiNsCheckObjectType (
229     ACPI_EVALUATE_INFO          *Info,
230     ACPI_OPERAND_OBJECT         **ReturnObjectPtr,
231     UINT32                      ExpectedBtypes,
232     UINT32                      PackageIndex)
233 {
234     ACPI_OPERAND_OBJECT         *ReturnObject = *ReturnObjectPtr;
235     ACPI_STATUS                 Status = AE_OK;
236     char                        TypeBuffer[48]; /* Room for 5 types */
237 
238 
239     /* A Namespace node should not get here, but make sure */
240 
241     if (ReturnObject &&
242         ACPI_GET_DESCRIPTOR_TYPE (ReturnObject) == ACPI_DESC_TYPE_NAMED)
243     {
244         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
245             "Invalid return type - Found a Namespace node [%4.4s] type %s",
246             ReturnObject->Node.Name.Ascii,
247             AcpiUtGetTypeName (ReturnObject->Node.Type)));
248         return (AE_AML_OPERAND_TYPE);
249     }
250 
251     /*
252      * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
253      * The bitmapped type allows multiple possible return types.
254      *
255      * Note, the cases below must handle all of the possible types returned
256      * from all of the predefined names (including elements of returned
257      * packages)
258      */
259     Info->ReturnBtype = AcpiNsGetBitmappedType (ReturnObject);
260     if (Info->ReturnBtype == ACPI_RTYPE_ANY)
261     {
262         /* Not one of the supported objects, must be incorrect */
263         goto TypeErrorExit;
264     }
265 
266     /* For reference objects, check that the reference type is correct */
267 
268     if ((Info->ReturnBtype & ExpectedBtypes) == ACPI_RTYPE_REFERENCE)
269     {
270         Status = AcpiNsCheckReference (Info, ReturnObject);
271         return (Status);
272     }
273 
274     /* Attempt simple repair of the returned object if necessary */
275 
276     Status = AcpiNsSimpleRepair (Info, ExpectedBtypes,
277         PackageIndex, ReturnObjectPtr);
278     if (ACPI_SUCCESS (Status))
279     {
280         return (AE_OK); /* Successful repair */
281     }
282 
283 
284 TypeErrorExit:
285 
286     /* Create a string with all expected types for this predefined object */
287 
288     AcpiUtGetExpectedReturnTypes (TypeBuffer, ExpectedBtypes);
289 
290     if (PackageIndex == ACPI_NOT_PACKAGE_ELEMENT)
291     {
292         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
293             "Return type mismatch - found %s, expected %s",
294             AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));
295     }
296     else
297     {
298         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
299             "Return Package type mismatch at index %u - "
300             "found %s, expected %s", PackageIndex,
301             AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));
302     }
303 
304     return (AE_AML_OPERAND_TYPE);
305 }
306 
307 
308 /*******************************************************************************
309  *
310  * FUNCTION:    AcpiNsCheckReference
311  *
312  * PARAMETERS:  Info            - Method execution information block
313  *              ReturnObject    - Object returned from the evaluation of a
314  *                                method or object
315  *
316  * RETURN:      Status
317  *
318  * DESCRIPTION: Check a returned reference object for the correct reference
319  *              type. The only reference type that can be returned from a
320  *              predefined method is a named reference. All others are invalid.
321  *
322  ******************************************************************************/
323 
324 static ACPI_STATUS
325 AcpiNsCheckReference (
326     ACPI_EVALUATE_INFO          *Info,
327     ACPI_OPERAND_OBJECT         *ReturnObject)
328 {
329 
330     /*
331      * Check the reference object for the correct reference type (opcode).
332      * The only type of reference that can be converted to an ACPI_OBJECT is
333      * a reference to a named object (reference class: NAME)
334      */
335     if (ReturnObject->Reference.Class == ACPI_REFCLASS_NAME)
336     {
337         return (AE_OK);
338     }
339 
340     ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
341         "Return type mismatch - unexpected reference object type [%s] %2.2X",
342         AcpiUtGetReferenceName (ReturnObject),
343         ReturnObject->Reference.Class));
344 
345     return (AE_AML_OPERAND_TYPE);
346 }
347 
348 
349 /*******************************************************************************
350  *
351  * FUNCTION:    AcpiNsGetBitmappedType
352  *
353  * PARAMETERS:  ReturnObject    - Object returned from method/obj evaluation
354  *
355  * RETURN:      Object return type. ACPI_RTYPE_ANY indicates that the object
356  *              type is not supported. ACPI_RTYPE_NONE indicates that no
357  *              object was returned (ReturnObject is NULL).
358  *
359  * DESCRIPTION: Convert object type into a bitmapped object return type.
360  *
361  ******************************************************************************/
362 
363 static UINT32
364 AcpiNsGetBitmappedType (
365     ACPI_OPERAND_OBJECT         *ReturnObject)
366 {
367     UINT32                      ReturnBtype;
368 
369 
370     if (!ReturnObject)
371     {
372         return (ACPI_RTYPE_NONE);
373     }
374 
375     /* Map ACPI_OBJECT_TYPE to internal bitmapped type */
376 
377     switch (ReturnObject->Common.Type)
378     {
379     case ACPI_TYPE_INTEGER:
380         ReturnBtype = ACPI_RTYPE_INTEGER;
381         break;
382 
383     case ACPI_TYPE_BUFFER:
384         ReturnBtype = ACPI_RTYPE_BUFFER;
385         break;
386 
387     case ACPI_TYPE_STRING:
388         ReturnBtype = ACPI_RTYPE_STRING;
389         break;
390 
391     case ACPI_TYPE_PACKAGE:
392         ReturnBtype = ACPI_RTYPE_PACKAGE;
393         break;
394 
395     case ACPI_TYPE_LOCAL_REFERENCE:
396         ReturnBtype = ACPI_RTYPE_REFERENCE;
397         break;
398 
399     default:
400         /* Not one of the supported objects, must be incorrect */
401 
402         ReturnBtype = ACPI_RTYPE_ANY;
403         break;
404     }
405 
406     return (ReturnBtype);
407 }
408