Path: blob/main/sys/contrib/dev/acpica/components/debugger/dbconvert.c
48406 views
/*******************************************************************************1*2* Module Name: dbconvert - debugger miscellaneous conversion routines3*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#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/acdebug.h>154155156#define _COMPONENT ACPI_CA_DEBUGGER157ACPI_MODULE_NAME ("dbconvert")158159160#define DB_DEFAULT_PKG_ELEMENTS 33161162163/*******************************************************************************164*165* FUNCTION: AcpiDbHexCharToValue166*167* PARAMETERS: HexChar - Ascii Hex digit, 0-9|a-f|A-F168* ReturnValue - Where the converted value is returned169*170* RETURN: Status171*172* DESCRIPTION: Convert a single hex character to a 4-bit number (0-16).173*174******************************************************************************/175176ACPI_STATUS177AcpiDbHexCharToValue (178int HexChar,179UINT8 *ReturnValue)180{181UINT8 Value;182183184/* Digit must be ascii [0-9a-fA-F] */185186if (!isxdigit (HexChar))187{188return (AE_BAD_HEX_CONSTANT);189}190191if (HexChar <= 0x39)192{193Value = (UINT8) (HexChar - 0x30);194}195else196{197Value = (UINT8) (toupper (HexChar) - 0x37);198}199200*ReturnValue = Value;201return (AE_OK);202}203204205/*******************************************************************************206*207* FUNCTION: AcpiDbHexByteToBinary208*209* PARAMETERS: HexByte - Double hex digit (0x00 - 0xFF) in format:210* HiByte then LoByte.211* ReturnValue - Where the converted value is returned212*213* RETURN: Status214*215* DESCRIPTION: Convert two hex characters to an 8 bit number (0 - 255).216*217******************************************************************************/218219static ACPI_STATUS220AcpiDbHexByteToBinary (221char *HexByte,222UINT8 *ReturnValue)223{224UINT8 Local0;225UINT8 Local1;226ACPI_STATUS Status;227228229/* High byte */230231Status = AcpiDbHexCharToValue (HexByte[0], &Local0);232if (ACPI_FAILURE (Status))233{234return (Status);235}236237/* Low byte */238239Status = AcpiDbHexCharToValue (HexByte[1], &Local1);240if (ACPI_FAILURE (Status))241{242return (Status);243}244245*ReturnValue = (UINT8) ((Local0 << 4) | Local1);246return (AE_OK);247}248249250/*******************************************************************************251*252* FUNCTION: AcpiDbConvertToBuffer253*254* PARAMETERS: String - Input string to be converted255* Object - Where the buffer object is returned256*257* RETURN: Status258*259* DESCRIPTION: Convert a string to a buffer object. String is treated a list260* of buffer elements, each separated by a space or comma.261*262******************************************************************************/263264static ACPI_STATUS265AcpiDbConvertToBuffer (266char *String,267ACPI_OBJECT *Object)268{269UINT32 i;270UINT32 j;271UINT32 Length;272UINT8 *Buffer;273ACPI_STATUS Status;274275276/* Skip all preceding white space*/277278AcpiUtRemoveWhitespace (&String);279280/* Generate the final buffer length */281282for (i = 0, Length = 0; String[i];)283{284i+=2;285Length++;286287while (String[i] &&288((String[i] == ',') || (String[i] == ' ')))289{290i++;291}292}293294Buffer = ACPI_ALLOCATE (Length);295if (!Buffer)296{297return (AE_NO_MEMORY);298}299300/* Convert the command line bytes to the buffer */301302for (i = 0, j = 0; String[i];)303{304Status = AcpiDbHexByteToBinary (&String[i], &Buffer[j]);305if (ACPI_FAILURE (Status))306{307ACPI_FREE (Buffer);308return (Status);309}310311j++;312i += 2;313while (String[i] &&314((String[i] == ',') || (String[i] == ' ')))315{316i++;317}318}319320Object->Type = ACPI_TYPE_BUFFER;321Object->Buffer.Pointer = Buffer;322Object->Buffer.Length = Length;323return (AE_OK);324}325326327/*******************************************************************************328*329* FUNCTION: AcpiDbConvertToPackage330*331* PARAMETERS: String - Input string to be converted332* Object - Where the package object is returned333*334* RETURN: Status335*336* DESCRIPTION: Convert a string to a package object. Handles nested packages337* via recursion with AcpiDbConvertToObject.338*339******************************************************************************/340341ACPI_STATUS342AcpiDbConvertToPackage (343char *String,344ACPI_OBJECT *Object)345{346char *This;347char *Next;348UINT32 i;349ACPI_OBJECT_TYPE Type;350ACPI_OBJECT *Elements;351ACPI_STATUS Status;352353354Elements = ACPI_ALLOCATE_ZEROED (355DB_DEFAULT_PKG_ELEMENTS * sizeof (ACPI_OBJECT));356if (!Elements)357return (AE_NO_MEMORY);358359This = String;360for (i = 0; i < (DB_DEFAULT_PKG_ELEMENTS - 1); i++)361{362This = AcpiDbGetNextToken (This, &Next, &Type);363if (!This)364{365break;366}367368/* Recursive call to convert each package element */369370Status = AcpiDbConvertToObject (Type, This, &Elements[i]);371if (ACPI_FAILURE (Status))372{373AcpiDbDeleteObjects (i + 1, Elements);374ACPI_FREE (Elements);375return (Status);376}377378This = Next;379}380381Object->Type = ACPI_TYPE_PACKAGE;382Object->Package.Count = i;383Object->Package.Elements = Elements;384return (AE_OK);385}386387388/*******************************************************************************389*390* FUNCTION: AcpiDbConvertToObject391*392* PARAMETERS: Type - Object type as determined by parser393* String - Input string to be converted394* Object - Where the new object is returned395*396* RETURN: Status397*398* DESCRIPTION: Convert a typed and tokenized string to an ACPI_OBJECT. Typing:399* 1) String objects were surrounded by quotes.400* 2) Buffer objects were surrounded by parentheses.401* 3) Package objects were surrounded by brackets "[]".402* 4) All standalone tokens are treated as integers.403*404******************************************************************************/405406ACPI_STATUS407AcpiDbConvertToObject (408ACPI_OBJECT_TYPE Type,409char *String,410ACPI_OBJECT *Object)411{412ACPI_STATUS Status = AE_OK;413414415switch (Type)416{417case ACPI_TYPE_STRING:418419Object->Type = ACPI_TYPE_STRING;420Object->String.Pointer = String;421Object->String.Length = (UINT32) strlen (String);422break;423424case ACPI_TYPE_BUFFER:425426Status = AcpiDbConvertToBuffer (String, Object);427break;428429case ACPI_TYPE_PACKAGE:430431Status = AcpiDbConvertToPackage (String, Object);432break;433434default:435436Object->Type = ACPI_TYPE_INTEGER;437Status = AcpiUtStrtoul64 (String, &Object->Integer.Value);438break;439}440441return (Status);442}443444445/*******************************************************************************446*447* FUNCTION: AcpiDbEncodePldBuffer448*449* PARAMETERS: PldInfo - _PLD buffer struct (Using local struct)450*451* RETURN: Encode _PLD buffer suitable for return value from _PLD452*453* DESCRIPTION: Bit-packs a _PLD buffer struct. Used to test the _PLD macros454*455******************************************************************************/456457UINT8 *458AcpiDbEncodePldBuffer (459ACPI_PLD_INFO *PldInfo)460{461UINT32 *Buffer;462UINT32 Dword;463464465Buffer = ACPI_ALLOCATE_ZEROED (ACPI_PLD_BUFFER_SIZE);466if (!Buffer)467{468return (NULL);469}470471/* First 32 bits */472473Dword = 0;474ACPI_PLD_SET_REVISION (&Dword, PldInfo->Revision);475ACPI_PLD_SET_IGNORE_COLOR (&Dword, PldInfo->IgnoreColor);476ACPI_PLD_SET_RED (&Dword, PldInfo->Red);477ACPI_PLD_SET_GREEN (&Dword, PldInfo->Green);478ACPI_PLD_SET_BLUE (&Dword, PldInfo->Blue);479ACPI_MOVE_32_TO_32 (&Buffer[0], &Dword);480481/* Second 32 bits */482483Dword = 0;484ACPI_PLD_SET_WIDTH (&Dword, PldInfo->Width);485ACPI_PLD_SET_HEIGHT (&Dword, PldInfo->Height);486ACPI_MOVE_32_TO_32 (&Buffer[1], &Dword);487488/* Third 32 bits */489490Dword = 0;491ACPI_PLD_SET_USER_VISIBLE (&Dword, PldInfo->UserVisible);492ACPI_PLD_SET_DOCK (&Dword, PldInfo->Dock);493ACPI_PLD_SET_LID (&Dword, PldInfo->Lid);494ACPI_PLD_SET_PANEL (&Dword, PldInfo->Panel);495ACPI_PLD_SET_VERTICAL (&Dword, PldInfo->VerticalPosition);496ACPI_PLD_SET_HORIZONTAL (&Dword, PldInfo->HorizontalPosition);497ACPI_PLD_SET_SHAPE (&Dword, PldInfo->Shape);498ACPI_PLD_SET_ORIENTATION (&Dword, PldInfo->GroupOrientation);499ACPI_PLD_SET_TOKEN (&Dword, PldInfo->GroupToken);500ACPI_PLD_SET_POSITION (&Dword, PldInfo->GroupPosition);501ACPI_PLD_SET_BAY (&Dword, PldInfo->Bay);502ACPI_MOVE_32_TO_32 (&Buffer[2], &Dword);503504/* Fourth 32 bits */505506Dword = 0;507ACPI_PLD_SET_EJECTABLE (&Dword, PldInfo->Ejectable);508ACPI_PLD_SET_OSPM_EJECT (&Dword, PldInfo->OspmEjectRequired);509ACPI_PLD_SET_CABINET (&Dword, PldInfo->CabinetNumber);510ACPI_PLD_SET_CARD_CAGE (&Dword, PldInfo->CardCageNumber);511ACPI_PLD_SET_REFERENCE (&Dword, PldInfo->Reference);512ACPI_PLD_SET_ROTATION (&Dword, PldInfo->Rotation);513ACPI_PLD_SET_ORDER (&Dword, PldInfo->Order);514ACPI_MOVE_32_TO_32 (&Buffer[3], &Dword);515516if (PldInfo->Revision >= 2)517{518/* Fifth 32 bits */519520Dword = 0;521ACPI_PLD_SET_VERT_OFFSET (&Dword, PldInfo->VerticalOffset);522ACPI_PLD_SET_HORIZ_OFFSET (&Dword, PldInfo->HorizontalOffset);523ACPI_MOVE_32_TO_32 (&Buffer[4], &Dword);524}525526return (ACPI_CAST_PTR (UINT8, Buffer));527}528529530/*******************************************************************************531*532* FUNCTION: AcpiDbDumpPldBuffer533*534* PARAMETERS: ObjDesc - Object returned from _PLD method535*536* RETURN: None.537*538* DESCRIPTION: Dumps formatted contents of a _PLD return buffer.539*540******************************************************************************/541542#define ACPI_PLD_OUTPUT "%20s : %-6X\n"543544void545AcpiDbDumpPldBuffer (546ACPI_OBJECT *ObjDesc)547{548ACPI_OBJECT *BufferDesc;549ACPI_PLD_INFO *PldInfo;550UINT8 *NewBuffer;551ACPI_STATUS Status;552553554/* Object must be of type Package with at least one Buffer element */555556if (ObjDesc->Type != ACPI_TYPE_PACKAGE)557{558return;559}560561BufferDesc = &ObjDesc->Package.Elements[0];562if (BufferDesc->Type != ACPI_TYPE_BUFFER)563{564return;565}566567/* Convert _PLD buffer to local _PLD struct */568569Status = AcpiDecodePldBuffer (BufferDesc->Buffer.Pointer,570BufferDesc->Buffer.Length, &PldInfo);571if (ACPI_FAILURE (Status))572{573return;574}575576/* Encode local _PLD struct back to a _PLD buffer */577578NewBuffer = AcpiDbEncodePldBuffer (PldInfo);579if (!NewBuffer)580{581goto Exit;582}583584/* The two bit-packed buffers should match */585586if (memcmp (NewBuffer, BufferDesc->Buffer.Pointer,587BufferDesc->Buffer.Length))588{589AcpiOsPrintf ("Converted _PLD buffer does not compare. New:\n");590591AcpiUtDumpBuffer (NewBuffer,592BufferDesc->Buffer.Length, DB_BYTE_DISPLAY, 0);593}594595/* First 32-bit dword */596597AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Revision", PldInfo->Revision);598AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_IgnoreColor", PldInfo->IgnoreColor);599AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Red", PldInfo->Red);600AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Green", PldInfo->Green);601AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Blue", PldInfo->Blue);602603/* Second 32-bit dword */604605AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Width", PldInfo->Width);606AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Height", PldInfo->Height);607608/* Third 32-bit dword */609610AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_UserVisible", PldInfo->UserVisible);611AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Dock", PldInfo->Dock);612AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Lid", PldInfo->Lid);613AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Panel", PldInfo->Panel);614AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_VerticalPosition", PldInfo->VerticalPosition);615AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_HorizontalPosition", PldInfo->HorizontalPosition);616AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Shape", PldInfo->Shape);617AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_GroupOrientation", PldInfo->GroupOrientation);618AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_GroupToken", PldInfo->GroupToken);619AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_GroupPosition", PldInfo->GroupPosition);620AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Bay", PldInfo->Bay);621622/* Fourth 32-bit dword */623624AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Ejectable", PldInfo->Ejectable);625AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_EjectRequired", PldInfo->OspmEjectRequired);626AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_CabinetNumber", PldInfo->CabinetNumber);627AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_CardCageNumber", PldInfo->CardCageNumber);628AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Reference", PldInfo->Reference);629AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Rotation", PldInfo->Rotation);630AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_Order", PldInfo->Order);631632/* Fifth 32-bit dword */633634if (BufferDesc->Buffer.Length > 16)635{636AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_VerticalOffset", PldInfo->VerticalOffset);637AcpiOsPrintf (ACPI_PLD_OUTPUT, "PLD_HorizontalOffset", PldInfo->HorizontalOffset);638}639640ACPI_FREE (NewBuffer);641Exit:642ACPI_FREE (PldInfo);643}644645646