Path: blob/main/sys/contrib/dev/acpica/components/namespace/nspredef.c
48524 views
/******************************************************************************1*2* Module Name: nspredef - Validation of ACPI predefined methods and objects3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#define ACPI_CREATE_PREDEFINED_TABLE152153#include <contrib/dev/acpica/include/acpi.h>154#include <contrib/dev/acpica/include/accommon.h>155#include <contrib/dev/acpica/include/acnamesp.h>156#include <contrib/dev/acpica/include/acpredef.h>157158159#define _COMPONENT ACPI_NAMESPACE160ACPI_MODULE_NAME ("nspredef")161162163/*******************************************************************************164*165* This module validates predefined ACPI objects that appear in the namespace,166* at the time they are evaluated (via AcpiEvaluateObject). The purpose of this167* validation is to detect problems with BIOS-exposed predefined ACPI objects168* before the results are returned to the ACPI-related drivers.169*170* There are several areas that are validated:171*172* 1) The number of input arguments as defined by the method/object in the173* ASL is validated against the ACPI specification.174* 2) The type of the return object (if any) is validated against the ACPI175* specification.176* 3) For returned package objects, the count of package elements is177* validated, as well as the type of each package element. Nested178* packages are supported.179*180* For any problems found, a warning message is issued.181*182******************************************************************************/183184185/* Local prototypes */186187static ACPI_STATUS188AcpiNsCheckReference (189ACPI_EVALUATE_INFO *Info,190ACPI_OPERAND_OBJECT *ReturnObject);191192static UINT32193AcpiNsGetBitmappedType (194ACPI_OPERAND_OBJECT *ReturnObject);195196197/*******************************************************************************198*199* FUNCTION: AcpiNsCheckReturnValue200*201* PARAMETERS: Node - Namespace node for the method/object202* Info - Method execution information block203* UserParamCount - Number of parameters actually passed204* ReturnStatus - Status from the object evaluation205* ReturnObjectPtr - Pointer to the object returned from the206* evaluation of a method or object207*208* RETURN: Status209*210* DESCRIPTION: Check the value returned from a predefined name.211*212******************************************************************************/213214ACPI_STATUS215AcpiNsCheckReturnValue (216ACPI_NAMESPACE_NODE *Node,217ACPI_EVALUATE_INFO *Info,218UINT32 UserParamCount,219ACPI_STATUS ReturnStatus,220ACPI_OPERAND_OBJECT **ReturnObjectPtr)221{222ACPI_STATUS Status;223const ACPI_PREDEFINED_INFO *Predefined;224225ACPI_FUNCTION_TRACE (NsCheckReturnValue);226227/* If not a predefined name, we cannot validate the return object */228229Predefined = Info->Predefined;230if (!Predefined)231{232return_ACPI_STATUS (AE_OK);233}234235/*236* If the method failed or did not actually return an object, we cannot237* validate the return object238*/239if ((ReturnStatus != AE_OK) &&240(ReturnStatus != AE_CTRL_RETURN_VALUE))241{242return_ACPI_STATUS (AE_OK);243}244245/*246* Return value validation and possible repair.247*248* 1) Don't perform return value validation/repair if this feature249* 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 perform256* any validation, just exit.257*/258if (AcpiGbl_DisableAutoRepair ||259(!Predefined->Info.ExpectedBtypes) ||260(Predefined->Info.ExpectedBtypes == ACPI_RTYPE_ALL))261{262return_ACPI_STATUS (AE_OK);263}264265/*266* Check that the type of the main return object is what is expected267* for this predefined name268*/269Status = AcpiNsCheckObjectType (Info, ReturnObjectPtr,270Predefined->Info.ExpectedBtypes, ACPI_NOT_PACKAGE_ELEMENT);271if (ACPI_FAILURE (Status))272{273goto Exit;274}275276/*277*278* 4) If there is no return value and it is optional, just return279* AE_OK (_WAK).280*/281if (!(*ReturnObjectPtr))282{283goto Exit;284}285286/*287* For returned Package objects, check the type of all sub-objects.288* Note: Package may have been newly created by call above.289*/290if ((*ReturnObjectPtr)->Common.Type == ACPI_TYPE_PACKAGE)291{292Info->ParentPackage = *ReturnObjectPtr;293Status = AcpiNsCheckPackage (Info, ReturnObjectPtr);294if (ACPI_FAILURE (Status))295{296/* We might be able to fix some errors */297298if ((Status != AE_AML_OPERAND_TYPE) &&299(Status != AE_AML_OPERAND_VALUE))300{301goto Exit;302}303}304}305306/*307* The return object was OK, or it was successfully repaired above.308* Now make some additional checks such as verifying that package309* objects are sorted correctly (if required) or buffer objects have310* the correct data width (bytes vs. dwords). These repairs are311* performed on a per-name basis, i.e., the code is specific to312* particular predefined names.313*/314Status = AcpiNsComplexRepairs (Info, Node, Status, ReturnObjectPtr);315316Exit:317/*318* If the object validation failed or if we successfully repaired one319* or more objects, mark the parent node to suppress further warning320* messages during the next evaluation of the same method/object.321*/322if (ACPI_FAILURE (Status) ||323(Info->ReturnFlags & ACPI_OBJECT_REPAIRED))324{325Node->Flags |= ANOBJ_EVALUATED;326}327328return_ACPI_STATUS (Status);329}330331332/*******************************************************************************333*334* FUNCTION: AcpiNsCheckObjectType335*336* PARAMETERS: Info - Method execution information block337* ReturnObjectPtr - Pointer to the object returned from the338* evaluation of a method or object339* ExpectedBtypes - Bitmap of expected return type(s)340* PackageIndex - Index of object within parent package (if341* applicable - ACPI_NOT_PACKAGE_ELEMENT342* otherwise)343*344* RETURN: Status345*346* DESCRIPTION: Check the type of the return object against the expected object347* type(s). Use of Btype allows multiple expected object types.348*349******************************************************************************/350351ACPI_STATUS352AcpiNsCheckObjectType (353ACPI_EVALUATE_INFO *Info,354ACPI_OPERAND_OBJECT **ReturnObjectPtr,355UINT32 ExpectedBtypes,356UINT32 PackageIndex)357{358ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;359ACPI_STATUS Status = AE_OK;360char TypeBuffer[96]; /* Room for 10 types */361362363/* A Namespace node should not get here, but make sure */364365if (ReturnObject &&366ACPI_GET_DESCRIPTOR_TYPE (ReturnObject) == ACPI_DESC_TYPE_NAMED)367{368ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,369"Invalid return type - Found a Namespace node [%4.4s] type %s",370ReturnObject->Node.Name.Ascii,371AcpiUtGetTypeName (ReturnObject->Node.Type)));372return (AE_AML_OPERAND_TYPE);373}374375/*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 returned380* from all of the predefined names (including elements of returned381* packages)382*/383Info->ReturnBtype = AcpiNsGetBitmappedType (ReturnObject);384if (Info->ReturnBtype == ACPI_RTYPE_ANY)385{386/* Not one of the supported objects, must be incorrect */387goto TypeErrorExit;388}389390/* For reference objects, check that the reference type is correct */391392if ((Info->ReturnBtype & ExpectedBtypes) == ACPI_RTYPE_REFERENCE)393{394Status = AcpiNsCheckReference (Info, ReturnObject);395return (Status);396}397398/* Attempt simple repair of the returned object if necessary */399400Status = AcpiNsSimpleRepair (Info, ExpectedBtypes,401PackageIndex, ReturnObjectPtr);402if (ACPI_SUCCESS (Status))403{404return (AE_OK); /* Successful repair */405}406407408TypeErrorExit:409410/* Create a string with all expected types for this predefined object */411412AcpiUtGetExpectedReturnTypes (TypeBuffer, ExpectedBtypes);413414if (!ReturnObject)415{416ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,417"Expected return object of type %s",418TypeBuffer));419}420else if (PackageIndex == ACPI_NOT_PACKAGE_ELEMENT)421{422ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,423"Return type mismatch - found %s, expected %s",424AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));425}426else427{428ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,429"Return Package type mismatch at index %u - "430"found %s, expected %s", PackageIndex,431AcpiUtGetObjectTypeName (ReturnObject), TypeBuffer));432}433434return (AE_AML_OPERAND_TYPE);435}436437438/*******************************************************************************439*440* FUNCTION: AcpiNsCheckReference441*442* PARAMETERS: Info - Method execution information block443* ReturnObject - Object returned from the evaluation of a444* method or object445*446* RETURN: Status447*448* DESCRIPTION: Check a returned reference object for the correct reference449* type. The only reference type that can be returned from a450* predefined method is a named reference. All others are invalid.451*452******************************************************************************/453454static ACPI_STATUS455AcpiNsCheckReference (456ACPI_EVALUATE_INFO *Info,457ACPI_OPERAND_OBJECT *ReturnObject)458{459460/*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 is463* a reference to a named object (reference class: NAME)464*/465if (ReturnObject->Reference.Class == ACPI_REFCLASS_NAME)466{467return (AE_OK);468}469470ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,471"Return type mismatch - unexpected reference object type [%s] %2.2X",472AcpiUtGetReferenceName (ReturnObject),473ReturnObject->Reference.Class));474475return (AE_AML_OPERAND_TYPE);476}477478479/*******************************************************************************480*481* FUNCTION: AcpiNsGetBitmappedType482*483* PARAMETERS: ReturnObject - Object returned from method/obj evaluation484*485* RETURN: Object return type. ACPI_RTYPE_ANY indicates that the object486* type is not supported. ACPI_RTYPE_NONE indicates that no487* object was returned (ReturnObject is NULL).488*489* DESCRIPTION: Convert object type into a bitmapped object return type.490*491******************************************************************************/492493static UINT32494AcpiNsGetBitmappedType (495ACPI_OPERAND_OBJECT *ReturnObject)496{497UINT32 ReturnBtype;498499500if (!ReturnObject)501{502return (ACPI_RTYPE_NONE);503}504505/* Map ACPI_OBJECT_TYPE to internal bitmapped type */506507switch (ReturnObject->Common.Type)508{509case ACPI_TYPE_INTEGER:510511ReturnBtype = ACPI_RTYPE_INTEGER;512break;513514case ACPI_TYPE_BUFFER:515516ReturnBtype = ACPI_RTYPE_BUFFER;517break;518519case ACPI_TYPE_STRING:520521ReturnBtype = ACPI_RTYPE_STRING;522break;523524case ACPI_TYPE_PACKAGE:525526ReturnBtype = ACPI_RTYPE_PACKAGE;527break;528529case ACPI_TYPE_LOCAL_REFERENCE:530531ReturnBtype = ACPI_RTYPE_REFERENCE;532break;533534default:535536/* Not one of the supported objects, must be incorrect */537538ReturnBtype = ACPI_RTYPE_ANY;539break;540}541542return (ReturnBtype);543}544545546