Path: blob/main/sys/contrib/dev/acpica/components/debugger/dbutils.c
48406 views
/*******************************************************************************1*2* Module Name: dbutils - AML debugger utilities3*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/acnamesp.h>154#include <contrib/dev/acpica/include/acdebug.h>155156157#define _COMPONENT ACPI_CA_DEBUGGER158ACPI_MODULE_NAME ("dbutils")159160161/* Local prototypes */162163#ifdef ACPI_OBSOLETE_FUNCTIONS164ACPI_STATUS165AcpiDbSecondPassParse (166ACPI_PARSE_OBJECT *Root);167168void169AcpiDbDumpBuffer (170UINT32 Address);171#endif172173174/*******************************************************************************175*176* FUNCTION: AcpiDbMatchArgument177*178* PARAMETERS: UserArgument - User command line179* Arguments - Array of commands to match against180*181* RETURN: Index into command array or ACPI_TYPE_NOT_FOUND if not found182*183* DESCRIPTION: Search command array for a command match184*185******************************************************************************/186187ACPI_OBJECT_TYPE188AcpiDbMatchArgument (189char *UserArgument,190ACPI_DB_ARGUMENT_INFO *Arguments)191{192UINT32 i;193194195if (!UserArgument || UserArgument[0] == 0)196{197return (ACPI_TYPE_NOT_FOUND);198}199200for (i = 0; Arguments[i].Name; i++)201{202if (strstr (203ACPI_CAST_PTR (char, Arguments[i].Name),204ACPI_CAST_PTR (char, UserArgument)) == Arguments[i].Name)205{206return (i);207}208}209210/* Argument not recognized */211212return (ACPI_TYPE_NOT_FOUND);213}214215216/*******************************************************************************217*218* FUNCTION: AcpiDbSetOutputDestination219*220* PARAMETERS: OutputFlags - Current flags word221*222* RETURN: None223*224* DESCRIPTION: Set the current destination for debugger output. Also sets225* the debug output level accordingly.226*227******************************************************************************/228229void230AcpiDbSetOutputDestination (231UINT32 OutputFlags)232{233234AcpiGbl_DbOutputFlags = (UINT8) OutputFlags;235236if ((OutputFlags & ACPI_DB_REDIRECTABLE_OUTPUT) &&237AcpiGbl_DbOutputToFile)238{239AcpiDbgLevel = AcpiGbl_DbDebugLevel;240}241else242{243AcpiDbgLevel = AcpiGbl_DbConsoleDebugLevel;244}245}246247248/*******************************************************************************249*250* FUNCTION: AcpiDbDumpExternalObject251*252* PARAMETERS: ObjDesc - External ACPI object to dump253* Level - Nesting level.254*255* RETURN: None256*257* DESCRIPTION: Dump the contents of an ACPI external object258*259******************************************************************************/260261void262AcpiDbDumpExternalObject (263ACPI_OBJECT *ObjDesc,264UINT32 Level)265{266UINT32 i;267268269if (!ObjDesc)270{271AcpiOsPrintf ("[Null Object]\n");272return;273}274275for (i = 0; i < Level; i++)276{277AcpiOsPrintf (" ");278}279280switch (ObjDesc->Type)281{282case ACPI_TYPE_ANY:283284AcpiOsPrintf ("[Null Object] (Type=0)\n");285break;286287case ACPI_TYPE_INTEGER:288289AcpiOsPrintf ("[Integer] = %8.8X%8.8X\n",290ACPI_FORMAT_UINT64 (ObjDesc->Integer.Value));291break;292293case ACPI_TYPE_STRING:294295AcpiOsPrintf ("[String] Length %.2X = ", ObjDesc->String.Length);296AcpiUtPrintString (ObjDesc->String.Pointer, ACPI_UINT8_MAX);297AcpiOsPrintf ("\n");298break;299300case ACPI_TYPE_BUFFER:301302AcpiOsPrintf ("[Buffer] Length %.2X = ", ObjDesc->Buffer.Length);303if (ObjDesc->Buffer.Length)304{305if (ObjDesc->Buffer.Length > 16)306{307AcpiOsPrintf ("\n");308}309310AcpiUtDebugDumpBuffer (311ACPI_CAST_PTR (UINT8, ObjDesc->Buffer.Pointer),312ObjDesc->Buffer.Length, DB_BYTE_DISPLAY, _COMPONENT);313}314else315{316AcpiOsPrintf ("\n");317}318break;319320case ACPI_TYPE_PACKAGE:321322AcpiOsPrintf ("[Package] Contains %u Elements:\n",323ObjDesc->Package.Count);324325for (i = 0; i < ObjDesc->Package.Count; i++)326{327AcpiDbDumpExternalObject (328&ObjDesc->Package.Elements[i], Level+1);329}330break;331332case ACPI_TYPE_LOCAL_REFERENCE:333334AcpiOsPrintf ("[Object Reference] = ");335AcpiDbDisplayInternalObject (ObjDesc->Reference.Handle, NULL);336break;337338case ACPI_TYPE_PROCESSOR:339340AcpiOsPrintf ("[Processor]\n");341break;342343case ACPI_TYPE_POWER:344345AcpiOsPrintf ("[Power Resource]\n");346break;347348default:349350AcpiOsPrintf ("[Unknown Type] %X\n", ObjDesc->Type);351break;352}353}354355356/*******************************************************************************357*358* FUNCTION: AcpiDbPrepNamestring359*360* PARAMETERS: Name - String to prepare361*362* RETURN: None363*364* DESCRIPTION: Translate all forward slashes and dots to backslashes.365*366******************************************************************************/367368void369AcpiDbPrepNamestring (370char *Name)371{372373if (!Name)374{375return;376}377378AcpiUtStrupr (Name);379380/* Convert a leading forward slash to a backslash */381382if (*Name == '/')383{384*Name = '\\';385}386387/* Ignore a leading backslash, this is the root prefix */388389if (ACPI_IS_ROOT_PREFIX (*Name))390{391Name++;392}393394/* Convert all slash path separators to dots */395396while (*Name)397{398if ((*Name == '/') ||399(*Name == '\\'))400{401*Name = '.';402}403404Name++;405}406}407408409/*******************************************************************************410*411* FUNCTION: AcpiDbLocalNsLookup412*413* PARAMETERS: Name - Name to lookup414*415* RETURN: Pointer to a namespace node, null on failure416*417* DESCRIPTION: Lookup a name in the ACPI namespace418*419* Note: Currently begins search from the root. Could be enhanced to use420* the current prefix (scope) node as the search beginning point.421*422******************************************************************************/423424ACPI_NAMESPACE_NODE *425AcpiDbLocalNsLookup (426char *Name)427{428char *InternalPath;429ACPI_STATUS Status;430ACPI_NAMESPACE_NODE *Node = NULL;431432433AcpiDbPrepNamestring (Name);434435/* Build an internal namestring */436437Status = AcpiNsInternalizeName (Name, &InternalPath);438if (ACPI_FAILURE (Status))439{440AcpiOsPrintf ("Invalid namestring: %s\n", Name);441return (NULL);442}443444/*445* Lookup the name.446* (Uses root node as the search starting point)447*/448Status = AcpiNsLookup (NULL, InternalPath, ACPI_TYPE_ANY,449ACPI_IMODE_EXECUTE, ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE,450NULL, &Node);451if (ACPI_FAILURE (Status))452{453AcpiOsPrintf ("Could not locate name: %s, %s\n",454Name, AcpiFormatException (Status));455}456457ACPI_FREE (InternalPath);458return (Node);459}460461462/*******************************************************************************463*464* FUNCTION: AcpiDbUint32ToHexString465*466* PARAMETERS: Value - The value to be converted to string467* Buffer - Buffer for result (not less than 11 bytes)468*469* RETURN: None470*471* DESCRIPTION: Convert the unsigned 32-bit value to the hexadecimal image472*473* NOTE: It is the caller's responsibility to ensure that the length of buffer474* is sufficient.475*476******************************************************************************/477478void479AcpiDbUint32ToHexString (480UINT32 Value,481char *Buffer)482{483int i;484485486if (Value == 0)487{488strcpy (Buffer, "0");489return;490}491492Buffer[8] = '\0';493494for (i = 7; i >= 0; i--)495{496Buffer[i] = AcpiGbl_UpperHexDigits [Value & 0x0F];497Value = Value >> 4;498}499}500501502#ifdef ACPI_OBSOLETE_FUNCTIONS503/*******************************************************************************504*505* FUNCTION: AcpiDbSecondPassParse506*507* PARAMETERS: Root - Root of the parse tree508*509* RETURN: Status510*511* DESCRIPTION: Second pass parse of the ACPI tables. We need to wait until512* second pass to parse the control methods513*514******************************************************************************/515516ACPI_STATUS517AcpiDbSecondPassParse (518ACPI_PARSE_OBJECT *Root)519{520ACPI_PARSE_OBJECT *Op = Root;521ACPI_PARSE_OBJECT *Method;522ACPI_PARSE_OBJECT *SearchOp;523ACPI_PARSE_OBJECT *StartOp;524ACPI_STATUS Status = AE_OK;525UINT32 BaseAmlOffset;526ACPI_WALK_STATE *WalkState;527528529ACPI_FUNCTION_ENTRY ();530531532AcpiOsPrintf ("Pass two parse ....\n");533534while (Op)535{536if (Op->Common.AmlOpcode == AML_METHOD_OP)537{538Method = Op;539540/* Create a new walk state for the parse */541542WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);543if (!WalkState)544{545return (AE_NO_MEMORY);546}547548/* Init the Walk State */549550WalkState->ParserState.Aml =551WalkState->ParserState.AmlStart = Method->Named.Data;552WalkState->ParserState.AmlEnd =553WalkState->ParserState.PkgEnd = Method->Named.Data +554Method->Named.Length;555WalkState->ParserState.StartScope = Op;556557WalkState->DescendingCallback = AcpiDsLoad1BeginOp;558WalkState->AscendingCallback = AcpiDsLoad1EndOp;559560/* Perform the AML parse */561562Status = AcpiPsParseAml (WalkState);563564BaseAmlOffset = (Method->Common.Value.Arg)->Common.AmlOffset + 1;565StartOp = (Method->Common.Value.Arg)->Common.Next;566SearchOp = StartOp;567568while (SearchOp)569{570SearchOp->Common.AmlOffset += BaseAmlOffset;571SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);572}573}574575if (Op->Common.AmlOpcode == AML_REGION_OP)576{577/* TBD: [Investigate] this isn't quite the right thing to do! */578/*579*580* Method = (ACPI_DEFERRED_OP *) Op;581* Status = AcpiPsParseAml (Op, Method->Body, Method->BodyLength);582*/583}584585if (ACPI_FAILURE (Status))586{587break;588}589590Op = AcpiPsGetDepthNext (Root, Op);591}592593return (Status);594}595596597/*******************************************************************************598*599* FUNCTION: AcpiDbDumpBuffer600*601* PARAMETERS: Address - Pointer to the buffer602*603* RETURN: None604*605* DESCRIPTION: Print a portion of a buffer606*607******************************************************************************/608609void610AcpiDbDumpBuffer (611UINT32 Address)612{613614AcpiOsPrintf ("\nLocation %X:\n", Address);615616AcpiDbgLevel |= ACPI_LV_TABLES;617AcpiUtDebugDumpBuffer (ACPI_TO_POINTER (Address), 64, DB_BYTE_DISPLAY,618ACPI_UINT32_MAX);619}620#endif621622623