Path: blob/main/sys/contrib/dev/acpica/compiler/aslhex.c
48373 views
/******************************************************************************1*2* Module Name: aslhex - ASCII hex output file generation (C, ASM, and ASL)3*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/acapps.h>153154#define _COMPONENT ACPI_COMPILER155ACPI_MODULE_NAME ("ashex")156157/*158* This module emits ASCII hex output files in either C, ASM, or ASL format159*/160161/* Local prototypes */162163static void164HxDoHexOutputC (165void);166167static void168HxDoHexOutputAsl (169void);170171static void172HxDoHexOutputAsm (173void);174175static UINT32176HxReadAmlOutputFile (177UINT8 *Buffer);178179180/*******************************************************************************181*182* FUNCTION: HxDoHexOutput183*184* PARAMETERS: None185*186* RETURN: None187*188* DESCRIPTION: Create the hex output file. Note: data is obtained by reading189* the entire AML output file that was previously generated.190*191******************************************************************************/192193void194HxDoHexOutput (195void)196{197198switch (AslGbl_HexOutputFlag)199{200case HEX_OUTPUT_C:201202HxDoHexOutputC ();203break;204205case HEX_OUTPUT_ASM:206207HxDoHexOutputAsm ();208break;209210case HEX_OUTPUT_ASL:211212HxDoHexOutputAsl ();213break;214215default:216217/* No other output types supported */218219break;220}221}222223224/*******************************************************************************225*226* FUNCTION: HxReadAmlOutputFile227*228* PARAMETERS: Buffer - Where to return data229*230* RETURN: None231*232* DESCRIPTION: Read a line of the AML output prior to formatting the data233*234******************************************************************************/235236static UINT32237HxReadAmlOutputFile (238UINT8 *Buffer)239{240UINT32 Actual;241242243Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,244AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle);245246if (ferror (AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle))247{248FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);249AslAbort ();250}251252return (Actual);253}254255256/*******************************************************************************257*258* FUNCTION: HxDoHexOutputC259*260* PARAMETERS: None261*262* RETURN: None263*264* DESCRIPTION: Create the hex output file. This is the same data as the AML265* output file, but formatted into hex/ascii bytes suitable for266* inclusion into a C source file.267*268* Note: the base name of the hex output file is prepended to269* all symbols as they are output to the file.270*271******************************************************************************/272273static void274HxDoHexOutputC (275void)276{277UINT8 FileData[HEX_TABLE_LINE_SIZE];278UINT32 LineLength;279UINT32 Offset = 0;280UINT32 AmlFileSize;281UINT32 i;282char *FileBasename;283284285/* Obtain the file basename (filename with no extension) */286287FileBasename = FlGetFileBasename (AslGbl_Files [ASL_FILE_HEX_OUTPUT].Filename);288289/* Get AML size, seek back to start */290291AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);292FlSeekFile (ASL_FILE_AML_OUTPUT, 0);293294/* Finish the file header and emit the non-data symbols */295296FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n");297FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",298AmlFileSize);299300FlPrintFile (ASL_FILE_HEX_OUTPUT, "#ifndef __%s_HEX__\n", FileBasename);301FlPrintFile (ASL_FILE_HEX_OUTPUT, "#define __%s_HEX__\n\n", FileBasename);302303AcpiUtStrlwr (FileBasename);304FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char %s_aml_code[] =\n{\n", FileBasename);305306while (Offset < AmlFileSize)307{308/* Read enough bytes needed for one output line */309310LineLength = HxReadAmlOutputFile (FileData);311if (!LineLength)312{313break;314}315316FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");317318for (i = 0; i < LineLength; i++)319{320/*321* Output each hex byte in the form: "0xnn,"322* Add a comma until the very last byte of the AML file323* (Some C compilers complain about a trailing comma)324*/325FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);326if ((Offset + i + 1) < AmlFileSize)327{328FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");329}330else331{332FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");333}334}335336/* Add fill spaces if needed for last line */337338if (LineLength < HEX_TABLE_LINE_SIZE)339{340FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",3415 * (HEX_TABLE_LINE_SIZE - LineLength), " ");342}343344/* Emit the offset and ascii dump for the entire line */345346FlPrintFile (ASL_FILE_HEX_OUTPUT, " /* %8.8X", Offset);347LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);348349FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",350HEX_TABLE_LINE_SIZE - LineLength + 1, " ");351352Offset += LineLength;353}354355FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n\n");356FlPrintFile (ASL_FILE_HEX_OUTPUT, "#endif\n");357}358359360/*******************************************************************************361*362* FUNCTION: HxDoHexOutputAsl363*364* PARAMETERS: None365*366* RETURN: None367*368* DESCRIPTION: Create the hex output file. This is the same data as the AML369* output file, but formatted into hex/ascii bytes suitable for370* inclusion into a C source file.371*372******************************************************************************/373374static void375HxDoHexOutputAsl (376void)377{378UINT8 FileData[HEX_TABLE_LINE_SIZE];379UINT32 LineLength;380UINT32 Offset = 0;381UINT32 AmlFileSize;382UINT32 i;383384385/* Get AML size, seek back to start */386387AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);388FlSeekFile (ASL_FILE_AML_OUTPUT, 0);389390FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");391FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",392AmlFileSize);393FlPrintFile (ASL_FILE_HEX_OUTPUT, " Name (BUF1, Buffer()\n {\n");394395while (Offset < AmlFileSize)396{397/* Read enough bytes needed for one output line */398399LineLength = HxReadAmlOutputFile (FileData);400if (!LineLength)401{402break;403}404405FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");406407for (i = 0; i < LineLength; i++)408{409/*410* Print each hex byte.411* Add a comma until the very last byte of the AML file412* (Some C compilers complain about a trailing comma)413*/414FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);415if ((Offset + i + 1) < AmlFileSize)416{417FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");418}419else420{421FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");422}423}424425/* Add fill spaces if needed for last line */426427if (LineLength < HEX_TABLE_LINE_SIZE)428{429FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",4305 * (HEX_TABLE_LINE_SIZE - LineLength), " ");431}432433/* Emit the offset and ascii dump for the entire line */434435FlPrintFile (ASL_FILE_HEX_OUTPUT, " /* %8.8X", Offset);436LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);437438FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",439HEX_TABLE_LINE_SIZE - LineLength + 1, " ");440441Offset += LineLength;442}443444FlPrintFile (ASL_FILE_HEX_OUTPUT, " })\n");445}446447448/*******************************************************************************449*450* FUNCTION: HxDoHexOutputAsm451*452* PARAMETERS: None453*454* RETURN: None455*456* DESCRIPTION: Create the hex output file. This is the same data as the AML457* output file, but formatted into hex/ascii bytes suitable for458* inclusion into a ASM source file.459*460******************************************************************************/461462static void463HxDoHexOutputAsm (464void)465{466UINT8 FileData[HEX_TABLE_LINE_SIZE];467UINT32 LineLength;468UINT32 Offset = 0;469UINT32 AmlFileSize;470UINT32 i;471472473/* Get AML size, seek back to start */474475AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);476FlSeekFile (ASL_FILE_AML_OUTPUT, 0);477478FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n");479FlPrintFile (ASL_FILE_HEX_OUTPUT, "; AML code block contains 0x%X bytes\n;\n",480AmlFileSize);481482while (Offset < AmlFileSize)483{484/* Read enough bytes needed for one output line */485486LineLength = HxReadAmlOutputFile (FileData);487if (!LineLength)488{489break;490}491492FlPrintFile (ASL_FILE_HEX_OUTPUT, " db ");493494for (i = 0; i < LineLength; i++)495{496/*497* Print each hex byte.498* Add a comma until the last byte of the line499*/500FlPrintFile (ASL_FILE_HEX_OUTPUT, "0%2.2Xh", FileData[i]);501if ((i + 1) < LineLength)502{503FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");504}505}506507FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");508509/* Add fill spaces if needed for last line */510511if (LineLength < HEX_TABLE_LINE_SIZE)512{513FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",5145 * (HEX_TABLE_LINE_SIZE - LineLength), " ");515}516517/* Emit the offset and ascii dump for the entire line */518519FlPrintFile (ASL_FILE_HEX_OUTPUT, " ; %8.8X", Offset);520LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);521522FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");523524Offset += LineLength;525}526527FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");528}529530531