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 - 2018, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #define ACPI_CREATE_PREDEFINED_TABLE
153 
154 #include "acpi.h"
155 #include "accommon.h"
156 #include "acnamesp.h"
157 #include "acpredef.h"
158 
159 
160 #define _COMPONENT          ACPI_NAMESPACE
161         ACPI_MODULE_NAME    ("nspredef")
162 
163 
164 /*******************************************************************************
165  *
166  * This module validates predefined ACPI objects that appear in the namespace,
167  * at the time they are evaluated (via AcpiEvaluateObject). The purpose of this
168  * validation is to detect problems with BIOS-exposed predefined ACPI objects
169  * before the results are returned to the ACPI-related drivers.
170  *
171  * There are several areas that are validated:
172  *
173  *  1) The number of input arguments as defined by the method/object in the
174  *     ASL is validated against the ACPI specification.
175  *  2) The type of the return object (if any) is validated against the ACPI
176  *     specification.
177  *  3) For returned package objects, the count of package elements is
178  *     validated, as well as the type of each package element. Nested
179  *     packages are supported.
180  *
181  * For any problems found, a warning message is issued.
182  *
183  ******************************************************************************/
184 
185 
186 /* Local prototypes */
187 
188 static ACPI_STATUS
189 AcpiNsCheckReference (
190     ACPI_EVALUATE_INFO          *Info,
191     ACPI_OPERAND_OBJECT         *ReturnObject);
192 
193 static UINT32
194 AcpiNsGetBitmappedType (
195     ACPI_OPERAND_OBJECT         *ReturnObject);
196 
197 
198 /*******************************************************************************
199  *
200  * FUNCTION:    AcpiNsCheckReturnValue
201  *
202  * PARAMETERS:  Node            - Namespace node for the method/object
203  *              Info            - Method execution information block
204  *              UserParamCount  - Number of parameters actually passed
205  *              ReturnStatus    - Status from the object evaluation
206  *              ReturnObjectPtr - Pointer to the object returned from the
207  *                                evaluation of a method or object
208  *
209  * RETURN:      Status
210  *
211  * DESCRIPTION: Check the value returned from a predefined name.
212  *
213  ******************************************************************************/
214 
215 ACPI_STATUS
AcpiNsCheckReturnValue(ACPI_NAMESPACE_NODE * Node,ACPI_EVALUATE_INFO * Info,UINT32 UserParamCount,ACPI_STATUS ReturnStatus,ACPI_OPERAND_OBJECT ** ReturnObjectPtr)216 AcpiNsCheckReturnValue (
217     ACPI_NAMESPACE_NODE         *Node,
218     ACPI_EVALUATE_INFO          *Info,
219     UINT32                      UserParamCount,
220     ACPI_STATUS                 ReturnStatus,
221     ACPI_OPERAND_OBJECT         **ReturnObjectPtr)
222 {
223     ACPI_STATUS                 Status;
224     const ACPI_PREDEFINED_INFO  *Predefined;
225 
226 
227     /* If not a predefined name, we cannot validate the return object */
228 
229     Predefined = Info->Predefined;
230     if (!Predefined)
231     {
232         return (AE_OK);
233     }
234 
235     /*
236      * If the method failed or did not actually return an object, we cannot
237      * validate the return object
238      */
239     if ((ReturnStatus != AE_OK) &&
240         (ReturnStatus != AE_CTRL_RETURN_VALUE))
241     {
242         return (AE_OK);
243     }
244 
245     /*
246      * Return value validation and possible repair.
247      *
248      * 1) Don't perform return value validation/repair if this feature
249      * has been disabled via a global option.
250      *
251      * 2) We have a return value, but if one wasn't expected, just exit,
252      * this is not a problem. For example, if the "Implicit Return"
253      * feature is enabled, methods will always return a value.
254      *
255      * 3) If the return value can be of any type, then we cannot perform
256      * any validation, just exit.
257      */
258     if (AcpiGbl_DisableAutoRepair ||
259         (!Predefined->Info.ExpectedBtypes) ||
260         (Predefined->Info.ExpectedBtypes == ACPI_RTYPE_ALL))
261     {
262         return (AE_OK);
263     }
264 
265     /*
266      * Check that the type of the main return object is what is expected
267      * for this predefined name
268      */
269     Status = AcpiNsCheckObjectType (Info, ReturnObjectPtr,
270         Predefined->Info.ExpectedBtypes, ACPI_NOT_PACKAGE_ELEMENT);
271     if (ACPI_FAILURE (Status))
272     {
273         goto Exit;
274     }
275 
276     /*
277      *
278      * 4) If there is no return value and it is optional, just return
279      * AE_OK (_WAK).
280      */
281     if (!(*ReturnObjectPtr))
282     {
283         goto Exit;
284     }
285 
286     /*
287      * For returned Package objects, check the type of all sub-objects.
288      * Note: Package may have been newly created by call above.
289      */
290     if ((*ReturnObjectPtr)->Common.Type == ACPI_TYPE_PACKAGE)
291     {
292         Info->ParentPackage = *ReturnObjectPtr;
293         Status = AcpiNsCheckPackage (Info, ReturnObjectPtr);
294         if (ACPI_FAILURE (Status))
295         {
296             /* We might be able to fix some errors */
297 
298             if ((Status != AE_AML_OPERAND_TYPE) &&
299                 (Status != AE_AML_OPERAND_VALUE))
300             {
301                 goto Exit;
302             }
303         }
304     }
305 
306     /*
307      * The return object was OK, or it was successfully repaired above.
308      * Now make some additional checks such as verifying that package
309      * objects are sorted correctly (if required) or buffer objects have
310      * the correct data width (bytes vs. dwords). These repairs are
311      * performed on a per-name basis, i.e., the code is specific to
312      * particular predefined names.
313      */
314     Status = AcpiNsComplexRepairs (Info, Node, Status, ReturnObjectPtr);
315 
316 Exit:
317     /*
318      * If the object validation failed or if we successfully repaired one
319      * or more objects, mark the parent node to suppress further warning
320      * messages during the next evaluation of the same method/object.
321      */
322     if (ACPI_FAILURE (Status) ||
323        (Info->ReturnFlags & ACPI_OBJECT_REPAIRED))
324     {
325         Node->Flags |= ANOBJ_EVALUATED;
326     }
327 
328     return (Status);
329 }
330 
331 
332 /*******************************************************************************
333  *
334  * FUNCTION:    AcpiNsCheckObjectType
335  *
336  * PARAMETERS:  Info            - Method execution information block
337  *              ReturnObjectPtr - Pointer to the object returned from the
338  *                                evaluation of a method or object
339  *              ExpectedBtypes  - Bitmap of expected return type(s)
340  *              PackageIndex    - Index of object within parent package (if
341  *                                applicable - ACPI_NOT_PACKAGE_ELEMENT
342  *                                otherwise)
343  *
344  * RETURN:      Status
345  *
346  * DESCRIPTION: Check the type of the return object against the expected object
347  *              type(s). Use of Btype allows multiple expected object types.
348  *
349  ******************************************************************************/
350 
351 ACPI_STATUS
AcpiNsCheckObjectType(ACPI_EVALUATE_INFO * Info,ACPI_OPERAND_OBJECT ** ReturnObjectPtr,UINT32 ExpectedBtypes,UINT32 PackageIndex)352 AcpiNsCheckObjectType (
353     ACPI_EVALUATE_INFO          *Info,
354     ACPI_OPERAND_OBJECT         **ReturnObjectPtr,
355     UINT32                      ExpectedBtypes,
356     UINT32                      PackageIndex)
357 {
358     ACPI_OPERAND_OBJECT         *ReturnObject = *ReturnObjectPtr;
359     ACPI_STATUS                 Status = AE_OK;
360     char                        TypeBuffer[96]; /* Room for 10 types */
361 
362 
363     /* A Namespace node should not get here, but make sure */
364 
365     if (ReturnObject &&
366         ACPI_GET_DESCRIPTOR_TYPE (ReturnObject) == ACPI_DESC_TYPE_NAMED)
367     {
368         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
369             "Invalid return type - Found a Namespace node [%4.4s] type %s",
370             ReturnObject->Node.Name.Ascii,
371             AcpiUtGetTypeName (ReturnObject->Node.Type)));
372         return (AE_AML_OPERAND_TYPE);
373     }
374 
375     /*
376      * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
377      * The bitmapped type allows multiple possible return types.
378      *
379      * Note, the cases below must handle all of the possible types returned
380      * from all of the predefined names (including elements of returned
381      * packages)
382      */
383     Info->ReturnBtype = AcpiNsGetBitmappedType (ReturnObject);
384     if (Info->ReturnBtype == ACPI_RTYPE_ANY)
385     {
386         /* Not one of the supported objects, must be incorrect */
387         goto TypeErrorExit;
388     }
389 
390     /* For reference objects, check that the reference type is correct */
391 
392     if ((Info->ReturnBtype & ExpectedBtypes) == ACPI_RTYPE_REFERENCE)
393     {
394         Status = AcpiNsCheckReference (Info, ReturnObject);
395         return (Status);
396     }
397 
398     /* Attempt simple repair of the returned object if necessary */
399 
400     Status = AcpiNsSimpleRepair (Info, ExpectedBtypes,
401         PackageIndex, ReturnObjectPtr);
402     if (ACPI_SUCCESS (Status))
403     {
404         return (AE_OK); /* Successful repair */
405     }
406 
407 
408 TypeErrorExit:
409 
410     /* Create a string with all expected types for this predefined object */
411 
412     AcpiUtGetExpectedReturnTypes (TypeBuffer, ExpectedBtypes);
413 
414     if (!ReturnObject)
415     {
416         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
417             "Expected return object of type %s",
418             TypeBuffer));
419     }
420     else if (PackageIndex == ACPI_NOT_PACKAGE_ELEMENT)
421     {
422         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
423             "Return type mismatch - found %s, expected %s",
424             AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));
425     }
426     else
427     {
428         ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
429             "Return Package type mismatch at index %u - "
430             "found %s, expected %s", PackageIndex,
431             AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));
432     }
433 
434     return (AE_AML_OPERAND_TYPE);
435 }
436 
437 
438 /*******************************************************************************
439  *
440  * FUNCTION:    AcpiNsCheckReference
441  *
442  * PARAMETERS:  Info            - Method execution information block
443  *              ReturnObject    - Object returned from the evaluation of a
444  *                                method or object
445  *
446  * RETURN:      Status
447  *
448  * DESCRIPTION: Check a returned reference object for the correct reference
449  *              type. The only reference type that can be returned from a
450  *              predefined method is a named reference. All others are invalid.
451  *
452  ******************************************************************************/
453 
454 static ACPI_STATUS
AcpiNsCheckReference(ACPI_EVALUATE_INFO * Info,ACPI_OPERAND_OBJECT * ReturnObject)455 AcpiNsCheckReference (
456     ACPI_EVALUATE_INFO          *Info,
457     ACPI_OPERAND_OBJECT         *ReturnObject)
458 {
459 
460     /*
461      * Check the reference object for the correct reference type (opcode).
462      * The only type of reference that can be converted to an ACPI_OBJECT is
463      * a reference to a named object (reference class: NAME)
464      */
465     if (ReturnObject->Reference.Class == ACPI_REFCLASS_NAME)
466     {
467         return (AE_OK);
468     }
469 
470     ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
471         "Return type mismatch - unexpected reference object type [%s] %2.2X",
472         AcpiUtGetReferenceName (ReturnObject),
473         ReturnObject->Reference.Class));
474 
475     return (AE_AML_OPERAND_TYPE);
476 }
477 
478 
479 /*******************************************************************************
480  *
481  * FUNCTION:    AcpiNsGetBitmappedType
482  *
483  * PARAMETERS:  ReturnObject    - Object returned from method/obj evaluation
484  *
485  * RETURN:      Object return type. ACPI_RTYPE_ANY indicates that the object
486  *              type is not supported. ACPI_RTYPE_NONE indicates that no
487  *              object was returned (ReturnObject is NULL).
488  *
489  * DESCRIPTION: Convert object type into a bitmapped object return type.
490  *
491  ******************************************************************************/
492 
493 static UINT32
AcpiNsGetBitmappedType(ACPI_OPERAND_OBJECT * ReturnObject)494 AcpiNsGetBitmappedType (
495     ACPI_OPERAND_OBJECT         *ReturnObject)
496 {
497     UINT32                      ReturnBtype;
498 
499 
500     if (!ReturnObject)
501     {
502         return (ACPI_RTYPE_NONE);
503     }
504 
505     /* Map ACPI_OBJECT_TYPE to internal bitmapped type */
506 
507     switch (ReturnObject->Common.Type)
508     {
509     case ACPI_TYPE_INTEGER:
510 
511         ReturnBtype = ACPI_RTYPE_INTEGER;
512         break;
513 
514     case ACPI_TYPE_BUFFER:
515 
516         ReturnBtype = ACPI_RTYPE_BUFFER;
517         break;
518 
519     case ACPI_TYPE_STRING:
520 
521         ReturnBtype = ACPI_RTYPE_STRING;
522         break;
523 
524     case ACPI_TYPE_PACKAGE:
525 
526         ReturnBtype = ACPI_RTYPE_PACKAGE;
527         break;
528 
529     case ACPI_TYPE_LOCAL_REFERENCE:
530 
531         ReturnBtype = ACPI_RTYPE_REFERENCE;
532         break;
533 
534     default:
535 
536         /* Not one of the supported objects, must be incorrect */
537 
538         ReturnBtype = ACPI_RTYPE_ANY;
539         break;
540     }
541 
542     return (ReturnBtype);
543 }
544