Path: blob/main/sys/contrib/dev/acpica/common/dmtables.c
48375 views
/******************************************************************************1*2* Module Name: dmtables - disassembler ACPI table support3*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/compiler/aslcompiler.h>152#include <contrib/dev/acpica/include/acdispat.h>153#include <contrib/dev/acpica/include/acnamesp.h>154#include <contrib/dev/acpica/include/actables.h>155#include <contrib/dev/acpica/include/acparser.h>156#include <contrib/dev/acpica/include/acapps.h>157#include <contrib/dev/acpica/include/acmacros.h>158#include <contrib/dev/acpica/include/acconvert.h>159160161#define _COMPONENT ACPI_TOOLS162ACPI_MODULE_NAME ("dmtables")163164165/* Local prototypes */166167static void168AdCreateTableHeader (169char *Filename,170ACPI_TABLE_HEADER *Table);171172static ACPI_STATUS173AdStoreTable (174ACPI_TABLE_HEADER *Table,175UINT32 *TableIndex);176177178extern ACPI_TABLE_DESC LocalTables[1];179extern ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;180181182/******************************************************************************183*184* FUNCTION: AdDisassemblerHeader185*186* PARAMETERS: Filename - Input file for the table187* TableType - Either AML or DataTable188*189* RETURN: None190*191* DESCRIPTION: Create the disassembler header, including ACPICA signon with192* optional current time and date.193*194*****************************************************************************/195196void197AdDisassemblerHeader (198char *Filename,199UINT8 TableType)200{201time_t Timer;202203204/* Header and input table info */205206AcpiOsPrintf ("/*\n");207AcpiOsPrintf (ACPI_COMMON_HEADER (AML_DISASSEMBLER_NAME, " * "));208209if (TableType == ACPI_IS_AML_TABLE)210{211if (AcpiGbl_CstyleDisassembly)212{213AcpiOsPrintf (214" * Disassembling to symbolic ASL+ operators\n"215" *\n");216}217else218{219AcpiOsPrintf (220" * Disassembling to non-symbolic legacy ASL operators\n"221" *\n");222}223}224225if (AslGbl_Deterministic)226{227AcpiOsPrintf (" * Disassembly of %s\n", Filename);228}229else230{231time (&Timer);232AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));233}234AcpiOsPrintf (" *\n");235}236237238/******************************************************************************239*240* FUNCTION: AdCreateTableHeader241*242* PARAMETERS: Filename - Input file for the table243* Table - Pointer to the raw table244*245* RETURN: None246*247* DESCRIPTION: Create the ASL table header, including ACPICA signon with248* current time and date.249*250*****************************************************************************/251252static void253AdCreateTableHeader (254char *Filename,255ACPI_TABLE_HEADER *Table)256{257UINT8 Checksum;258259260/* Reset globals for External statements */261262AcpiGbl_NumExternalMethods = 0;263AcpiGbl_ResolvedExternalMethods = 0;264265/*266* Print file header and dump original table header267*/268AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);269270AcpiOsPrintf (" * Original Table Header:\n");271AcpiOsPrintf (" * Signature \"%4.4s\"\n", Table->Signature);272AcpiOsPrintf (" * Length 0x%8.8X (%u)\n", Table->Length, Table->Length);273274/* Print and validate the revision */275276AcpiOsPrintf (" * Revision 0x%2.2X", Table->Revision);277278switch (Table->Revision)279{280case 0:281282AcpiOsPrintf (" **** Invalid Revision");283break;284285case 1:286287/* Revision of DSDT controls the ACPI integer width */288289if (ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_DSDT))290{291AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");292}293break;294295default:296297break;298}299300/* Print and validate the table checksum */301302AcpiOsPrintf ("\n * Checksum 0x%2.2X", Table->Checksum);303304Checksum = AcpiUtChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);305if (Checksum)306{307AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",308(UINT8) (Table->Checksum - Checksum));309}310311AcpiOsPrintf ("\n");312AcpiOsPrintf (" * OEM ID \"%.6s\"\n", Table->OemId);313AcpiOsPrintf (" * OEM Table ID \"%.8s\"\n", Table->OemTableId);314AcpiOsPrintf (" * OEM Revision 0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);315AcpiOsPrintf (" * Compiler ID \"%.4s\"\n", Table->AslCompilerId);316AcpiOsPrintf (" * Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);317AcpiOsPrintf (" */\n");318319/*320* Print comments that come before this definition block.321*/322if (AcpiGbl_CaptureComments)323{324ASL_CV_PRINT_ONE_COMMENT(AcpiGbl_ParseOpRoot,AML_COMMENT_STANDARD, NULL, 0);325}326327/*328* Open the ASL definition block.329*330* Note: the AMLFilename string is left zero-length in order to just let331* the compiler create it when the disassembled file is compiled. This332* makes it easier to rename the disassembled ASL file if needed.333*/334AcpiOsPrintf (335"DefinitionBlock (\"\", \"%4.4s\", %u, \"%.6s\", \"%.8s\", 0x%8.8X)\n",336Table->Signature, Table->Revision,337Table->OemId, Table->OemTableId, Table->OemRevision);338}339340341/******************************************************************************342*343* FUNCTION: AdDisplayTables344*345* PARAMETERS: Filename - Input file for the table346* Table - Pointer to the raw table347*348* RETURN: Status349*350* DESCRIPTION: Display (disassemble) loaded tables and dump raw tables351*352*****************************************************************************/353354ACPI_STATUS355AdDisplayTables (356char *Filename,357ACPI_TABLE_HEADER *Table)358{359360361if (!AcpiGbl_ParseOpRoot)362{363return (AE_NOT_EXIST);364}365366if (!AcpiGbl_DmOpt_Listing)367{368AdCreateTableHeader (Filename, Table);369}370371AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);372MpEmitMappingInfo ();373374if (AcpiGbl_DmOpt_Listing)375{376AcpiOsPrintf ("\n\nTable Header:\n");377AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),378DB_BYTE_DISPLAY, ACPI_UINT32_MAX);379380AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);381AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),382Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);383}384385return (AE_OK);386}387388389/*******************************************************************************390*391* FUNCTION: AdStoreTable392*393* PARAMETERS: Table - Table header394* TableIndex - Where the table index is returned395*396* RETURN: Status and table index.397*398* DESCRIPTION: Add an ACPI table to the global table list399*400******************************************************************************/401402static ACPI_STATUS403AdStoreTable (404ACPI_TABLE_HEADER *Table,405UINT32 *TableIndex)406{407ACPI_STATUS Status;408ACPI_TABLE_DESC *TableDesc;409410411Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);412if (ACPI_FAILURE (Status))413{414return (Status);415}416417/* Initialize added table */418419AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),420ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);421Status = AcpiTbValidateTable (TableDesc);422return (Status);423}424425426/******************************************************************************427*428* FUNCTION: AdGetLocalTables429*430* PARAMETERS: None431*432* RETURN: Status433*434* DESCRIPTION: Get the ACPI tables from either memory or a file435*436*****************************************************************************/437438ACPI_STATUS439AdGetLocalTables (440void)441{442ACPI_STATUS Status;443ACPI_TABLE_HEADER TableHeader;444ACPI_TABLE_HEADER *NewTable;445UINT32 TableIndex;446447448/* Get the DSDT via table override */449450ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);451Status = AcpiOsTableOverride (&TableHeader, &NewTable);452if (ACPI_FAILURE (Status) || !NewTable)453{454fprintf (stderr, "Could not obtain DSDT\n");455return (AE_NO_ACPI_TABLES);456}457458AdWriteTable (NewTable, NewTable->Length,459ACPI_SIG_DSDT, NewTable->OemTableId);460461/* Store DSDT in the Table Manager */462463Status = AdStoreTable (NewTable, &TableIndex);464if (ACPI_FAILURE (Status))465{466fprintf (stderr, "Could not store DSDT\n");467return (AE_NO_ACPI_TABLES);468}469470return (AE_OK);471}472473474/******************************************************************************475*476* FUNCTION: AdParseTable477*478* PARAMETERS: Table - Pointer to the raw table479* OwnerId - Returned OwnerId of the table480* LoadTable - If add table to the global table list481* External - If this is an external table482*483* RETURN: Status484*485* DESCRIPTION: Parse an ACPI AML table486*487*****************************************************************************/488489ACPI_STATUS490AdParseTable (491ACPI_TABLE_HEADER *Table,492ACPI_OWNER_ID *OwnerId,493BOOLEAN LoadTable,494BOOLEAN External)495{496ACPI_STATUS Status = AE_OK;497ACPI_WALK_STATE *WalkState;498UINT8 *AmlStart;499UINT32 AmlLength;500UINT32 TableIndex;501502503if (!Table)504{505return (AE_NOT_EXIST);506}507508/* Pass 1: Parse everything except control method bodies */509510fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);511512AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);513AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));514515AcpiUtSetIntegerWidth (Table->Revision);516517/* Create the root object */518519AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);520if (!AcpiGbl_ParseOpRoot)521{522return (AE_NO_MEMORY);523}524525#ifdef ACPI_ASL_COMPILER526if (AcpiGbl_CaptureComments)527{528AcpiGbl_ParseOpRoot->Common.CvFilename = AcpiGbl_FileTreeRoot->Filename;529}530else531{532AcpiGbl_ParseOpRoot->Common.CvFilename = NULL;533}534#endif535536/* Create and initialize a new walk state */537538WalkState = AcpiDsCreateWalkState (0, AcpiGbl_ParseOpRoot, NULL, NULL);539if (!WalkState)540{541return (AE_NO_MEMORY);542}543544Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,545NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);546if (ACPI_FAILURE (Status))547{548return (Status);549}550551WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;552553Status = AcpiPsParseAml (WalkState);554if (ACPI_FAILURE (Status))555{556return (Status);557}558559/* If LoadTable is FALSE, we are parsing the last loaded table */560561TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;562563/* Pass 2 */564565if (LoadTable)566{567Status = AdStoreTable (Table, &TableIndex);568if (ACPI_FAILURE (Status))569{570return (Status);571}572Status = AcpiTbAllocateOwnerId (TableIndex);573if (ACPI_FAILURE (Status))574{575return (Status);576}577if (OwnerId)578{579Status = AcpiTbGetOwnerId (TableIndex, OwnerId);580if (ACPI_FAILURE (Status))581{582return (Status);583}584}585}586587fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);588589Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);590if (ACPI_FAILURE (Status))591{592return (Status);593}594595/* No need to parse control methods of external table */596597if (External)598{599return (AE_OK);600}601602/*603* Pass 3: Parse control methods and link their parse trees604* into the main parse tree605*/606fprintf (stderr,607"Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");608609(void) AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);610fprintf (stderr, "\n");611612/* Process Resource Templates */613614AcpiDmFindResources (AcpiGbl_ParseOpRoot);615616fprintf (stderr, "Parsing completed\n");617return (AE_OK);618}619620621