Path: blob/main/sys/contrib/dev/acpica/components/executer/exconcat.c
48521 views
/******************************************************************************1*2* Module Name: exconcat - Concatenate-type AML operators3*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 ANY8586* 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/acinterp.h>155#include <contrib/dev/acpica/include/amlresrc.h>156157158#define _COMPONENT ACPI_EXECUTER159ACPI_MODULE_NAME ("exconcat")160161/* Local Prototypes */162163static ACPI_STATUS164AcpiExConvertToObjectTypeString (165ACPI_OPERAND_OBJECT *ObjDesc,166ACPI_OPERAND_OBJECT **ResultDesc);167168169/*******************************************************************************170*171* FUNCTION: AcpiExDoConcatenate172*173* PARAMETERS: Operand0 - First source object174* Operand1 - Second source object175* ActualReturnDesc - Where to place the return object176* WalkState - Current walk state177*178* RETURN: Status179*180* DESCRIPTION: Concatenate two objects with the ACPI-defined conversion181* rules as necessary.182* NOTE:183* Per the ACPI spec (up to 6.1), Concatenate only supports Integer,184* String, and Buffer objects. However, we support all objects here185* as an extension. This improves the usefulness of both Concatenate186* and the Printf/Fprintf macros. The extension returns a string187* describing the object type for the other objects.188* 02/2016.189*190******************************************************************************/191192ACPI_STATUS193AcpiExDoConcatenate (194ACPI_OPERAND_OBJECT *Operand0,195ACPI_OPERAND_OBJECT *Operand1,196ACPI_OPERAND_OBJECT **ActualReturnDesc,197ACPI_WALK_STATE *WalkState)198{199ACPI_OPERAND_OBJECT *LocalOperand0 = Operand0;200ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;201ACPI_OPERAND_OBJECT *TempOperand1 = NULL;202ACPI_OPERAND_OBJECT *ReturnDesc;203char *Buffer;204ACPI_OBJECT_TYPE Operand0Type;205ACPI_OBJECT_TYPE Operand1Type;206ACPI_STATUS Status;207208209ACPI_FUNCTION_TRACE (ExDoConcatenate);210211212/* Operand 0 preprocessing */213214switch (Operand0->Common.Type)215{216case ACPI_TYPE_INTEGER:217case ACPI_TYPE_STRING:218case ACPI_TYPE_BUFFER:219220Operand0Type = Operand0->Common.Type;221break;222223default:224225/* For all other types, get the "object type" string */226227Status = AcpiExConvertToObjectTypeString (228Operand0, &LocalOperand0);229if (ACPI_FAILURE (Status))230{231goto Cleanup;232}233234Operand0Type = ACPI_TYPE_STRING;235break;236}237238/* Operand 1 preprocessing */239240switch (Operand1->Common.Type)241{242case ACPI_TYPE_INTEGER:243case ACPI_TYPE_STRING:244case ACPI_TYPE_BUFFER:245246Operand1Type = Operand1->Common.Type;247break;248249default:250251/* For all other types, get the "object type" string */252253Status = AcpiExConvertToObjectTypeString (254Operand1, &LocalOperand1);255if (ACPI_FAILURE (Status))256{257goto Cleanup;258}259260Operand1Type = ACPI_TYPE_STRING;261break;262}263264/*265* Convert the second operand if necessary. The first operand (0)266* determines the type of the second operand (1) (See the Data Types267* section of the ACPI specification). Both object types are268* guaranteed to be either Integer/String/Buffer by the operand269* resolution mechanism.270*/271switch (Operand0Type)272{273case ACPI_TYPE_INTEGER:274275Status = AcpiExConvertToInteger (LocalOperand1, &TempOperand1,276ACPI_IMPLICIT_CONVERSION);277break;278279case ACPI_TYPE_BUFFER:280281Status = AcpiExConvertToBuffer (LocalOperand1, &TempOperand1);282break;283284case ACPI_TYPE_STRING:285286switch (Operand1Type)287{288case ACPI_TYPE_INTEGER:289case ACPI_TYPE_STRING:290case ACPI_TYPE_BUFFER:291292/* Other types have already been converted to string */293294Status = AcpiExConvertToString (295LocalOperand1, &TempOperand1, ACPI_IMPLICIT_CONVERT_HEX);296break;297298default:299300Status = AE_OK;301break;302}303break;304305default:306307ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",308Operand0->Common.Type));309Status = AE_AML_INTERNAL;310}311312if (ACPI_FAILURE (Status))313{314goto Cleanup;315}316317/* Take care with any newly created operand objects */318319if ((LocalOperand1 != Operand1) &&320(LocalOperand1 != TempOperand1))321{322AcpiUtRemoveReference (LocalOperand1);323}324325LocalOperand1 = TempOperand1;326327/*328* Both operands are now known to be the same object type329* (Both are Integer, String, or Buffer), and we can now perform330* the concatenation.331*332* There are three cases to handle, as per the ACPI spec:333*334* 1) Two Integers concatenated to produce a new Buffer335* 2) Two Strings concatenated to produce a new String336* 3) Two Buffers concatenated to produce a new Buffer337*/338switch (Operand0Type)339{340case ACPI_TYPE_INTEGER:341342/* Result of two Integers is a Buffer */343/* Need enough buffer space for two integers */344345ReturnDesc = AcpiUtCreateBufferObject (346(ACPI_SIZE) ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));347if (!ReturnDesc)348{349Status = AE_NO_MEMORY;350goto Cleanup;351}352353Buffer = (char *) ReturnDesc->Buffer.Pointer;354355/* Copy the first integer, LSB first */356357memcpy (Buffer, &Operand0->Integer.Value,358AcpiGbl_IntegerByteWidth);359360/* Copy the second integer (LSB first) after the first */361362memcpy (Buffer + AcpiGbl_IntegerByteWidth,363&LocalOperand1->Integer.Value, AcpiGbl_IntegerByteWidth);364break;365366case ACPI_TYPE_STRING:367368/* Result of two Strings is a String */369370ReturnDesc = AcpiUtCreateStringObject (371((ACPI_SIZE) LocalOperand0->String.Length +372LocalOperand1->String.Length));373if (!ReturnDesc)374{375Status = AE_NO_MEMORY;376goto Cleanup;377}378379Buffer = ReturnDesc->String.Pointer;380381/* Concatenate the strings */382383strcpy (Buffer, LocalOperand0->String.Pointer);384strcat (Buffer, LocalOperand1->String.Pointer);385break;386387case ACPI_TYPE_BUFFER:388389/* Result of two Buffers is a Buffer */390391ReturnDesc = AcpiUtCreateBufferObject (392((ACPI_SIZE) Operand0->Buffer.Length +393LocalOperand1->Buffer.Length));394if (!ReturnDesc)395{396Status = AE_NO_MEMORY;397goto Cleanup;398}399400Buffer = (char *) ReturnDesc->Buffer.Pointer;401402/* Concatenate the buffers */403404memcpy (Buffer, Operand0->Buffer.Pointer,405Operand0->Buffer.Length);406memcpy (Buffer + Operand0->Buffer.Length,407LocalOperand1->Buffer.Pointer,408LocalOperand1->Buffer.Length);409break;410411default:412413/* Invalid object type, should not happen here */414415ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",416Operand0->Common.Type));417Status = AE_AML_INTERNAL;418goto Cleanup;419}420421*ActualReturnDesc = ReturnDesc;422423Cleanup:424if (LocalOperand0 != Operand0)425{426AcpiUtRemoveReference (LocalOperand0);427}428429if (LocalOperand1 != Operand1)430{431AcpiUtRemoveReference (LocalOperand1);432}433434return_ACPI_STATUS (Status);435}436437438/*******************************************************************************439*440* FUNCTION: AcpiExConvertToObjectTypeString441*442* PARAMETERS: ObjDesc - Object to be converted443* ReturnDesc - Where to place the return object444*445* RETURN: Status446*447* DESCRIPTION: Convert an object of arbitrary type to a string object that448* contains the namestring for the object. Used for the449* concatenate operator.450*451******************************************************************************/452453static ACPI_STATUS454AcpiExConvertToObjectTypeString (455ACPI_OPERAND_OBJECT *ObjDesc,456ACPI_OPERAND_OBJECT **ResultDesc)457{458ACPI_OPERAND_OBJECT *ReturnDesc;459const char *TypeString;460461462TypeString = AcpiUtGetTypeName (ObjDesc->Common.Type);463464ReturnDesc = AcpiUtCreateStringObject (465((ACPI_SIZE) strlen (TypeString) + 9)); /* 9 For "[ Object]" */466if (!ReturnDesc)467{468return (AE_NO_MEMORY);469}470471strcpy (ReturnDesc->String.Pointer, "[");472strcat (ReturnDesc->String.Pointer, TypeString);473strcat (ReturnDesc->String.Pointer, " Object]");474475*ResultDesc = ReturnDesc;476return (AE_OK);477}478479480/*******************************************************************************481*482* FUNCTION: AcpiExConcatTemplate483*484* PARAMETERS: Operand0 - First source object485* Operand1 - Second source object486* ActualReturnDesc - Where to place the return object487* WalkState - Current walk state488*489* RETURN: Status490*491* DESCRIPTION: Concatenate two resource templates492*493******************************************************************************/494495ACPI_STATUS496AcpiExConcatTemplate (497ACPI_OPERAND_OBJECT *Operand0,498ACPI_OPERAND_OBJECT *Operand1,499ACPI_OPERAND_OBJECT **ActualReturnDesc,500ACPI_WALK_STATE *WalkState)501{502ACPI_STATUS Status;503ACPI_OPERAND_OBJECT *ReturnDesc;504UINT8 *NewBuf;505UINT8 *EndTag;506ACPI_SIZE Length0;507ACPI_SIZE Length1;508ACPI_SIZE NewLength;509510511ACPI_FUNCTION_TRACE (ExConcatTemplate);512513514/*515* Find the EndTag descriptor in each resource template.516* Note1: returned pointers point TO the EndTag, not past it.517* Note2: zero-length buffers are allowed; treated like one EndTag518*/519520/* Get the length of the first resource template */521522Status = AcpiUtGetResourceEndTag (Operand0, &EndTag);523if (ACPI_FAILURE (Status))524{525return_ACPI_STATUS (Status);526}527528Length0 = ACPI_PTR_DIFF (EndTag, Operand0->Buffer.Pointer);529530/* Get the length of the second resource template */531532Status = AcpiUtGetResourceEndTag (Operand1, &EndTag);533if (ACPI_FAILURE (Status))534{535return_ACPI_STATUS (Status);536}537538Length1 = ACPI_PTR_DIFF (EndTag, Operand1->Buffer.Pointer);539540/* Combine both lengths, minimum size will be 2 for EndTag */541542NewLength = Length0 + Length1 + sizeof (AML_RESOURCE_END_TAG);543544/* Create a new buffer object for the result (with one EndTag) */545546ReturnDesc = AcpiUtCreateBufferObject (NewLength);547if (!ReturnDesc)548{549return_ACPI_STATUS (AE_NO_MEMORY);550}551552/*553* Copy the templates to the new buffer, 0 first, then 1 follows. One554* EndTag descriptor is copied from Operand1.555*/556NewBuf = ReturnDesc->Buffer.Pointer;557memcpy (NewBuf, Operand0->Buffer.Pointer, Length0);558memcpy (NewBuf + Length0, Operand1->Buffer.Pointer, Length1);559560/* Insert EndTag and set the checksum to zero, means "ignore checksum" */561562NewBuf[NewLength - 1] = 0;563NewBuf[NewLength - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;564565/* Return the completed resource template */566567*ActualReturnDesc = ReturnDesc;568return_ACPI_STATUS (AE_OK);569}570571572