Path: blob/main/sys/contrib/dev/acpica/components/namespace/nsconvert.c
48524 views
/******************************************************************************1*2* Module Name: nsconvert - Object conversions for objects returned by3* predefined methods4*5*****************************************************************************/67/******************************************************************************8*9* 1. Copyright Notice10*11* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.12* All rights reserved.13*14* 2. License15*16* 2.1. This is your license from Intel Corp. under its intellectual property17* rights. You may have additional license terms from the party that provided18* you this software, covering your right to use that party's intellectual19* property rights.20*21* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a22* copy of the source code appearing in this file ("Covered Code") an23* irrevocable, perpetual, worldwide license under Intel's copyrights in the24* base code distributed originally by Intel ("Original Intel Code") to copy,25* make derivatives, distribute, use and display any portion of the Covered26* Code in any form, with the right to sublicense such rights; and27*28* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent29* license (with the right to sublicense), under only those claims of Intel30* 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 thereof32* solely to the minimum extent necessary to exercise the above copyright33* license, and in no event shall the patent license extend to any additions34* to or modifications of the Original Intel Code. No other license or right35* is granted directly or by implication, estoppel or otherwise;36*37* The above copyright and patent license is granted only if the following38* conditions are met:39*40* 3. Conditions41*42* 3.1. Redistribution of Source with Rights to Further Distribute Source.43* Redistribution of source code of any substantial portion of the Covered44* Code or modification with rights to further distribute source must include45* 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 to48* contain a file documenting the changes Licensee made to create that Covered49* Code and the date of any change. Licensee must include in that file the50* documentation of any changes made by any predecessor Licensee. Licensee51* 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 Covered56* Code or modification without rights to further distribute source must57* include the following Disclaimer and Export Compliance provision in the58* documentation and/or other materials provided with distribution. In59* addition, Licensee may not authorize further sublicense of source of any60* portion of the Covered Code, and must include terms to the effect that the61* license from Licensee to its licensee is limited to the intellectual62* property embodied in the software Licensee provides to its licensee, and63* not to intellectual property embodied in modifications its licensee may64* make.65*66* 3.3. Redistribution of Executable. Redistribution in executable form of any67* substantial portion of the Covered Code or modification must reproduce the68* above Copyright Notice, and the following Disclaimer and Export Compliance69* provision in the documentation and/or other materials provided with the70* distribution.71*72* 3.4. Intel retains all right, title, and interest in and to the Original73* Intel Code.74*75* 3.5. Neither the name Intel nor any other trademark owned or controlled by76* Intel shall be used in advertising or otherwise to promote the sale, use or77* other dealings in products derived from or relating to the Covered Code78* without prior written authorization from Intel.79*80* 4. Disclaimer and Export Compliance81*82* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED83* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE84* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,85* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY86* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY87* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A88* PARTICULAR PURPOSE.89*90* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES91* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR92* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,93* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY94* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL95* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS96* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY97* LIMITED REMEDY.98*99* 4.3. Licensee shall not export, either directly or indirectly, any of this100* software or system incorporating such software without first obtaining any101* required license or other approval from the U. S. Department of Commerce or102* any other agency or department of the United States Government. In the103* event Licensee exports any such software from the United States or104* re-exports any such software from a foreign destination, Licensee shall105* ensure that the distribution and export/re-export of the software is in106* compliance with all laws, regulations, orders, or other restrictions of the107* U.S. Export Administration Regulations. Licensee agrees that neither it nor108* any of its subsidiaries will export/re-export any technical data, process,109* software, or service, directly or indirectly, to any country for which the110* United States government or any agency thereof requires an export license,111* other governmental approval, or letter of assurance, without first obtaining112* such license, approval or letter.113*114*****************************************************************************115*116* Alternatively, you may choose to be licensed under the terms of the117* following license:118*119* Redistribution and use in source and binary forms, with or without120* modification, are permitted provided that the following conditions121* are met:122* 1. Redistributions of source code must retain the above copyright123* notice, this list of conditions, and the following disclaimer,124* without modification.125* 2. Redistributions in binary form must reproduce at minimum a disclaimer126* substantially similar to the "NO WARRANTY" disclaimer below127* ("Disclaimer") and any redistribution must be conditioned upon128* including a substantially similar Disclaimer requirement for further129* binary redistribution.130* 3. Neither the names of the above-listed copyright holders nor the names131* of any contributors may be used to endorse or promote products derived132* from this software without specific prior written permission.133*134* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS135* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT136* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR137* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT138* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,139* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT140* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,141* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY142* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT143* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE144* 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 the147* GNU General Public License ("GPL") version 2 as published by the Free148* Software Foundation.149*150*****************************************************************************/151152#include <contrib/dev/acpica/include/acpi.h>153#include <contrib/dev/acpica/include/accommon.h>154#include <contrib/dev/acpica/include/acnamesp.h>155#include <contrib/dev/acpica/include/acinterp.h>156#include <contrib/dev/acpica/include/acpredef.h>157#include <contrib/dev/acpica/include/amlresrc.h>158159#define _COMPONENT ACPI_NAMESPACE160ACPI_MODULE_NAME ("nsconvert")161162163/*******************************************************************************164*165* FUNCTION: AcpiNsConvertToInteger166*167* PARAMETERS: OriginalObject - Object to be converted168* ReturnObject - Where the new converted object is returned169*170* RETURN: Status. AE_OK if conversion was successful.171*172* DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.173*174******************************************************************************/175176ACPI_STATUS177AcpiNsConvertToInteger (178ACPI_OPERAND_OBJECT *OriginalObject,179ACPI_OPERAND_OBJECT **ReturnObject)180{181ACPI_OPERAND_OBJECT *NewObject;182ACPI_STATUS Status;183UINT64 Value = 0;184UINT32 i;185186187switch (OriginalObject->Common.Type)188{189case ACPI_TYPE_STRING:190191/* String-to-Integer conversion */192193Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer, &Value);194if (ACPI_FAILURE (Status))195{196return (Status);197}198break;199200case ACPI_TYPE_BUFFER:201202/* Buffer-to-Integer conversion. Max buffer size is 64 bits. */203204if (OriginalObject->Buffer.Length > 8)205{206return (AE_AML_OPERAND_TYPE);207}208209/* Extract each buffer byte to create the integer */210211for (i = 0; i < OriginalObject->Buffer.Length; i++)212{213Value |= ((UINT64)214OriginalObject->Buffer.Pointer[i] << (i * 8));215}216break;217218default:219220return (AE_AML_OPERAND_TYPE);221}222223NewObject = AcpiUtCreateIntegerObject (Value);224if (!NewObject)225{226return (AE_NO_MEMORY);227}228229*ReturnObject = NewObject;230return (AE_OK);231}232233234/*******************************************************************************235*236* FUNCTION: AcpiNsConvertToString237*238* PARAMETERS: OriginalObject - Object to be converted239* ReturnObject - Where the new converted object is returned240*241* RETURN: Status. AE_OK if conversion was successful.242*243* DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.244*245******************************************************************************/246247ACPI_STATUS248AcpiNsConvertToString (249ACPI_OPERAND_OBJECT *OriginalObject,250ACPI_OPERAND_OBJECT **ReturnObject)251{252ACPI_OPERAND_OBJECT *NewObject;253ACPI_SIZE Length;254ACPI_STATUS Status;255256257switch (OriginalObject->Common.Type)258{259case ACPI_TYPE_INTEGER:260/*261* Integer-to-String conversion. Commonly, convert262* an integer of value 0 to a NULL string. The last element of263* _BIF and _BIX packages occasionally need this fix.264*/265if (OriginalObject->Integer.Value == 0)266{267/* Allocate a new NULL string object */268269NewObject = AcpiUtCreateStringObject (0);270if (!NewObject)271{272return (AE_NO_MEMORY);273}274}275else276{277Status = AcpiExConvertToString (OriginalObject,278&NewObject, ACPI_IMPLICIT_CONVERT_HEX);279if (ACPI_FAILURE (Status))280{281return (Status);282}283}284break;285286case ACPI_TYPE_BUFFER:287/*288* Buffer-to-String conversion. Use a ToString289* conversion, no transform performed on the buffer data. The best290* example of this is the _BIF method, where the string data from291* the battery is often (incorrectly) returned as buffer object(s).292*/293Length = 0;294while ((Length < OriginalObject->Buffer.Length) &&295(OriginalObject->Buffer.Pointer[Length]))296{297Length++;298}299300/* Allocate a new string object */301302NewObject = AcpiUtCreateStringObject (Length);303if (!NewObject)304{305return (AE_NO_MEMORY);306}307308/*309* Copy the raw buffer data with no transform. String is already NULL310* terminated at Length+1.311*/312memcpy (NewObject->String.Pointer,313OriginalObject->Buffer.Pointer, Length);314break;315316default:317318return (AE_AML_OPERAND_TYPE);319}320321*ReturnObject = NewObject;322return (AE_OK);323}324325326/*******************************************************************************327*328* FUNCTION: AcpiNsConvertToBuffer329*330* PARAMETERS: OriginalObject - Object to be converted331* ReturnObject - Where the new converted object is returned332*333* RETURN: Status. AE_OK if conversion was successful.334*335* DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.336*337******************************************************************************/338339ACPI_STATUS340AcpiNsConvertToBuffer (341ACPI_OPERAND_OBJECT *OriginalObject,342ACPI_OPERAND_OBJECT **ReturnObject)343{344ACPI_OPERAND_OBJECT *NewObject;345ACPI_STATUS Status;346ACPI_OPERAND_OBJECT **Elements;347UINT32 *DwordBuffer;348UINT32 Count;349UINT32 i;350351352switch (OriginalObject->Common.Type)353{354case ACPI_TYPE_INTEGER:355/*356* Integer-to-Buffer conversion.357* Convert the Integer to a packed-byte buffer. _MAT and other358* objects need this sometimes, if a read has been performed on a359* Field object that is less than or equal to the global integer360* size (32 or 64 bits).361*/362Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);363if (ACPI_FAILURE (Status))364{365return (Status);366}367break;368369case ACPI_TYPE_STRING:370371/* String-to-Buffer conversion. Simple data copy */372373NewObject = AcpiUtCreateBufferObject374(OriginalObject->String.Length);375if (!NewObject)376{377return (AE_NO_MEMORY);378}379380memcpy (NewObject->Buffer.Pointer,381OriginalObject->String.Pointer, OriginalObject->String.Length);382break;383384case ACPI_TYPE_PACKAGE:385/*386* This case is often seen for predefined names that must return a387* Buffer object with multiple DWORD integers within. For example,388* _FDE and _GTM. The Package can be converted to a Buffer.389*/390391/* All elements of the Package must be integers */392393Elements = OriginalObject->Package.Elements;394Count = OriginalObject->Package.Count;395396for (i = 0; i < Count; i++)397{398if ((!*Elements) ||399((*Elements)->Common.Type != ACPI_TYPE_INTEGER))400{401return (AE_AML_OPERAND_TYPE);402}403Elements++;404}405406/* Create the new buffer object to replace the Package */407408NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));409if (!NewObject)410{411return (AE_NO_MEMORY);412}413414/* Copy the package elements (integers) to the buffer as DWORDs */415416Elements = OriginalObject->Package.Elements;417DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);418419for (i = 0; i < Count; i++)420{421*DwordBuffer = (UINT32) (*Elements)->Integer.Value;422DwordBuffer++;423Elements++;424}425break;426427default:428429return (AE_AML_OPERAND_TYPE);430}431432*ReturnObject = NewObject;433return (AE_OK);434}435436437/*******************************************************************************438*439* FUNCTION: AcpiNsConvertToUnicode440*441* PARAMETERS: Scope - Namespace node for the method/object442* OriginalObject - ASCII String Object to be converted443* ReturnObject - Where the new converted object is returned444*445* RETURN: Status. AE_OK if conversion was successful.446*447* DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.448*449******************************************************************************/450451ACPI_STATUS452AcpiNsConvertToUnicode (453ACPI_NAMESPACE_NODE *Scope,454ACPI_OPERAND_OBJECT *OriginalObject,455ACPI_OPERAND_OBJECT **ReturnObject)456{457ACPI_OPERAND_OBJECT *NewObject;458char *AsciiString;459UINT16 *UnicodeBuffer;460UINT32 UnicodeLength;461UINT32 i;462463464if (!OriginalObject)465{466return (AE_OK);467}468469/* If a Buffer was returned, it must be at least two bytes long */470471if (OriginalObject->Common.Type == ACPI_TYPE_BUFFER)472{473if (OriginalObject->Buffer.Length < 2)474{475return (AE_AML_OPERAND_VALUE);476}477478*ReturnObject = NULL;479return (AE_OK);480}481482/*483* The original object is an ASCII string. Convert this string to484* a unicode buffer.485*/486AsciiString = OriginalObject->String.Pointer;487UnicodeLength = (OriginalObject->String.Length * 2) + 2;488489/* Create a new buffer object for the Unicode data */490491NewObject = AcpiUtCreateBufferObject (UnicodeLength);492if (!NewObject)493{494return (AE_NO_MEMORY);495}496497UnicodeBuffer = ACPI_CAST_PTR (UINT16, NewObject->Buffer.Pointer);498499/* Convert ASCII to Unicode */500501for (i = 0; i < OriginalObject->String.Length; i++)502{503UnicodeBuffer[i] = (UINT16) AsciiString[i];504}505506*ReturnObject = NewObject;507return (AE_OK);508}509510511/*******************************************************************************512*513* FUNCTION: AcpiNsConvertToResource514*515* PARAMETERS: Scope - Namespace node for the method/object516* OriginalObject - Object to be converted517* ReturnObject - Where the new converted object is returned518*519* RETURN: Status. AE_OK if conversion was successful520*521* DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate522* Buffer.523*524******************************************************************************/525526ACPI_STATUS527AcpiNsConvertToResource (528ACPI_NAMESPACE_NODE *Scope,529ACPI_OPERAND_OBJECT *OriginalObject,530ACPI_OPERAND_OBJECT **ReturnObject)531{532ACPI_OPERAND_OBJECT *NewObject;533UINT8 *Buffer;534535536/*537* We can fix the following cases for an expected resource template:538* 1. No return value (interpreter slack mode is disabled)539* 2. A "Return (Zero)" statement540* 3. A "Return empty buffer" statement541*542* We will return a buffer containing a single EndTag543* resource descriptor.544*/545if (OriginalObject)546{547switch (OriginalObject->Common.Type)548{549case ACPI_TYPE_INTEGER:550551/* We can only repair an Integer==0 */552553if (OriginalObject->Integer.Value)554{555return (AE_AML_OPERAND_TYPE);556}557break;558559case ACPI_TYPE_BUFFER:560561if (OriginalObject->Buffer.Length)562{563/* Additional checks can be added in the future */564565*ReturnObject = NULL;566return (AE_OK);567}568break;569570case ACPI_TYPE_STRING:571default:572573return (AE_AML_OPERAND_TYPE);574}575}576577/* Create the new buffer object for the resource descriptor */578579NewObject = AcpiUtCreateBufferObject (2);580if (!NewObject)581{582return (AE_NO_MEMORY);583}584585Buffer = ACPI_CAST_PTR (UINT8, NewObject->Buffer.Pointer);586587/* Initialize the Buffer with a single EndTag descriptor */588589Buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);590Buffer[1] = 0x00;591592*ReturnObject = NewObject;593return (AE_OK);594}595596597/*******************************************************************************598*599* FUNCTION: AcpiNsConvertToReference600*601* PARAMETERS: Scope - Namespace node for the method/object602* OriginalObject - Object to be converted603* ReturnObject - Where the new converted object is returned604*605* RETURN: Status. AE_OK if conversion was successful606*607* DESCRIPTION: Attempt to convert a Integer object to a ObjectReference.608* Buffer.609*610******************************************************************************/611612ACPI_STATUS613AcpiNsConvertToReference (614ACPI_NAMESPACE_NODE *Scope,615ACPI_OPERAND_OBJECT *OriginalObject,616ACPI_OPERAND_OBJECT **ReturnObject)617{618ACPI_OPERAND_OBJECT *NewObject = NULL;619ACPI_STATUS Status;620ACPI_NAMESPACE_NODE *Node;621ACPI_GENERIC_STATE ScopeInfo;622char *Name;623624625ACPI_FUNCTION_NAME (NsConvertToReference);626627628/* Convert path into internal presentation */629630Status = AcpiNsInternalizeName (OriginalObject->String.Pointer, &Name);631if (ACPI_FAILURE (Status))632{633return_ACPI_STATUS (Status);634}635636/* Find the namespace node */637638ScopeInfo.Scope.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Scope);639Status = AcpiNsLookup (&ScopeInfo, Name,640ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,641ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE, NULL, &Node);642if (ACPI_FAILURE (Status))643{644/* Check if we are resolving a named reference within a package */645646ACPI_ERROR_NAMESPACE (&ScopeInfo,647OriginalObject->String.Pointer, Status);648goto ErrorExit;649}650651/* Create and init a new internal ACPI object */652653NewObject = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);654if (!NewObject)655{656Status = AE_NO_MEMORY;657goto ErrorExit;658}659NewObject->Reference.Node = Node;660NewObject->Reference.Object = Node->Object;661NewObject->Reference.Class = ACPI_REFCLASS_NAME;662663/*664* Increase reference of the object if needed (the object is likely a665* null for device nodes).666*/667AcpiUtAddReference (Node->Object);668669ErrorExit:670ACPI_FREE (Name);671*ReturnObject = NewObject;672return (Status);673}674675676